sim-expr-tree-core 0.1.0

Finite namespace, policy, and storage records for SIM expression trees.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use std::collections::BTreeMap;

use sim_table_core::TablePath;

use crate::{CellId, DirId, EffectiveCodecPolicy};

/// Backend family named by a mounted expression-tree store descriptor.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum BackendKind {
    /// Process-local memory backend.
    Memory,
    /// Filesystem-backed Table/Dir backend.
    Filesystem,
    /// Database-backed Table/Dir backend.
    Database,
    /// Read-only backend wrapper.
    ReadOnly,
    /// Already-composed mounted namespace backend.
    MountedNamespace,
}

/// Monotonic observation of a mounted backend generation.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MountEpoch(u64);

impl MountEpoch {
    /// Create an epoch from a backend supplied generation.
    pub fn new(value: u64) -> Self {
        Self(value)
    }

    /// Borrow the raw generation value.
    pub fn value(self) -> u64 {
        self.0
    }

    /// Return the next observed generation.
    pub fn next_after(self) -> Self {
        Self(self.0.saturating_add(1))
    }
}

/// Mounted target shape. Table mounts are leaves; Dir mounts can be traversed.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MountResource {
    /// A mounted Table leaf.
    Table,
    /// A mounted Dir subtree.
    Dir,
}

/// Explicit mount descriptor stored outside authored source cells.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MountDescriptor {
    path: TablePath,
    resource: MountResource,
    backend: BackendKind,
    epoch: MountEpoch,
}

impl MountDescriptor {
    /// Create an explicit Table mount descriptor.
    pub fn table(path: TablePath, backend: BackendKind, epoch: MountEpoch) -> Self {
        Self {
            path,
            resource: MountResource::Table,
            backend,
            epoch,
        }
    }

    /// Create an explicit Dir mount descriptor.
    pub fn dir(path: TablePath, backend: BackendKind, epoch: MountEpoch) -> Self {
        Self {
            path,
            resource: MountResource::Dir,
            backend,
            epoch,
        }
    }

    /// Absolute mount path.
    pub fn path(&self) -> &TablePath {
        &self.path
    }

    /// Mounted target shape.
    pub fn resource(&self) -> MountResource {
        self.resource
    }

    /// Mounted backend family.
    pub fn backend(&self) -> BackendKind {
        self.backend
    }

    /// Last observed backend epoch.
    pub fn epoch(&self) -> MountEpoch {
        self.epoch
    }

    fn set_epoch(&mut self, epoch: MountEpoch) {
        self.epoch = epoch;
    }
}

/// Authored source-store entry. Operational and derived values have no variants here.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SourceEntry {
    expr: String,
    codec: Option<String>,
}

impl SourceEntry {
    /// Create an authored expression entry.
    pub fn new(expr: impl Into<String>) -> Self {
        Self {
            expr: expr.into(),
            codec: None,
        }
    }

    /// Attach the source codec used to parse the authored expression.
    pub fn with_codec(mut self, codec: impl Into<String>) -> Self {
        self.codec = Some(codec.into());
        self
    }

    /// Authored expression text.
    pub fn expr(&self) -> &str {
        &self.expr
    }

    /// Optional source codec.
    pub fn codec(&self) -> Option<&str> {
        self.codec.as_deref()
    }
}

/// Control-store entry for operational state.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ControlEntry {
    /// Durable generated-name or scheduler counter.
    Counter(u64),
    /// Effective policy snapshot.
    Policy(EffectiveCodecPolicy),
    /// UI preference kept out of authored source.
    UiPreference(String),
    /// Last observed mount backend epoch.
    MountEpoch(MountEpoch),
}

/// Derived-store entry for rebuildable calculation artifacts.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DerivedEntry {
    /// Dependency graph materialization.
    Graph(String),
    /// Cached value for a calculated cell.
    CachedValue(String),
    /// Calculation receipt or scheduler evidence.
    Receipt(String),
}

/// Recoverable source/control commit staged across separate backends.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PendingCommit {
    source_writes: BTreeMap<CellId, SourceEntry>,
    control_writes: BTreeMap<String, ControlEntry>,
    phase: CommitPhase,
}

