@lix-js/sdk 0.6.0-preview.5 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (274) hide show
  1. package/README.md +76 -4
  2. package/dist/errors.d.ts +7 -0
  3. package/dist/errors.js +19 -0
  4. package/dist/index.d.ts +4 -5
  5. package/dist/index.js +3 -3
  6. package/dist/native.d.ts +1 -0
  7. package/dist/native.js +47 -0
  8. package/dist/open-lix.d.ts +38 -207
  9. package/dist/open-lix.js +59 -284
  10. package/dist/result.d.ts +18 -0
  11. package/dist/result.js +48 -0
  12. package/dist/types.d.ts +114 -1
  13. package/dist/value.d.ts +28 -0
  14. package/dist/value.js +245 -0
  15. package/package.json +38 -71
  16. package/SKILL.md +0 -507
  17. package/dist/builtin-schemas.d.ts +0 -1
  18. package/dist/builtin-schemas.js +0 -1
  19. package/dist/engine-wasm/index.d.ts +0 -87
  20. package/dist/engine-wasm/index.js +0 -339
  21. package/dist/engine-wasm/wasm/lix_engine.d.ts +0 -79
  22. package/dist/engine-wasm/wasm/lix_engine.js +0 -833
  23. package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
  24. package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +0 -27
  25. package/dist/generated/builtin-schemas.d.ts +0 -427
  26. package/dist/generated/builtin-schemas.js +0 -643
  27. package/dist/sqlite/index.d.ts +0 -12
  28. package/dist/sqlite/index.js +0 -359
  29. package/dist-engine-src/README.md +0 -18
  30. package/dist-engine-src/src/backend/capabilities.rs +0 -67
  31. package/dist-engine-src/src/backend/conformance/baseline.rs +0 -1127
  32. package/dist-engine-src/src/backend/conformance/factory.rs +0 -93
  33. package/dist-engine-src/src/backend/conformance/failure_tests.rs +0 -608
  34. package/dist-engine-src/src/backend/conformance/fixtures.rs +0 -26
  35. package/dist-engine-src/src/backend/conformance/mod.rs +0 -75
  36. package/dist-engine-src/src/backend/conformance/model.rs +0 -28
  37. package/dist-engine-src/src/backend/conformance/model_based.rs +0 -257
  38. package/dist-engine-src/src/backend/conformance/persistence.rs +0 -204
  39. package/dist-engine-src/src/backend/conformance/projection.rs +0 -21
  40. package/dist-engine-src/src/backend/conformance/pushdown.rs +0 -24
  41. package/dist-engine-src/src/backend/conformance/runner.rs +0 -90
  42. package/dist-engine-src/src/backend/conformance/scan.rs +0 -24
  43. package/dist-engine-src/src/backend/conformance/write.rs +0 -16
  44. package/dist-engine-src/src/backend/error.rs +0 -94
  45. package/dist-engine-src/src/backend/in_memory.rs +0 -670
  46. package/dist-engine-src/src/backend/mod.rs +0 -39
  47. package/dist-engine-src/src/backend/predicate.rs +0 -80
  48. package/dist-engine-src/src/backend/traits.rs +0 -260
  49. package/dist-engine-src/src/backend/types.rs +0 -239
  50. package/dist-engine-src/src/binary_cas/chunking.rs +0 -31
  51. package/dist-engine-src/src/binary_cas/codec.rs +0 -346
  52. package/dist-engine-src/src/binary_cas/context.rs +0 -139
  53. package/dist-engine-src/src/binary_cas/kv.rs +0 -1038
  54. package/dist-engine-src/src/binary_cas/mod.rs +0 -11
  55. package/dist-engine-src/src/binary_cas/types.rs +0 -121
  56. package/dist-engine-src/src/branch/context.rs +0 -40
  57. package/dist-engine-src/src/branch/lifecycle.rs +0 -221
  58. package/dist-engine-src/src/branch/mod.rs +0 -13
  59. package/dist-engine-src/src/branch/refs.rs +0 -321
  60. package/dist-engine-src/src/branch/stage_rows.rs +0 -67
  61. package/dist-engine-src/src/branch/types.rs +0 -21
  62. package/dist-engine-src/src/catalog/context.rs +0 -412
  63. package/dist-engine-src/src/catalog/mod.rs +0 -10
  64. package/dist-engine-src/src/catalog/schema.rs +0 -4
  65. package/dist-engine-src/src/catalog/snapshot.rs +0 -1114
  66. package/dist-engine-src/src/cel/context.rs +0 -86
  67. package/dist-engine-src/src/cel/error.rs +0 -19
  68. package/dist-engine-src/src/cel/mod.rs +0 -8
  69. package/dist-engine-src/src/cel/provider.rs +0 -9
  70. package/dist-engine-src/src/cel/runtime.rs +0 -167
  71. package/dist-engine-src/src/cel/value.rs +0 -50
  72. package/dist-engine-src/src/changelog/bench_support.rs +0 -785
  73. package/dist-engine-src/src/changelog/change.rs +0 -1
  74. package/dist-engine-src/src/changelog/codec.rs +0 -497
  75. package/dist-engine-src/src/changelog/commit.rs +0 -1
  76. package/dist-engine-src/src/changelog/context.rs +0 -1614
  77. package/dist-engine-src/src/changelog/mod.rs +0 -29
  78. package/dist-engine-src/src/changelog/store.rs +0 -163
  79. package/dist-engine-src/src/changelog/test_support.rs +0 -54
  80. package/dist-engine-src/src/changelog/types.rs +0 -213
  81. package/dist-engine-src/src/commit_graph/context.rs +0 -944
  82. package/dist-engine-src/src/commit_graph/mod.rs +0 -9
  83. package/dist-engine-src/src/commit_graph/types.rs +0 -89
  84. package/dist-engine-src/src/commit_graph/walker.rs +0 -786
  85. package/dist-engine-src/src/common/error.rs +0 -347
  86. package/dist-engine-src/src/common/fingerprint.rs +0 -3
  87. package/dist-engine-src/src/common/fs_path.rs +0 -1336
  88. package/dist-engine-src/src/common/identity.rs +0 -145
  89. package/dist-engine-src/src/common/json_pointer.rs +0 -67
  90. package/dist-engine-src/src/common/metadata.rs +0 -40
  91. package/dist-engine-src/src/common/mod.rs +0 -23
  92. package/dist-engine-src/src/common/types.rs +0 -105
  93. package/dist-engine-src/src/common/wire.rs +0 -222
  94. package/dist-engine-src/src/domain.rs +0 -320
  95. package/dist-engine-src/src/engine.rs +0 -203
  96. package/dist-engine-src/src/entity_pk.rs +0 -402
  97. package/dist-engine-src/src/functions/context.rs +0 -296
  98. package/dist-engine-src/src/functions/deterministic.rs +0 -113
  99. package/dist-engine-src/src/functions/mod.rs +0 -18
  100. package/dist-engine-src/src/functions/provider.rs +0 -130
  101. package/dist-engine-src/src/functions/state.rs +0 -335
  102. package/dist-engine-src/src/functions/types.rs +0 -37
  103. package/dist-engine-src/src/init.rs +0 -692
  104. package/dist-engine-src/src/json_store/compression.rs +0 -77
  105. package/dist-engine-src/src/json_store/context.rs +0 -172
  106. package/dist-engine-src/src/json_store/encoded.rs +0 -15
  107. package/dist-engine-src/src/json_store/mod.rs +0 -38
  108. package/dist-engine-src/src/json_store/store.rs +0 -494
  109. package/dist-engine-src/src/json_store/types.rs +0 -212
  110. package/dist-engine-src/src/lib.rs +0 -92
  111. package/dist-engine-src/src/live_state/context.rs +0 -1883
  112. package/dist-engine-src/src/live_state/mod.rs +0 -21
  113. package/dist-engine-src/src/live_state/overlay.rs +0 -75
  114. package/dist-engine-src/src/live_state/reader.rs +0 -23
  115. package/dist-engine-src/src/live_state/types.rs +0 -231
  116. package/dist-engine-src/src/live_state/visibility.rs +0 -666
  117. package/dist-engine-src/src/plugin/archive.rs +0 -438
  118. package/dist-engine-src/src/plugin/component.rs +0 -183
  119. package/dist-engine-src/src/plugin/install.rs +0 -619
  120. package/dist-engine-src/src/plugin/manifest.rs +0 -516
  121. package/dist-engine-src/src/plugin/materializer.rs +0 -202
  122. package/dist-engine-src/src/plugin/mod.rs +0 -33
  123. package/dist-engine-src/src/plugin/plugin_manifest.json +0 -119
  124. package/dist-engine-src/src/plugin/storage.rs +0 -74
  125. package/dist-engine-src/src/schema/annotations/defaults.rs +0 -275
  126. package/dist-engine-src/src/schema/annotations/mod.rs +0 -1
  127. package/dist-engine-src/src/schema/builtin/lix_account.json +0 -21
  128. package/dist-engine-src/src/schema/builtin/lix_active_account.json +0 -29
  129. package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +0 -29
  130. package/dist-engine-src/src/schema/builtin/lix_branch_descriptor.json +0 -34
  131. package/dist-engine-src/src/schema/builtin/lix_branch_ref.json +0 -48
  132. package/dist-engine-src/src/schema/builtin/lix_change.json +0 -63
  133. package/dist-engine-src/src/schema/builtin/lix_change_author.json +0 -45
  134. package/dist-engine-src/src/schema/builtin/lix_commit.json +0 -24
  135. package/dist-engine-src/src/schema/builtin/lix_commit_edge.json +0 -53
  136. package/dist-engine-src/src/schema/builtin/lix_directory_descriptor.json +0 -52
  137. package/dist-engine-src/src/schema/builtin/lix_file_descriptor.json +0 -52
  138. package/dist-engine-src/src/schema/builtin/lix_key_value.json +0 -40
  139. package/dist-engine-src/src/schema/builtin/lix_label.json +0 -29
  140. package/dist-engine-src/src/schema/builtin/lix_label_assignment.json +0 -74
  141. package/dist-engine-src/src/schema/builtin/lix_registered_schema.json +0 -25
  142. package/dist-engine-src/src/schema/builtin/mod.rs +0 -220
  143. package/dist-engine-src/src/schema/compatibility.rs +0 -787
  144. package/dist-engine-src/src/schema/definition.json +0 -187
  145. package/dist-engine-src/src/schema/definition.rs +0 -742
  146. package/dist-engine-src/src/schema/key.rs +0 -138
  147. package/dist-engine-src/src/schema/mod.rs +0 -20
  148. package/dist-engine-src/src/schema/seed.rs +0 -14
  149. package/dist-engine-src/src/schema/tests.rs +0 -780
  150. package/dist-engine-src/src/session/context.rs +0 -1059
  151. package/dist-engine-src/src/session/create_branch.rs +0 -94
  152. package/dist-engine-src/src/session/execute.rs +0 -681
  153. package/dist-engine-src/src/session/merge/analysis.rs +0 -108
  154. package/dist-engine-src/src/session/merge/branch.rs +0 -417
  155. package/dist-engine-src/src/session/merge/conflicts.rs +0 -63
  156. package/dist-engine-src/src/session/merge/mod.rs +0 -10
  157. package/dist-engine-src/src/session/merge/stats.rs +0 -61
  158. package/dist-engine-src/src/session/mod.rs +0 -30
  159. package/dist-engine-src/src/session/switch_branch.rs +0 -113
  160. package/dist-engine-src/src/session/transaction.rs +0 -557
  161. package/dist-engine-src/src/sql2/bind/classify.rs +0 -102
  162. package/dist-engine-src/src/sql2/bind/error.rs +0 -5
  163. package/dist-engine-src/src/sql2/bind/expr.rs +0 -29
  164. package/dist-engine-src/src/sql2/bind/mod.rs +0 -12
  165. package/dist-engine-src/src/sql2/bind/public_udf.rs +0 -306
  166. package/dist-engine-src/src/sql2/bind/read.rs +0 -65
  167. package/dist-engine-src/src/sql2/bind/statement.rs +0 -2236
  168. package/dist-engine-src/src/sql2/bind/table.rs +0 -273
  169. package/dist-engine-src/src/sql2/bind/write.rs +0 -86
  170. package/dist-engine-src/src/sql2/branch_scope.rs +0 -436
  171. package/dist-engine-src/src/sql2/catalog/capability.rs +0 -20
  172. package/dist-engine-src/src/sql2/catalog/entity_surface.rs +0 -296
  173. package/dist-engine-src/src/sql2/catalog/mod.rs +0 -15
  174. package/dist-engine-src/src/sql2/catalog/registry.rs +0 -556
  175. package/dist-engine-src/src/sql2/catalog/schema.rs +0 -88
  176. package/dist-engine-src/src/sql2/catalog/surface.rs +0 -41
  177. package/dist-engine-src/src/sql2/change_materialization.rs +0 -122
  178. package/dist-engine-src/src/sql2/context.rs +0 -317
  179. package/dist-engine-src/src/sql2/dml.rs +0 -148
  180. package/dist-engine-src/src/sql2/error.rs +0 -215
  181. package/dist-engine-src/src/sql2/exec/bound_public_write.rs +0 -1593
  182. package/dist-engine-src/src/sql2/exec/datafusion.rs +0 -5266
  183. package/dist-engine-src/src/sql2/exec/fast_write.rs +0 -82
  184. package/dist-engine-src/src/sql2/exec/mod.rs +0 -24
  185. package/dist-engine-src/src/sql2/exec/write.rs +0 -661
  186. package/dist-engine-src/src/sql2/filesystem_planner.rs +0 -1485
  187. package/dist-engine-src/src/sql2/filesystem_predicates.rs +0 -159
  188. package/dist-engine-src/src/sql2/filesystem_visibility.rs +0 -383
  189. package/dist-engine-src/src/sql2/history_projection.rs +0 -56
  190. package/dist-engine-src/src/sql2/history_route.rs +0 -661
  191. package/dist-engine-src/src/sql2/mod.rs +0 -52
  192. package/dist-engine-src/src/sql2/optimize/datafusion.rs +0 -1
  193. package/dist-engine-src/src/sql2/optimize/mod.rs +0 -2
  194. package/dist-engine-src/src/sql2/optimize/simple_write.rs +0 -116
  195. package/dist-engine-src/src/sql2/parse/mod.rs +0 -69
  196. package/dist-engine-src/src/sql2/parse/normalize.rs +0 -1
  197. package/dist-engine-src/src/sql2/plan/branch_scope.rs +0 -24
  198. package/dist-engine-src/src/sql2/plan/mod.rs +0 -5
  199. package/dist-engine-src/src/sql2/plan/predicate.rs +0 -22
  200. package/dist-engine-src/src/sql2/plan/write.rs +0 -147
  201. package/dist-engine-src/src/sql2/predicate_typecheck.rs +0 -504
  202. package/dist-engine-src/src/sql2/providers/branch.rs +0 -1206
  203. package/dist-engine-src/src/sql2/providers/change.rs +0 -445
  204. package/dist-engine-src/src/sql2/providers/directory.rs +0 -2422
  205. package/dist-engine-src/src/sql2/providers/directory_history.rs +0 -645
  206. package/dist-engine-src/src/sql2/providers/entity.rs +0 -1484
  207. package/dist-engine-src/src/sql2/providers/entity_history.rs +0 -452
  208. package/dist-engine-src/src/sql2/providers/file.rs +0 -3686
  209. package/dist-engine-src/src/sql2/providers/file_history.rs +0 -924
  210. package/dist-engine-src/src/sql2/providers/history.rs +0 -426
  211. package/dist-engine-src/src/sql2/providers/lix_state.rs +0 -2542
  212. package/dist-engine-src/src/sql2/providers/mod.rs +0 -508
  213. package/dist-engine-src/src/sql2/read_only.rs +0 -63
  214. package/dist-engine-src/src/sql2/record_batch.rs +0 -17
  215. package/dist-engine-src/src/sql2/result_metadata.rs +0 -29
  216. package/dist-engine-src/src/sql2/runtime.rs +0 -60
  217. package/dist-engine-src/src/sql2/session.rs +0 -83
  218. package/dist-engine-src/src/sql2/storage/constraints.rs +0 -1
  219. package/dist-engine-src/src/sql2/storage/mod.rs +0 -1
  220. package/dist-engine-src/src/sql2/test_support/differential.rs +0 -712
  221. package/dist-engine-src/src/sql2/test_support/generators.rs +0 -354
  222. package/dist-engine-src/src/sql2/test_support/mod.rs +0 -2
  223. package/dist-engine-src/src/sql2/udfs/common.rs +0 -295
  224. package/dist-engine-src/src/sql2/udfs/lix_active_branch_commit_id.rs +0 -53
  225. package/dist-engine-src/src/sql2/udfs/lix_empty_blob.rs +0 -47
  226. package/dist-engine-src/src/sql2/udfs/lix_json.rs +0 -100
  227. package/dist-engine-src/src/sql2/udfs/lix_json_get.rs +0 -99
  228. package/dist-engine-src/src/sql2/udfs/lix_json_get_text.rs +0 -99
  229. package/dist-engine-src/src/sql2/udfs/lix_text_decode.rs +0 -82
  230. package/dist-engine-src/src/sql2/udfs/lix_text_encode.rs +0 -85
  231. package/dist-engine-src/src/sql2/udfs/lix_timestamp.rs +0 -76
  232. package/dist-engine-src/src/sql2/udfs/lix_uuid_v7.rs +0 -76
  233. package/dist-engine-src/src/sql2/udfs/mod.rs +0 -86
  234. package/dist-engine-src/src/sql2/write_normalization.rs +0 -368
  235. package/dist-engine-src/src/storage/conformance.rs +0 -399
  236. package/dist-engine-src/src/storage/context.rs +0 -620
  237. package/dist-engine-src/src/storage/mod.rs +0 -52
  238. package/dist-engine-src/src/storage/point.rs +0 -440
  239. package/dist-engine-src/src/storage/read_scope.rs +0 -67
  240. package/dist-engine-src/src/storage/reader.rs +0 -867
  241. package/dist-engine-src/src/storage/scan.rs +0 -784
  242. package/dist-engine-src/src/storage/spaces.rs +0 -236
  243. package/dist-engine-src/src/storage/stats.rs +0 -80
  244. package/dist-engine-src/src/storage/write_set.rs +0 -962
  245. package/dist-engine-src/src/storage_bench.rs +0 -171
  246. package/dist-engine-src/src/test_support.rs +0 -450
  247. package/dist-engine-src/src/tracked_state/bench_support.rs +0 -394
  248. package/dist-engine-src/src/tracked_state/codec.rs +0 -1183
  249. package/dist-engine-src/src/tracked_state/commit_root_rebuild.rs +0 -358
  250. package/dist-engine-src/src/tracked_state/context.rs +0 -2801
  251. package/dist-engine-src/src/tracked_state/diff.rs +0 -2140
  252. package/dist-engine-src/src/tracked_state/merge.rs +0 -478
  253. package/dist-engine-src/src/tracked_state/mod.rs +0 -35
  254. package/dist-engine-src/src/tracked_state/row_materialization.rs +0 -275
  255. package/dist-engine-src/src/tracked_state/storage.rs +0 -427
  256. package/dist-engine-src/src/tracked_state/tree.rs +0 -3063
  257. package/dist-engine-src/src/tracked_state/types.rs +0 -238
  258. package/dist-engine-src/src/transaction/bench_support.rs +0 -407
  259. package/dist-engine-src/src/transaction/commit.rs +0 -1592
  260. package/dist-engine-src/src/transaction/context.rs +0 -1653
  261. package/dist-engine-src/src/transaction/mod.rs +0 -24
  262. package/dist-engine-src/src/transaction/normalization.rs +0 -877
  263. package/dist-engine-src/src/transaction/prep.rs +0 -37
  264. package/dist-engine-src/src/transaction/schema_resolver.rs +0 -163
  265. package/dist-engine-src/src/transaction/staging.rs +0 -1525
  266. package/dist-engine-src/src/transaction/types.rs +0 -403
  267. package/dist-engine-src/src/transaction/validation.rs +0 -5766
  268. package/dist-engine-src/src/untracked_state/codec.rs +0 -615
  269. package/dist-engine-src/src/untracked_state/context.rs +0 -98
  270. package/dist-engine-src/src/untracked_state/materialization.rs +0 -63
  271. package/dist-engine-src/src/untracked_state/mod.rs +0 -15
  272. package/dist-engine-src/src/untracked_state/storage.rs +0 -898
  273. package/dist-engine-src/src/untracked_state/types.rs +0 -146
  274. package/dist-engine-src/src/wasm/mod.rs +0 -60
