Skip to main content

waratah_temporal/
write.rs

1//! WritePlugin: derive O-axis temporal-index postings from native terms.
2//!
3//! Runs **after** plain encoding conceptually but implements the same write
4//! hook as geo: every quad whose object projects onto the tick axis
5//! ([`waratah_storage::temporal_term_interval`]) gets budget-covered
6//! postings (instants exactly one leaf) attached to the batch, stamped with
7//! the batch's `commit_ts`. The encoded quad list is untouched — temporal
8//! terms are already storage-native — so the plugin contributes only
9//! [`PreparedInsert::temporal_puts`]. (And note: on delete batches the
10//! fan-out deliberately **drops** the derived postings — never retract a
11//! posting because one referencing quad died. Postings are term-scoped, and
12//! sibling quads sharing the literal stay live; a dead term's posting is a
13//! harmless superset candidate, and the rebuild's liveness walk is the only
14//! garbage collector.)
15
16use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering};
17
18use waratah_compute::ComputeError;
19use waratah_compute::plugin::{PreparedInsert, WritePlugin};
20use waratah_storage::{CommitTs, DyadicCovererConfig, StorageOp, postings_for_term};
21
22/// Coarsest allowed covering node level (default: the whole axis).
23pub const TEMPORAL_MIN_LEVEL: u8 = 0;
24
25/// Finest allowed covering node level (default: width ≈ a quarter
26/// microsecond — below any interval anyone posts deliberately).
27pub const TEMPORAL_MAX_LEVEL: u8 = 110;
28
29/// Maximum nodes per interval (default 32 — a calendar month posts ~a dozen
30/// nodes; a 200-Myr era posts 32).
31pub const TEMPORAL_MAX_NODES: usize = 32;
32
33static MIN_LEVEL: AtomicU8 = AtomicU8::new(TEMPORAL_MIN_LEVEL);
34static MAX_LEVEL: AtomicU8 = AtomicU8::new(TEMPORAL_MAX_LEVEL);
35static MAX_NODES: AtomicUsize = AtomicUsize::new(TEMPORAL_MAX_NODES);
36
37/// Coverer configuration for ingest postings and query windows (process
38/// configuration; a change makes existing postings valid-but-looser
39/// supersets — correct, only candidate volume grows — and is made
40/// consistent by `waratah temporal rebuild-index`).
41pub fn ingest_coverer_config() -> DyadicCovererConfig {
42    DyadicCovererConfig {
43        min_level: MIN_LEVEL.load(Ordering::Relaxed),
44        max_level: MAX_LEVEL.load(Ordering::Relaxed),
45        max_nodes: MAX_NODES.load(Ordering::Relaxed).max(1),
46    }
47}
48
49/// Override the process coverer config (CLI / compute config / tests).
50///
51/// NOT cluster-wide (v1): two computes with different flags silently mix
52/// covering generations on the same CF; an operator must run
53/// `waratah temporal rebuild-index` after changing these.
54pub fn set_coverer_config(config: DyadicCovererConfig) {
55    MIN_LEVEL.store(config.min_level, Ordering::Relaxed);
56    MAX_LEVEL.store(config.max_level.min(128), Ordering::Relaxed);
57    MAX_NODES.store(config.max_nodes.max(1), Ordering::Relaxed);
58}
59
60/// Temporal write hook.
61#[derive(Debug, Default)]
62pub struct TemporalWritePlugin;
63
64impl WritePlugin for TemporalWritePlugin {
65    fn prepare_insert(
66        &self,
67        quads: &[oxrdf::Quad],
68        commit_ts: CommitTs,
69        _op: StorageOp,
70    ) -> Result<PreparedInsert, ComputeError> {
71        let mut out = PreparedInsert {
72            object_rewrites: Vec::new(),
73            blobs: Vec::new(),
74            geo_puts: Vec::new(),
75            temporal_puts: Vec::new(),
76            text_docs: Vec::new(),
77            warnings: Vec::new(),
78            spike_reductions: 0,
79        };
80        let config = ingest_coverer_config();
81        for quad in quads {
82            // Only literal objects can be temporal terms; encode the object
83            // alone (cheap — the fan-out encodes the full quad anyway).
84            let oxrdf::Term::Literal(literal) = &quad.object else {
85                continue;
86            };
87            let encoded = waratah_storage::EncodedTerm::from(literal);
88            let postings = postings_for_term(&encoded, commit_ts, &config);
89            out.temporal_puts.extend(postings);
90        }
91        Ok(out)
92    }
93}