@lix-js/sdk 0.6.0-preview.1 → 0.6.0-preview.3

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