@@ -1,1038 +0,0 @@
1
- #![allow(dead_code)]
2
-
3
- use crate::binary_cas::chunking::fastcdc_chunk_ranges;
4
- use crate::binary_cas::codec::{
5
- decode_binary_cas_chunk, decode_binary_cas_manifest, decode_binary_cas_manifest_chunk,
6
- encode_binary_cas_chunk, encode_binary_cas_manifest, encode_binary_cas_manifest_chunk,
7
- encode_binary_chunk_payload, BinaryCasManifest, BinaryChunkCodec,
8
- };
9
- use crate::binary_cas::{
10
- BlobBytesBatch, BlobExistsBatch, BlobHash, BlobLayout, BlobMetadata, BlobMetadataBatch,
11
- BlobWrite, BlobWriteReceipt,
12
- };
13
- use crate::storage::{PointReadPlan, ScanPlan, StorageRead, StorageSpace, StorageWriteSet};
14
- use crate::storage::{
15
- StorageGetOptions, StorageKey, StoragePrefix, StorageProjectedValue, StorageScanOptions,
16
- StorageSpaceId, StorageValue,
17
- };
18
- use crate::LixError;
19
- use bytes::Bytes;
20
- use std::collections::{HashMap, HashSet};
21
-
22
- pub(crate) const BINARY_CAS_MANIFEST_NAMESPACE: &str = "binary_cas.manifest";
23
- pub(crate) const BINARY_CAS_MANIFEST_CHUNK_NAMESPACE: &str = "binary_cas.manifest_chunk";
24
- pub(crate) const BINARY_CAS_CHUNK_NAMESPACE: &str = "binary_cas.chunk";
25
- pub(crate) const BINARY_CAS_MANIFEST_SPACE: StorageSpace =
26
- StorageSpace::new(StorageSpaceId(0x0005_0001), BINARY_CAS_MANIFEST_NAMESPACE);
27
- pub(crate) const BINARY_CAS_MANIFEST_CHUNK_SPACE: StorageSpace = StorageSpace::new(
28
- StorageSpaceId(0x0005_0002),
29
- BINARY_CAS_MANIFEST_CHUNK_NAMESPACE,
30
- );
31
- pub(crate) const BINARY_CAS_CHUNK_SPACE: StorageSpace =
32
- StorageSpace::new(StorageSpaceId(0x0005_0003), BINARY_CAS_CHUNK_NAMESPACE);
33
-
34
- #[derive(Debug, Clone, PartialEq, Eq)]
35
- pub(crate) struct KvBlobManifestChunk {
36
- pub(crate) chunk_hash: [u8; 32],
37
- pub(crate) chunk_size: u64,
38
- }
39
-
40
- #[derive(Debug, Clone, PartialEq, Eq)]
41
- pub(crate) struct KvChunk {
42
- pub(crate) codec: BinaryChunkCodec,
43
- pub(crate) uncompressed_len: u64,
44
- pub(crate) data: Vec<u8>,
45
- }
46
-
47
- pub(crate) async fn load_manifest(
48
- store: &impl StorageRead,
49
- blob_hash: BlobHash,
50
- ) -> Result<Option<BinaryCasManifest>, LixError> {
51
- let Some(bytes) = get_one(store, BINARY_CAS_MANIFEST_SPACE, manifest_key(blob_hash)).await?
52
- else {
53
- return Ok(None);
54
- };
55
- decode_binary_cas_manifest(&bytes).map(Some)
56
- }
57
-
58
- #[cfg(feature = "storage-benches")]
59
- pub(crate) async fn count_manifests(store: &impl StorageRead) -> Result<usize, LixError> {
60
- Ok(
61
- scan_all_values(store, BINARY_CAS_MANIFEST_SPACE, Vec::new())
62
- .await?
63
- .len(),
64
- )
65
- }
66
-
67
- pub(crate) fn stage_manifest(
68
- writes: &mut StorageWriteSet,
69
- blob_hash: BlobHash,
70
- manifest: &BinaryCasManifest,
71
- ) {
72
- writes.put(
73
- BINARY_CAS_MANIFEST_SPACE,
74
- key(manifest_key(blob_hash)),
75
- value(encode_binary_cas_manifest(manifest)),
76
- );
77
- }
78
-
79
- pub(crate) async fn scan_manifest_chunks(
80
- store: &impl StorageRead,
81
- blob_hash: BlobHash,
82
- ) -> Result<Vec<KvBlobManifestChunk>, LixError> {
83
- scan_all_values(
84
- store,
85
- BINARY_CAS_MANIFEST_CHUNK_SPACE,
86
- manifest_chunk_prefix(blob_hash),
87
- )
88
- .await?
89
- .into_iter()
90
- .map(|value| {
91
- let (chunk_hash, chunk_size) = decode_binary_cas_manifest_chunk(&value)?;
92
- Ok(KvBlobManifestChunk {
93
- chunk_hash,
94
- chunk_size,
95
- })
96
- })
97
- .collect()
98
- }
99
-
100
- pub(crate) fn stage_manifest_chunk(
101
- writes: &mut StorageWriteSet,
102
- blob_hash: BlobHash,
103
- chunk_index: u64,
104
- chunk: &KvBlobManifestChunk,
105
- ) {
106
- writes.put(
107
- BINARY_CAS_MANIFEST_CHUNK_SPACE,
108
- key(manifest_chunk_key(blob_hash, chunk_index)),
109
- value(encode_binary_cas_manifest_chunk(
110
- &chunk.chunk_hash,
111
- chunk.chunk_size,
112
- )),
113
- );
114
- }
115
-
116
- pub(crate) async fn load_chunk(
117
- store: &impl StorageRead,
118
- chunk_hash: BlobHash,
119
- ) -> Result<Option<KvChunk>, LixError> {
120
- let Some(bytes) = get_one(store, BINARY_CAS_CHUNK_SPACE, chunk_key(chunk_hash)).await? else {
121
- return Ok(None);
122
- };
123
- let (codec, uncompressed_len, payload) = decode_binary_cas_chunk(&bytes)?;
124
- Ok(Some(KvChunk {
125
- codec,
126
- uncompressed_len,
127
- data: payload.to_vec(),
128
- }))
129
- }
130
-
131
- pub(crate) fn stage_chunk(writes: &mut StorageWriteSet, chunk_hash: BlobHash, chunk: &KvChunk) {
132
- writes.put(
133
- BINARY_CAS_CHUNK_SPACE,
134
- key(chunk_key(chunk_hash)),
135
- value(encode_binary_cas_chunk(
136
- chunk.codec,
137
- chunk.uncompressed_len,
138
- &chunk.data,
139
- )),
140
- );
141
- }
142
-
143
- async fn get_one(
144
- store: &impl StorageRead,
145
- space: StorageSpace,
146
- key: Vec<u8>,
147
- ) -> Result<Option<Vec<u8>>, LixError> {
148
- let result = PointReadPlan::new(space, &[StorageKey(Bytes::from(key))])
149
- .materialize(store, StorageGetOptions::default())?;
150
- Ok(result
151
- .value
152
- .into_iter()
153
- .next()
154
- .flatten()
155
- .and_then(full_value))
156
- }
157
-
158
- async fn scan_all_values(
159
- store: &impl StorageRead,
160
- space: StorageSpace,
161
- prefix: Vec<u8>,
162
- ) -> Result<Vec<Vec<u8>>, LixError> {
163
- let plan = ScanPlan::prefix(
164
- space,
165
- StoragePrefix {
166
- bytes: Bytes::from(prefix),
167
- },
168
- );
169
- let mut values = Vec::new();
170
- let mut resume_after = None;
171
- loop {
172
- let page = plan.collect(
173
- store,
174
- StorageScanOptions {
175
- resume_after: resume_after.as_ref(),
176
- ..StorageScanOptions::default()
177
- },
178
- )?;
179
- resume_after = page.value.entries.last().map(|entry| entry.key.clone());
180
- values.extend(
181
- page.value
182
- .entries
183
- .into_iter()
184
- .filter_map(|entry| full_value(entry.value)),
185
- );
186
- if !page.value.has_more || resume_after.is_none() {
187
- break;
188
- }
189
- }
190
- Ok(values)
191
- }
192
-
193
- pub(crate) async fn load_metadata_many(
194
- store: &impl StorageRead,
195
- hashes: &[BlobHash],
196
- ) -> Result<BlobMetadataBatch, LixError> {
197
- if hashes.is_empty() {
198
- return Ok(BlobMetadataBatch::new(Vec::new()));
199
- }
200
- let rows = point_values(
201
- store,
202
- BINARY_CAS_MANIFEST_SPACE,
203
- hashes.iter().map(|hash| manifest_key(*hash)).collect(),
204
- )?;
205
- if rows.len() != hashes.len() {
206
- return Err(LixError::new(
207
- "LIX_ERROR_UNKNOWN",
208
- format!(
209
- "binary CAS metadata read expected {} rows, got {}",
210
- hashes.len(),
211
- rows.len()
212
- ),
213
- ));
214
- }
215
- let entries = rows
216
- .into_iter()
217
- .zip(hashes.iter().copied())
218
- .map(|(row, hash)| {
219
- row.map(|bytes| {
220
- let manifest = decode_binary_cas_manifest(&bytes)?;
221
- metadata_from_manifest(hash, manifest)
222
- })
223
- .transpose()
224
- })
225
- .collect::<Result<Vec<_>, _>>()?;
226
- Ok(BlobMetadataBatch::new(entries))
227
- }
228
-
229
- pub(crate) async fn exists_many(
230
- store: &impl StorageRead,
231
- hashes: &[BlobHash],
232
- ) -> Result<BlobExistsBatch, LixError> {
233
- Ok(BlobExistsBatch::new(
234
- load_metadata_many(store, hashes)
235
- .await?
236
- .into_vec()
237
- .into_iter()
238
- .map(|metadata| metadata.is_some())
239
- .collect(),
240
- ))
241
- }
242
-
243
- pub(crate) async fn load_bytes_many(
244
- store: &impl StorageRead,
245
- hashes: &[BlobHash],
246
- ) -> Result<BlobBytesBatch, LixError> {
247
- let metadata = load_metadata_many(store, hashes).await?.into_vec();
248
- let mut chunked_manifests = Vec::new();
249
- let mut requested_chunks = Vec::new();
250
- let mut seen_chunks = HashSet::new();
251
-
252
- for (index, metadata) in metadata.iter().enumerate() {
253
- let Some(metadata) = metadata else {
254
- continue;
255
- };
256
- match &metadata.layout {
257
- BlobLayout::Empty => {}
258
- BlobLayout::SingleChunk { chunk_hash } => {
259
- if seen_chunks.insert(*chunk_hash) {
260
- requested_chunks.push(*chunk_hash);
261
- }
262
- }
263
- BlobLayout::Chunked { chunk_count } => {
264
- let manifest_chunks = scan_manifest_chunks(store, metadata.hash).await?;
265
- if manifest_chunks.len() != *chunk_count as usize {
266
- return Err(LixError::new(
267
- "LIX_ERROR_UNKNOWN",
268
- format!(
269
- "binary CAS blob '{}' expected {} chunks, found {}",
270
- metadata.hash.to_hex(),
271
- chunk_count,
272
- manifest_chunks.len()
273
- ),
274
- ));
275
- }
276
- for manifest_chunk in &manifest_chunks {
277
- let chunk_hash = BlobHash::from_bytes(manifest_chunk.chunk_hash);
278
- if seen_chunks.insert(chunk_hash) {
279
- requested_chunks.push(chunk_hash);
280
- }
281
- }
282
- chunked_manifests.push((index, manifest_chunks));
283
- }
284
- }
285
- }
286
-
287
- let chunk_rows = load_chunk_rows(store, &requested_chunks).await?;
288
- let chunk_rows_by_hash = requested_chunks
289
- .into_iter()
290
- .zip(chunk_rows.into_iter())
291
- .collect::<HashMap<_, _>>();
292
- let chunked_manifests_by_index = chunked_manifests
293
- .into_iter()
294
- .collect::<HashMap<usize, Vec<KvBlobManifestChunk>>>();
295
-
296
- let entries = metadata
297
- .into_iter()
298
- .enumerate()
299
- .map(|(index, metadata)| {
300
- metadata
301
- .map(|metadata| {
302
- assemble_blob_bytes(
303
- &metadata,
304
- &chunk_rows_by_hash,
305
- chunked_manifests_by_index.get(&index),
306
- )
307
- })
308
- .transpose()
309
- })
310
- .collect::<Result<Vec<_>, _>>()?;
311
- Ok(BlobBytesBatch::new(entries))
312
- }
313
-
314
- async fn load_chunk_rows(
315
- store: &impl StorageRead,
316
- hashes: &[BlobHash],
317
- ) -> Result<Vec<Option<Vec<u8>>>, LixError> {
318
- if hashes.is_empty() {
319
- return Ok(Vec::new());
320
- }
321
- point_values(
322
- store,
323
- BINARY_CAS_CHUNK_SPACE,
324
- hashes.iter().map(|hash| chunk_key(*hash)).collect(),
325
- )
326
- }
327
-
328
- fn point_values(
329
- store: &impl StorageRead,
330
- space: StorageSpace,
331
- keys: Vec<Vec<u8>>,
332
- ) -> Result<Vec<Option<Vec<u8>>>, LixError> {
333
- let keys = keys
334
- .into_iter()
335
- .map(|key| StorageKey(Bytes::from(key)))
336
- .collect::<Vec<_>>();
337
- let result =
338
- PointReadPlan::new(space, &keys).materialize(store, StorageGetOptions::default())?;
339
- Ok(result
340
- .value
341
- .into_iter()
342
- .map(|value| value.and_then(full_value))
343
- .collect())
344
- }
345
-
346
- fn key(bytes: Vec<u8>) -> StorageKey {
347
- StorageKey(Bytes::from(bytes))
348
- }
349
-
350
- fn value(bytes: Vec<u8>) -> StorageValue {
351
- StorageValue {
352
- bytes: Bytes::from(bytes),
353
- }
354
- }
355
-
356
- fn full_value(value: StorageProjectedValue) -> Option<Vec<u8>> {
357
- match value {
358
- StorageProjectedValue::FullValue(bytes) => Some(bytes.to_vec()),
359
- StorageProjectedValue::KeyOnly => None,
360
- }
361
- }
362
-
363
- fn assemble_blob_bytes(
364
- metadata: &BlobMetadata,
365
- chunk_rows_by_hash: &HashMap<BlobHash, Option<Vec<u8>>>,
366
- chunked_manifest: Option<&Vec<KvBlobManifestChunk>>,
367
- ) -> Result<Vec<u8>, LixError> {
368
- let expected_blob_size = persisted_size_to_usize(metadata.size_bytes, "binary CAS blob")?;
369
- let bytes = match &metadata.layout {
370
- BlobLayout::Empty => {
371
- if metadata.hash != BlobHash::from_content(&[]) {
372
- return Err(LixError::new(
373
- "LIX_ERROR_UNKNOWN",
374
- format!(
375
- "binary CAS blob '{}' failed content-address verification",
376
- metadata.hash.to_hex()
377
- ),
378
- ));
379
- }
380
- Vec::new()
381
- }
382
- BlobLayout::SingleChunk { chunk_hash } => {
383
- let chunk = decode_chunk_from_map(
384
- chunk_rows_by_hash,
385
- metadata.hash,
386
- *chunk_hash,
387
- expected_blob_size,
388
- )?;
389
- if *chunk_hash != metadata.hash && BlobHash::from_content(&chunk) != metadata.hash {
390
- return Err(LixError::new(
391
- "LIX_ERROR_UNKNOWN",
392
- format!(
393
- "binary CAS blob '{}' failed content-address verification",
394
- metadata.hash.to_hex()
395
- ),
396
- ));
397
- }
398
- chunk
399
- }
400
- BlobLayout::Chunked { chunk_count } => {
401
- let Some(manifest_chunks) = chunked_manifest else {
402
- return Err(LixError::new(
403
- "LIX_ERROR_UNKNOWN",
404
- format!(
405
- "binary CAS blob '{}' missing chunk manifest",
406
- metadata.hash.to_hex()
407
- ),
408
- ));
409
- };
410
- if manifest_chunks.len() != *chunk_count as usize {
411
- return Err(LixError::new(
412
- "LIX_ERROR_UNKNOWN",
413
- format!(
414
- "binary CAS blob '{}' expected {} chunks, found {}",
415
- metadata.hash.to_hex(),
416
- chunk_count,
417
- manifest_chunks.len()
418
- ),
419
- ));
420
- }
421
- let mut out = Vec::with_capacity(expected_blob_size);
422
- for manifest_chunk in manifest_chunks {
423
- let chunk_hash = BlobHash::from_bytes(manifest_chunk.chunk_hash);
424
- let expected_chunk_size =
425
- persisted_size_to_usize(manifest_chunk.chunk_size, "binary CAS chunk")?;
426
- let chunk = decode_chunk_from_map(
427
- chunk_rows_by_hash,
428
- metadata.hash,
429
- chunk_hash,
430
- expected_chunk_size,
431
- )?;
432
- out.extend_from_slice(&chunk);
433
- }
434
- if out.len() != expected_blob_size {
435
- return Err(LixError::new(
436
- "LIX_ERROR_UNKNOWN",
437
- format!(
438
- "binary CAS blob '{}' expected {} bytes, decoded {} bytes",
439
- metadata.hash.to_hex(),
440
- expected_blob_size,
441
- out.len()
442
- ),
443
- ));
444
- }
445
- if BlobHash::from_content(&out) != metadata.hash {
446
- return Err(LixError::new(
447
- "LIX_ERROR_UNKNOWN",
448
- format!(
449
- "binary CAS blob '{}' failed content-address verification",
450
- metadata.hash.to_hex()
451
- ),
452
- ));
453
- }
454
- out
455
- }
456
- };
457
- Ok(bytes)
458
- }
459
-
460
- fn decode_chunk_from_map(
461
- chunk_rows_by_hash: &HashMap<BlobHash, Option<Vec<u8>>>,
462
- blob_hash: BlobHash,
463
- chunk_hash: BlobHash,
464
- expected_chunk_size: usize,
465
- ) -> Result<Vec<u8>, LixError> {
466
- let Some(Some(chunk_bytes)) = chunk_rows_by_hash.get(&chunk_hash) else {
467
- return Err(LixError::new(
468
- "LIX_ERROR_UNKNOWN",
469
- format!(
470
- "binary CAS chunk '{}' is missing for blob '{}'",
471
- chunk_hash.to_hex(),
472
- blob_hash.to_hex()
473
- ),
474
- ));
475
- };
476
- decode_and_verify_chunk(chunk_bytes, expected_chunk_size, blob_hash, chunk_hash)
477
- }
478
-
479
- fn decode_and_verify_chunk(
480
- chunk_bytes: &[u8],
481
- expected_chunk_size: usize,
482
- blob_hash: BlobHash,
483
- chunk_hash: BlobHash,
484
- ) -> Result<Vec<u8>, LixError> {
485
- let (codec, uncompressed_len, chunk_payload) = decode_binary_cas_chunk(chunk_bytes)?;
486
- if uncompressed_len != expected_chunk_size as u64 {
487
- return Err(LixError::new(
488
- "LIX_ERROR_UNKNOWN",
489
- format!(
490
- "binary CAS chunk '{}' for blob '{}' expected {} uncompressed bytes, row says {}",
491
- chunk_hash.to_hex(),
492
- blob_hash.to_hex(),
493
- expected_chunk_size,
494
- uncompressed_len
495
- ),
496
- ));
497
- }
498
- let BinaryChunkCodec::Raw = codec;
499
- if chunk_payload.len() != expected_chunk_size {
500
- return Err(LixError::new(
501
- "LIX_ERROR_UNKNOWN",
502
- format!(
503
- "binary CAS chunk '{}' for blob '{}' expected {} decoded bytes, got {}",
504
- chunk_hash.to_hex(),
505
- blob_hash.to_hex(),
506
- expected_chunk_size,
507
- chunk_payload.len()
508
- ),
509
- ));
510
- }
511
- if BlobHash::from_content(chunk_payload) != chunk_hash {
512
- return Err(LixError::new(
513
- "LIX_ERROR_UNKNOWN",
514
- format!(
515
- "binary CAS chunk '{}' for blob '{}' failed content-address verification",
516
- chunk_hash.to_hex(),
517
- blob_hash.to_hex()
518
- ),
519
- ));
520
- }
521
- Ok(chunk_payload.to_vec())
522
- }
523
-
524
- pub(crate) fn stage_blob_write(
525
- writes: &mut StorageWriteSet,
526
- blob_hashes: &mut HashSet<[u8; 32]>,
527
- chunk_keys: &mut HashSet<Vec<u8>>,
528
- write: &BlobWrite<'_>,
529
- ) -> Result<BlobWriteReceipt, LixError> {
530
- let blob_hash = BlobHash::from_content(write.bytes);
531
- let chunk_ranges = fastcdc_chunk_ranges(write.bytes);
532
- let layout = match chunk_ranges.as_slice() {
533
- [] => BlobLayout::Empty,
534
- [(start, end)] => BlobLayout::SingleChunk {
535
- chunk_hash: BlobHash::from_content(&write.bytes[*start..*end]),
536
- },
537
- _ => BlobLayout::Chunked {
538
- chunk_count: u32::try_from(chunk_ranges.len()).map_err(|_| {
539
- LixError::new(
540
- "LIX_ERROR_UNKNOWN",
541
- "binary CAS blob has too many chunks for manifest".to_string(),
542
- )
543
- })?,
544
- },
545
- };
546
- let receipt = BlobWriteReceipt {
547
- hash: blob_hash,
548
- size_bytes: write.bytes.len() as u64,
549
- layout: layout.clone(),
550
- };
551
- if !blob_hashes.insert(blob_hash.into_bytes()) {
552
- return Ok(receipt);
553
- }
554
-
555
- match &layout {
556
- BlobLayout::Empty => {
557
- stage_manifest(
558
- writes,
559
- blob_hash,
560
- &BinaryCasManifest::Empty { size_bytes: 0 },
561
- );
562
- }
563
- BlobLayout::SingleChunk { chunk_hash } => {
564
- let chunk_hash = *chunk_hash;
565
- stage_manifest(
566
- writes,
567
- blob_hash,
568
- &BinaryCasManifest::SingleChunk {
569
- size_bytes: write.bytes.len() as u64,
570
- chunk_hash: chunk_hash.into_bytes(),
571
- },
572
- );
573
- if chunk_keys.insert(chunk_key(chunk_hash)) {
574
- let encoded_chunk = encode_binary_chunk_payload(write.bytes);
575
- stage_chunk(
576
- writes,
577
- chunk_hash,
578
- &KvChunk {
579
- codec: encoded_chunk.codec,
580
- uncompressed_len: write.bytes.len() as u64,
581
- data: encoded_chunk.data,
582
- },
583
- );
584
- }
585
- }
586
- BlobLayout::Chunked { chunk_count } => {
587
- stage_manifest(
588
- writes,
589
- blob_hash,
590
- &BinaryCasManifest::Chunked {
591
- size_bytes: write.bytes.len() as u64,
592
- chunk_count: *chunk_count,
593
- },
594
- );
595
-
596
- for (chunk_index, (start, end)) in chunk_ranges.into_iter().enumerate() {
597
- let chunk_data = &write.bytes[start..end];
598
- let chunk_hash = BlobHash::from_content(chunk_data);
599
- let chunk_key = chunk_key(chunk_hash);
600
- if chunk_keys.insert(chunk_key.clone()) {
601
- let encoded_chunk = encode_binary_chunk_payload(chunk_data);
602
- stage_chunk(
603
- writes,
604
- chunk_hash,
605
- &KvChunk {
606
- codec: encoded_chunk.codec,
607
- uncompressed_len: chunk_data.len() as u64,
608
- data: encoded_chunk.data,
609
- },
610
- );
611
- }
612
-
613
- stage_manifest_chunk(
614
- writes,
615
- blob_hash,
616
- chunk_index as u64,
617
- &KvBlobManifestChunk {
618
- chunk_hash: *chunk_hash.as_bytes(),
619
- chunk_size: chunk_data.len() as u64,
620
- },
621
- );
622
- }
623
- }
624
- }
625
- Ok(receipt)
626
- }
627
-
628
- fn metadata_from_manifest(
629
- hash: BlobHash,
630
- manifest: BinaryCasManifest,
631
- ) -> Result<BlobMetadata, LixError> {
632
- let size_bytes = manifest.size_bytes();
633
- let layout = match manifest {
634
- BinaryCasManifest::Empty { size_bytes } => {
635
- if size_bytes != 0 {
636
- return Err(LixError::new(
637
- "LIX_ERROR_UNKNOWN",
638
- format!(
639
- "binary CAS empty blob '{}' has nonzero size {size_bytes}",
640
- hash.to_hex()
641
- ),
642
- ));
643
- }
644
- BlobLayout::Empty
645
- }
646
- BinaryCasManifest::SingleChunk { chunk_hash, .. } => BlobLayout::SingleChunk {
647
- chunk_hash: BlobHash::from_bytes(chunk_hash),
648
- },
649
- BinaryCasManifest::Chunked { chunk_count, .. } => BlobLayout::Chunked { chunk_count },
650
- };
651
- Ok(BlobMetadata {
652
- hash,
653
- size_bytes,
654
- layout,
655
- })
656
- }
657
-
658
- fn manifest_key(blob_hash: BlobHash) -> Vec<u8> {
659
- blob_hash.as_bytes().to_vec()
660
- }
661
-
662
- fn manifest_chunk_prefix(blob_hash: BlobHash) -> Vec<u8> {
663
- blob_hash.as_bytes().to_vec()
664
- }
665
-
666
- fn manifest_chunk_key(blob_hash: BlobHash, chunk_index: u64) -> Vec<u8> {
667
- let mut out = Vec::with_capacity(40);
668
- out.extend_from_slice(blob_hash.as_bytes());
669
- out.extend_from_slice(&chunk_index.to_be_bytes());
670
- out
671
- }
672
-
673
- fn chunk_key(chunk_hash: BlobHash) -> Vec<u8> {
674
- chunk_hash.as_bytes().to_vec()
675
- }
676
-
677
- fn persisted_size_to_usize(size: u64, label: &str) -> Result<usize, LixError> {
678
- usize::try_from(size).map_err(|_| {
679
- LixError::new(
680
- "LIX_ERROR_UNKNOWN",
681
- format!("{label} size {size} does not fit in this runtime"),
682
- )
683
- })
684
- }
685
-
686
- #[cfg(test)]
687
- mod tests {
688
- use super::*;
689
- use crate::binary_cas::BinaryCasContext;
690
- use crate::storage::StorageContext;
691
- use crate::storage::{InMemoryStorageBackend, StorageReadOptions, StorageWriteOptions};
692
-
693
- #[tokio::test]
694
- async fn stores_manifest_chunks_in_scan_order() {
695
- let storage = StorageContext::new(InMemoryStorageBackend::new());
696
- let blob_hash = BlobHash::from_content(b"blob-a");
697
- let chunk_a_hash = BlobHash::from_content(b"chunk-a").into_bytes();
698
- let chunk_b_hash = BlobHash::from_content(b"chunk-b").into_bytes();
699
-
700
- {
701
- let mut writes = storage.new_write_set();
702
- stage_manifest(
703
- &mut writes,
704
- blob_hash,
705
- &BinaryCasManifest::Chunked {
706
- size_bytes: 12,
707
- chunk_count: 2,
708
- },
709
- );
710
- stage_manifest_chunk(
711
- &mut writes,
712
- blob_hash,
713
- 1,
714
- &KvBlobManifestChunk {
715
- chunk_hash: chunk_b_hash,
716
- chunk_size: 6,
717
- },
718
- );
719
- stage_manifest_chunk(
720
- &mut writes,
721
- blob_hash,
722
- 0,
723
- &KvBlobManifestChunk {
724
- chunk_hash: chunk_a_hash,
725
- chunk_size: 6,
726
- },
727
- );
728
- storage
729
- .commit_write_set(writes, StorageWriteOptions::default())
730
- .expect("manifest writes should commit");
731
- }
732
-
733
- let store = storage
734
- .begin_read(StorageReadOptions::default())
735
- .expect("read should open");
736
- assert_eq!(
737
- load_manifest(&store, blob_hash)
738
- .await
739
- .expect("manifest should load"),
740
- Some(BinaryCasManifest::Chunked {
741
- size_bytes: 12,
742
- chunk_count: 2,
743
- })
744
- );
745
- let store = storage
746
- .begin_read(StorageReadOptions::default())
747
- .expect("read should open");
748
- assert_eq!(
749
- scan_manifest_chunks(&store, blob_hash)
750
- .await
751
- .expect("manifest chunks should scan"),
752
- vec![
753
- KvBlobManifestChunk {
754
- chunk_hash: chunk_a_hash,
755
- chunk_size: 6,
756
- },
757
- KvBlobManifestChunk {
758
- chunk_hash: chunk_b_hash,
759
- chunk_size: 6,
760
- },
761
- ]
762
- );
763
- }
764
-
765
- #[tokio::test]
766
- async fn stores_encoded_chunks_by_chunk_hash() {
767
- let storage = StorageContext::new(InMemoryStorageBackend::new());
768
- let chunk = KvChunk {
769
- codec: BinaryChunkCodec::Raw,
770
- uncompressed_len: 5,
771
- data: b"hello".to_vec(),
772
- };
773
- let chunk_hash = BlobHash::from_content(b"chunk-a");
774
-
775
- {
776
- let mut writes = storage.new_write_set();
777
- stage_chunk(&mut writes, chunk_hash, &chunk);
778
- storage
779
- .commit_write_set(writes, StorageWriteOptions::default())
780
- .expect("chunk should commit");
781
- }
782
-
783
- let store = storage
784
- .begin_read(StorageReadOptions::default())
785
- .expect("read should open");
786
- assert_eq!(
787
- load_chunk(&store, chunk_hash)
788
- .await
789
- .expect("chunk should load"),
790
- Some(chunk)
791
- );
792
- }
793
-
794
- #[test]
795
- fn binary_hash_keys_are_compact_and_manifest_chunks_sort_by_index() {
796
- let blob_hash = BlobHash::from_content(b"blob");
797
- let manifest_key = manifest_key(blob_hash);
798
- let chunk_key = chunk_key(BlobHash::from_content(b"chunk"));
799
- let first = manifest_chunk_key(blob_hash, 1);
800
- let second = manifest_chunk_key(blob_hash, 2);
801
- let later = manifest_chunk_key(blob_hash, 10);
802
-
803
- assert_eq!(manifest_key.len(), 32);
804
- assert_eq!(chunk_key.len(), 32);
805
- assert_eq!(first.len(), 40);
806
- assert!(first < second);
807
- assert!(second < later);
808
- }
809
-
810
- #[tokio::test]
811
- async fn public_kv_api_roundtrips_blob_bytes() {
812
- let storage = StorageContext::new(InMemoryStorageBackend::new());
813
- let data = b"hello chunked kv cas";
814
- let blob_hash = BlobHash::from_content(data);
815
-
816
- {
817
- let mut writes = storage.new_write_set();
818
- let mut writer = BinaryCasContext::new().writer(&mut writes);
819
- writer.stage_bytes(data).expect("blob write should stage");
820
- storage
821
- .commit_write_set(writes, StorageWriteOptions::default())
822
- .expect("blob write should commit");
823
- }
824
-
825
- let store = storage
826
- .begin_read(StorageReadOptions::default())
827
- .expect("read should open");
828
- assert_eq!(
829
- load_bytes_many(&store, &[blob_hash])
830
- .await
831
- .expect("blob should load")
832
- .into_vec(),
833
- vec![Some(data.to_vec())]
834
- );
835
- let store = storage
836
- .begin_read(StorageReadOptions::default())
837
- .expect("read should open");
838
- assert_eq!(
839
- load_manifest(&store, blob_hash)
840
- .await
841
- .expect("manifest should load"),
842
- Some(BinaryCasManifest::SingleChunk {
843
- size_bytes: data.len() as u64,
844
- chunk_hash: BlobHash::from_content(data).into_bytes(),
845
- })
846
- );
847
- let store = storage
848
- .begin_read(StorageReadOptions::default())
849
- .expect("read should open");
850
- assert_eq!(
851
- scan_manifest_chunks(&store, blob_hash)
852
- .await
853
- .expect("single-chunk blob should not spill manifest chunks"),
854
- Vec::<KvBlobManifestChunk>::new()
855
- );
856
- let store = storage
857
- .begin_read(StorageReadOptions::default())
858
- .expect("read should open");
859
- assert_eq!(
860
- exists_many(&store, &[blob_hash])
861
- .await
862
- .expect("blob exists should succeed")
863
- .into_vec(),
864
- vec![true]
865
- );
866
- }
867
-
868
- #[tokio::test]
869
- async fn read_rejects_chunk_bytes_that_do_not_match_manifest_hash() {
870
- let storage = StorageContext::new(InMemoryStorageBackend::new());
871
- let data = b"same length";
872
- let corrupted = b"SAME length";
873
- let blob_hash = BlobHash::from_content(data);
874
-
875
- {
876
- let mut writes = storage.new_write_set();
877
- let mut writer = BinaryCasContext::new().writer(&mut writes);
878
- writer.stage_bytes(data).expect("blob write should stage");
879
- storage
880
- .commit_write_set(writes, StorageWriteOptions::default())
881
- .expect("blob write should commit");
882
- }
883
-
884
- {
885
- let mut writes = storage.new_write_set();
886
- writes.put(
887
- BINARY_CAS_CHUNK_SPACE,
888
- key(chunk_key(blob_hash)),
889
- value(encode_binary_cas_chunk(
890
- BinaryChunkCodec::Raw,
891
- corrupted.len() as u64,
892
- corrupted,
893
- )),
894
- );
895
- storage
896
- .commit_write_set(writes, StorageWriteOptions::default())
897
- .expect("corrupt chunk should overwrite");
898
- }
899
-
900
- let store = storage
901
- .begin_read(StorageReadOptions::default())
902
- .expect("read should open");
903
- let error = load_bytes_many(&store, &[blob_hash])
904
- .await
905
- .expect_err("corrupt chunk should be rejected");
906
- assert!(error
907
- .message
908
- .contains("failed content-address verification"));
909
- }
910
-
911
- #[tokio::test]
912
- async fn read_rejects_manifest_that_assembles_wrong_blob_hash() {
913
- let storage = StorageContext::new(InMemoryStorageBackend::new());
914
- let expected = b"expected bytes";
915
- let substituted = b"different byte";
916
- assert_eq!(expected.len(), substituted.len());
917
- let expected_blob_hash = BlobHash::from_content(expected);
918
- let substituted_chunk_hash = BlobHash::from_content(substituted);
919
-
920
- {
921
- let mut writes = storage.new_write_set();
922
- stage_manifest(
923
- &mut writes,
924
- expected_blob_hash,
925
- &BinaryCasManifest::Chunked {
926
- size_bytes: expected.len() as u64,
927
- chunk_count: 1,
928
- },
929
- );
930
- stage_manifest_chunk(
931
- &mut writes,
932
- expected_blob_hash,
933
- 0,
934
- &KvBlobManifestChunk {
935
- chunk_hash: BlobHash::from_content(substituted).into_bytes(),
936
- chunk_size: substituted.len() as u64,
937
- },
938
- );
939
- stage_chunk(
940
- &mut writes,
941
- substituted_chunk_hash,
942
- &KvChunk {
943
- codec: BinaryChunkCodec::Raw,
944
- uncompressed_len: substituted.len() as u64,
945
- data: substituted.to_vec(),
946
- },
947
- );
948
- storage
949
- .commit_write_set(writes, StorageWriteOptions::default())
950
- .expect("wrong manifest fixture should commit");
951
- }
952
-
953
- let store = storage
954
- .begin_read(StorageReadOptions::default())
955
- .expect("read should open");
956
- let error = load_bytes_many(&store, &[expected_blob_hash])
957
- .await
958
- .expect_err("wrong assembled blob should be rejected");
959
- assert!(error
960
- .message
961
- .contains("failed content-address verification"));
962
- }
963
-
964
- #[tokio::test]
965
- async fn public_kv_api_roundtrips_empty_blob() {
966
- let storage = StorageContext::new(InMemoryStorageBackend::new());
967
- let data = b"";
968
- let blob_hash = BlobHash::from_content(data);
969
-
970
- {
971
- let mut writes = storage.new_write_set();
972
- let mut writer = BinaryCasContext::new().writer(&mut writes);
973
- writer.stage_bytes(data).expect("blob write should stage");
974
- storage
975
- .commit_write_set(writes, StorageWriteOptions::default())
976
- .expect("blob write should commit");
977
- }
978
-
979
- let store = storage
980
- .begin_read(StorageReadOptions::default())
981
- .expect("read should open");
982
- assert_eq!(
983
- load_bytes_many(&store, &[blob_hash])
984
- .await
985
- .expect("empty blob should load")
986
- .into_vec(),
987
- vec![Some(Vec::new())]
988
- );
989
- let store = storage
990
- .begin_read(StorageReadOptions::default())
991
- .expect("read should open");
992
- assert_eq!(
993
- scan_manifest_chunks(&store, blob_hash)
994
- .await
995
- .expect("empty blob chunks should scan"),
996
- Vec::<KvBlobManifestChunk>::new()
997
- );
998
- }
999
-
1000
- #[tokio::test]
1001
- async fn public_kv_api_roundtrips_multi_chunk_blob() {
1002
- let storage = StorageContext::new(InMemoryStorageBackend::new());
1003
- let data = (0..600_000)
1004
- .map(|index| (index % 251) as u8)
1005
- .collect::<Vec<_>>();
1006
- let blob_hash = BlobHash::from_content(&data);
1007
-
1008
- {
1009
- let mut writes = storage.new_write_set();
1010
- let mut writer = BinaryCasContext::new().writer(&mut writes);
1011
- writer.stage_bytes(&data).expect("blob write should stage");
1012
- storage
1013
- .commit_write_set(writes, StorageWriteOptions::default())
1014
- .expect("blob write should commit");
1015
- }
1016
-
1017
- let store = storage
1018
- .begin_read(StorageReadOptions::default())
1019
- .expect("read should open");
1020
- assert_eq!(
1021
- load_bytes_many(&store, &[blob_hash])
1022
- .await
1023
- .expect("large blob should load")
1024
- .into_vec(),
1025
- vec![Some(data.clone())]
1026
- );
1027
- let store = storage
1028
- .begin_read(StorageReadOptions::default())
1029
- .expect("read should open");
1030
- assert!(
1031
- scan_manifest_chunks(&store, blob_hash)
1032
- .await
1033
- .expect("large blob chunks should scan")
1034
- .len()
1035
- > 1
1036
- );
1037
- }
1038
- }