@lix-js/sdk 0.6.0-preview.3 → 0.6.0-preview.5
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.
- package/README.md +1 -1
- package/SKILL.md +105 -65
- package/dist/engine-wasm/index.js +4 -4
- package/dist/engine-wasm/wasm/lix_engine.d.ts +30 -6
- package/dist/engine-wasm/wasm/lix_engine.js +187 -117
- package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
- package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +14 -8
- package/dist/generated/builtin-schemas.d.ts +69 -69
- package/dist/generated/builtin-schemas.js +94 -94
- package/dist/open-lix.d.ts +42 -28
- package/dist/open-lix.js +49 -10
- package/dist/sqlite/index.js +86 -30
- package/dist-engine-src/README.md +3 -3
- package/dist-engine-src/src/backend/capabilities.rs +67 -0
- package/dist-engine-src/src/backend/conformance/baseline.rs +1127 -0
- package/dist-engine-src/src/backend/conformance/factory.rs +93 -0
- package/dist-engine-src/src/backend/conformance/failure_tests.rs +608 -0
- package/dist-engine-src/src/backend/conformance/fixtures.rs +26 -0
- package/dist-engine-src/src/backend/conformance/mod.rs +75 -0
- package/dist-engine-src/src/backend/conformance/model.rs +28 -0
- package/dist-engine-src/src/backend/conformance/model_based.rs +257 -0
- package/dist-engine-src/src/backend/conformance/persistence.rs +204 -0
- package/dist-engine-src/src/backend/conformance/projection.rs +21 -0
- package/dist-engine-src/src/backend/conformance/pushdown.rs +24 -0
- package/dist-engine-src/src/backend/conformance/runner.rs +90 -0
- package/dist-engine-src/src/backend/conformance/scan.rs +24 -0
- package/dist-engine-src/src/backend/conformance/write.rs +16 -0
- package/dist-engine-src/src/backend/error.rs +94 -0
- package/dist-engine-src/src/backend/in_memory.rs +670 -0
- package/dist-engine-src/src/backend/mod.rs +36 -9
- package/dist-engine-src/src/backend/predicate.rs +80 -0
- package/dist-engine-src/src/backend/traits.rs +260 -0
- package/dist-engine-src/src/backend/types.rs +224 -81
- package/dist-engine-src/src/binary_cas/context.rs +8 -8
- package/dist-engine-src/src/binary_cas/kv.rs +234 -259
- package/dist-engine-src/src/{version → branch}/context.rs +12 -12
- package/dist-engine-src/src/branch/lifecycle.rs +221 -0
- package/dist-engine-src/src/branch/mod.rs +13 -0
- package/dist-engine-src/src/branch/refs.rs +321 -0
- package/dist-engine-src/src/branch/stage_rows.rs +67 -0
- package/dist-engine-src/src/branch/types.rs +21 -0
- package/dist-engine-src/src/catalog/context.rs +18 -18
- package/dist-engine-src/src/catalog/snapshot.rs +8 -8
- package/dist-engine-src/src/changelog/bench_support.rs +785 -0
- package/dist-engine-src/src/changelog/change.rs +1 -0
- package/dist-engine-src/src/changelog/codec.rs +497 -0
- package/dist-engine-src/src/changelog/commit.rs +1 -0
- package/dist-engine-src/src/changelog/context.rs +1614 -0
- package/dist-engine-src/src/changelog/mod.rs +29 -0
- package/dist-engine-src/src/changelog/store.rs +163 -0
- package/dist-engine-src/src/changelog/test_support.rs +54 -0
- package/dist-engine-src/src/changelog/types.rs +213 -0
- package/dist-engine-src/src/commit_graph/context.rs +317 -274
- package/dist-engine-src/src/commit_graph/mod.rs +2 -4
- package/dist-engine-src/src/commit_graph/types.rs +22 -42
- package/dist-engine-src/src/commit_graph/walker.rs +133 -103
- package/dist-engine-src/src/common/error.rs +52 -18
- package/dist-engine-src/src/common/identity.rs +2 -2
- package/dist-engine-src/src/common/mod.rs +1 -1
- package/dist-engine-src/src/domain.rs +42 -46
- package/dist-engine-src/src/engine.rs +74 -96
- package/dist-engine-src/src/{entity_identity.rs → entity_pk.rs} +89 -92
- package/dist-engine-src/src/functions/context.rs +56 -52
- package/dist-engine-src/src/functions/state.rs +51 -52
- package/dist-engine-src/src/init.rs +288 -154
- package/dist-engine-src/src/json_store/context.rs +15 -266
- package/dist-engine-src/src/json_store/mod.rs +26 -0
- package/dist-engine-src/src/json_store/store.rs +103 -718
- package/dist-engine-src/src/json_store/types.rs +4 -9
- package/dist-engine-src/src/lib.rs +49 -19
- package/dist-engine-src/src/live_state/context.rs +654 -790
- package/dist-engine-src/src/live_state/mod.rs +9 -3
- package/dist-engine-src/src/live_state/overlay.rs +4 -4
- package/dist-engine-src/src/live_state/types.rs +30 -21
- package/dist-engine-src/src/live_state/visibility.rs +514 -71
- package/dist-engine-src/src/plugin/install.rs +48 -48
- package/dist-engine-src/src/plugin/manifest.rs +7 -7
- package/dist-engine-src/src/plugin/materializer.rs +0 -275
- package/dist-engine-src/src/plugin/plugin_manifest.json +4 -3
- package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +2 -2
- package/dist-engine-src/src/schema/builtin/lix_branch_descriptor.json +34 -0
- package/dist-engine-src/src/schema/builtin/lix_branch_ref.json +48 -0
- package/dist-engine-src/src/schema/builtin/lix_change.json +3 -3
- package/dist-engine-src/src/schema/builtin/lix_commit.json +1 -1
- package/dist-engine-src/src/schema/builtin/lix_label_assignment.json +6 -6
- package/dist-engine-src/src/schema/builtin/mod.rs +18 -20
- package/dist-engine-src/src/schema/compatibility.rs +11 -11
- package/dist-engine-src/src/schema/definition.json +2 -2
- package/dist-engine-src/src/schema/definition.rs +5 -5
- package/dist-engine-src/src/schema/key.rs +3 -3
- package/dist-engine-src/src/schema/mod.rs +1 -1
- package/dist-engine-src/src/schema/tests.rs +18 -18
- package/dist-engine-src/src/session/context.rs +819 -124
- package/dist-engine-src/src/session/create_branch.rs +94 -0
- package/dist-engine-src/src/session/execute.rs +260 -57
- package/dist-engine-src/src/session/merge/analysis.rs +9 -3
- package/dist-engine-src/src/session/merge/{version.rs → branch.rs} +119 -129
- package/dist-engine-src/src/session/merge/conflicts.rs +2 -2
- package/dist-engine-src/src/session/merge/mod.rs +5 -6
- package/dist-engine-src/src/session/merge/stats.rs +7 -11
- package/dist-engine-src/src/session/mod.rs +19 -16
- package/dist-engine-src/src/session/switch_branch.rs +113 -0
- package/dist-engine-src/src/session/transaction.rs +557 -0
- package/dist-engine-src/src/sql2/bind/classify.rs +102 -0
- package/dist-engine-src/src/sql2/bind/error.rs +5 -0
- package/dist-engine-src/src/sql2/bind/expr.rs +29 -0
- package/dist-engine-src/src/sql2/bind/mod.rs +12 -0
- package/dist-engine-src/src/sql2/{udfs/public_call.rs → bind/public_udf.rs} +98 -3
- package/dist-engine-src/src/sql2/bind/read.rs +65 -0
- package/dist-engine-src/src/sql2/bind/statement.rs +2236 -0
- package/dist-engine-src/src/sql2/bind/table.rs +273 -0
- package/dist-engine-src/src/sql2/bind/write.rs +86 -0
- package/dist-engine-src/src/sql2/branch_scope.rs +436 -0
- package/dist-engine-src/src/sql2/catalog/capability.rs +20 -0
- package/dist-engine-src/src/sql2/catalog/entity_surface.rs +296 -0
- package/dist-engine-src/src/sql2/catalog/mod.rs +15 -0
- package/dist-engine-src/src/sql2/catalog/registry.rs +556 -0
- package/dist-engine-src/src/sql2/catalog/schema.rs +88 -0
- package/dist-engine-src/src/sql2/catalog/surface.rs +41 -0
- package/dist-engine-src/src/sql2/change_materialization.rs +122 -0
- package/dist-engine-src/src/sql2/context.rs +36 -30
- package/dist-engine-src/src/sql2/error.rs +4 -5
- package/dist-engine-src/src/sql2/exec/bound_public_write.rs +1593 -0
- package/dist-engine-src/src/sql2/exec/datafusion.rs +5266 -0
- package/dist-engine-src/src/sql2/exec/fast_write.rs +82 -0
- package/dist-engine-src/src/sql2/exec/mod.rs +24 -0
- package/dist-engine-src/src/sql2/exec/write.rs +661 -0
- package/dist-engine-src/src/sql2/filesystem_planner.rs +72 -77
- package/dist-engine-src/src/sql2/filesystem_visibility.rs +21 -21
- package/dist-engine-src/src/sql2/history_projection.rs +8 -8
- package/dist-engine-src/src/sql2/history_route.rs +35 -31
- package/dist-engine-src/src/sql2/mod.rs +30 -24
- package/dist-engine-src/src/sql2/optimize/datafusion.rs +1 -0
- package/dist-engine-src/src/sql2/optimize/mod.rs +2 -0
- package/dist-engine-src/src/sql2/optimize/simple_write.rs +116 -0
- package/dist-engine-src/src/sql2/parse/mod.rs +69 -0
- package/dist-engine-src/src/sql2/parse/normalize.rs +1 -0
- package/dist-engine-src/src/sql2/plan/branch_scope.rs +24 -0
- package/dist-engine-src/src/sql2/plan/mod.rs +5 -0
- package/dist-engine-src/src/sql2/plan/predicate.rs +22 -0
- package/dist-engine-src/src/sql2/plan/write.rs +147 -0
- package/dist-engine-src/src/sql2/predicate_typecheck.rs +258 -0
- package/dist-engine-src/src/sql2/{version_provider.rs → providers/branch.rs} +218 -214
- package/dist-engine-src/src/sql2/{change_provider.rs → providers/change.rs} +156 -42
- package/dist-engine-src/src/sql2/{directory_provider.rs → providers/directory.rs} +291 -322
- package/dist-engine-src/src/sql2/{directory_history_provider.rs → providers/directory_history.rs} +56 -42
- package/dist-engine-src/src/sql2/providers/entity.rs +1484 -0
- package/dist-engine-src/src/sql2/{entity_history_provider.rs → providers/entity_history.rs} +43 -31
- package/dist-engine-src/src/sql2/{file_provider.rs → providers/file.rs} +323 -316
- package/dist-engine-src/src/sql2/{file_history_provider.rs → providers/file_history.rs} +60 -46
- package/dist-engine-src/src/sql2/{history_provider.rs → providers/history.rs} +46 -32
- package/dist-engine-src/src/sql2/{lix_state_provider.rs → providers/lix_state.rs} +359 -329
- package/dist-engine-src/src/sql2/providers/mod.rs +508 -0
- package/dist-engine-src/src/sql2/read_only.rs +2 -2
- package/dist-engine-src/src/sql2/session.rs +47 -96
- package/dist-engine-src/src/sql2/storage/constraints.rs +1 -0
- package/dist-engine-src/src/sql2/storage/mod.rs +1 -0
- package/dist-engine-src/src/sql2/test_support/differential.rs +712 -0
- package/dist-engine-src/src/sql2/test_support/generators.rs +354 -0
- package/dist-engine-src/src/sql2/test_support/mod.rs +2 -0
- package/dist-engine-src/src/sql2/udfs/{lix_active_version_commit_id.rs → lix_active_branch_commit_id.rs} +7 -7
- package/dist-engine-src/src/sql2/udfs/mod.rs +3 -6
- package/dist-engine-src/src/sql2/write_normalization.rs +45 -22
- package/dist-engine-src/src/storage/conformance.rs +399 -0
- package/dist-engine-src/src/storage/context.rs +552 -288
- package/dist-engine-src/src/storage/mod.rs +48 -10
- package/dist-engine-src/src/storage/point.rs +440 -0
- package/dist-engine-src/src/storage/read_scope.rs +43 -64
- package/dist-engine-src/src/storage/reader.rs +867 -0
- package/dist-engine-src/src/storage/scan.rs +784 -0
- package/dist-engine-src/src/storage/spaces.rs +236 -0
- package/dist-engine-src/src/storage/stats.rs +80 -0
- package/dist-engine-src/src/storage/write_set.rs +962 -0
- package/dist-engine-src/src/storage_bench.rs +136 -4828
- package/dist-engine-src/src/test_support.rs +360 -138
- package/dist-engine-src/src/tracked_state/bench_support.rs +394 -0
- package/dist-engine-src/src/tracked_state/codec.rs +155 -1057
- package/dist-engine-src/src/tracked_state/commit_root_rebuild.rs +358 -0
- package/dist-engine-src/src/tracked_state/context.rs +1927 -993
- package/dist-engine-src/src/tracked_state/diff.rs +1715 -261
- package/dist-engine-src/src/tracked_state/merge.rs +74 -88
- package/dist-engine-src/src/tracked_state/mod.rs +19 -16
- package/dist-engine-src/src/tracked_state/{materialization.rs → row_materialization.rs} +50 -178
- package/dist-engine-src/src/tracked_state/storage.rs +243 -191
- package/dist-engine-src/src/tracked_state/tree.rs +247 -371
- package/dist-engine-src/src/tracked_state/types.rs +49 -42
- package/dist-engine-src/src/transaction/bench_support.rs +407 -0
- package/dist-engine-src/src/transaction/commit.rs +821 -713
- package/dist-engine-src/src/transaction/context.rs +705 -600
- package/dist-engine-src/src/transaction/mod.rs +13 -2
- package/dist-engine-src/src/transaction/normalization.rs +63 -76
- package/dist-engine-src/src/transaction/prep.rs +13 -13
- package/dist-engine-src/src/transaction/schema_resolver.rs +19 -5
- package/dist-engine-src/src/transaction/staging.rs +228 -434
- package/dist-engine-src/src/transaction/types.rs +41 -98
- package/dist-engine-src/src/transaction/validation.rs +382 -446
- package/dist-engine-src/src/untracked_state/codec.rs +337 -29
- package/dist-engine-src/src/untracked_state/context.rs +7 -7
- package/dist-engine-src/src/untracked_state/materialization.rs +2 -2
- package/dist-engine-src/src/untracked_state/mod.rs +1 -1
- package/dist-engine-src/src/untracked_state/storage.rs +659 -157
- package/dist-engine-src/src/untracked_state/types.rs +21 -21
- package/package.json +71 -68
- package/dist-engine-src/src/backend/kv.rs +0 -358
- package/dist-engine-src/src/backend/testing.rs +0 -658
- package/dist-engine-src/src/commit_store/codec.rs +0 -887
- package/dist-engine-src/src/commit_store/context.rs +0 -944
- package/dist-engine-src/src/commit_store/materialization.rs +0 -84
- package/dist-engine-src/src/commit_store/mod.rs +0 -16
- package/dist-engine-src/src/commit_store/storage.rs +0 -600
- package/dist-engine-src/src/commit_store/types.rs +0 -215
- package/dist-engine-src/src/schema/builtin/lix_version_descriptor.json +0 -34
- package/dist-engine-src/src/schema/builtin/lix_version_ref.json +0 -48
- package/dist-engine-src/src/session/create_version.rs +0 -88
- package/dist-engine-src/src/session/merge/apply.rs +0 -23
- package/dist-engine-src/src/session/optimization9_sql2_bench.rs +0 -100
- package/dist-engine-src/src/session/switch_version.rs +0 -109
- package/dist-engine-src/src/sql2/classify.rs +0 -182
- package/dist-engine-src/src/sql2/entity_provider.rs +0 -3211
- package/dist-engine-src/src/sql2/execute.rs +0 -3440
- package/dist-engine-src/src/sql2/public_bind/assignment.rs +0 -46
- package/dist-engine-src/src/sql2/public_bind/capability.rs +0 -41
- package/dist-engine-src/src/sql2/public_bind/dml.rs +0 -166
- package/dist-engine-src/src/sql2/public_bind/mod.rs +0 -25
- package/dist-engine-src/src/sql2/public_bind/table.rs +0 -168
- package/dist-engine-src/src/sql2/version_scope.rs +0 -394
- package/dist-engine-src/src/storage/types.rs +0 -501
- package/dist-engine-src/src/tracked_state/by_file_index.rs +0 -98
- package/dist-engine-src/src/tracked_state/materializer.rs +0 -488
- package/dist-engine-src/src/transaction/live_state_overlay.rs +0 -35
- package/dist-engine-src/src/version/lifecycle.rs +0 -221
- package/dist-engine-src/src/version/mod.rs +0 -13
- package/dist-engine-src/src/version/refs.rs +0 -330
- package/dist-engine-src/src/version/stage_rows.rs +0 -67
- package/dist-engine-src/src/version/types.rs +0 -21
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//! Change-specific behavior lives here once changelog storage is implemented.
|
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
use super::types::{
|
|
2
|
+
ChangeRecord, ChangeRecordView, CommitChangeRef, CommitChangeRefChunk,
|
|
3
|
+
CommitChangeRefChunkView, CommitChangeRefView, CommitRecord, CommitRecordView, EntityPkRef,
|
|
4
|
+
};
|
|
5
|
+
use crate::common::LixError;
|
|
6
|
+
use crate::entity_pk::EntityPk;
|
|
7
|
+
use crate::json_store::JsonRef;
|
|
8
|
+
|
|
9
|
+
const COMMIT_MAGIC: &[u8; 5] = b"LXCM1";
|
|
10
|
+
const CHANGE_MAGIC: &[u8; 5] = b"LXCH1";
|
|
11
|
+
const COMMIT_CHANGE_REF_CHUNK_MAGIC: &[u8; 5] = b"LXCR1";
|
|
12
|
+
|
|
13
|
+
pub(crate) fn encode_commit_record(record: &CommitRecord) -> Result<Vec<u8>, LixError> {
|
|
14
|
+
let mut bytes = Vec::new();
|
|
15
|
+
bytes.extend_from_slice(COMMIT_MAGIC);
|
|
16
|
+
write_u32(&mut bytes, record.format_version);
|
|
17
|
+
write_str(&mut bytes, &record.commit_id)?;
|
|
18
|
+
write_str_vec(&mut bytes, &record.parent_commit_ids)?;
|
|
19
|
+
write_str(&mut bytes, &record.change_id)?;
|
|
20
|
+
write_str_vec(&mut bytes, &record.author_account_ids)?;
|
|
21
|
+
write_str(&mut bytes, &record.created_at)?;
|
|
22
|
+
Ok(bytes)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
pub(crate) fn view_commit_record(bytes: &[u8]) -> Result<CommitRecordView<'_>, LixError> {
|
|
26
|
+
let mut cursor = ByteCursor::new(bytes);
|
|
27
|
+
cursor.expect_magic(COMMIT_MAGIC, "commit record")?;
|
|
28
|
+
let view = CommitRecordView {
|
|
29
|
+
format_version: cursor.read_u32("format_version")?,
|
|
30
|
+
commit_id: cursor.read_str("commit_id")?,
|
|
31
|
+
parent_commit_ids: cursor.read_str_vec("parent_commit_ids")?,
|
|
32
|
+
change_id: cursor.read_str("change_id")?,
|
|
33
|
+
author_account_ids: cursor.read_str_vec("author_account_ids")?,
|
|
34
|
+
created_at: cursor.read_str("created_at")?,
|
|
35
|
+
};
|
|
36
|
+
cursor.expect_end("commit record")?;
|
|
37
|
+
Ok(view)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
pub(crate) fn decode_commit_record(bytes: &[u8]) -> Result<CommitRecord, LixError> {
|
|
41
|
+
let view = view_commit_record(bytes)?;
|
|
42
|
+
Ok(CommitRecord {
|
|
43
|
+
format_version: view.format_version,
|
|
44
|
+
commit_id: view.commit_id.to_string(),
|
|
45
|
+
parent_commit_ids: view
|
|
46
|
+
.parent_commit_ids
|
|
47
|
+
.iter()
|
|
48
|
+
.map(|value| (*value).to_string())
|
|
49
|
+
.collect(),
|
|
50
|
+
change_id: view.change_id.to_string(),
|
|
51
|
+
author_account_ids: view
|
|
52
|
+
.author_account_ids
|
|
53
|
+
.iter()
|
|
54
|
+
.map(|value| (*value).to_string())
|
|
55
|
+
.collect(),
|
|
56
|
+
created_at: view.created_at.to_string(),
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
pub(crate) fn encode_change_record(record: &ChangeRecord) -> Result<Vec<u8>, LixError> {
|
|
61
|
+
let mut bytes = Vec::new();
|
|
62
|
+
bytes.extend_from_slice(CHANGE_MAGIC);
|
|
63
|
+
write_u32(&mut bytes, record.format_version);
|
|
64
|
+
write_str(&mut bytes, &record.change_id)?;
|
|
65
|
+
write_str(&mut bytes, &record.schema_key)?;
|
|
66
|
+
write_entity_pk(&mut bytes, &record.entity_pk)?;
|
|
67
|
+
write_optional_str(&mut bytes, record.file_id.as_deref())?;
|
|
68
|
+
write_optional_json_ref(&mut bytes, record.snapshot_ref)?;
|
|
69
|
+
write_optional_json_ref(&mut bytes, record.metadata_ref)?;
|
|
70
|
+
write_str(&mut bytes, &record.created_at)?;
|
|
71
|
+
Ok(bytes)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
pub(crate) fn view_change_record(bytes: &[u8]) -> Result<ChangeRecordView<'_>, LixError> {
|
|
75
|
+
let mut cursor = ByteCursor::new(bytes);
|
|
76
|
+
cursor.expect_magic(CHANGE_MAGIC, "change record")?;
|
|
77
|
+
let view = ChangeRecordView {
|
|
78
|
+
format_version: cursor.read_u32("format_version")?,
|
|
79
|
+
change_id: cursor.read_str("change_id")?,
|
|
80
|
+
schema_key: cursor.read_str("schema_key")?,
|
|
81
|
+
entity_pk: cursor.read_entity_pk("entity_pk")?,
|
|
82
|
+
file_id: cursor.read_optional_str("file_id")?,
|
|
83
|
+
snapshot_ref: cursor.read_optional_json_ref("snapshot_ref")?,
|
|
84
|
+
metadata_ref: cursor.read_optional_json_ref("metadata_ref")?,
|
|
85
|
+
created_at: cursor.read_str("created_at")?,
|
|
86
|
+
};
|
|
87
|
+
cursor.expect_end("change record")?;
|
|
88
|
+
Ok(view)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
pub(crate) fn decode_change_record(bytes: &[u8]) -> Result<ChangeRecord, LixError> {
|
|
92
|
+
let view = view_change_record(bytes)?;
|
|
93
|
+
Ok(ChangeRecord {
|
|
94
|
+
format_version: view.format_version,
|
|
95
|
+
change_id: view.change_id.to_string(),
|
|
96
|
+
schema_key: view.schema_key.to_string(),
|
|
97
|
+
entity_pk: entity_pk_from_ref(view.entity_pk)?,
|
|
98
|
+
file_id: view.file_id.map(str::to_string),
|
|
99
|
+
snapshot_ref: view.snapshot_ref,
|
|
100
|
+
metadata_ref: view.metadata_ref,
|
|
101
|
+
created_at: view.created_at.to_string(),
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
pub(crate) fn encode_commit_change_ref_chunk(
|
|
106
|
+
chunk: &CommitChangeRefChunk,
|
|
107
|
+
) -> Result<Vec<u8>, LixError> {
|
|
108
|
+
let mut bytes = Vec::new();
|
|
109
|
+
bytes.extend_from_slice(COMMIT_CHANGE_REF_CHUNK_MAGIC);
|
|
110
|
+
write_u32(&mut bytes, chunk.format_version);
|
|
111
|
+
let schema_keys = commit_change_ref_schema_dictionary(&chunk.entries);
|
|
112
|
+
let file_ids = commit_change_ref_file_dictionary(&chunk.entries);
|
|
113
|
+
write_len(
|
|
114
|
+
&mut bytes,
|
|
115
|
+
schema_keys.len(),
|
|
116
|
+
"commit change ref schema dictionary",
|
|
117
|
+
)?;
|
|
118
|
+
for schema_key in &schema_keys {
|
|
119
|
+
write_str(&mut bytes, schema_key)?;
|
|
120
|
+
}
|
|
121
|
+
write_len(
|
|
122
|
+
&mut bytes,
|
|
123
|
+
file_ids.len(),
|
|
124
|
+
"commit change ref file dictionary",
|
|
125
|
+
)?;
|
|
126
|
+
for file_id in &file_ids {
|
|
127
|
+
write_optional_str(&mut bytes, *file_id)?;
|
|
128
|
+
}
|
|
129
|
+
write_len(&mut bytes, chunk.entries.len(), "commit change ref entries")?;
|
|
130
|
+
for entry in &chunk.entries {
|
|
131
|
+
let schema_index = dictionary_index(
|
|
132
|
+
schema_keys.iter().copied(),
|
|
133
|
+
entry.schema_key.as_str(),
|
|
134
|
+
"commit change ref schema dictionary",
|
|
135
|
+
)?;
|
|
136
|
+
let file_index = optional_dictionary_index(
|
|
137
|
+
file_ids.iter().copied(),
|
|
138
|
+
entry.file_id.as_deref(),
|
|
139
|
+
"commit change ref file dictionary",
|
|
140
|
+
)?;
|
|
141
|
+
write_u16_index(&mut bytes, schema_index, "commit change ref schema index")?;
|
|
142
|
+
write_u16_index(&mut bytes, file_index, "commit change ref file index")?;
|
|
143
|
+
write_entity_pk_compact(&mut bytes, &entry.entity_pk)?;
|
|
144
|
+
write_str(&mut bytes, &entry.change_id)?;
|
|
145
|
+
}
|
|
146
|
+
Ok(bytes)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
pub(crate) fn view_commit_change_ref_chunk<'a>(
|
|
150
|
+
bytes: &'a [u8],
|
|
151
|
+
commit_id: &'a str,
|
|
152
|
+
) -> Result<CommitChangeRefChunkView<'a>, LixError> {
|
|
153
|
+
let mut cursor = ByteCursor::new(bytes);
|
|
154
|
+
cursor.expect_magic(COMMIT_CHANGE_REF_CHUNK_MAGIC, "commit change ref chunk")?;
|
|
155
|
+
let format_version = cursor.read_u32("format_version")?;
|
|
156
|
+
let schema_keys = cursor.read_str_vec("commit change ref schema dictionary")?;
|
|
157
|
+
let file_ids = cursor.read_optional_str_vec("commit change ref file dictionary")?;
|
|
158
|
+
let entry_count = cursor.read_len("commit change ref entries")?;
|
|
159
|
+
let mut entries = Vec::with_capacity(entry_count);
|
|
160
|
+
for index in 0..entry_count {
|
|
161
|
+
let context = format!("commit change ref entries[{index}]");
|
|
162
|
+
let schema_index = cursor.read_u16(&format!("{context}.schema_index"))? as usize;
|
|
163
|
+
let file_index = cursor.read_u16(&format!("{context}.file_index"))? as usize;
|
|
164
|
+
entries.push(CommitChangeRefView {
|
|
165
|
+
schema_key: *schema_keys.get(schema_index).ok_or_else(|| {
|
|
166
|
+
LixError::new(
|
|
167
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
168
|
+
format!("changelog {context}.schema_index is out of bounds"),
|
|
169
|
+
)
|
|
170
|
+
})?,
|
|
171
|
+
file_id: *file_ids.get(file_index).ok_or_else(|| {
|
|
172
|
+
LixError::new(
|
|
173
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
174
|
+
format!("changelog {context}.file_index is out of bounds"),
|
|
175
|
+
)
|
|
176
|
+
})?,
|
|
177
|
+
entity_pk: cursor.read_entity_pk_compact(&format!("{context}.entity_pk"))?,
|
|
178
|
+
change_id: cursor.read_str(&format!("{context}.change_id"))?,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
cursor.expect_end("commit change ref chunk")?;
|
|
182
|
+
Ok(CommitChangeRefChunkView {
|
|
183
|
+
format_version,
|
|
184
|
+
commit_id,
|
|
185
|
+
entries,
|
|
186
|
+
})
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
pub(crate) fn decode_commit_change_ref_chunk(
|
|
190
|
+
bytes: &[u8],
|
|
191
|
+
commit_id: &str,
|
|
192
|
+
) -> Result<CommitChangeRefChunk, LixError> {
|
|
193
|
+
let view = view_commit_change_ref_chunk(bytes, commit_id)?;
|
|
194
|
+
Ok(CommitChangeRefChunk {
|
|
195
|
+
format_version: view.format_version,
|
|
196
|
+
commit_id: view.commit_id.to_string(),
|
|
197
|
+
entries: view
|
|
198
|
+
.entries
|
|
199
|
+
.iter()
|
|
200
|
+
.map(|entry| {
|
|
201
|
+
Ok(CommitChangeRef {
|
|
202
|
+
schema_key: entry.schema_key.to_string(),
|
|
203
|
+
file_id: entry.file_id.map(str::to_string),
|
|
204
|
+
entity_pk: entity_pk_from_ref(entry.entity_pk.clone())?,
|
|
205
|
+
change_id: entry.change_id.to_string(),
|
|
206
|
+
})
|
|
207
|
+
})
|
|
208
|
+
.collect::<Result<Vec<_>, LixError>>()?,
|
|
209
|
+
})
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
fn commit_change_ref_schema_dictionary(entries: &[CommitChangeRef]) -> Vec<&str> {
|
|
213
|
+
let mut values = Vec::new();
|
|
214
|
+
for entry in entries {
|
|
215
|
+
if !values.contains(&entry.schema_key.as_str()) {
|
|
216
|
+
values.push(entry.schema_key.as_str());
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
values
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
fn commit_change_ref_file_dictionary(entries: &[CommitChangeRef]) -> Vec<Option<&str>> {
|
|
223
|
+
let mut values = Vec::new();
|
|
224
|
+
for entry in entries {
|
|
225
|
+
let value = entry.file_id.as_deref();
|
|
226
|
+
if !values.contains(&value) {
|
|
227
|
+
values.push(value);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
values
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
fn dictionary_index<'a>(
|
|
234
|
+
mut values: impl Iterator<Item = &'a str>,
|
|
235
|
+
needle: &str,
|
|
236
|
+
context: &str,
|
|
237
|
+
) -> Result<usize, LixError> {
|
|
238
|
+
values.position(|value| value == needle).ok_or_else(|| {
|
|
239
|
+
LixError::new(
|
|
240
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
241
|
+
format!("changelog {context} is missing value '{needle}'"),
|
|
242
|
+
)
|
|
243
|
+
})
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
fn optional_dictionary_index<'a>(
|
|
247
|
+
mut values: impl Iterator<Item = Option<&'a str>>,
|
|
248
|
+
needle: Option<&str>,
|
|
249
|
+
context: &str,
|
|
250
|
+
) -> Result<usize, LixError> {
|
|
251
|
+
values.position(|value| value == needle).ok_or_else(|| {
|
|
252
|
+
LixError::new(
|
|
253
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
254
|
+
format!("changelog {context} is missing optional value"),
|
|
255
|
+
)
|
|
256
|
+
})
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
fn write_str_vec(out: &mut Vec<u8>, values: &[String]) -> Result<(), LixError> {
|
|
260
|
+
write_len(out, values.len(), "string vec")?;
|
|
261
|
+
for value in values {
|
|
262
|
+
write_str(out, value)?;
|
|
263
|
+
}
|
|
264
|
+
Ok(())
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
fn write_entity_pk(out: &mut Vec<u8>, identity: &EntityPk) -> Result<(), LixError> {
|
|
268
|
+
write_len(out, identity.parts.len(), "entity primary key parts")?;
|
|
269
|
+
for part in &identity.parts {
|
|
270
|
+
write_str(out, part)?;
|
|
271
|
+
}
|
|
272
|
+
Ok(())
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
fn write_entity_pk_compact(out: &mut Vec<u8>, identity: &EntityPk) -> Result<(), LixError> {
|
|
276
|
+
if identity.parts.len() == 1 {
|
|
277
|
+
out.push(1);
|
|
278
|
+
write_str(out, &identity.parts[0])
|
|
279
|
+
} else {
|
|
280
|
+
out.push(0);
|
|
281
|
+
write_entity_pk(out, identity)
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
fn write_optional_str(out: &mut Vec<u8>, value: Option<&str>) -> Result<(), LixError> {
|
|
286
|
+
match value {
|
|
287
|
+
Some(value) => {
|
|
288
|
+
out.push(1);
|
|
289
|
+
write_str(out, value)
|
|
290
|
+
}
|
|
291
|
+
None => {
|
|
292
|
+
out.push(0);
|
|
293
|
+
Ok(())
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
fn write_optional_json_ref(out: &mut Vec<u8>, value: Option<JsonRef>) -> Result<(), LixError> {
|
|
299
|
+
match value {
|
|
300
|
+
Some(value) => {
|
|
301
|
+
out.push(1);
|
|
302
|
+
out.extend_from_slice(value.as_hash_bytes());
|
|
303
|
+
}
|
|
304
|
+
None => out.push(0),
|
|
305
|
+
}
|
|
306
|
+
Ok(())
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
fn write_str(out: &mut Vec<u8>, value: &str) -> Result<(), LixError> {
|
|
310
|
+
write_len(out, value.len(), "string bytes")?;
|
|
311
|
+
out.extend_from_slice(value.as_bytes());
|
|
312
|
+
Ok(())
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
fn write_len(out: &mut Vec<u8>, len: usize, context: &str) -> Result<(), LixError> {
|
|
316
|
+
let len = u32::try_from(len).map_err(|_| {
|
|
317
|
+
LixError::new(
|
|
318
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
319
|
+
format!("changelog {context} length exceeds u32"),
|
|
320
|
+
)
|
|
321
|
+
})?;
|
|
322
|
+
write_u32(out, len);
|
|
323
|
+
Ok(())
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
fn write_u32(out: &mut Vec<u8>, value: u32) {
|
|
327
|
+
out.extend_from_slice(&value.to_be_bytes());
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
fn write_u16_index(out: &mut Vec<u8>, value: usize, context: &str) -> Result<(), LixError> {
|
|
331
|
+
let value = u16::try_from(value).map_err(|_| {
|
|
332
|
+
LixError::new(
|
|
333
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
334
|
+
format!("changelog {context} exceeds u16"),
|
|
335
|
+
)
|
|
336
|
+
})?;
|
|
337
|
+
out.extend_from_slice(&value.to_be_bytes());
|
|
338
|
+
Ok(())
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
fn entity_pk_from_ref(value: EntityPkRef<'_>) -> Result<EntityPk, LixError> {
|
|
342
|
+
EntityPk::from_parts(value.parts.iter().map(|part| (*part).to_string()).collect()).map_err(
|
|
343
|
+
|error| {
|
|
344
|
+
LixError::new(
|
|
345
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
346
|
+
format!("changelog entity primary key is invalid: {error}"),
|
|
347
|
+
)
|
|
348
|
+
},
|
|
349
|
+
)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
#[derive(Clone)]
|
|
353
|
+
struct ByteCursor<'a> {
|
|
354
|
+
bytes: &'a [u8],
|
|
355
|
+
offset: usize,
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
impl<'a> ByteCursor<'a> {
|
|
359
|
+
fn new(bytes: &'a [u8]) -> Self {
|
|
360
|
+
Self { bytes, offset: 0 }
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
fn expect_magic(&mut self, magic: &[u8], context: &str) -> Result<(), LixError> {
|
|
364
|
+
let found = self.read_bytes(magic.len(), context)?;
|
|
365
|
+
if found != magic {
|
|
366
|
+
return Err(LixError::new(
|
|
367
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
368
|
+
format!("changelog {context} has invalid magic"),
|
|
369
|
+
));
|
|
370
|
+
}
|
|
371
|
+
Ok(())
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
fn read_str_vec(&mut self, context: &str) -> Result<Vec<&'a str>, LixError> {
|
|
375
|
+
let len = self.read_len(context)?;
|
|
376
|
+
let mut values = Vec::with_capacity(len);
|
|
377
|
+
for index in 0..len {
|
|
378
|
+
values.push(self.read_str(&format!("{context}[{index}]"))?);
|
|
379
|
+
}
|
|
380
|
+
Ok(values)
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
fn read_optional_str_vec(&mut self, context: &str) -> Result<Vec<Option<&'a str>>, LixError> {
|
|
384
|
+
let len = self.read_len(context)?;
|
|
385
|
+
let mut values = Vec::with_capacity(len);
|
|
386
|
+
for index in 0..len {
|
|
387
|
+
values.push(self.read_optional_str(&format!("{context}[{index}]"))?);
|
|
388
|
+
}
|
|
389
|
+
Ok(values)
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
fn read_entity_pk(&mut self, context: &str) -> Result<EntityPkRef<'a>, LixError> {
|
|
393
|
+
let len = self.read_len(context)?;
|
|
394
|
+
let mut parts = Vec::with_capacity(len);
|
|
395
|
+
for index in 0..len {
|
|
396
|
+
parts.push(self.read_str(&format!("{context}.parts[{index}]"))?);
|
|
397
|
+
}
|
|
398
|
+
Ok(EntityPkRef { parts })
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
fn read_entity_pk_compact(&mut self, context: &str) -> Result<EntityPkRef<'a>, LixError> {
|
|
402
|
+
match self.read_bytes(1, context)?[0] {
|
|
403
|
+
0 => self.read_entity_pk(context),
|
|
404
|
+
1 => Ok(EntityPkRef {
|
|
405
|
+
parts: vec![self.read_str(&format!("{context}.part"))?],
|
|
406
|
+
}),
|
|
407
|
+
_ => Err(LixError::new(
|
|
408
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
409
|
+
format!("changelog {context} compact entity primary key flag is invalid"),
|
|
410
|
+
)),
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
fn read_optional_str(&mut self, context: &str) -> Result<Option<&'a str>, LixError> {
|
|
415
|
+
match self.read_flag(context)? {
|
|
416
|
+
false => Ok(None),
|
|
417
|
+
true => Ok(Some(self.read_str(context)?)),
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
fn read_optional_json_ref(&mut self, context: &str) -> Result<Option<JsonRef>, LixError> {
|
|
422
|
+
match self.read_flag(context)? {
|
|
423
|
+
false => Ok(None),
|
|
424
|
+
true => {
|
|
425
|
+
let bytes = self.read_bytes(32, context)?;
|
|
426
|
+
let mut hash = [0; 32];
|
|
427
|
+
hash.copy_from_slice(bytes);
|
|
428
|
+
Ok(Some(JsonRef::from_hash_bytes(hash)))
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
fn read_str(&mut self, context: &str) -> Result<&'a str, LixError> {
|
|
434
|
+
let len = self.read_len(context)?;
|
|
435
|
+
let bytes = self.read_bytes(len, context)?;
|
|
436
|
+
std::str::from_utf8(bytes).map_err(|error| {
|
|
437
|
+
LixError::new(
|
|
438
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
439
|
+
format!("changelog {context} contains invalid UTF-8: {error}"),
|
|
440
|
+
)
|
|
441
|
+
})
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
fn read_flag(&mut self, context: &str) -> Result<bool, LixError> {
|
|
445
|
+
let byte = self.read_bytes(1, context)?[0];
|
|
446
|
+
match byte {
|
|
447
|
+
0 => Ok(false),
|
|
448
|
+
1 => Ok(true),
|
|
449
|
+
_ => Err(LixError::new(
|
|
450
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
451
|
+
format!("changelog {context} option flag is invalid"),
|
|
452
|
+
)),
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
fn read_len(&mut self, context: &str) -> Result<usize, LixError> {
|
|
457
|
+
Ok(self.read_u32(context)? as usize)
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
fn read_u32(&mut self, context: &str) -> Result<u32, LixError> {
|
|
461
|
+
let bytes = self.read_bytes(4, context)?;
|
|
462
|
+
Ok(u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
fn read_u16(&mut self, context: &str) -> Result<u16, LixError> {
|
|
466
|
+
let bytes = self.read_bytes(2, context)?;
|
|
467
|
+
Ok(u16::from_be_bytes([bytes[0], bytes[1]]))
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
fn read_bytes(&mut self, len: usize, context: &str) -> Result<&'a [u8], LixError> {
|
|
471
|
+
let end = self.offset.checked_add(len).ok_or_else(|| {
|
|
472
|
+
LixError::new(
|
|
473
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
474
|
+
format!("changelog {context} length overflows"),
|
|
475
|
+
)
|
|
476
|
+
})?;
|
|
477
|
+
if end > self.bytes.len() {
|
|
478
|
+
return Err(LixError::new(
|
|
479
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
480
|
+
format!("changelog {context} is truncated"),
|
|
481
|
+
));
|
|
482
|
+
}
|
|
483
|
+
let out = &self.bytes[self.offset..end];
|
|
484
|
+
self.offset = end;
|
|
485
|
+
Ok(out)
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
fn expect_end(&self, context: &str) -> Result<(), LixError> {
|
|
489
|
+
if self.offset != self.bytes.len() {
|
|
490
|
+
return Err(LixError::new(
|
|
491
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
492
|
+
format!("changelog {context} has trailing bytes"),
|
|
493
|
+
));
|
|
494
|
+
}
|
|
495
|
+
Ok(())
|
|
496
|
+
}
|
|
497
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//! Commit-specific behavior lives here once changelog storage is implemented.
|