@lix-js/sdk 0.6.0-preview.4 → 0.6.0

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 (223) 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 +39 -201
  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 +20 -50
  16. package/SKILL.md +0 -506
  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 -821
  23. package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
  24. package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +0 -26
  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 -303
  29. package/dist-engine-src/README.md +0 -18
  30. package/dist-engine-src/src/backend/kv.rs +0 -358
  31. package/dist-engine-src/src/backend/mod.rs +0 -12
  32. package/dist-engine-src/src/backend/testing.rs +0 -658
  33. package/dist-engine-src/src/backend/types.rs +0 -96
  34. package/dist-engine-src/src/binary_cas/chunking.rs +0 -31
  35. package/dist-engine-src/src/binary_cas/codec.rs +0 -346
  36. package/dist-engine-src/src/binary_cas/context.rs +0 -139
  37. package/dist-engine-src/src/binary_cas/kv.rs +0 -1063
  38. package/dist-engine-src/src/binary_cas/mod.rs +0 -11
  39. package/dist-engine-src/src/binary_cas/types.rs +0 -121
  40. package/dist-engine-src/src/catalog/context.rs +0 -412
  41. package/dist-engine-src/src/catalog/mod.rs +0 -10
  42. package/dist-engine-src/src/catalog/schema.rs +0 -4
  43. package/dist-engine-src/src/catalog/snapshot.rs +0 -1114
  44. package/dist-engine-src/src/cel/context.rs +0 -86
  45. package/dist-engine-src/src/cel/error.rs +0 -19
  46. package/dist-engine-src/src/cel/mod.rs +0 -8
  47. package/dist-engine-src/src/cel/provider.rs +0 -9
  48. package/dist-engine-src/src/cel/runtime.rs +0 -167
  49. package/dist-engine-src/src/cel/value.rs +0 -50
  50. package/dist-engine-src/src/commit_graph/context.rs +0 -901
  51. package/dist-engine-src/src/commit_graph/mod.rs +0 -11
  52. package/dist-engine-src/src/commit_graph/types.rs +0 -109
  53. package/dist-engine-src/src/commit_graph/walker.rs +0 -756
  54. package/dist-engine-src/src/commit_store/codec.rs +0 -887
  55. package/dist-engine-src/src/commit_store/context.rs +0 -944
  56. package/dist-engine-src/src/commit_store/materialization.rs +0 -84
  57. package/dist-engine-src/src/commit_store/mod.rs +0 -16
  58. package/dist-engine-src/src/commit_store/storage.rs +0 -600
  59. package/dist-engine-src/src/commit_store/types.rs +0 -215
  60. package/dist-engine-src/src/common/error.rs +0 -313
  61. package/dist-engine-src/src/common/fingerprint.rs +0 -3
  62. package/dist-engine-src/src/common/fs_path.rs +0 -1336
  63. package/dist-engine-src/src/common/identity.rs +0 -145
  64. package/dist-engine-src/src/common/json_pointer.rs +0 -67
  65. package/dist-engine-src/src/common/metadata.rs +0 -40
  66. package/dist-engine-src/src/common/mod.rs +0 -23
  67. package/dist-engine-src/src/common/types.rs +0 -105
  68. package/dist-engine-src/src/common/wire.rs +0 -222
  69. package/dist-engine-src/src/domain.rs +0 -324
  70. package/dist-engine-src/src/engine.rs +0 -225
  71. package/dist-engine-src/src/entity_identity.rs +0 -405
  72. package/dist-engine-src/src/functions/context.rs +0 -292
  73. package/dist-engine-src/src/functions/deterministic.rs +0 -113
  74. package/dist-engine-src/src/functions/mod.rs +0 -18
  75. package/dist-engine-src/src/functions/provider.rs +0 -130
  76. package/dist-engine-src/src/functions/state.rs +0 -336
  77. package/dist-engine-src/src/functions/types.rs +0 -37
  78. package/dist-engine-src/src/init.rs +0 -558
  79. package/dist-engine-src/src/json_store/compression.rs +0 -77
  80. package/dist-engine-src/src/json_store/context.rs +0 -423
  81. package/dist-engine-src/src/json_store/encoded.rs +0 -15
  82. package/dist-engine-src/src/json_store/mod.rs +0 -12
  83. package/dist-engine-src/src/json_store/store.rs +0 -1109
  84. package/dist-engine-src/src/json_store/types.rs +0 -217
  85. package/dist-engine-src/src/lib.rs +0 -62
  86. package/dist-engine-src/src/live_state/context.rs +0 -2019
  87. package/dist-engine-src/src/live_state/mod.rs +0 -15
  88. package/dist-engine-src/src/live_state/overlay.rs +0 -75
  89. package/dist-engine-src/src/live_state/reader.rs +0 -23
  90. package/dist-engine-src/src/live_state/types.rs +0 -222
  91. package/dist-engine-src/src/live_state/visibility.rs +0 -223
  92. package/dist-engine-src/src/plugin/archive.rs +0 -438
  93. package/dist-engine-src/src/plugin/component.rs +0 -183
  94. package/dist-engine-src/src/plugin/install.rs +0 -619
  95. package/dist-engine-src/src/plugin/manifest.rs +0 -516
  96. package/dist-engine-src/src/plugin/materializer.rs +0 -477
  97. package/dist-engine-src/src/plugin/mod.rs +0 -33
  98. package/dist-engine-src/src/plugin/plugin_manifest.json +0 -118
  99. package/dist-engine-src/src/plugin/storage.rs +0 -74
  100. package/dist-engine-src/src/schema/annotations/defaults.rs +0 -275
  101. package/dist-engine-src/src/schema/annotations/mod.rs +0 -1
  102. package/dist-engine-src/src/schema/builtin/lix_account.json +0 -21
  103. package/dist-engine-src/src/schema/builtin/lix_active_account.json +0 -29
  104. package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +0 -29
  105. package/dist-engine-src/src/schema/builtin/lix_change.json +0 -63
  106. package/dist-engine-src/src/schema/builtin/lix_change_author.json +0 -45
  107. package/dist-engine-src/src/schema/builtin/lix_commit.json +0 -24
  108. package/dist-engine-src/src/schema/builtin/lix_commit_edge.json +0 -53
  109. package/dist-engine-src/src/schema/builtin/lix_directory_descriptor.json +0 -52
  110. package/dist-engine-src/src/schema/builtin/lix_file_descriptor.json +0 -52
  111. package/dist-engine-src/src/schema/builtin/lix_key_value.json +0 -40
  112. package/dist-engine-src/src/schema/builtin/lix_label.json +0 -29
  113. package/dist-engine-src/src/schema/builtin/lix_label_assignment.json +0 -74
  114. package/dist-engine-src/src/schema/builtin/lix_registered_schema.json +0 -25
  115. package/dist-engine-src/src/schema/builtin/lix_version_descriptor.json +0 -34
  116. package/dist-engine-src/src/schema/builtin/lix_version_ref.json +0 -48
  117. package/dist-engine-src/src/schema/builtin/mod.rs +0 -222
  118. package/dist-engine-src/src/schema/compatibility.rs +0 -787
  119. package/dist-engine-src/src/schema/definition.json +0 -187
  120. package/dist-engine-src/src/schema/definition.rs +0 -742
  121. package/dist-engine-src/src/schema/key.rs +0 -138
  122. package/dist-engine-src/src/schema/mod.rs +0 -20
  123. package/dist-engine-src/src/schema/seed.rs +0 -14
  124. package/dist-engine-src/src/schema/tests.rs +0 -780
  125. package/dist-engine-src/src/session/context.rs +0 -404
  126. package/dist-engine-src/src/session/create_version.rs +0 -88
  127. package/dist-engine-src/src/session/execute.rs +0 -541
  128. package/dist-engine-src/src/session/merge/analysis.rs +0 -102
  129. package/dist-engine-src/src/session/merge/apply.rs +0 -23
  130. package/dist-engine-src/src/session/merge/conflicts.rs +0 -63
  131. package/dist-engine-src/src/session/merge/mod.rs +0 -11
  132. package/dist-engine-src/src/session/merge/stats.rs +0 -65
  133. package/dist-engine-src/src/session/merge/version.rs +0 -427
  134. package/dist-engine-src/src/session/mod.rs +0 -27
  135. package/dist-engine-src/src/session/optimization9_sql2_bench.rs +0 -100
  136. package/dist-engine-src/src/session/switch_version.rs +0 -110
  137. package/dist-engine-src/src/session/transaction.rs +0 -76
  138. package/dist-engine-src/src/sql2/change_provider.rs +0 -331
  139. package/dist-engine-src/src/sql2/classify.rs +0 -174
  140. package/dist-engine-src/src/sql2/context.rs +0 -311
  141. package/dist-engine-src/src/sql2/directory_history_provider.rs +0 -631
  142. package/dist-engine-src/src/sql2/directory_provider.rs +0 -2453
  143. package/dist-engine-src/src/sql2/dml.rs +0 -148
  144. package/dist-engine-src/src/sql2/entity_history_provider.rs +0 -440
  145. package/dist-engine-src/src/sql2/entity_provider.rs +0 -3211
  146. package/dist-engine-src/src/sql2/error.rs +0 -215
  147. package/dist-engine-src/src/sql2/execute.rs +0 -3533
  148. package/dist-engine-src/src/sql2/file_history_provider.rs +0 -910
  149. package/dist-engine-src/src/sql2/file_provider.rs +0 -3679
  150. package/dist-engine-src/src/sql2/filesystem_planner.rs +0 -1490
  151. package/dist-engine-src/src/sql2/filesystem_predicates.rs +0 -159
  152. package/dist-engine-src/src/sql2/filesystem_visibility.rs +0 -383
  153. package/dist-engine-src/src/sql2/history_projection.rs +0 -56
  154. package/dist-engine-src/src/sql2/history_provider.rs +0 -412
  155. package/dist-engine-src/src/sql2/history_route.rs +0 -657
  156. package/dist-engine-src/src/sql2/lix_state_provider.rs +0 -2512
  157. package/dist-engine-src/src/sql2/mod.rs +0 -47
  158. package/dist-engine-src/src/sql2/predicate_typecheck.rs +0 -246
  159. package/dist-engine-src/src/sql2/public_bind/assignment.rs +0 -46
  160. package/dist-engine-src/src/sql2/public_bind/capability.rs +0 -41
  161. package/dist-engine-src/src/sql2/public_bind/dml.rs +0 -172
  162. package/dist-engine-src/src/sql2/public_bind/mod.rs +0 -26
  163. package/dist-engine-src/src/sql2/public_bind/table.rs +0 -168
  164. package/dist-engine-src/src/sql2/read_only.rs +0 -63
  165. package/dist-engine-src/src/sql2/record_batch.rs +0 -17
  166. package/dist-engine-src/src/sql2/result_metadata.rs +0 -29
  167. package/dist-engine-src/src/sql2/runtime.rs +0 -60
  168. package/dist-engine-src/src/sql2/session.rs +0 -132
  169. package/dist-engine-src/src/sql2/udfs/common.rs +0 -295
  170. package/dist-engine-src/src/sql2/udfs/lix_active_version_commit_id.rs +0 -53
  171. package/dist-engine-src/src/sql2/udfs/lix_empty_blob.rs +0 -47
  172. package/dist-engine-src/src/sql2/udfs/lix_json.rs +0 -100
  173. package/dist-engine-src/src/sql2/udfs/lix_json_get.rs +0 -99
  174. package/dist-engine-src/src/sql2/udfs/lix_json_get_text.rs +0 -99
  175. package/dist-engine-src/src/sql2/udfs/lix_text_decode.rs +0 -82
  176. package/dist-engine-src/src/sql2/udfs/lix_text_encode.rs +0 -85
  177. package/dist-engine-src/src/sql2/udfs/lix_timestamp.rs +0 -76
  178. package/dist-engine-src/src/sql2/udfs/lix_uuid_v7.rs +0 -76
  179. package/dist-engine-src/src/sql2/udfs/mod.rs +0 -89
  180. package/dist-engine-src/src/sql2/udfs/public_call.rs +0 -238
  181. package/dist-engine-src/src/sql2/version_provider.rs +0 -1202
  182. package/dist-engine-src/src/sql2/version_scope.rs +0 -394
  183. package/dist-engine-src/src/sql2/write_normalization.rs +0 -345
  184. package/dist-engine-src/src/storage/context.rs +0 -356
  185. package/dist-engine-src/src/storage/mod.rs +0 -14
  186. package/dist-engine-src/src/storage/read_scope.rs +0 -88
  187. package/dist-engine-src/src/storage/types.rs +0 -501
  188. package/dist-engine-src/src/storage_bench.rs +0 -4863
  189. package/dist-engine-src/src/test_support.rs +0 -228
  190. package/dist-engine-src/src/tracked_state/by_file_index.rs +0 -98
  191. package/dist-engine-src/src/tracked_state/codec.rs +0 -2085
  192. package/dist-engine-src/src/tracked_state/context.rs +0 -1867
  193. package/dist-engine-src/src/tracked_state/diff.rs +0 -686
  194. package/dist-engine-src/src/tracked_state/materialization.rs +0 -403
  195. package/dist-engine-src/src/tracked_state/materializer.rs +0 -488
  196. package/dist-engine-src/src/tracked_state/merge.rs +0 -492
  197. package/dist-engine-src/src/tracked_state/mod.rs +0 -32
  198. package/dist-engine-src/src/tracked_state/storage.rs +0 -375
  199. package/dist-engine-src/src/tracked_state/tree.rs +0 -3187
  200. package/dist-engine-src/src/tracked_state/types.rs +0 -231
  201. package/dist-engine-src/src/transaction/commit.rs +0 -1484
  202. package/dist-engine-src/src/transaction/context.rs +0 -1548
  203. package/dist-engine-src/src/transaction/live_state_overlay.rs +0 -35
  204. package/dist-engine-src/src/transaction/mod.rs +0 -13
  205. package/dist-engine-src/src/transaction/normalization.rs +0 -890
  206. package/dist-engine-src/src/transaction/prep.rs +0 -37
  207. package/dist-engine-src/src/transaction/schema_resolver.rs +0 -149
  208. package/dist-engine-src/src/transaction/staging.rs +0 -1731
  209. package/dist-engine-src/src/transaction/types.rs +0 -460
  210. package/dist-engine-src/src/transaction/validation.rs +0 -5830
  211. package/dist-engine-src/src/untracked_state/codec.rs +0 -307
  212. package/dist-engine-src/src/untracked_state/context.rs +0 -98
  213. package/dist-engine-src/src/untracked_state/materialization.rs +0 -63
  214. package/dist-engine-src/src/untracked_state/mod.rs +0 -15
  215. package/dist-engine-src/src/untracked_state/storage.rs +0 -396
  216. package/dist-engine-src/src/untracked_state/types.rs +0 -146
  217. package/dist-engine-src/src/version/context.rs +0 -40
  218. package/dist-engine-src/src/version/lifecycle.rs +0 -221
  219. package/dist-engine-src/src/version/mod.rs +0 -13
  220. package/dist-engine-src/src/version/refs.rs +0 -330
  221. package/dist-engine-src/src/version/stage_rows.rs +0 -67
  222. package/dist-engine-src/src/version/types.rs +0 -21
  223. package/dist-engine-src/src/wasm/mod.rs +0 -60
@@ -1,96 +0,0 @@
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
- }
@@ -1,31 +0,0 @@
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
- }
@@ -1,346 +0,0 @@
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
- }
@@ -1,139 +0,0 @@
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
- }