impl PendingCommit {
    fn new(
        source_writes: BTreeMap<CellId, SourceEntry>,
        control_writes: BTreeMap<String, ControlEntry>,
    ) -> Self {
        Self {
            source_writes,
            control_writes,
            phase: CommitPhase::Prepared,
        }
    }

    /// Whether the source side of the transaction boundary was persisted.
    pub fn source_committed(&self) -> bool {
        self.phase >= CommitPhase::SourceCommitted
    }

    /// Whether the control side of the transaction boundary was persisted.
    pub fn control_committed(&self) -> bool {
        self.phase >= CommitPhase::ControlCommitted
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum CommitPhase {
    Prepared,
    SourceCommitted,
    ControlCommitted,
}

/// Store composition failures.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum StoreError {
    /// A composed expression tree must have a root Dir.
    MissingRootDir,
    /// A mount path cannot be root and must not duplicate or conflict with existing mounts.
    InvalidMount(String),
    /// A table mount was treated as a directory.
    TableMountIsLeaf(TablePath),
    /// A persisted mount descriptor is corrupt.
    CorruptMount(String),
}

/// Typed source/control/derived stores plus explicit Table/Dir mount descriptors.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ExprTreeStores {
    root_dir: DirId,
    source: BTreeMap<CellId, SourceEntry>,
    control: BTreeMap<String, ControlEntry>,
    derived: BTreeMap<CellId, DerivedEntry>,
    mounts: BTreeMap<String, MountDescriptor>,
}

impl ExprTreeStores {
    /// Compose a tree over an existing root directory.
    pub fn new(root_dir: DirId) -> Result<Self, StoreError> {
        if root_dir.as_str().is_empty() {
            return Err(StoreError::MissingRootDir);
        }
        Ok(Self {
            root_dir,
            source: BTreeMap::new(),
            control: BTreeMap::new(),
            derived: BTreeMap::new(),
            mounts: BTreeMap::new(),
        })
    }

    /// Reopen persisted stores, validating the mount table before accepting it.
    pub fn reopen(
        root_dir: DirId,
        source: BTreeMap<CellId, SourceEntry>,
        control: BTreeMap<String, ControlEntry>,
        derived: BTreeMap<CellId, DerivedEntry>,
        mounts: Vec<MountDescriptor>,
    ) -> Result<Self, StoreError> {
        let mut stores = Self::new(root_dir)?;
        stores.source = source;
        stores.control = control;
        stores.derived = derived;
        for descriptor in mounts {
            stores.mount(descriptor)?;
        }
        Ok(stores)
    }

    /// Root directory required by the mounted namespace owner.
    pub fn root_dir(&self) -> &DirId {
        &self.root_dir
    }

    /// Authored source entries only.
    pub fn source_entry(&self, id: &CellId) -> Option<&SourceEntry> {
        self.source.get(id)
    }

    /// Operational control entries only.
    pub fn control_entry(&self, key: &str) -> Option<&ControlEntry> {
        self.control.get(key)
    }

    /// Rebuildable derived entries only.
    pub fn derived_entry(&self, id: &CellId) -> Option<&DerivedEntry> {
        self.derived.get(id)
    }

    /// All current explicit mounts.
    pub fn mounts(&self) -> impl Iterator<Item = &MountDescriptor> {
        self.mounts.values()
    }

    /// Return a Table or Dir value to the caller without mutating the namespace.
    pub fn return_value_without_mounting(&self, _resource: MountResource) -> usize {
        self.mounts.len()
    }

    /// Explicitly mount a Table or Dir descriptor.
    pub fn mount(&mut self, descriptor: MountDescriptor) -> Result<(), StoreError> {
        validate_mount(&descriptor)?;
        let key = mount_key(descriptor.path());
        if self.mounts.contains_key(&key) {
            return Err(StoreError::InvalidMount(format!(
                "duplicate mount point {}",
                descriptor.path()
            )));
        }
        for existing in self.mounts.values() {
            if is_prefix(existing.path(), descriptor.path())
                && existing.resource() == MountResource::Table
                && existing.path() != descriptor.path()
            {
                return Err(StoreError::TableMountIsLeaf(existing.path().clone()));
            }
            if is_prefix(descriptor.path(), existing.path())
                && descriptor.resource() == MountResource::Table
            {
                return Err(StoreError::InvalidMount(format!(
                    "table mount {} would parent existing mount {}",
                    descriptor.path(),
                    existing.path()
                )));
            }
        }
        self.mounts.insert(key, descriptor);
        Ok(())
    }

