@lix-js/sdk 0.6.0-preview.0 → 0.6.0-preview.2

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 (196) hide show
  1. package/README.md +9 -0
  2. package/SKILL.md +468 -0
  3. package/dist/engine-wasm/index.d.ts +15 -11
  4. package/dist/engine-wasm/index.js +105 -38
  5. package/dist/engine-wasm/wasm/lix_engine.d.ts +14 -2
  6. package/dist/engine-wasm/wasm/lix_engine.js +18 -17
  7. package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
  8. package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +2 -1
  9. package/dist/generated/builtin-schemas.d.ts +31 -41
  10. package/dist/generated/builtin-schemas.js +52 -56
  11. package/dist/open-lix.d.ts +141 -24
  12. package/dist/open-lix.js +199 -35
  13. package/dist/sqlite/index.js +99 -22
  14. package/dist-engine-src/README.md +18 -0
  15. package/dist-engine-src/src/backend/kv.rs +358 -0
  16. package/dist-engine-src/src/backend/mod.rs +12 -0
  17. package/dist-engine-src/src/backend/testing.rs +658 -0
  18. package/dist-engine-src/src/backend/types.rs +96 -0
  19. package/dist-engine-src/src/binary_cas/chunking.rs +31 -0
  20. package/dist-engine-src/src/binary_cas/codec.rs +346 -0
  21. package/dist-engine-src/src/binary_cas/context.rs +139 -0
  22. package/dist-engine-src/src/binary_cas/kv.rs +1063 -0
  23. package/dist-engine-src/src/binary_cas/mod.rs +11 -0
  24. package/dist-engine-src/src/binary_cas/types.rs +127 -0
  25. package/dist-engine-src/src/cel/context.rs +86 -0
  26. package/dist-engine-src/src/cel/error.rs +19 -0
  27. package/dist-engine-src/src/cel/mod.rs +8 -0
  28. package/dist-engine-src/src/cel/provider.rs +9 -0
  29. package/dist-engine-src/src/cel/runtime.rs +167 -0
  30. package/dist-engine-src/src/cel/value.rs +50 -0
  31. package/dist-engine-src/src/changelog/codec.rs +321 -0
  32. package/dist-engine-src/src/changelog/context.rs +92 -0
  33. package/dist-engine-src/src/changelog/materialization.rs +121 -0
  34. package/dist-engine-src/src/changelog/mod.rs +13 -0
  35. package/dist-engine-src/src/changelog/reader.rs +20 -0
  36. package/dist-engine-src/src/changelog/storage.rs +220 -0
  37. package/dist-engine-src/src/changelog/types.rs +38 -0
  38. package/dist-engine-src/src/commit_graph/context.rs +1588 -0
  39. package/dist-engine-src/src/commit_graph/mod.rs +12 -0
  40. package/dist-engine-src/src/commit_graph/types.rs +145 -0
  41. package/dist-engine-src/src/commit_graph/walker.rs +780 -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 +135 -0
  46. package/dist-engine-src/src/common/metadata.rs +35 -0
  47. package/dist-engine-src/src/common/mod.rs +23 -0
  48. package/dist-engine-src/src/common/types.rs +105 -0
  49. package/dist-engine-src/src/common/wire.rs +222 -0
  50. package/dist-engine-src/src/engine.rs +239 -0
  51. package/dist-engine-src/src/entity_identity.rs +285 -0
  52. package/dist-engine-src/src/functions/context.rs +327 -0
  53. package/dist-engine-src/src/functions/deterministic.rs +113 -0
  54. package/dist-engine-src/src/functions/mod.rs +18 -0
  55. package/dist-engine-src/src/functions/provider.rs +130 -0
  56. package/dist-engine-src/src/functions/state.rs +363 -0
  57. package/dist-engine-src/src/functions/types.rs +37 -0
  58. package/dist-engine-src/src/init.rs +505 -0
  59. package/dist-engine-src/src/json_store/compression.rs +77 -0
  60. package/dist-engine-src/src/json_store/context.rs +129 -0
  61. package/dist-engine-src/src/json_store/encoded.rs +15 -0
  62. package/dist-engine-src/src/json_store/mod.rs +9 -0
  63. package/dist-engine-src/src/json_store/store.rs +236 -0
  64. package/dist-engine-src/src/json_store/types.rs +52 -0
  65. package/dist-engine-src/src/lib.rs +61 -0
  66. package/dist-engine-src/src/live_state/context.rs +2241 -0
  67. package/dist-engine-src/src/live_state/mod.rs +15 -0
  68. package/dist-engine-src/src/live_state/overlay.rs +75 -0
  69. package/dist-engine-src/src/live_state/reader.rs +23 -0
  70. package/dist-engine-src/src/live_state/types.rs +239 -0
  71. package/dist-engine-src/src/live_state/visibility.rs +218 -0
  72. package/dist-engine-src/src/plugin/archive.rs +441 -0
  73. package/dist-engine-src/src/plugin/component.rs +183 -0
  74. package/dist-engine-src/src/plugin/install.rs +637 -0
  75. package/dist-engine-src/src/plugin/manifest.rs +516 -0
  76. package/dist-engine-src/src/plugin/materializer.rs +477 -0
  77. package/dist-engine-src/src/plugin/mod.rs +33 -0
  78. package/dist-engine-src/src/plugin/plugin_manifest.json +119 -0
  79. package/dist-engine-src/src/plugin/storage.rs +74 -0
  80. package/dist-engine-src/src/schema/annotations/defaults.rs +280 -0
  81. package/dist-engine-src/src/schema/annotations/mod.rs +1 -0
  82. package/dist-engine-src/src/schema/builtin/lix_account.json +22 -0
  83. package/dist-engine-src/src/schema/builtin/lix_active_account.json +30 -0
  84. package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +30 -0
  85. package/dist-engine-src/src/schema/builtin/lix_change.json +62 -0
  86. package/dist-engine-src/src/schema/builtin/lix_change_author.json +46 -0
  87. package/dist-engine-src/src/schema/builtin/lix_change_set.json +18 -0
  88. package/dist-engine-src/src/schema/builtin/lix_change_set_element.json +75 -0
  89. package/dist-engine-src/src/schema/builtin/lix_commit.json +62 -0
  90. package/dist-engine-src/src/schema/builtin/lix_commit_edge.json +46 -0
  91. package/dist-engine-src/src/schema/builtin/lix_directory_descriptor.json +53 -0
  92. package/dist-engine-src/src/schema/builtin/lix_entity_label.json +63 -0
  93. package/dist-engine-src/src/schema/builtin/lix_file_descriptor.json +53 -0
  94. package/dist-engine-src/src/schema/builtin/lix_key_value.json +41 -0
  95. package/dist-engine-src/src/schema/builtin/lix_label.json +22 -0
  96. package/dist-engine-src/src/schema/builtin/lix_registered_schema.json +31 -0
  97. package/dist-engine-src/src/schema/builtin/lix_version_descriptor.json +35 -0
  98. package/dist-engine-src/src/schema/builtin/lix_version_ref.json +49 -0
  99. package/dist-engine-src/src/schema/builtin/mod.rs +271 -0
  100. package/dist-engine-src/src/schema/definition.json +157 -0
  101. package/dist-engine-src/src/schema/definition.rs +636 -0
  102. package/dist-engine-src/src/schema/key.rs +206 -0
  103. package/dist-engine-src/src/schema/mod.rs +20 -0
  104. package/dist-engine-src/src/schema/seed.rs +14 -0
  105. package/dist-engine-src/src/schema/tests.rs +739 -0
  106. package/dist-engine-src/src/schema_registry.rs +294 -0
  107. package/dist-engine-src/src/session/context.rs +366 -0
  108. package/dist-engine-src/src/session/create_version.rs +80 -0
  109. package/dist-engine-src/src/session/execute.rs +447 -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 +62 -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 +437 -0
  116. package/dist-engine-src/src/session/mod.rs +25 -0
  117. package/dist-engine-src/src/session/switch_version.rs +121 -0
  118. package/dist-engine-src/src/sql2/change_provider.rs +337 -0
  119. package/dist-engine-src/src/sql2/classify.rs +147 -0
  120. package/dist-engine-src/src/sql2/commit_derived_provider.rs +591 -0
  121. package/dist-engine-src/src/sql2/context.rs +307 -0
  122. package/dist-engine-src/src/sql2/directory_history_provider.rs +623 -0
  123. package/dist-engine-src/src/sql2/directory_provider.rs +2405 -0
  124. package/dist-engine-src/src/sql2/dml.rs +148 -0
  125. package/dist-engine-src/src/sql2/entity_history_provider.rs +444 -0
  126. package/dist-engine-src/src/sql2/entity_provider.rs +2700 -0
  127. package/dist-engine-src/src/sql2/error.rs +196 -0
  128. package/dist-engine-src/src/sql2/execute.rs +3379 -0
  129. package/dist-engine-src/src/sql2/file_history_provider.rs +902 -0
  130. package/dist-engine-src/src/sql2/file_provider.rs +3254 -0
  131. package/dist-engine-src/src/sql2/filesystem_planner.rs +1526 -0
  132. package/dist-engine-src/src/sql2/filesystem_predicates.rs +159 -0
  133. package/dist-engine-src/src/sql2/filesystem_visibility.rs +369 -0
  134. package/dist-engine-src/src/sql2/history_projection.rs +80 -0
  135. package/dist-engine-src/src/sql2/history_provider.rs +418 -0
  136. package/dist-engine-src/src/sql2/history_route.rs +643 -0
  137. package/dist-engine-src/src/sql2/lix_state_provider.rs +2430 -0
  138. package/dist-engine-src/src/sql2/mod.rs +43 -0
  139. package/dist-engine-src/src/sql2/read_only.rs +65 -0
  140. package/dist-engine-src/src/sql2/record_batch.rs +17 -0
  141. package/dist-engine-src/src/sql2/result_metadata.rs +29 -0
  142. package/dist-engine-src/src/sql2/runtime.rs +60 -0
  143. package/dist-engine-src/src/sql2/session.rs +135 -0
  144. package/dist-engine-src/src/sql2/udfs/common.rs +295 -0
  145. package/dist-engine-src/src/sql2/udfs/lix_active_version_commit_id.rs +53 -0
  146. package/dist-engine-src/src/sql2/udfs/lix_empty_blob.rs +47 -0
  147. package/dist-engine-src/src/sql2/udfs/lix_json.rs +100 -0
  148. package/dist-engine-src/src/sql2/udfs/lix_json_get.rs +99 -0
  149. package/dist-engine-src/src/sql2/udfs/lix_json_get_text.rs +99 -0
  150. package/dist-engine-src/src/sql2/udfs/lix_text_decode.rs +82 -0
  151. package/dist-engine-src/src/sql2/udfs/lix_text_encode.rs +85 -0
  152. package/dist-engine-src/src/sql2/udfs/lix_uuid_v7.rs +76 -0
  153. package/dist-engine-src/src/sql2/udfs/mod.rs +82 -0
  154. package/dist-engine-src/src/sql2/version_provider.rs +1187 -0
  155. package/dist-engine-src/src/sql2/version_scope.rs +394 -0
  156. package/dist-engine-src/src/sql2/write_normalization.rs +345 -0
  157. package/dist-engine-src/src/storage/context.rs +356 -0
  158. package/dist-engine-src/src/storage/mod.rs +14 -0
  159. package/dist-engine-src/src/storage/read_scope.rs +88 -0
  160. package/dist-engine-src/src/storage/types.rs +501 -0
  161. package/dist-engine-src/src/storage_bench.rs +3406 -0
  162. package/dist-engine-src/src/test_support.rs +81 -0
  163. package/dist-engine-src/src/tracked_state/by_file_index.rs +102 -0
  164. package/dist-engine-src/src/tracked_state/codec.rs +747 -0
  165. package/dist-engine-src/src/tracked_state/context.rs +983 -0
  166. package/dist-engine-src/src/tracked_state/diff.rs +494 -0
  167. package/dist-engine-src/src/tracked_state/materialization.rs +141 -0
  168. package/dist-engine-src/src/tracked_state/merge.rs +474 -0
  169. package/dist-engine-src/src/tracked_state/mod.rs +31 -0
  170. package/dist-engine-src/src/tracked_state/rebuild.rs +771 -0
  171. package/dist-engine-src/src/tracked_state/storage.rs +243 -0
  172. package/dist-engine-src/src/tracked_state/tree.rs +2744 -0
  173. package/dist-engine-src/src/tracked_state/tree_types.rs +176 -0
  174. package/dist-engine-src/src/tracked_state/types.rs +61 -0
  175. package/dist-engine-src/src/transaction/commit.rs +1224 -0
  176. package/dist-engine-src/src/transaction/context.rs +1307 -0
  177. package/dist-engine-src/src/transaction/live_state_overlay.rs +34 -0
  178. package/dist-engine-src/src/transaction/mod.rs +11 -0
  179. package/dist-engine-src/src/transaction/normalization.rs +1026 -0
  180. package/dist-engine-src/src/transaction/schema_resolver.rs +127 -0
  181. package/dist-engine-src/src/transaction/staging.rs +1436 -0
  182. package/dist-engine-src/src/transaction/types.rs +351 -0
  183. package/dist-engine-src/src/transaction/validation.rs +4811 -0
  184. package/dist-engine-src/src/untracked_state/codec.rs +363 -0
  185. package/dist-engine-src/src/untracked_state/context.rs +82 -0
  186. package/dist-engine-src/src/untracked_state/materialization.rs +157 -0
  187. package/dist-engine-src/src/untracked_state/mod.rs +17 -0
  188. package/dist-engine-src/src/untracked_state/storage.rs +348 -0
  189. package/dist-engine-src/src/untracked_state/types.rs +96 -0
  190. package/dist-engine-src/src/version/context.rs +52 -0
  191. package/dist-engine-src/src/version/mod.rs +12 -0
  192. package/dist-engine-src/src/version/refs.rs +421 -0
  193. package/dist-engine-src/src/version/stage_rows.rs +71 -0
  194. package/dist-engine-src/src/version/types.rs +21 -0
  195. package/dist-engine-src/src/wasm/mod.rs +60 -0
  196. package/package.json +68 -63
