@lix-js/sdk 0.6.0-preview.2 → 0.6.0-preview.4
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/SKILL.md +46 -8
- package/dist/engine-wasm/wasm/lix_engine.d.ts +25 -1
- package/dist/engine-wasm/wasm/lix_engine.js +60 -2
- package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
- package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +5 -0
- package/dist/generated/builtin-schemas.d.ts +87 -162
- package/dist/generated/builtin-schemas.js +139 -236
- package/dist/open-lix.d.ts +10 -3
- package/dist/open-lix.js +39 -0
- package/dist-engine-src/src/binary_cas/types.rs +0 -6
- package/dist-engine-src/src/catalog/context.rs +412 -0
- package/dist-engine-src/src/catalog/mod.rs +10 -0
- package/dist-engine-src/src/catalog/schema.rs +4 -0
- package/dist-engine-src/src/catalog/snapshot.rs +1114 -0
- package/dist-engine-src/src/cel/mod.rs +1 -1
- package/dist-engine-src/src/cel/provider.rs +1 -1
- package/dist-engine-src/src/commit_graph/context.rs +328 -1015
- package/dist-engine-src/src/commit_graph/mod.rs +2 -3
- package/dist-engine-src/src/commit_graph/types.rs +7 -43
- package/dist-engine-src/src/commit_graph/walker.rs +57 -81
- package/dist-engine-src/src/commit_store/codec.rs +887 -0
- package/dist-engine-src/src/commit_store/context.rs +944 -0
- package/dist-engine-src/src/commit_store/materialization.rs +84 -0
- package/dist-engine-src/src/commit_store/mod.rs +16 -0
- package/dist-engine-src/src/commit_store/storage.rs +600 -0
- package/dist-engine-src/src/commit_store/types.rs +215 -0
- package/dist-engine-src/src/common/identity.rs +15 -5
- package/dist-engine-src/src/common/json_pointer.rs +67 -0
- package/dist-engine-src/src/common/metadata.rs +17 -12
- package/dist-engine-src/src/common/mod.rs +5 -5
- package/dist-engine-src/src/domain.rs +324 -0
- package/dist-engine-src/src/engine.rs +29 -43
- package/dist-engine-src/src/entity_identity.rs +238 -118
- package/dist-engine-src/src/functions/context.rs +17 -52
- package/dist-engine-src/src/functions/deterministic.rs +1 -1
- package/dist-engine-src/src/functions/mod.rs +1 -1
- package/dist-engine-src/src/functions/provider.rs +4 -4
- package/dist-engine-src/src/functions/state.rs +39 -66
- package/dist-engine-src/src/functions/types.rs +1 -1
- package/dist-engine-src/src/init.rs +204 -151
- package/dist-engine-src/src/json_store/context.rs +354 -60
- package/dist-engine-src/src/json_store/encoded.rs +6 -6
- package/dist-engine-src/src/json_store/mod.rs +4 -1
- package/dist-engine-src/src/json_store/store.rs +884 -11
- package/dist-engine-src/src/json_store/types.rs +166 -1
- package/dist-engine-src/src/lib.rs +11 -10
- package/dist-engine-src/src/live_state/context.rs +608 -830
- package/dist-engine-src/src/live_state/mod.rs +3 -3
- package/dist-engine-src/src/live_state/overlay.rs +7 -7
- package/dist-engine-src/src/live_state/reader.rs +5 -5
- package/dist-engine-src/src/live_state/types.rs +19 -36
- package/dist-engine-src/src/live_state/visibility.rs +19 -14
- package/dist-engine-src/src/plugin/archive.rs +3 -6
- package/dist-engine-src/src/plugin/install.rs +0 -18
- package/dist-engine-src/src/plugin/plugin_manifest.json +0 -1
- package/dist-engine-src/src/schema/annotations/defaults.rs +2 -7
- package/dist-engine-src/src/schema/builtin/lix_account.json +0 -1
- package/dist-engine-src/src/schema/builtin/lix_active_account.json +0 -1
- package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +0 -1
- package/dist-engine-src/src/schema/builtin/lix_change.json +11 -10
- package/dist-engine-src/src/schema/builtin/lix_change_author.json +0 -1
- package/dist-engine-src/src/schema/builtin/lix_commit.json +8 -46
- package/dist-engine-src/src/schema/builtin/lix_commit_edge.json +29 -22
- package/dist-engine-src/src/schema/builtin/lix_directory_descriptor.json +0 -1
- package/dist-engine-src/src/schema/builtin/lix_file_descriptor.json +0 -1
- package/dist-engine-src/src/schema/builtin/lix_key_value.json +0 -1
- package/dist-engine-src/src/schema/builtin/lix_label.json +10 -3
- package/dist-engine-src/src/schema/builtin/lix_label_assignment.json +74 -0
- package/dist-engine-src/src/schema/builtin/lix_registered_schema.json +2 -8
- package/dist-engine-src/src/schema/builtin/lix_version_descriptor.json +0 -1
- package/dist-engine-src/src/schema/builtin/lix_version_ref.json +0 -1
- package/dist-engine-src/src/schema/builtin/mod.rs +10 -59
- package/dist-engine-src/src/schema/compatibility.rs +787 -0
- package/dist-engine-src/src/schema/definition.json +47 -17
- package/dist-engine-src/src/schema/definition.rs +202 -96
- package/dist-engine-src/src/schema/key.rs +9 -77
- package/dist-engine-src/src/schema/mod.rs +4 -4
- package/dist-engine-src/src/schema/tests.rs +133 -92
- package/dist-engine-src/src/session/context.rs +86 -48
- package/dist-engine-src/src/session/create_version.rs +22 -14
- package/dist-engine-src/src/session/execute.rs +117 -23
- package/dist-engine-src/src/session/merge/apply.rs +4 -4
- package/dist-engine-src/src/session/merge/conflicts.rs +3 -2
- package/dist-engine-src/src/session/merge/stats.rs +1 -1
- package/dist-engine-src/src/session/merge/version.rs +35 -45
- package/dist-engine-src/src/session/mod.rs +9 -7
- package/dist-engine-src/src/session/optimization9_sql2_bench.rs +100 -0
- package/dist-engine-src/src/session/switch_version.rs +17 -28
- package/dist-engine-src/src/session/transaction.rs +76 -0
- package/dist-engine-src/src/sql2/change_provider.rs +14 -20
- package/dist-engine-src/src/sql2/classify.rs +75 -48
- package/dist-engine-src/src/sql2/context.rs +22 -18
- package/dist-engine-src/src/sql2/directory_history_provider.rs +28 -20
- package/dist-engine-src/src/sql2/directory_provider.rs +131 -83
- package/dist-engine-src/src/sql2/entity_history_provider.rs +10 -14
- package/dist-engine-src/src/sql2/entity_provider.rs +680 -169
- package/dist-engine-src/src/sql2/error.rs +24 -5
- package/dist-engine-src/src/sql2/execute.rs +426 -272
- package/dist-engine-src/src/sql2/file_history_provider.rs +29 -21
- package/dist-engine-src/src/sql2/file_provider.rs +533 -108
- package/dist-engine-src/src/sql2/filesystem_planner.rs +58 -94
- package/dist-engine-src/src/sql2/filesystem_visibility.rs +37 -23
- package/dist-engine-src/src/sql2/history_projection.rs +3 -27
- package/dist-engine-src/src/sql2/history_provider.rs +11 -17
- package/dist-engine-src/src/sql2/history_route.rs +22 -8
- package/dist-engine-src/src/sql2/lix_state_provider.rs +178 -96
- package/dist-engine-src/src/sql2/mod.rs +8 -4
- package/dist-engine-src/src/sql2/predicate_typecheck.rs +246 -0
- package/dist-engine-src/src/sql2/public_bind/assignment.rs +46 -0
- package/dist-engine-src/src/sql2/public_bind/capability.rs +41 -0
- package/dist-engine-src/src/sql2/public_bind/dml.rs +172 -0
- package/dist-engine-src/src/sql2/public_bind/mod.rs +26 -0
- package/dist-engine-src/src/sql2/public_bind/table.rs +168 -0
- package/dist-engine-src/src/sql2/read_only.rs +10 -12
- package/dist-engine-src/src/sql2/session.rs +7 -10
- package/dist-engine-src/src/sql2/udfs/lix_timestamp.rs +76 -0
- package/dist-engine-src/src/sql2/udfs/mod.rs +8 -1
- package/dist-engine-src/src/sql2/udfs/public_call.rs +238 -0
- package/dist-engine-src/src/sql2/version_provider.rs +46 -31
- package/dist-engine-src/src/sql2/version_scope.rs +4 -4
- package/dist-engine-src/src/storage_bench.rs +1782 -325
- package/dist-engine-src/src/test_support.rs +183 -36
- package/dist-engine-src/src/tracked_state/by_file_index.rs +20 -24
- package/dist-engine-src/src/tracked_state/codec.rs +1519 -181
- package/dist-engine-src/src/tracked_state/context.rs +1155 -271
- package/dist-engine-src/src/tracked_state/diff.rs +249 -57
- package/dist-engine-src/src/tracked_state/materialization.rs +365 -103
- package/dist-engine-src/src/tracked_state/materializer.rs +488 -0
- package/dist-engine-src/src/tracked_state/merge.rs +37 -19
- package/dist-engine-src/src/tracked_state/mod.rs +8 -7
- package/dist-engine-src/src/tracked_state/storage.rs +138 -6
- package/dist-engine-src/src/tracked_state/tree.rs +695 -252
- package/dist-engine-src/src/tracked_state/types.rs +176 -6
- package/dist-engine-src/src/transaction/commit.rs +695 -435
- package/dist-engine-src/src/transaction/context.rs +551 -310
- package/dist-engine-src/src/transaction/live_state_overlay.rs +9 -8
- package/dist-engine-src/src/transaction/mod.rs +2 -0
- package/dist-engine-src/src/transaction/normalization.rs +311 -447
- package/dist-engine-src/src/transaction/prep.rs +37 -0
- package/dist-engine-src/src/transaction/schema_resolver.rs +93 -71
- package/dist-engine-src/src/transaction/staging.rs +701 -406
- package/dist-engine-src/src/transaction/types.rs +231 -122
- package/dist-engine-src/src/transaction/validation.rs +2717 -1698
- package/dist-engine-src/src/untracked_state/codec.rs +40 -96
- package/dist-engine-src/src/untracked_state/context.rs +21 -5
- package/dist-engine-src/src/untracked_state/materialization.rs +10 -104
- package/dist-engine-src/src/untracked_state/mod.rs +3 -5
- package/dist-engine-src/src/untracked_state/storage.rs +105 -57
- package/dist-engine-src/src/untracked_state/types.rs +63 -13
- package/dist-engine-src/src/version/context.rs +1 -13
- package/dist-engine-src/src/version/lifecycle.rs +221 -0
- package/dist-engine-src/src/version/mod.rs +3 -2
- package/dist-engine-src/src/version/refs.rs +12 -103
- package/dist-engine-src/src/version/stage_rows.rs +15 -19
- package/package.json +1 -1
- package/dist-engine-src/src/changelog/codec.rs +0 -321
- package/dist-engine-src/src/changelog/context.rs +0 -92
- package/dist-engine-src/src/changelog/materialization.rs +0 -121
- package/dist-engine-src/src/changelog/mod.rs +0 -13
- package/dist-engine-src/src/changelog/reader.rs +0 -20
- package/dist-engine-src/src/changelog/storage.rs +0 -220
- package/dist-engine-src/src/changelog/types.rs +0 -38
- package/dist-engine-src/src/schema/builtin/lix_change_set.json +0 -18
- package/dist-engine-src/src/schema/builtin/lix_change_set_element.json +0 -75
- package/dist-engine-src/src/schema/builtin/lix_entity_label.json +0 -63
- package/dist-engine-src/src/schema_registry.rs +0 -294
- package/dist-engine-src/src/sql2/commit_derived_provider.rs +0 -591
- package/dist-engine-src/src/tracked_state/rebuild.rs +0 -771
- package/dist-engine-src/src/tracked_state/tree_types.rs +0 -176
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
use crate::commit_store::{Change, ChangeLocator, Commit, CommitStoreContext};
|
|
2
|
+
use crate::storage::StorageReader;
|
|
3
|
+
use crate::tracked_state::context::{TrackedStateMaterializer, TrackedStateWriteReport};
|
|
4
|
+
use crate::tracked_state::types::TrackedStateKey;
|
|
5
|
+
use crate::tracked_state::TrackedStateDeltaRef;
|
|
6
|
+
use crate::LixError;
|
|
7
|
+
use std::collections::{BTreeMap, BTreeSet};
|
|
8
|
+
|
|
9
|
+
/// Owned materialization delta used only by explicit projection-root hydration.
|
|
10
|
+
///
|
|
11
|
+
/// Normal transaction commits already have borrowed `ChangeRef` and
|
|
12
|
+
/// `ChangeLocatorRef` values available while staging commit_store.
|
|
13
|
+
/// Materialization loads those facts back from storage, so it owns the decoded
|
|
14
|
+
/// data internally and immediately passes a borrowed view into the same
|
|
15
|
+
/// tracked-state root writer.
|
|
16
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
17
|
+
pub(crate) struct MaterializationDelta {
|
|
18
|
+
pub(crate) change: Change,
|
|
19
|
+
pub(crate) locator: ChangeLocator,
|
|
20
|
+
pub(crate) created_at: String,
|
|
21
|
+
pub(crate) updated_at: String,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
impl MaterializationDelta {
|
|
25
|
+
pub(crate) fn as_ref(&self) -> TrackedStateDeltaRef<'_> {
|
|
26
|
+
TrackedStateDeltaRef {
|
|
27
|
+
change: self.change.as_ref(),
|
|
28
|
+
locator: self.locator.as_ref(),
|
|
29
|
+
created_at: &self.created_at,
|
|
30
|
+
updated_at: &self.updated_at,
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
36
|
+
pub(crate) struct MaterializationInput {
|
|
37
|
+
pub(crate) commit_id: String,
|
|
38
|
+
pub(crate) parent_commit_id: Option<String>,
|
|
39
|
+
pub(crate) deltas: Vec<MaterializationDelta>,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
struct LocatedChange {
|
|
43
|
+
locator: ChangeLocator,
|
|
44
|
+
change: Change,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// Explicit projection-root materialization over commit_store.
|
|
48
|
+
///
|
|
49
|
+
/// Normal transaction commits must use `TrackedStateWriter::stage_delta` with
|
|
50
|
+
/// already prepared commit_store refs. This path exists for deliberate
|
|
51
|
+
/// materialization only.
|
|
52
|
+
pub(crate) async fn materialize_root_at<S>(
|
|
53
|
+
materializer: &mut TrackedStateMaterializer<'_, S>,
|
|
54
|
+
commit_id: &str,
|
|
55
|
+
) -> Result<TrackedStateWriteReport, LixError>
|
|
56
|
+
where
|
|
57
|
+
S: StorageReader + ?Sized,
|
|
58
|
+
{
|
|
59
|
+
let input =
|
|
60
|
+
build_materialization_input(materializer.store, materializer.commit_store, commit_id)
|
|
61
|
+
.await?;
|
|
62
|
+
let delta_refs = input
|
|
63
|
+
.deltas
|
|
64
|
+
.iter()
|
|
65
|
+
.map(MaterializationDelta::as_ref)
|
|
66
|
+
.collect::<Vec<_>>();
|
|
67
|
+
materializer
|
|
68
|
+
.tracked_state
|
|
69
|
+
.writer(materializer.store, materializer.writes)
|
|
70
|
+
.stage_projection_root(
|
|
71
|
+
&input.commit_id,
|
|
72
|
+
input.parent_commit_id.as_deref(),
|
|
73
|
+
delta_refs,
|
|
74
|
+
)
|
|
75
|
+
.await
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async fn build_materialization_input<S>(
|
|
79
|
+
store: &mut S,
|
|
80
|
+
commit_store: &CommitStoreContext,
|
|
81
|
+
commit_id: &str,
|
|
82
|
+
) -> Result<MaterializationInput, LixError>
|
|
83
|
+
where
|
|
84
|
+
S: StorageReader + ?Sized,
|
|
85
|
+
{
|
|
86
|
+
let lineage = load_first_parent_lineage(store, commit_store, commit_id).await?;
|
|
87
|
+
let mut located_changes = Vec::new();
|
|
88
|
+
for commit in lineage {
|
|
89
|
+
located_changes
|
|
90
|
+
.append(&mut load_commit_located_changes(store, commit_store, &commit).await?);
|
|
91
|
+
}
|
|
92
|
+
let deltas = project_materialization_deltas(located_changes);
|
|
93
|
+
|
|
94
|
+
Ok(MaterializationInput {
|
|
95
|
+
commit_id: commit_id.to_string(),
|
|
96
|
+
parent_commit_id: None,
|
|
97
|
+
deltas,
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async fn load_first_parent_lineage<S>(
|
|
102
|
+
store: &mut S,
|
|
103
|
+
commit_store: &CommitStoreContext,
|
|
104
|
+
commit_id: &str,
|
|
105
|
+
) -> Result<Vec<Commit>, LixError>
|
|
106
|
+
where
|
|
107
|
+
S: StorageReader + ?Sized,
|
|
108
|
+
{
|
|
109
|
+
let mut lineage = Vec::new();
|
|
110
|
+
let mut seen = BTreeSet::new();
|
|
111
|
+
let mut current = Some(commit_id.to_string());
|
|
112
|
+
while let Some(current_id) = current {
|
|
113
|
+
if !seen.insert(current_id.clone()) {
|
|
114
|
+
return Err(LixError::new(
|
|
115
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
116
|
+
format!(
|
|
117
|
+
"tracked_state materialization found first-parent cycle at commit '{current_id}'"
|
|
118
|
+
),
|
|
119
|
+
));
|
|
120
|
+
}
|
|
121
|
+
let commit = commit_store
|
|
122
|
+
.load_commit_from(store, ¤t_id)
|
|
123
|
+
.await?
|
|
124
|
+
.ok_or_else(|| missing_commit_error(¤t_id))?;
|
|
125
|
+
current = commit.parent_ids.first().cloned();
|
|
126
|
+
lineage.push(commit);
|
|
127
|
+
}
|
|
128
|
+
lineage.reverse();
|
|
129
|
+
Ok(lineage)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async fn load_commit_located_changes<S>(
|
|
133
|
+
store: &mut S,
|
|
134
|
+
commit_store: &CommitStoreContext,
|
|
135
|
+
commit: &Commit,
|
|
136
|
+
) -> Result<Vec<LocatedChange>, LixError>
|
|
137
|
+
where
|
|
138
|
+
S: StorageReader + ?Sized,
|
|
139
|
+
{
|
|
140
|
+
let mut located_changes = Vec::new();
|
|
141
|
+
for pack_id in 0..commit.change_pack_count {
|
|
142
|
+
let changes = commit_store
|
|
143
|
+
.load_change_pack_from(store, &commit.id, pack_id)
|
|
144
|
+
.await?
|
|
145
|
+
.ok_or_else(|| missing_pack_error("change", &commit.id, pack_id))?;
|
|
146
|
+
for (source_ordinal, change) in changes.into_iter().enumerate() {
|
|
147
|
+
let locator = ChangeLocator {
|
|
148
|
+
source_commit_id: commit.id.clone(),
|
|
149
|
+
source_pack_id: pack_id,
|
|
150
|
+
source_ordinal: u32::try_from(source_ordinal).map_err(|_| {
|
|
151
|
+
LixError::new(
|
|
152
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
153
|
+
"tracked_state materialization change pack ordinal exceeds u32",
|
|
154
|
+
)
|
|
155
|
+
})?,
|
|
156
|
+
change_id: change.id.clone(),
|
|
157
|
+
};
|
|
158
|
+
located_changes.push(LocatedChange { locator, change });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
let mut adopted_locators = Vec::new();
|
|
163
|
+
for pack_id in 0..commit.membership_pack_count {
|
|
164
|
+
let mut locators = commit_store
|
|
165
|
+
.load_membership_pack_from(store, &commit.id, pack_id)
|
|
166
|
+
.await?
|
|
167
|
+
.ok_or_else(|| missing_pack_error("membership", &commit.id, pack_id))?;
|
|
168
|
+
adopted_locators.append(&mut locators);
|
|
169
|
+
}
|
|
170
|
+
let adopted_changes = load_changes_by_locators(store, commit_store, &adopted_locators).await?;
|
|
171
|
+
located_changes.extend(
|
|
172
|
+
adopted_locators
|
|
173
|
+
.into_iter()
|
|
174
|
+
.zip(adopted_changes)
|
|
175
|
+
.map(|(locator, change)| LocatedChange { locator, change }),
|
|
176
|
+
);
|
|
177
|
+
Ok(located_changes)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
fn project_materialization_deltas(
|
|
181
|
+
changes: impl IntoIterator<Item = LocatedChange>,
|
|
182
|
+
) -> Vec<MaterializationDelta> {
|
|
183
|
+
let mut projected = BTreeMap::<TrackedStateKey, MaterializationDelta>::new();
|
|
184
|
+
for LocatedChange { locator, change } in changes {
|
|
185
|
+
let key = TrackedStateKey {
|
|
186
|
+
schema_key: change.schema_key.clone(),
|
|
187
|
+
file_id: change.file_id.clone(),
|
|
188
|
+
entity_id: change.entity_id.clone(),
|
|
189
|
+
};
|
|
190
|
+
let created_at = projected
|
|
191
|
+
.get(&key)
|
|
192
|
+
.map(|delta| delta.created_at.clone())
|
|
193
|
+
.unwrap_or_else(|| change.created_at.clone());
|
|
194
|
+
let updated_at = change.created_at.clone();
|
|
195
|
+
projected.insert(
|
|
196
|
+
key,
|
|
197
|
+
MaterializationDelta {
|
|
198
|
+
change,
|
|
199
|
+
locator,
|
|
200
|
+
created_at,
|
|
201
|
+
updated_at,
|
|
202
|
+
},
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
projected.into_values().collect()
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async fn load_changes_by_locators(
|
|
209
|
+
store: &mut (impl StorageReader + ?Sized),
|
|
210
|
+
commit_store: &CommitStoreContext,
|
|
211
|
+
locators: &[ChangeLocator],
|
|
212
|
+
) -> Result<Vec<Change>, LixError> {
|
|
213
|
+
let mut packs = BTreeMap::<(String, u32), Vec<Change>>::new();
|
|
214
|
+
for locator in locators {
|
|
215
|
+
let key = (locator.source_commit_id.clone(), locator.source_pack_id);
|
|
216
|
+
if packs.contains_key(&key) {
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
let changes = commit_store
|
|
220
|
+
.load_change_pack_from(store, &locator.source_commit_id, locator.source_pack_id)
|
|
221
|
+
.await?
|
|
222
|
+
.ok_or_else(|| {
|
|
223
|
+
missing_pack_error("change", &locator.source_commit_id, locator.source_pack_id)
|
|
224
|
+
})?;
|
|
225
|
+
packs.insert(key, changes);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
locators
|
|
229
|
+
.iter()
|
|
230
|
+
.map(|locator| change_from_loaded_packs(&packs, locator))
|
|
231
|
+
.collect()
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
fn change_from_loaded_packs(
|
|
235
|
+
packs: &BTreeMap<(String, u32), Vec<Change>>,
|
|
236
|
+
locator: &ChangeLocator,
|
|
237
|
+
) -> Result<Change, LixError> {
|
|
238
|
+
let key = (locator.source_commit_id.clone(), locator.source_pack_id);
|
|
239
|
+
let changes = packs.get(&key).ok_or_else(|| {
|
|
240
|
+
LixError::new(
|
|
241
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
242
|
+
format!(
|
|
243
|
+
"tracked_state materialization lost loaded change pack ({}, {})",
|
|
244
|
+
locator.source_commit_id, locator.source_pack_id
|
|
245
|
+
),
|
|
246
|
+
)
|
|
247
|
+
})?;
|
|
248
|
+
let change = changes
|
|
249
|
+
.get(usize::try_from(locator.source_ordinal).map_err(|_| {
|
|
250
|
+
LixError::new(
|
|
251
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
252
|
+
"tracked_state materialization locator ordinal does not fit usize",
|
|
253
|
+
)
|
|
254
|
+
})?)
|
|
255
|
+
.ok_or_else(|| {
|
|
256
|
+
LixError::new(
|
|
257
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
258
|
+
format!(
|
|
259
|
+
"tracked_state materialization locator for '{}' points past pack ({}, {})",
|
|
260
|
+
locator.change_id, locator.source_commit_id, locator.source_pack_id
|
|
261
|
+
),
|
|
262
|
+
)
|
|
263
|
+
})?;
|
|
264
|
+
if change.id != locator.change_id {
|
|
265
|
+
return Err(LixError::new(
|
|
266
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
267
|
+
format!(
|
|
268
|
+
"tracked_state materialization locator expected '{}' but found '{}'",
|
|
269
|
+
locator.change_id, change.id
|
|
270
|
+
),
|
|
271
|
+
));
|
|
272
|
+
}
|
|
273
|
+
Ok(change.clone())
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
fn missing_pack_error(label: &str, commit_id: &str, pack_id: u32) -> LixError {
|
|
277
|
+
LixError::new(
|
|
278
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
279
|
+
format!("tracked_state materialization missing {label} pack ({commit_id}, {pack_id})"),
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
fn missing_commit_error(commit_id: &str) -> LixError {
|
|
284
|
+
LixError::new(
|
|
285
|
+
LixError::CODE_INTERNAL_ERROR,
|
|
286
|
+
format!("tracked_state materialization missing commit '{commit_id}'"),
|
|
287
|
+
)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
#[cfg(test)]
|
|
291
|
+
mod tests {
|
|
292
|
+
use super::*;
|
|
293
|
+
use crate::commit_store::ChangeLocator;
|
|
294
|
+
use crate::entity_identity::EntityIdentity;
|
|
295
|
+
|
|
296
|
+
#[test]
|
|
297
|
+
fn materialization_delta_ref_borrows_owned_facts() {
|
|
298
|
+
let delta = MaterializationDelta {
|
|
299
|
+
change: Change {
|
|
300
|
+
id: "change-1".to_string(),
|
|
301
|
+
entity_id: EntityIdentity::single("entity-1"),
|
|
302
|
+
schema_key: "schema".to_string(),
|
|
303
|
+
file_id: Some("file".to_string()),
|
|
304
|
+
snapshot_ref: None,
|
|
305
|
+
metadata_ref: None,
|
|
306
|
+
created_at: "2026-01-01T00:00:00Z".to_string(),
|
|
307
|
+
},
|
|
308
|
+
locator: ChangeLocator {
|
|
309
|
+
source_commit_id: "commit-1".to_string(),
|
|
310
|
+
source_pack_id: 7,
|
|
311
|
+
source_ordinal: 11,
|
|
312
|
+
change_id: "change-1".to_string(),
|
|
313
|
+
},
|
|
314
|
+
created_at: "2026-01-01T00:00:00Z".to_string(),
|
|
315
|
+
updated_at: "2026-02-01T00:00:00Z".to_string(),
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
let delta_ref = delta.as_ref();
|
|
319
|
+
|
|
320
|
+
assert_eq!(delta_ref.change.id, "change-1");
|
|
321
|
+
assert_eq!(delta_ref.change.schema_key, "schema");
|
|
322
|
+
assert_eq!(delta_ref.change.file_id, Some("file"));
|
|
323
|
+
assert_eq!(delta_ref.locator.source_commit_id, "commit-1");
|
|
324
|
+
assert_eq!(delta_ref.locator.source_pack_id, 7);
|
|
325
|
+
assert_eq!(delta_ref.locator.source_ordinal, 11);
|
|
326
|
+
assert_eq!(delta_ref.created_at, "2026-01-01T00:00:00Z");
|
|
327
|
+
assert_eq!(delta_ref.updated_at, "2026-02-01T00:00:00Z");
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
#[test]
|
|
331
|
+
fn change_from_loaded_packs_resolves_locator_by_pack_and_ordinal() {
|
|
332
|
+
let mut packs = BTreeMap::new();
|
|
333
|
+
packs.insert(
|
|
334
|
+
("source-commit".to_string(), 3),
|
|
335
|
+
vec![change("change-0"), change("change-1"), change("change-2")],
|
|
336
|
+
);
|
|
337
|
+
let locator = ChangeLocator {
|
|
338
|
+
source_commit_id: "source-commit".to_string(),
|
|
339
|
+
source_pack_id: 3,
|
|
340
|
+
source_ordinal: 1,
|
|
341
|
+
change_id: "change-1".to_string(),
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
let resolved = change_from_loaded_packs(&packs, &locator).expect("locator should resolve");
|
|
345
|
+
|
|
346
|
+
assert_eq!(resolved.id, "change-1");
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
#[test]
|
|
350
|
+
fn change_from_loaded_packs_rejects_locator_change_id_mismatch() {
|
|
351
|
+
let mut packs = BTreeMap::new();
|
|
352
|
+
packs.insert(("source-commit".to_string(), 3), vec![change("actual")]);
|
|
353
|
+
let locator = ChangeLocator {
|
|
354
|
+
source_commit_id: "source-commit".to_string(),
|
|
355
|
+
source_pack_id: 3,
|
|
356
|
+
source_ordinal: 0,
|
|
357
|
+
change_id: "expected".to_string(),
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
let error =
|
|
361
|
+
change_from_loaded_packs(&packs, &locator).expect_err("mismatched locator should fail");
|
|
362
|
+
|
|
363
|
+
assert!(error.message.contains("expected"));
|
|
364
|
+
assert!(error.message.contains("actual"));
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
#[test]
|
|
368
|
+
fn project_materialization_deltas_keeps_first_seen_created_at_and_latest_updated_at() {
|
|
369
|
+
let deltas = project_materialization_deltas(vec![
|
|
370
|
+
located_change(
|
|
371
|
+
"commit-1",
|
|
372
|
+
0,
|
|
373
|
+
"change-create",
|
|
374
|
+
"entity-1",
|
|
375
|
+
"2026-01-01T00:00:00Z",
|
|
376
|
+
),
|
|
377
|
+
located_change(
|
|
378
|
+
"commit-2",
|
|
379
|
+
0,
|
|
380
|
+
"change-update",
|
|
381
|
+
"entity-1",
|
|
382
|
+
"2026-02-01T00:00:00Z",
|
|
383
|
+
),
|
|
384
|
+
]);
|
|
385
|
+
|
|
386
|
+
assert_eq!(deltas.len(), 1);
|
|
387
|
+
let delta = &deltas[0];
|
|
388
|
+
assert_eq!(delta.change.id, "change-update");
|
|
389
|
+
assert_eq!(delta.locator.source_commit_id, "commit-2");
|
|
390
|
+
assert_eq!(delta.created_at, "2026-01-01T00:00:00Z");
|
|
391
|
+
assert_eq!(delta.updated_at, "2026-02-01T00:00:00Z");
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
#[test]
|
|
395
|
+
fn project_materialization_deltas_uses_adopted_change_time_not_target_commit_time() {
|
|
396
|
+
let deltas = project_materialization_deltas(vec![located_change(
|
|
397
|
+
"source-commit",
|
|
398
|
+
0,
|
|
399
|
+
"adopted-change",
|
|
400
|
+
"entity-1",
|
|
401
|
+
"2026-01-01T00:00:00Z",
|
|
402
|
+
)]);
|
|
403
|
+
|
|
404
|
+
assert_eq!(deltas.len(), 1);
|
|
405
|
+
assert_eq!(deltas[0].created_at, "2026-01-01T00:00:00Z");
|
|
406
|
+
assert_eq!(deltas[0].updated_at, "2026-01-01T00:00:00Z");
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
#[test]
|
|
410
|
+
fn project_materialization_deltas_tracks_entities_independently() {
|
|
411
|
+
let deltas = project_materialization_deltas(vec![
|
|
412
|
+
located_change(
|
|
413
|
+
"commit-1",
|
|
414
|
+
0,
|
|
415
|
+
"entity-a-create",
|
|
416
|
+
"entity-a",
|
|
417
|
+
"2026-01-01T00:00:00Z",
|
|
418
|
+
),
|
|
419
|
+
located_change(
|
|
420
|
+
"commit-1",
|
|
421
|
+
1,
|
|
422
|
+
"entity-b-create",
|
|
423
|
+
"entity-b",
|
|
424
|
+
"2026-01-02T00:00:00Z",
|
|
425
|
+
),
|
|
426
|
+
located_change(
|
|
427
|
+
"commit-2",
|
|
428
|
+
0,
|
|
429
|
+
"entity-a-update",
|
|
430
|
+
"entity-a",
|
|
431
|
+
"2026-02-01T00:00:00Z",
|
|
432
|
+
),
|
|
433
|
+
]);
|
|
434
|
+
|
|
435
|
+
let entity_a = deltas
|
|
436
|
+
.iter()
|
|
437
|
+
.find(|delta| delta.change.entity_id == EntityIdentity::single("entity-a"))
|
|
438
|
+
.expect("entity-a delta");
|
|
439
|
+
let entity_b = deltas
|
|
440
|
+
.iter()
|
|
441
|
+
.find(|delta| delta.change.entity_id == EntityIdentity::single("entity-b"))
|
|
442
|
+
.expect("entity-b delta");
|
|
443
|
+
assert_eq!(entity_a.change.id, "entity-a-update");
|
|
444
|
+
assert_eq!(entity_a.created_at, "2026-01-01T00:00:00Z");
|
|
445
|
+
assert_eq!(entity_a.updated_at, "2026-02-01T00:00:00Z");
|
|
446
|
+
assert_eq!(entity_b.change.id, "entity-b-create");
|
|
447
|
+
assert_eq!(entity_b.created_at, "2026-01-02T00:00:00Z");
|
|
448
|
+
assert_eq!(entity_b.updated_at, "2026-01-02T00:00:00Z");
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
fn change(id: &str) -> Change {
|
|
452
|
+
Change {
|
|
453
|
+
id: id.to_string(),
|
|
454
|
+
entity_id: EntityIdentity::single("entity-1"),
|
|
455
|
+
schema_key: "schema".to_string(),
|
|
456
|
+
file_id: Some("file".to_string()),
|
|
457
|
+
snapshot_ref: None,
|
|
458
|
+
metadata_ref: None,
|
|
459
|
+
created_at: "2026-01-01T00:00:00Z".to_string(),
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
fn located_change(
|
|
464
|
+
commit_id: &str,
|
|
465
|
+
source_ordinal: u32,
|
|
466
|
+
change_id: &str,
|
|
467
|
+
entity_id: &str,
|
|
468
|
+
created_at: &str,
|
|
469
|
+
) -> LocatedChange {
|
|
470
|
+
LocatedChange {
|
|
471
|
+
locator: ChangeLocator {
|
|
472
|
+
source_commit_id: commit_id.to_string(),
|
|
473
|
+
source_pack_id: 0,
|
|
474
|
+
source_ordinal,
|
|
475
|
+
change_id: change_id.to_string(),
|
|
476
|
+
},
|
|
477
|
+
change: Change {
|
|
478
|
+
id: change_id.to_string(),
|
|
479
|
+
entity_id: EntityIdentity::single(entity_id),
|
|
480
|
+
schema_key: "schema".to_string(),
|
|
481
|
+
file_id: Some("file".to_string()),
|
|
482
|
+
snapshot_ref: None,
|
|
483
|
+
metadata_ref: None,
|
|
484
|
+
created_at: created_at.to_string(),
|
|
485
|
+
},
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
use std::collections::{BTreeMap, BTreeSet};
|
|
2
2
|
|
|
3
3
|
use crate::tracked_state::{
|
|
4
|
-
TrackedStateDiff, TrackedStateDiffEntry, TrackedStateDiffIdentity,
|
|
4
|
+
MaterializedTrackedStateRow, TrackedStateDiff, TrackedStateDiffEntry, TrackedStateDiffIdentity,
|
|
5
5
|
};
|
|
6
6
|
use crate::LixError;
|
|
7
7
|
|
|
@@ -34,7 +34,7 @@ pub(crate) enum TrackedStateMergePatch {
|
|
|
34
34
|
Adopt {
|
|
35
35
|
identity: TrackedStateDiffIdentity,
|
|
36
36
|
change_id: String,
|
|
37
|
-
projected_row:
|
|
37
|
+
projected_row: MaterializedTrackedStateRow,
|
|
38
38
|
},
|
|
39
39
|
}
|
|
40
40
|
|
|
@@ -52,7 +52,7 @@ impl TrackedStateMergePatch {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
pub(crate) fn projected_row(&self) -> &
|
|
55
|
+
pub(crate) fn projected_row(&self) -> &MaterializedTrackedStateRow {
|
|
56
56
|
match self {
|
|
57
57
|
Self::Adopt { projected_row, .. } => projected_row,
|
|
58
58
|
}
|
|
@@ -128,7 +128,7 @@ fn diff_by_identity(
|
|
|
128
128
|
format!(
|
|
129
129
|
"tracked-state merge received duplicate diff entry for schema '{}' entity '{}'",
|
|
130
130
|
entry.identity.schema_key,
|
|
131
|
-
entry.identity.entity_id.
|
|
131
|
+
entry.identity.entity_id.as_json_array_text()?
|
|
132
132
|
),
|
|
133
133
|
));
|
|
134
134
|
}
|
|
@@ -146,7 +146,7 @@ fn adopt_source_change_patch(
|
|
|
146
146
|
format!(
|
|
147
147
|
"tracked-state merge cannot apply source removal for schema '{}' entity '{}' without a tombstone row",
|
|
148
148
|
entry.identity.schema_key,
|
|
149
|
-
entry.identity.entity_id.
|
|
149
|
+
entry.identity.entity_id.as_json_array_text()?
|
|
150
150
|
),
|
|
151
151
|
));
|
|
152
152
|
};
|
|
@@ -168,14 +168,15 @@ fn same_final_state(target: &TrackedStateDiffEntry, source: &TrackedStateDiffEnt
|
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
-
fn row_is_live(row: &
|
|
171
|
+
fn row_is_live(row: &MaterializedTrackedStateRow) -> bool {
|
|
172
172
|
row.snapshot_content.is_some()
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
fn tracked_row_payload_eq(
|
|
176
|
-
left
|
|
177
|
-
|
|
178
|
-
|
|
175
|
+
fn tracked_row_payload_eq(
|
|
176
|
+
left: &MaterializedTrackedStateRow,
|
|
177
|
+
right: &MaterializedTrackedStateRow,
|
|
178
|
+
) -> bool {
|
|
179
|
+
left.snapshot_content == right.snapshot_content && left.metadata == right.metadata
|
|
179
180
|
}
|
|
180
181
|
|
|
181
182
|
#[cfg(test)]
|
|
@@ -418,8 +419,8 @@ mod tests {
|
|
|
418
419
|
fn entry(
|
|
419
420
|
entity_id: &str,
|
|
420
421
|
kind: TrackedStateDiffKind,
|
|
421
|
-
before: Option<
|
|
422
|
-
after: Option<
|
|
422
|
+
before: Option<MaterializedTrackedStateRow>,
|
|
423
|
+
after: Option<MaterializedTrackedStateRow>,
|
|
423
424
|
) -> TrackedStateDiffEntry {
|
|
424
425
|
TrackedStateDiffEntry {
|
|
425
426
|
identity: TrackedStateDiffIdentity {
|
|
@@ -436,35 +437,52 @@ mod tests {
|
|
|
436
437
|
fn patch_ids(plan: &TrackedStateMergePlan) -> Vec<String> {
|
|
437
438
|
plan.patches
|
|
438
439
|
.iter()
|
|
439
|
-
.map(|entry|
|
|
440
|
+
.map(|entry| {
|
|
441
|
+
entry
|
|
442
|
+
.identity()
|
|
443
|
+
.entity_id
|
|
444
|
+
.as_single_string_owned()
|
|
445
|
+
.expect("identity")
|
|
446
|
+
})
|
|
440
447
|
.collect()
|
|
441
448
|
}
|
|
442
449
|
|
|
443
450
|
fn conflict_ids(plan: &TrackedStateMergePlan) -> Vec<String> {
|
|
444
451
|
plan.conflicts
|
|
445
452
|
.iter()
|
|
446
|
-
.map(|entry|
|
|
453
|
+
.map(|entry| {
|
|
454
|
+
entry
|
|
455
|
+
.identity
|
|
456
|
+
.entity_id
|
|
457
|
+
.as_single_string_owned()
|
|
458
|
+
.expect("identity")
|
|
459
|
+
})
|
|
447
460
|
.collect()
|
|
448
461
|
}
|
|
449
462
|
|
|
450
|
-
fn tombstone(entity_id: &str, change_id: &str) ->
|
|
463
|
+
fn tombstone(entity_id: &str, change_id: &str) -> MaterializedTrackedStateRow {
|
|
451
464
|
let mut row = row(entity_id, change_id);
|
|
452
465
|
row.snapshot_content = None;
|
|
466
|
+
row.deleted = true;
|
|
453
467
|
row
|
|
454
468
|
}
|
|
455
469
|
|
|
456
|
-
fn row(entity_id: &str, change_id: &str) ->
|
|
470
|
+
fn row(entity_id: &str, change_id: &str) -> MaterializedTrackedStateRow {
|
|
457
471
|
row_with_value(entity_id, change_id, "value")
|
|
458
472
|
}
|
|
459
473
|
|
|
460
|
-
fn row_with_value(
|
|
461
|
-
|
|
474
|
+
fn row_with_value(
|
|
475
|
+
entity_id: &str,
|
|
476
|
+
change_id: &str,
|
|
477
|
+
value: &str,
|
|
478
|
+
) -> MaterializedTrackedStateRow {
|
|
479
|
+
MaterializedTrackedStateRow {
|
|
462
480
|
entity_id: EntityIdentity::single(entity_id),
|
|
463
481
|
schema_key: "test_schema".to_string(),
|
|
464
482
|
file_id: None,
|
|
465
483
|
snapshot_content: Some(format!("{{\"value\":\"{value}\"}}")),
|
|
466
484
|
metadata: None,
|
|
467
|
-
|
|
485
|
+
deleted: false,
|
|
468
486
|
created_at: "2026-01-01T00:00:00Z".to_string(),
|
|
469
487
|
updated_at: "2026-01-01T00:00:00Z".to_string(),
|
|
470
488
|
change_id: change_id.to_string(),
|
|
@@ -3,29 +3,30 @@ mod codec;
|
|
|
3
3
|
mod context;
|
|
4
4
|
mod diff;
|
|
5
5
|
mod materialization;
|
|
6
|
+
mod materializer;
|
|
6
7
|
mod merge;
|
|
7
|
-
pub(crate) mod rebuild;
|
|
8
8
|
mod storage;
|
|
9
9
|
mod tree;
|
|
10
|
-
mod tree_types;
|
|
11
10
|
mod types;
|
|
12
11
|
|
|
13
12
|
#[allow(unused_imports)]
|
|
14
|
-
pub(crate) use context::{
|
|
13
|
+
pub(crate) use context::{
|
|
14
|
+
TrackedStateContext, TrackedStateMaterializer, TrackedStateStoreReader, TrackedStateWriter,
|
|
15
|
+
};
|
|
15
16
|
#[allow(unused_imports)]
|
|
16
17
|
pub(crate) use diff::{
|
|
17
18
|
TrackedStateDiff, TrackedStateDiffEntry, TrackedStateDiffIdentity, TrackedStateDiffKind,
|
|
18
19
|
TrackedStateDiffRequest,
|
|
19
20
|
};
|
|
20
|
-
pub(crate) use materialization::{
|
|
21
|
-
canonicalize_materialized_row, materialize_value, TrackedMaterializationProjection,
|
|
22
|
-
};
|
|
21
|
+
pub(crate) use materialization::{materialize_index_entries, TrackedMaterializationProjection};
|
|
23
22
|
#[allow(unused_imports)]
|
|
24
23
|
pub(crate) use merge::{
|
|
25
24
|
plan_merge, TrackedStateMergeConflict, TrackedStateMergePatch, TrackedStateMergePlan,
|
|
26
25
|
};
|
|
26
|
+
pub(crate) use storage::{load_delta_pack, DeltaJsonPackIndexesRef};
|
|
27
27
|
#[allow(unused_imports)]
|
|
28
28
|
pub(crate) use types::{
|
|
29
|
-
|
|
29
|
+
MaterializedTrackedStateRow, TrackedStateDeltaRef, TrackedStateFilter,
|
|
30
|
+
TrackedStateIndexValueRef, TrackedStateKeyRef, TrackedStateProjection, TrackedStateRowRequest,
|
|
30
31
|
TrackedStateScanRequest,
|
|
31
32
|
};
|