    /// Removes and returns one explicit mount descriptor.
    pub fn unmount(&mut self, path: &TablePath) -> Result<MountDescriptor, StoreError> {
        let key = mount_key(path);
        let descriptor = self
            .mounts
            .remove(&key)
            .ok_or_else(|| StoreError::InvalidMount(format!("missing mount point {path}")))?;
        self.control.remove(&format!("mount-epoch:{path}"));
        Ok(descriptor)
    }

    /// Record a mounted backend epoch in the control store.
    pub fn observe_mount_epoch(
        &mut self,
        path: &TablePath,
        epoch: MountEpoch,
    ) -> Result<(), StoreError> {
        let mount = self
            .mounts
            .get_mut(&mount_key(path))
            .ok_or_else(|| StoreError::CorruptMount(format!("missing mount {}", path)))?;
        mount.set_epoch(epoch);
        self.control.insert(
            format!("mount-epoch:{}", path),
            ControlEntry::MountEpoch(epoch),
        );
        Ok(())
    }

    /// Prepare a recoverable source/control commit.
    ///
    /// The transaction boundary is exactly source plus control. Recovery replays the
    /// same pending record until both sides are durable. Derived entries are
    /// rebuildable and are not part of this boundary.
    pub fn prepare_source_control_commit(
        source_writes: BTreeMap<CellId, SourceEntry>,
        control_writes: BTreeMap<String, ControlEntry>,
    ) -> PendingCommit {
        PendingCommit::new(source_writes, control_writes)
    }

    /// Persist the source side of a prepared commit.
    pub fn commit_source(&mut self, pending: &mut PendingCommit) {
        self.source.extend(pending.source_writes.clone());
        pending.phase = CommitPhase::SourceCommitted;
    }

    /// Persist the control side of a source-committed commit.
    pub fn commit_control(&mut self, pending: &mut PendingCommit) {
        self.control.extend(pending.control_writes.clone());
        pending.phase = CommitPhase::ControlCommitted;
    }

    /// Finish or replay a partially persisted source/control commit.
    pub fn recover_commit(&mut self, pending: &mut PendingCommit) {
        if !pending.source_committed() {
            self.commit_source(pending);
        }
        if !pending.control_committed() {
            self.commit_control(pending);
        }
    }

    /// Store a rebuildable derived value outside source entries.
    pub fn put_derived(&mut self, id: CellId, entry: DerivedEntry) {
        self.derived.insert(id, entry);
    }

    /// Store an operational control value outside source entries.
    pub fn put_control(&mut self, key: impl Into<String>, entry: ControlEntry) {
        self.control.insert(key.into(), entry);
    }

    /// Removes authored source for a deleted cell.
    pub fn remove_source(&mut self, id: &CellId) -> Option<SourceEntry> {
        self.source.remove(id)
    }
}

fn validate_mount(descriptor: &MountDescriptor) -> Result<(), StoreError> {
    if descriptor.path().is_root() {
        return Err(StoreError::InvalidMount(
            "root is supplied as the required root Dir, not as a mount".to_owned(),
        ));
    }
    if descriptor.backend() == BackendKind::ReadOnly && descriptor.resource() == MountResource::Dir
    {
        return Ok(());
    }
    Ok(())
}

fn is_prefix(candidate: &TablePath, path: &TablePath) -> bool {
    let candidate_segments = segments(candidate);
    let path_segments = segments(path);
    candidate_segments.len() <= path_segments.len()
        && candidate_segments
            .iter()
            .zip(path_segments.iter())
            .all(|(left, right)| left == right)
}

fn segments(path: &TablePath) -> Vec<&str> {
    path.segments().iter().map(String::as_str).collect()
}

fn mount_key(path: &TablePath) -> String {
    path.to_absolute_reference()
}

#[cfg(test)]
pub(crate) fn source_keys_for_test(stores: &ExprTreeStores) -> std::collections::BTreeSet<CellId> {
    stores.source.keys().cloned().collect()
}