@@ -0,0 +1,96 @@
1
+ use async_trait::async_trait;
2
+
3
+ use crate::backend::{
4
+ BackendKvEntryPage, BackendKvExistsBatch, BackendKvGetRequest, BackendKvKeyPage,
5
+ BackendKvScanRequest, BackendKvValueBatch, BackendKvValuePage, BackendKvWriteBatch,
6
+ BackendKvWriteStats,
7
+ };
8
+ use crate::LixError;
9
+
10
+ #[async_trait]
11
+ pub trait Backend: Send + Sync {
12
+ async fn begin_read_transaction(
13
+ &self,
14
+ ) -> Result<Box<dyn BackendReadTransaction + Send + Sync + 'static>, LixError>;
15
+
16
+ async fn begin_write_transaction(
17
+ &self,
18
+ ) -> Result<Box<dyn BackendWriteTransaction + Send + Sync + 'static>, LixError>;
19
+
20
+ /// Releases physical resources held by this backend handle.
21
+ ///
22
+ /// This is a resource lifecycle operation, not a durability boundary and
23
+ /// not a destructive operation. Successful write transactions are durable
24
+ /// when their commit returns; callers should not rely on `close` to save
25
+ /// data. Implementations that do not own external resources may keep the
26
+ /// default no-op behavior.
27
+ async fn close(&self) -> Result<(), LixError> {
28
+ Ok(())
29
+ }
30
+
31
+ /// Destroys the physical storage target represented by this backend.
32
+ ///
33
+ /// This is a persistence lifecycle operation, not a logical SQL operation.
34
+ ///
35
+ /// Callers should treat the backend as the authority for what constitutes
36
+ /// the full storage target. For example:
37
+ ///
38
+ /// - native SQLite may delete the main database file plus WAL/SHM sidecars
39
+ /// - wasm/opfs SQLite may clear the persisted OPFS target
40
+ /// - Postgres may drop or clear the configured schema/database target
41
+ ///
42
+ /// Callers must not attempt to infer or delete backend-owned physical
43
+ /// artifacts themselves.
44
+ ///
45
+ /// Implementations may choose not to support destroy if the backend
46
+ /// instance does not have enough information or authority to remove its
47
+ /// target.
48
+ async fn destroy(&self) -> Result<(), LixError> {
49
+ Err(LixError {
50
+ code: "LIX_ERROR_UNKNOWN".to_string(),
51
+ message: "destroy is not supported by this backend".to_string(),
52
+ hint: None,
53
+ details: None,
54
+ })
55
+ }
56
+ }
57
+
58
+ #[async_trait]
59
+ pub trait BackendReadTransaction: Send + Sync {
60
+ async fn get_values(
61
+ &mut self,
62
+ request: BackendKvGetRequest,
63
+ ) -> Result<BackendKvValueBatch, LixError>;
64
+
65
+ async fn exists_many(
66
+ &mut self,
67
+ request: BackendKvGetRequest,
68
+ ) -> Result<BackendKvExistsBatch, LixError>;
69
+
70
+ async fn scan_keys(
71
+ &mut self,
72
+ request: BackendKvScanRequest,
73
+ ) -> Result<BackendKvKeyPage, LixError>;
74
+
75
+ async fn scan_values(
76
+ &mut self,
77
+ request: BackendKvScanRequest,
78
+ ) -> Result<BackendKvValuePage, LixError>;
79
+
80
+ async fn scan_entries(
81
+ &mut self,
82
+ request: BackendKvScanRequest,
83
+ ) -> Result<BackendKvEntryPage, LixError>;
84
+
85
+ async fn rollback(self: Box<Self>) -> Result<(), LixError>;
86
+ }
87
+
88
+ #[async_trait]
89
+ pub trait BackendWriteTransaction: BackendReadTransaction {
90
+ async fn write_kv_batch(
91
+ &mut self,
92
+ batch: BackendKvWriteBatch,
93
+ ) -> Result<BackendKvWriteStats, LixError>;
94
+
95
+ async fn commit(self: Box<Self>) -> Result<(), LixError>;
96
+ }
@@ -0,0 +1,31 @@
1
+ const FASTCDC_MIN_CHUNK_BYTES: usize = 16 * 1024;
2
+ const FASTCDC_AVG_CHUNK_BYTES: usize = 64 * 1024;
3
+ const FASTCDC_MAX_CHUNK_BYTES: usize = 256 * 1024;
4
+ const SINGLE_CHUNK_FAST_PATH_MAX_BYTES: usize = 64 * 1024;
5
+
6
+ #[allow(dead_code)]
7
+ pub(crate) fn should_materialize_chunk_cas(data: &[u8]) -> bool {
8
+ data.len() > SINGLE_CHUNK_FAST_PATH_MAX_BYTES
9
+ }
10
+
11
+ pub(crate) fn fastcdc_chunk_ranges(data: &[u8]) -> Vec<(usize, usize)> {
12
+ if data.is_empty() {
13
+ return Vec::new();
14
+ }
15
+ if data.len() <= SINGLE_CHUNK_FAST_PATH_MAX_BYTES {
16
+ return vec![(0, data.len())];
17
+ }
18
+
19
+ fastcdc::v2020::FastCDC::new(
20
+ data,
21
+ FASTCDC_MIN_CHUNK_BYTES as u32,
22
+ FASTCDC_AVG_CHUNK_BYTES as u32,
23
+ FASTCDC_MAX_CHUNK_BYTES as u32,
24
+ )
25
+ .map(|chunk| {
26
+ let start = chunk.offset as usize;
27
+ let end = start + (chunk.length as usize);
28
+ (start, end)
29
+ })
30
+ .collect()
31
+ }
@@ -0,0 +1,346 @@
1
+ use crate::LixError;
2
+
3
+ // Binary CAS physical rows:
4
+ // - manifest: BCM2 | kind:u8 | blob_size:u64 | kind payload
5
+ // - empty payload: []
6
+ // - single payload: chunk_hash:[u8;32]
7
+ // - chunked payload: chunk_count:u32
8
+ // - manifest chunk: BCC1 | chunk_hash:[u8;32] | uncompressed_len:u64
9
+ // - chunk: BCK1 | codec:u8 | uncompressed_len:u64 | payload:[u8]
10
+ const MANIFEST_MAGIC: &[u8; 4] = b"BCM2";
11
+ const MANIFEST_CHUNK_MAGIC: &[u8; 4] = b"BCC1";
12
+ const CHUNK_MAGIC: &[u8; 4] = b"BCK1";
13
+ const MANIFEST_KIND_EMPTY: u8 = 0;
14
+ const MANIFEST_KIND_SINGLE_CHUNK: u8 = 1;
15
+ const MANIFEST_KIND_CHUNKED: u8 = 2;
16
+ const CHUNK_CODEC_RAW_TAG: u8 = 0;
17
+ const HASH_BYTES: usize = 32;
18
+ const MANIFEST_HEADER_BYTES: usize = 4 + 1 + 8;
19
+ const EMPTY_MANIFEST_BYTES: usize = MANIFEST_HEADER_BYTES;
20
+ const SINGLE_CHUNK_MANIFEST_BYTES: usize = MANIFEST_HEADER_BYTES + HASH_BYTES;
21
+ const CHUNKED_MANIFEST_BYTES: usize = MANIFEST_HEADER_BYTES + 4;
22
+ const MANIFEST_CHUNK_BYTES: usize = 4 + HASH_BYTES + 8;
23
+ const CHUNK_HEADER_BYTES: usize = 4 + 1 + 8;
24
+
25
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
26
+ pub(crate) enum BinaryChunkCodec {
27
+ Raw,
28
+ }
29
+
30
+ impl BinaryChunkCodec {
31
+ fn tag(self) -> u8 {
32
+ match self {
33
+ Self::Raw => CHUNK_CODEC_RAW_TAG,
34
+ }
35
+ }
36
+
37
+ fn from_tag(tag: u8) -> Result<Self, LixError> {
38
+ match tag {
39
+ CHUNK_CODEC_RAW_TAG => Ok(Self::Raw),
40
+ other => Err(codec_error(format!(
41
+ "unsupported binary CAS chunk codec tag {other}"
42
+ ))),
43
+ }
44
+ }
45
+ }
46
+
47
+ #[derive(Debug, Clone)]
48
+ pub(crate) struct EncodedBinaryChunkPayload {
49
+ pub(crate) codec: BinaryChunkCodec,
50
+ pub(crate) data: Vec<u8>,
51
+ }
52
+
53
+ #[derive(Debug, Clone, PartialEq, Eq)]
54
+ pub(crate) enum BinaryCasManifest {
55
+ Empty {
56
+ size_bytes: u64,
57
+ },
58
+ SingleChunk {
59
+ size_bytes: u64,
60
+ chunk_hash: [u8; HASH_BYTES],
61
+ },
62
+ Chunked {
63
+ size_bytes: u64,
64
+ chunk_count: u32,
65
+ },
66
+ }
67
+
68
+ impl BinaryCasManifest {
69
+ pub(crate) fn size_bytes(&self) -> u64 {
70
+ match self {
71
+ Self::Empty { size_bytes }
72
+ | Self::SingleChunk { size_bytes, .. }
73
+ | Self::Chunked { size_bytes, .. } => *size_bytes,
74
+ }
75
+ }
76
+ }
77
+
78
+ #[cfg(test)]
79
+ pub(crate) fn binary_blob_hash_hex(data: &[u8]) -> String {
80
+ crate::common::stable_content_fingerprint_hex(data)
81
+ }
82
+
83
+ pub(crate) fn binary_blob_hash_bytes(data: &[u8]) -> [u8; HASH_BYTES] {
84
+ *blake3::hash(data).as_bytes()
85
+ }
86
+
87
+ pub(crate) fn hash_hex_to_bytes(hash_hex: &str, label: &str) -> Result<[u8; HASH_BYTES], LixError> {
88
+ if hash_hex.len() != HASH_BYTES * 2 {
89
+ return Err(codec_error(format!(
90
+ "{label} hash must be {} hex characters, got {}",
91
+ HASH_BYTES * 2,
92
+ hash_hex.len()
93
+ )));
94
+ }
95
+
96
+ let mut out = [0u8; HASH_BYTES];
97
+ let bytes = hash_hex.as_bytes();
98
+ for index in 0..HASH_BYTES {
99
+ out[index] =
100
+ (hex_value(bytes[index * 2], label)? << 4) | hex_value(bytes[index * 2 + 1], label)?;
101
+ }
102
+ Ok(out)
103
+ }
104
+
105
+ pub(crate) fn hash_bytes_to_hex(bytes: &[u8; HASH_BYTES]) -> String {
106
+ blake3::Hash::from_bytes(*bytes).to_hex().to_string()
107
+ }
108
+
109
+ pub(crate) fn encode_binary_cas_manifest(manifest: &BinaryCasManifest) -> Vec<u8> {
110
+ let capacity = match manifest {
111
+ BinaryCasManifest::Empty { .. } => EMPTY_MANIFEST_BYTES,
112
+ BinaryCasManifest::SingleChunk { .. } => SINGLE_CHUNK_MANIFEST_BYTES,
113
+ BinaryCasManifest::Chunked { .. } => CHUNKED_MANIFEST_BYTES,
114
+ };
115
+ let mut out = Vec::with_capacity(capacity);
116
+ out.extend_from_slice(MANIFEST_MAGIC);
117
+ match manifest {
118
+ BinaryCasManifest::Empty { size_bytes } => {
119
+ out.push(MANIFEST_KIND_EMPTY);
120
+ out.extend_from_slice(&size_bytes.to_be_bytes());
121
+ }
122
+ BinaryCasManifest::SingleChunk {
123
+ size_bytes,
124
+ chunk_hash,
125
+ } => {
126
+ out.push(MANIFEST_KIND_SINGLE_CHUNK);
127
+ out.extend_from_slice(&size_bytes.to_be_bytes());
128
+ out.extend_from_slice(chunk_hash);
129
+ }
130
+ BinaryCasManifest::Chunked {
131
+ size_bytes,
132
+ chunk_count,
133
+ } => {
134
+ out.push(MANIFEST_KIND_CHUNKED);
135
+ out.extend_from_slice(&size_bytes.to_be_bytes());
136
+ out.extend_from_slice(&chunk_count.to_be_bytes());
137
+ }
138
+ }
139
+ out
140
+ }
141
+
142
+ pub(crate) fn decode_binary_cas_manifest(bytes: &[u8]) -> Result<BinaryCasManifest, LixError> {
143
+ if bytes.len() < MANIFEST_HEADER_BYTES {
144
+ return Err(codec_error(format!(
145
+ "binary CAS manifest must be at least {MANIFEST_HEADER_BYTES} bytes, got {}",
146
+ bytes.len()
147
+ )));
148
+ }
149
+ require_magic(bytes, MANIFEST_MAGIC, "binary CAS manifest")?;
150
+ let size_bytes = u64::from_be_bytes(bytes[5..13].try_into().expect("fixed slice"));
151
+ match bytes[4] {
152
+ MANIFEST_KIND_EMPTY => {
153
+ require_len(bytes, EMPTY_MANIFEST_BYTES, "binary CAS empty manifest")?;
154
+ Ok(BinaryCasManifest::Empty { size_bytes })
155
+ }
156
+ MANIFEST_KIND_SINGLE_CHUNK => {
157
+ require_len(
158
+ bytes,
159
+ SINGLE_CHUNK_MANIFEST_BYTES,
160
+ "binary CAS single-chunk manifest",
161
+ )?;
162
+ let chunk_hash = bytes[13..45].try_into().expect("fixed slice");
163
+ Ok(BinaryCasManifest::SingleChunk {
164
+ size_bytes,
165
+ chunk_hash,
166
+ })
167
+ }
168
+ MANIFEST_KIND_CHUNKED => {
169
+ require_len(bytes, CHUNKED_MANIFEST_BYTES, "binary CAS chunked manifest")?;
170
+ let chunk_count = u32::from_be_bytes(bytes[13..17].try_into().expect("fixed slice"));
171
+ Ok(BinaryCasManifest::Chunked {
172
+ size_bytes,
173
+ chunk_count,
174
+ })
175
+ }
176
+ other => Err(codec_error(format!(
177
+ "unsupported binary CAS manifest kind {other}"
178
+ ))),
179
+ }
180
+ }
181
+
182
+ pub(crate) fn encode_binary_cas_manifest_chunk(
183
+ chunk_hash: &[u8; HASH_BYTES],
184
+ chunk_size: u64,
185
+ ) -> Vec<u8> {
186
+ let mut out = Vec::with_capacity(MANIFEST_CHUNK_BYTES);
187
+ out.extend_from_slice(MANIFEST_CHUNK_MAGIC);
188
+ out.extend_from_slice(chunk_hash);
189
+ out.extend_from_slice(&chunk_size.to_be_bytes());
190
+ out
191
+ }
192
+
193
+ pub(crate) fn decode_binary_cas_manifest_chunk(
194
+ bytes: &[u8],
195
+ ) -> Result<([u8; HASH_BYTES], u64), LixError> {
196
+ if bytes.len() != MANIFEST_CHUNK_BYTES {
197
+ return Err(codec_error(format!(
198
+ "binary CAS manifest chunk must be {MANIFEST_CHUNK_BYTES} bytes, got {}",
199
+ bytes.len()
200
+ )));
201
+ }
202
+ require_magic(bytes, MANIFEST_CHUNK_MAGIC, "binary CAS manifest chunk")?;
203
+ let chunk_hash = bytes[4..36].try_into().expect("fixed slice");
204
+ let chunk_size = u64::from_be_bytes(bytes[36..44].try_into().expect("fixed slice"));
205
+ Ok((chunk_hash, chunk_size))
206
+ }
207
+
208
+ pub(crate) fn encode_binary_cas_chunk(
209
+ codec: BinaryChunkCodec,
210
+ uncompressed_len: u64,
211
+ payload: &[u8],
212
+ ) -> Vec<u8> {
213
+ let mut out = Vec::with_capacity(CHUNK_HEADER_BYTES + payload.len());
214
+ out.extend_from_slice(CHUNK_MAGIC);
215
+ out.push(codec.tag());
216
+ out.extend_from_slice(&uncompressed_len.to_be_bytes());
217
+ out.extend_from_slice(payload);
218
+ out
219
+ }
220
+
221
+ pub(crate) fn decode_binary_cas_chunk(
222
+ bytes: &[u8],
223
+ ) -> Result<(BinaryChunkCodec, u64, &[u8]), LixError> {
224
+ if bytes.len() < CHUNK_HEADER_BYTES {
225
+ return Err(codec_error(format!(
226
+ "binary CAS chunk must be at least {CHUNK_HEADER_BYTES} bytes, got {}",
227
+ bytes.len()
228
+ )));
229
+ }
230
+ require_magic(bytes, CHUNK_MAGIC, "binary CAS chunk")?;
231
+ let codec = BinaryChunkCodec::from_tag(bytes[4])?;
232
+ let uncompressed_len = u64::from_be_bytes(bytes[5..13].try_into().expect("fixed slice"));
233
+ Ok((codec, uncompressed_len, &bytes[CHUNK_HEADER_BYTES..]))
234
+ }
235
+
236
+ fn require_magic(bytes: &[u8], expected: &[u8; 4], label: &str) -> Result<(), LixError> {
237
+ if &bytes[..4] == expected {
238
+ return Ok(());
239
+ }
240
+ Err(codec_error(format!(
241
+ "{label} has unsupported binary format"
242
+ )))
243
+ }
244
+
245
+ fn require_len(bytes: &[u8], expected: usize, label: &str) -> Result<(), LixError> {
246
+ if bytes.len() == expected {
247
+ return Ok(());
248
+ }
249
+ Err(codec_error(format!(
250
+ "{label} must be {expected} bytes, got {}",
251
+ bytes.len()
252
+ )))
253
+ }
254
+
255
+ fn hex_value(byte: u8, label: &str) -> Result<u8, LixError> {
256
+ match byte {
257
+ b'0'..=b'9' => Ok(byte - b'0'),
258
+ b'a'..=b'f' => Ok(byte - b'a' + 10),
259
+ b'A'..=b'F' => Ok(byte - b'A' + 10),
260
+ _ => Err(codec_error(format!("{label} hash contains non-hex bytes"))),
261
+ }
262
+ }
263
+
264
+ fn codec_error(message: String) -> LixError {
265
+ LixError::new("LIX_ERROR_UNKNOWN", message)
266
+ }
267
+
268
+ pub(crate) fn encode_binary_chunk_payload(chunk_data: &[u8]) -> EncodedBinaryChunkPayload {
269
+ EncodedBinaryChunkPayload {
270
+ codec: BinaryChunkCodec::Raw,
271
+ data: chunk_data.to_vec(),
272
+ }
273
+ }
274
+
275
+ #[cfg(test)]
276
+ mod tests {
277
+ use super::*;
278
+
279
+ #[test]
280
+ fn manifests_roundtrip_fixed_binary_rows() {
281
+ let chunk_hash = binary_blob_hash_bytes(b"chunk");
282
+ let cases = vec![
283
+ (
284
+ BinaryCasManifest::Empty { size_bytes: 0 },
285
+ EMPTY_MANIFEST_BYTES,
286
+ ),
287
+ (
288
+ BinaryCasManifest::SingleChunk {
289
+ size_bytes: 42,
290
+ chunk_hash,
291
+ },
292
+ SINGLE_CHUNK_MANIFEST_BYTES,
293
+ ),
294
+ (
295
+ BinaryCasManifest::Chunked {
296
+ size_bytes: 42,
297
+ chunk_count: 7,
298
+ },
299
+ CHUNKED_MANIFEST_BYTES,
300
+ ),
301
+ ];
302
+ for (manifest, expected_len) in cases {
303
+ let encoded = encode_binary_cas_manifest(&manifest);
304
+ assert_eq!(encoded.len(), expected_len);
305
+ assert_eq!(decode_binary_cas_manifest(&encoded).unwrap(), manifest);
306
+ }
307
+ }
308
+
309
+ #[test]
310
+ fn manifest_chunk_roundtrips_fixed_binary_row() {
311
+ let hash = binary_blob_hash_bytes(b"chunk");
312
+ let encoded = encode_binary_cas_manifest_chunk(&hash, 1024);
313
+ assert_eq!(encoded.len(), MANIFEST_CHUNK_BYTES);
314
+ assert_eq!(
315
+ decode_binary_cas_manifest_chunk(&encoded).unwrap(),
316
+ (hash, 1024)
317
+ );
318
+ }
319
+
320
+ #[test]
321
+ fn chunk_roundtrips_payload_as_remaining_bytes() {
322
+ let payload = b"hello payload";
323
+ let encoded = encode_binary_cas_chunk(BinaryChunkCodec::Raw, payload.len() as u64, payload);
324
+ assert_eq!(&encoded[..4], CHUNK_MAGIC);
325
+ let (codec, uncompressed_len, decoded_payload) = decode_binary_cas_chunk(&encoded).unwrap();
326
+ assert_eq!(codec, BinaryChunkCodec::Raw);
327
+ assert_eq!(uncompressed_len, payload.len() as u64);
328
+ assert_eq!(decoded_payload, payload);
329
+ }
330
+
331
+ #[test]
332
+ fn wrong_magic_is_rejected() {
333
+ let mut encoded = encode_binary_cas_manifest(&BinaryCasManifest::Empty { size_bytes: 0 });
334
+ encoded[0] = b'X';
335
+ let error = decode_binary_cas_manifest(&encoded).unwrap_err();
336
+ assert!(error.message.contains("unsupported binary format"));
337
+ }
338
+
339
+ #[test]
340
+ fn hex_hashes_roundtrip_to_32_byte_keys() {
341
+ let hash_hex = binary_blob_hash_hex(b"blob");
342
+ let hash_bytes = hash_hex_to_bytes(&hash_hex, "test").unwrap();
343
+ assert_eq!(hash_bytes.len(), 32);
344
+ assert_eq!(hash_bytes_to_hex(&hash_bytes), hash_hex);
345
+ }
346
+ }
@@ -0,0 +1,139 @@
1
+ use async_trait::async_trait;
2
+
3
+ use crate::binary_cas::{
4
+ BlobBytesBatch, BlobExistsBatch, BlobHash, BlobMetadataBatch, BlobWrite, BlobWriteReceipt,
5
+ };
6
+ use crate::storage::{StorageReader, StorageWriteSet};
7
+ use crate::LixError;
8
+ use std::collections::HashSet;
9
+
10
+ #[async_trait]
11
+ pub(crate) trait BlobDataReader: Send + Sync {
12
+ async fn load_bytes_many(&self, hashes: &[BlobHash]) -> Result<BlobBytesBatch, LixError>;
13
+ }
14
+
15
+ /// Long-lived Binary CAS context factory.
16
+ ///
17
+ /// The context does not own storage. Callers explicitly provide a KV store via
18
+ /// `reader(...)` or `writer(...)`, keeping backend and transaction ownership at
19
+ /// the execution layer.
20
+ pub(crate) struct BinaryCasContext;
21
+
22
+ impl BinaryCasContext {
23
+ pub(crate) fn new() -> Self {
24
+ Self
25
+ }
26
+
27
+ /// Creates a Binary CAS reader over any storage reader.
28
+ ///
29
+ /// The reader can be a read transaction or the active write transaction
30
+ /// when reads must participate in transaction-local visibility.
31
+ pub(crate) fn reader<S>(&self, store: S) -> BinaryCasStoreReader<S>
32
+ where
33
+ S: StorageReader,
34
+ {
35
+ BinaryCasStoreReader { store }
36
+ }
37
+
38
+ pub(crate) fn writer<'a>(&self, writes: &'a mut StorageWriteSet) -> BinaryCasWriter<'a> {
39
+ BinaryCasWriter::new(writes)
40
+ }
41
+ }
42
+
43
+ #[async_trait]
44
+ impl<S> BlobDataReader for BinaryCasStoreReader<S>
45
+ where
46
+ S: StorageReader + Clone + Send + Sync,
47
+ {
48
+ async fn load_bytes_many(&self, hashes: &[BlobHash]) -> Result<BlobBytesBatch, LixError> {
49
+ let mut reader = BinaryCasStoreReader {
50
+ store: self.store.clone(),
51
+ };
52
+ BinaryCasStoreReader::load_bytes_many(&mut reader, hashes).await
53
+ }
54
+ }
55
+
56
+ /// Binary CAS reader over a caller-supplied KV store.
57
+ pub(crate) struct BinaryCasStoreReader<S> {
58
+ store: S,
59
+ }
60
+
61
+ impl<S> BinaryCasStoreReader<S>
62
+ where
63
+ S: StorageReader,
64
+ {
65
+ #[allow(dead_code)]
66
+ pub(crate) async fn exists_many(
67
+ &mut self,
68
+ hashes: &[BlobHash],
69
+ ) -> Result<BlobExistsBatch, LixError> {
70
+ crate::binary_cas::kv::exists_many(&mut self.store, hashes).await
71
+ }
72
+
73
+ #[allow(dead_code)]
74
+ pub(crate) async fn load_metadata_many(
75
+ &mut self,
76
+ hashes: &[BlobHash],
77
+ ) -> Result<BlobMetadataBatch, LixError> {
78
+ crate::binary_cas::kv::load_metadata_many(&mut self.store, hashes).await
79
+ }
80
+
81
+ pub(crate) async fn load_bytes_many(
82
+ &mut self,
83
+ hashes: &[BlobHash],
84
+ ) -> Result<BlobBytesBatch, LixError> {
85
+ crate::binary_cas::kv::load_bytes_many(&mut self.store, hashes).await
86
+ }
87
+
88
+ #[cfg(feature = "storage-benches")]
89
+ pub(crate) async fn count_blob_manifests(&mut self) -> Result<usize, LixError> {
90
+ crate::binary_cas::kv::count_manifests(&mut self.store).await
91
+ }
92
+ }
93
+
94
+ /// Transaction-scoped Binary CAS writer.
95
+ ///
96
+ /// This type does not begin, commit, or roll back transactions. It only writes
97
+ /// CAS data into the transaction supplied by the caller.
98
+ pub(crate) struct BinaryCasWriter<'a> {
99
+ writes: &'a mut StorageWriteSet,
100
+ blob_hashes: HashSet<[u8; 32]>,
101
+ chunk_keys: HashSet<Vec<u8>>,
102
+ }
103
+
104
+ impl<'a> BinaryCasWriter<'a> {
105
+ fn new(writes: &'a mut StorageWriteSet) -> Self {
106
+ Self {
107
+ writes,
108
+ blob_hashes: HashSet::new(),
109
+ chunk_keys: HashSet::new(),
110
+ }
111
+ }
112
+
113
+ pub(crate) fn stage_bytes(&mut self, bytes: &[u8]) -> Result<BlobWriteReceipt, LixError> {
114
+ crate::binary_cas::kv::stage_blob_write(
115
+ self.writes,
116
+ &mut self.blob_hashes,
117
+ &mut self.chunk_keys,
118
+ &BlobWrite { bytes },
119
+ )
120
+ }
121
+
122
+ #[allow(dead_code)]
123
+ pub(crate) fn stage_many(
124
+ &mut self,
125
+ writes: &[BlobWrite<'_>],
126
+ ) -> Result<Vec<BlobWriteReceipt>, LixError> {
127
+ writes
128
+ .iter()
129
+ .map(|write| {
130
+ crate::binary_cas::kv::stage_blob_write(
131
+ self.writes,
132
+ &mut self.blob_hashes,
133
+ &mut self.chunk_keys,
134
+ write,
135
+ )
136
+ })
137
+ .collect()
138
+ }
139
+ }