Skip to main content

reddb_engine/storage/
entity.rs

1//! Unified Entity Model
2//!
3//! Provides a single entity type that can represent table rows, graph nodes,
4//! graph edges, or vectors with seamless interoperability.
5
6use std::collections::HashMap;
7use std::sync::Arc;
8
9use reddb_types::Value;
10
11/// The first entity-id handed to user-inserted data. Ids `1..FIRST_USER_ENTITY_ID`
12/// are reserved for the internal collection-descriptor and config-default entities
13/// the engine seeds at boot, so the first user-inserted `rid` is a STABLE,
14/// documented value regardless of how many config defaults a build ships.
15///
16/// Before this floor existed the offset drifted upward by one for every config
17/// default added (101 → 114 over time), silently breaking the documented
18/// file-format invariant (#1369). The boot sequence bumps the allocator up to
19/// this floor after seeding internals; it only ever raises the counter, so a
20/// database that already holds user data is untouched. Mirrors
21/// `FIRST_USER_LABEL_ID` in the graph label registry.
22pub const FIRST_USER_ENTITY_ID: u64 = 1024;
23
24pub use super::entity_id::EntityId;
25
26/// The kind of entity (what storage type it belongs to)
27#[derive(Debug, Clone, PartialEq, Eq, Hash)]
28pub enum EntityKind {
29    /// A row in a structured table (hot path — kept inline for cache performance)
30    TableRow { table: Arc<str>, row_id: u64 },
31    /// A node in the graph (boxed — saves ~56 bytes per entity for table rows)
32    GraphNode(Box<GraphNodeKind>),
33    /// An edge in the graph (boxed)
34    GraphEdge(Box<GraphEdgeKind>),
35    /// A vector in a collection
36    Vector { collection: String },
37    /// A time-series data point (boxed)
38    TimeSeriesPoint(Box<TimeSeriesPointKind>),
39    /// A queue message
40    QueueMessage { queue: String, position: u64 },
41}
42
43#[derive(Debug, Clone, PartialEq, Eq, Hash)]
44pub struct GraphNodeKind {
45    pub label: String,
46    pub node_type: String,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq, Hash)]
50pub struct GraphEdgeKind {
51    pub label: String,
52    pub from_node: String,
53    pub to_node: String,
54    pub weight: u32,
55}
56
57#[derive(Debug, Clone, PartialEq, Eq, Hash)]
58pub struct TimeSeriesPointKind {
59    pub series: String,
60    pub metric: String,
61}
62
63impl EntityKind {
64    /// Get the storage type as a string
65    pub fn storage_type(&self) -> &'static str {
66        match self {
67            Self::TableRow { .. } => "table",
68            Self::GraphNode(_) => "graph_node",
69            Self::GraphEdge(_) => "graph_edge",
70            Self::Vector { .. } => "vector",
71            Self::TimeSeriesPoint(_) => "timeseries",
72            Self::QueueMessage { .. } => "queue",
73        }
74    }
75
76    /// Get the collection/table name
77    pub fn collection(&self) -> &str {
78        match self {
79            Self::TableRow { table, .. } => table,
80            Self::GraphNode(n) => &n.label,
81            Self::GraphEdge(e) => &e.label,
82            Self::Vector { collection } => collection,
83            Self::TimeSeriesPoint(ts) => &ts.series,
84            Self::QueueMessage { queue, .. } => queue,
85        }
86    }
87}
88
89/// The actual data content of an entity
90#[derive(Debug, Clone)]
91pub enum EntityData {
92    /// Table row data
93    Row(RowData),
94    /// Graph node data
95    Node(NodeData),
96    /// Graph edge data
97    Edge(EdgeData),
98    /// Vector data
99    Vector(VectorData),
100    /// Time-series data point
101    TimeSeries(TimeSeriesData),
102    /// Queue message data
103    QueueMessage(QueueMessageData),
104}
105
106impl EntityData {
107    /// Check if this is row data
108    pub fn is_row(&self) -> bool {
109        matches!(self, Self::Row(_))
110    }
111
112    /// Check if this is node data
113    pub fn is_node(&self) -> bool {
114        matches!(self, Self::Node(_))
115    }
116
117    /// Check if this is edge data
118    pub fn is_edge(&self) -> bool {
119        matches!(self, Self::Edge(_))
120    }
121
122    /// Check if this is vector data
123    pub fn is_vector(&self) -> bool {
124        matches!(self, Self::Vector(_))
125    }
126
127    /// Get as row data
128    pub fn as_row(&self) -> Option<&RowData> {
129        match self {
130            Self::Row(r) => Some(r),
131            _ => None,
132        }
133    }
134
135    /// Get as node data
136    pub fn as_node(&self) -> Option<&NodeData> {
137        match self {
138            Self::Node(n) => Some(n),
139            _ => None,
140        }
141    }
142
143    /// Get as edge data
144    pub fn as_edge(&self) -> Option<&EdgeData> {
145        match self {
146            Self::Edge(e) => Some(e),
147            _ => None,
148        }
149    }
150
151    /// Get as vector data
152    pub fn as_vector(&self) -> Option<&VectorData> {
153        match self {
154            Self::Vector(v) => Some(v),
155            _ => None,
156        }
157    }
158}
159
160/// Data for a table row
161#[derive(Debug, Clone)]
162pub struct RowData {
163    /// Column values in schema order
164    pub columns: Vec<Value>,
165    /// Named column access (optional, for convenience)
166    pub named: Option<HashMap<String, Value>>,
167    /// Shared column schema: column names in order (maps index → name).
168    /// When set, `columns` holds the values and `named` is None.
169    /// This saves ~60% memory vs per-row HashMap.
170    pub schema: Option<std::sync::Arc<Vec<String>>>,
171}
172
173impl RowData {
174    /// Create new row data from column values
175    pub fn new(columns: Vec<Value>) -> Self {
176        Self {
177            columns,
178            named: None,
179            schema: None,
180        }
181    }
182
183    /// Create row data with named columns
184    pub fn with_names(columns: Vec<Value>, names: Vec<String>) -> Self {
185        let named: HashMap<String, Value> =
186            names.into_iter().zip(columns.iter().cloned()).collect();
187        Self {
188            columns,
189            named: Some(named),
190            schema: None,
191        }
192    }
193
194    /// Replace a decoded named row with values in the authoritative schema's
195    /// order. Different dynamic shapes retain their named representation.
196    pub(crate) fn compact_with_schema(&mut self, schema: &std::sync::Arc<Vec<String>>) -> bool {
197        let Some(named) = &self.named else {
198            return false;
199        };
200        if named.len() != schema.len() || schema.iter().any(|name| !named.contains_key(name)) {
201            return false;
202        }
203        // Existing positional values are independently observable. Only drop
204        // their duplicate copy when the declared order agrees with them.
205        if !self.columns.is_empty()
206            && (self.columns.len() != schema.len()
207                || schema
208                    .iter()
209                    .zip(&self.columns)
210                    .any(|(name, value)| named.get(name) != Some(value)))
211        {
212            return false;
213        }
214        let mut named = self.named.take().expect("validated named row");
215        self.columns = schema
216            .iter()
217            .map(|name| named.remove(name).expect("validated schema field"))
218            .collect();
219        self.schema = Some(std::sync::Arc::clone(schema));
220        true
221    }
222
223    /// Get a named field value — checks named HashMap first, then schema+columns.
224    pub fn get_field(&self, name: &str) -> Option<&Value> {
225        // Fast path: named HashMap
226        if let Some(ref named) = self.named {
227            return named.get(name);
228        }
229        // Columnar path: use schema to find index
230        if let Some(ref schema) = self.schema {
231            if let Some(idx) = schema.iter().position(|s| s == name) {
232                return self.columns.get(idx);
233            }
234        }
235        None
236    }
237
238    /// Iterate over all (name, value) pairs — works for both named and columnar.
239    pub fn iter_fields(&self) -> Box<dyn Iterator<Item = (&str, &Value)> + '_> {
240        if let Some(ref named) = self.named {
241            Box::new(named.iter().map(|(k, v)| (k.as_str(), v)))
242        } else if let Some(ref schema) = self.schema {
243            Box::new(
244                schema
245                    .iter()
246                    .zip(self.columns.iter())
247                    .map(|(k, v)| (k.as_str(), v)),
248            )
249        } else {
250            Box::new(std::iter::empty())
251        }
252    }
253
254    /// Get column by index
255    pub fn get(&self, index: usize) -> Option<&Value> {
256        self.columns.get(index)
257    }
258
259    /// Get column by name
260    pub fn get_by_name(&self, name: &str) -> Option<&Value> {
261        self.named.as_ref()?.get(name)
262    }
263
264    /// Number of columns
265    pub fn len(&self) -> usize {
266        self.columns.len()
267    }
268
269    /// Check if empty
270    pub fn is_empty(&self) -> bool {
271        self.columns.is_empty()
272    }
273}
274
275/// Data for a graph node
276#[derive(Debug, Clone)]
277pub struct NodeData {
278    /// Node properties
279    pub properties: HashMap<String, Value>,
280}
281
282impl NodeData {
283    /// Create new node data
284    pub fn new() -> Self {
285        Self {
286            properties: HashMap::new(),
287        }
288    }
289
290    /// Create with properties
291    pub fn with_properties(properties: HashMap<String, Value>) -> Self {
292        Self { properties }
293    }
294
295    /// Set a property
296    pub fn set(&mut self, key: impl Into<String>, value: Value) {
297        self.properties.insert(key.into(), value);
298    }
299
300    /// Get a property
301    pub fn get(&self, key: &str) -> Option<&Value> {
302        self.properties.get(key)
303    }
304
305    /// Check if property exists
306    pub fn has(&self, key: &str) -> bool {
307        self.properties.contains_key(key)
308    }
309}
310
311impl Default for NodeData {
312    fn default() -> Self {
313        Self::new()
314    }
315}
316
317/// Data for a graph edge
318#[derive(Debug, Clone)]
319pub struct EdgeData {
320    /// Edge properties
321    pub properties: HashMap<String, Value>,
322    /// Edge weight (for weighted graphs)
323    pub weight: f32,
324}
325
326impl EdgeData {
327    /// Create new edge data
328    pub fn new(weight: f32) -> Self {
329        Self {
330            properties: HashMap::new(),
331            weight,
332        }
333    }
334
335    /// Create with properties
336    pub fn with_properties(weight: f32, properties: HashMap<String, Value>) -> Self {
337        Self { properties, weight }
338    }
339
340    /// Set a property
341    pub fn set(&mut self, key: impl Into<String>, value: Value) {
342        self.properties.insert(key.into(), value);
343    }
344
345    /// Get a property
346    pub fn get(&self, key: &str) -> Option<&Value> {
347        self.properties.get(key)
348    }
349}
350
351impl Default for EdgeData {
352    fn default() -> Self {
353        Self::new(1.0)
354    }
355}
356
357/// Data for a vector
358#[derive(Debug, Clone)]
359pub struct VectorData {
360    /// Dense vector (primary embedding)
361    pub dense: Vec<f32>,
362    /// Optional sparse vector
363    pub sparse: Option<SparseVector>,
364    /// Original content (if applicable)
365    pub content: Option<String>,
366}
367
368impl VectorData {
369    /// Create new vector data from dense vector
370    pub fn new(dense: Vec<f32>) -> Self {
371        Self {
372            dense,
373            sparse: None,
374            content: None,
375        }
376    }
377
378    /// Create with sparse vector
379    pub fn with_sparse(dense: Vec<f32>, sparse: SparseVector) -> Self {
380        Self {
381            dense,
382            sparse: Some(sparse),
383            content: None,
384        }
385    }
386
387    /// Set content
388    pub fn with_content(mut self, content: impl Into<String>) -> Self {
389        self.content = Some(content.into());
390        self
391    }
392
393    /// Get dimension
394    pub fn dimension(&self) -> usize {
395        self.dense.len()
396    }
397
398    /// Check if has sparse component
399    pub fn is_hybrid(&self) -> bool {
400        self.sparse.is_some()
401    }
402}
403
404/// Time-series data point
405#[derive(Debug, Clone)]
406pub struct TimeSeriesData {
407    /// Metric name (e.g., "cpu.idle")
408    pub metric: String,
409    /// Interned `(metric, canonical tags)` id for native time-series points.
410    pub series_id: Option<u64>,
411    /// Timestamp in nanoseconds since epoch
412    pub timestamp_ns: u64,
413    /// Metric value
414    pub value: f64,
415    /// Legacy/read-through dimensional tags (e.g., {"host": "srv1"}).
416    pub tags: std::collections::HashMap<String, String>,
417    /// Per-point fields that are not part of the immutable series identity.
418    pub fields: std::collections::HashMap<String, Value>,
419}
420
421/// Queue message data
422#[derive(Debug, Clone)]
423pub struct QueueMessageData {
424    /// Message payload
425    pub payload: Value,
426    /// Optional priority (higher = more urgent)
427    pub priority: Option<i32>,
428    /// Enqueue timestamp (nanoseconds)
429    pub enqueued_at_ns: u64,
430    /// Number of delivery attempts
431    pub attempts: u32,
432    /// Maximum delivery attempts before DLQ
433    pub max_attempts: u32,
434    /// Whether the message has been acknowledged
435    pub acked: bool,
436}
437
438/// Sparse vector representation
439#[derive(Debug, Clone)]
440pub struct SparseVector {
441    /// Indices of non-zero elements
442    pub indices: Vec<u32>,
443    /// Values at those indices
444    pub values: Vec<f32>,
445    /// Total dimension (may be larger than indices.len())
446    pub dimension: usize,
447}
448
449impl SparseVector {
450    /// Create new sparse vector
451    pub fn new(indices: Vec<u32>, values: Vec<f32>, dimension: usize) -> Self {
452        debug_assert_eq!(indices.len(), values.len());
453        Self {
454            indices,
455            values,
456            dimension,
457        }
458    }
459
460    /// Number of non-zero elements
461    pub fn nnz(&self) -> usize {
462        self.indices.len()
463    }
464
465    /// Sparsity ratio
466    pub fn sparsity(&self) -> f32 {
467        if self.dimension == 0 {
468            1.0
469        } else {
470            1.0 - (self.nnz() as f32 / self.dimension as f32)
471        }
472    }
473
474    /// Get value at index (0 if not present)
475    pub fn get(&self, index: u32) -> f32 {
476        self.indices
477            .iter()
478            .position(|&i| i == index)
479            .map(|pos| self.values[pos])
480            .unwrap_or(0.0)
481    }
482}
483
484/// A slot for embedding a specific aspect of an entity
485#[derive(Debug, Clone)]
486pub struct EmbeddingSlot {
487    /// Slot name (e.g., "content", "summary", "title", "code")
488    pub name: String,
489    /// The embedding vector
490    pub vector: Vec<f32>,
491    /// Model used to generate embedding
492    pub model: String,
493    /// Vector dimension
494    pub dimension: usize,
495    /// Generation timestamp
496    pub generated_at: u64,
497}
498
499use crate::clock::current_unix_secs;
500
501impl EmbeddingSlot {
502    /// Create a new embedding slot
503    pub fn new(name: impl Into<String>, vector: Vec<f32>, model: impl Into<String>) -> Self {
504        let dimension = vector.len();
505        Self {
506            name: name.into(),
507            vector,
508            model: model.into(),
509            dimension,
510            generated_at: current_unix_secs(),
511        }
512    }
513}
514
515/// A unified entity that can represent any storage type
516#[derive(Debug, Clone)]
517pub struct UnifiedEntity {
518    /// Unique entity identifier
519    pub id: EntityId,
520    /// Stable user-visible identity shared by all physical versions.
521    ///
522    /// `None` is the legacy encoding and resolves to `id`.
523    logical_id: Option<EntityId>,
524    /// What kind of entity this is
525    pub kind: EntityKind,
526    /// Creation timestamp
527    pub created_at: u64,
528    /// Last update timestamp
529    pub updated_at: u64,
530    /// The actual data content
531    pub data: EntityData,
532    /// Sequence ID for ordering/versioning
533    pub sequence_id: u64,
534    /// Field-name bloom filter (u64, zero-allocation).
535    ///
536    /// Each bit encodes one possible mid-character value: for field name `n`
537    /// the bit position is `n.as_bytes()[n.len()/2] & 63`. OR of all user
538    /// field names present in this entity. Cleared for schema-based bulk rows
539    /// (all rows share the same schema so bloom is segment-level).
540    ///
541    /// The compiled filter computes `required_bloom` from predicate field names
542    /// at compile time. If `entity.field_bloom & required_bloom != required_bloom`,
543    /// the entity cannot match and is skipped before any HashMap probe.
544    pub field_bloom: u64,
545    /// MVCC creation transaction ID (Phase 2.3 PG parity).
546    ///
547    /// `0` means "pre-MVCC" / auto-commit — visible to every snapshot. When
548    /// a BEGIN-wrapped INSERT runs, it stamps `xmin` with the transaction's
549    /// snapshot id so other concurrent transactions only see the row after
550    /// the writer commits (snapshot isolation semantics).
551    ///
552    /// Visibility rule: `xmin <= snapshot.xid && (xmax == 0 || xmax > snapshot.xid)`.
553    pub xmin: u64,
554    /// MVCC deletion transaction ID (Phase 2.3 PG parity).
555    ///
556    /// `0` means "live". Set to the deleting transaction's snapshot id on
557    /// DELETE/UPDATE (row is kept until VACUUM reclaims it). Snapshots with
558    /// `xid < xmax` still see the row; newer snapshots skip it.
559    pub xmax: u64,
560    /// Optional auxiliary data (embeddings, cross-refs).
561    /// None for most table rows — saves 40 bytes/entity.
562    aux: Option<Box<EntityAux>>,
563}
564
565/// Auxiliary entity data — only allocated when needed.
566#[derive(Debug, Clone, Default)]
567pub struct EntityAux {
568    /// Embedding slots (for multi-vector support)
569    pub embeddings: Vec<EmbeddingSlot>,
570    /// Cross-references to other entities
571    pub cross_refs: Vec<CrossRef>,
572}
573
574impl UnifiedEntity {
575    /// Normalize persisted runtime-owned collection identities while retaining
576    /// exact user collection names. This is idempotent and intentionally does
577    /// not rewrite graph labels, which are data rather than collection keys.
578    pub fn canonicalize_system_collection_names(&mut self) {
579        match &mut self.kind {
580            EntityKind::TableRow { table, .. } => {
581                if let Some(canonical) = super::system_collection_names::canonical_name(table) {
582                    *table = Arc::from(canonical);
583                }
584            }
585            EntityKind::Vector { collection } => {
586                if let Some(canonical) = super::system_collection_names::canonical_name(collection)
587                {
588                    *collection = canonical.to_owned();
589                }
590            }
591            EntityKind::TimeSeriesPoint(point) => {
592                if let Some(canonical) =
593                    super::system_collection_names::canonical_name(&point.series)
594                {
595                    point.series = canonical.to_owned();
596                }
597            }
598            EntityKind::QueueMessage { queue, .. } => {
599                if let Some(canonical) = super::system_collection_names::canonical_name(queue) {
600                    *queue = canonical.to_owned();
601                }
602            }
603            EntityKind::GraphNode(_) | EntityKind::GraphEdge(_) => {}
604        }
605        if let Some(aux) = &mut self.aux {
606            for reference in &mut aux.cross_refs {
607                if let Some(canonical) =
608                    super::system_collection_names::canonical_name(&reference.target_collection)
609                {
610                    reference.target_collection = canonical.to_owned();
611                }
612            }
613        }
614    }
615
616    /// Access embeddings (returns empty slice if no aux data).
617    pub fn embeddings(&self) -> &[EmbeddingSlot] {
618        self.aux
619            .as_ref()
620            .map(|a| a.embeddings.as_slice())
621            .unwrap_or(&[])
622    }
623
624    /// Access cross-references (returns empty slice if no aux data).
625    pub fn cross_refs(&self) -> &[CrossRef] {
626        self.aux
627            .as_ref()
628            .map(|a| a.cross_refs.as_slice())
629            .unwrap_or(&[])
630    }
631
632    /// Get mutable embeddings (allocates aux if needed).
633    pub fn embeddings_mut(&mut self) -> &mut Vec<EmbeddingSlot> {
634        &mut self.aux.get_or_insert_with(Default::default).embeddings
635    }
636
637    /// Get mutable cross-refs (allocates aux if needed).
638    pub fn cross_refs_mut(&mut self) -> &mut Vec<CrossRef> {
639        &mut self.aux.get_or_insert_with(Default::default).cross_refs
640    }
641
642    /// Check if entity has any auxiliary data.
643    pub fn has_aux(&self) -> bool {
644        self.aux.is_some()
645    }
646}
647
648/// Compute one bit of a field-name bloom filter.
649///
650/// Uses the mid-character trick from MongoDB's field-name bloom:
651/// the bit position is the mid-byte value clamped to 0..63. Zero-allocation,
652/// ~1.5% false-positive rate for ≤5 distinct field names.
653#[inline]
654pub fn field_name_bloom(name: &str) -> u64 {
655    let b = name.as_bytes();
656    if b.is_empty() {
657        return 0;
658    }
659    1u64 << (b[b.len() / 2] & 63)
660}
661
662/// Compute the combined field-name bloom for all user-level fields in `data`.
663/// Returns 0 for schema-based rows (all rows share the same schema, so the
664/// per-entity bloom would be identical — caller uses a segment-level bloom).
665pub fn compute_entity_field_bloom(data: &EntityData) -> u64 {
666    match data {
667        EntityData::Row(row) => {
668            if row.schema.is_some() {
669                // Schema path: bloom is identical for every row in this table.
670                // Don't store per-entity — use segment-level bloom instead.
671                return 0;
672            }
673            if let Some(named) = &row.named {
674                let mut bloom = named.keys().fold(0u64, |acc, k| acc | field_name_bloom(k));
675                // Single-source documents no longer materialise promoted
676                // columns, so the body's top-level keys are the only place a
677                // `WHERE`/projection field lives. Fold them into the bloom so
678                // the field-bloom gate doesn't reject a row before the body
679                // read fallback ever runs. Inert for non-document rows (no
680                // binary-container `body` field) — `container_field_names`
681                // self-gates on the RDOC magic.
682                if let Some(Value::Json(bytes)) = named.get("body") {
683                    if let Ok(names) = reddb_types::document_body_codec::field_names(bytes) {
684                        for name in names {
685                            bloom |= field_name_bloom(&name);
686                        }
687                    }
688                }
689                bloom
690            } else {
691                0
692            }
693        }
694        EntityData::Node(node) => node
695            .properties
696            .keys()
697            .fold(0u64, |acc, k| acc | field_name_bloom(k)),
698        EntityData::Edge(edge) => edge
699            .properties
700            .keys()
701            .fold(0u64, |acc, k| acc | field_name_bloom(k)),
702        // Vectors, time-series, queue: no user-named fields worth blooming.
703        _ => 0,
704    }
705}
706
707impl UnifiedEntity {
708    /// Create a new unified entity
709    pub fn new(id: EntityId, kind: EntityKind, data: EntityData) -> Self {
710        let now = current_unix_secs();
711        let field_bloom = compute_entity_field_bloom(&data);
712
713        Self {
714            id,
715            logical_id: None,
716            kind,
717            created_at: now,
718            updated_at: now,
719            data,
720            sequence_id: 0,
721            field_bloom,
722            // Pre-MVCC default: xmin/xmax = 0 means visible to every snapshot.
723            // Transactional writers stamp real snapshot IDs after allocation.
724            xmin: 0,
725            xmax: 0,
726            aux: None,
727        }
728    }
729
730    /// MVCC visibility check (Phase 2.3 PG parity).
731    ///
732    /// Returns `true` when this tuple is visible under the provided
733    /// snapshot xid. Pre-MVCC rows (`xmin == 0`, `xmax == 0`) are visible
734    /// to every snapshot — preserves full compatibility with existing
735    /// data inserted before the MVCC headers existed.
736    ///
737    /// Snapshot isolation rule:
738    ///   - `xmin == 0 || xmin <= snapshot_xid`  (creator committed before snapshot)
739    ///   - `xmax == 0 || xmax > snapshot_xid`   (deleter committed after snapshot)
740    #[inline]
741    pub fn is_visible(&self, snapshot_xid: u64) -> bool {
742        if self.xmin != 0 && self.xmin > snapshot_xid {
743            return false;
744        }
745        if self.xmax != 0 && self.xmax <= snapshot_xid {
746            return false;
747        }
748        true
749    }
750
751    /// Stamp `xmin` (creation transaction ID). Called by the runtime on
752    /// INSERT inside an active transaction.
753    #[inline]
754    pub fn set_xmin(&mut self, xid: u64) {
755        self.xmin = xid;
756    }
757
758    /// Stamp `xmax` (deletion transaction ID). Called by the runtime on
759    /// DELETE/UPDATE inside an active transaction — the tuple survives
760    /// until VACUUM reclaims it.
761    #[inline]
762    pub fn set_xmax(&mut self, xid: u64) {
763        self.xmax = xid;
764    }
765
766    /// Stable user-visible identity. Legacy rows without an explicit
767    /// logical id map to their physical entity id.
768    #[inline]
769    pub fn logical_id(&self) -> EntityId {
770        self.logical_id.unwrap_or(self.id)
771    }
772
773    /// Returns true when the entity carries an explicit logical id on disk.
774    #[inline]
775    pub fn has_explicit_logical_id(&self) -> bool {
776        self.logical_id.is_some()
777    }
778
779    /// Set the stable user-visible identity for this physical version.
780    #[inline]
781    pub fn set_logical_id(&mut self, logical_id: EntityId) {
782        self.logical_id = Some(logical_id);
783    }
784
785    /// Ensure entities written by the current engine carry explicit
786    /// logical identity. Table rows + documents (Phase 1/2) and graph
787    /// nodes/edges (Phase 3) participate in the multi-model MVCC
788    /// versioning rollout and so need a stable logical id for
789    /// version-chain selection. Stamping the logical id is inert for
790    /// non-versioned collections: history only accrues through
791    /// `install_versioned_table_row_update`, which is gated on the
792    /// collection `versioned` flag, so a stamped-but-never-superseded
793    /// entity keeps `logical_id == id` and behaves exactly as before.
794    /// Vectors are intentionally excluded pending their read-path follow
795    /// up (no snapshot-honoring `VECTOR SEARCH`).
796    #[inline]
797    pub fn ensure_table_logical_id(&mut self) {
798        if self.logical_id.is_none()
799            && matches!(
800                self.kind,
801                EntityKind::TableRow { .. } | EntityKind::GraphNode(_) | EntityKind::GraphEdge(_)
802            )
803        {
804            self.logical_id = Some(self.id);
805        }
806    }
807
808    /// Create a table row entity
809    pub fn table_row(
810        id: EntityId,
811        table: impl Into<Arc<str>>,
812        row_id: u64,
813        columns: Vec<Value>,
814    ) -> Self {
815        Self::new(
816            id,
817            EntityKind::TableRow {
818                table: table.into(),
819                row_id,
820            },
821            EntityData::Row(RowData::new(columns)),
822        )
823    }
824
825    /// Create a graph node entity
826    pub fn graph_node(
827        id: EntityId,
828        label: impl Into<String>,
829        node_type: impl Into<String>,
830        properties: HashMap<String, Value>,
831    ) -> Self {
832        Self::new(
833            id,
834            EntityKind::GraphNode(Box::new(GraphNodeKind {
835                label: label.into(),
836                node_type: node_type.into(),
837            })),
838            EntityData::Node(NodeData::with_properties(properties)),
839        )
840    }
841
842    /// Create a graph edge entity
843    pub fn graph_edge(
844        id: EntityId,
845        label: impl Into<String>,
846        from: impl Into<String>,
847        to: impl Into<String>,
848        weight: f32,
849        properties: HashMap<String, Value>,
850    ) -> Self {
851        Self::new(
852            id,
853            EntityKind::GraphEdge(Box::new(GraphEdgeKind {
854                label: label.into(),
855                from_node: from.into(),
856                to_node: to.into(),
857                weight: (weight * 1000.0) as u32,
858            })),
859            EntityData::Edge(EdgeData::with_properties(weight, properties)),
860        )
861    }
862
863    /// Create a vector entity
864    pub fn vector(id: EntityId, collection: impl Into<String>, vector: Vec<f32>) -> Self {
865        Self::new(
866            id,
867            EntityKind::Vector {
868                collection: collection.into(),
869            },
870            EntityData::Vector(VectorData::new(vector)),
871        )
872    }
873
874    /// Add an embedding to this entity
875    pub fn add_embedding(&mut self, slot: EmbeddingSlot) {
876        self.embeddings_mut().push(slot);
877        self.touch();
878    }
879
880    /// Add a cross-reference
881    pub fn add_cross_ref(&mut self, cross_ref: CrossRef) {
882        self.cross_refs_mut().push(cross_ref);
883        self.touch();
884    }
885
886    /// Get embedding by slot name
887    pub fn get_embedding(&self, name: &str) -> Option<&EmbeddingSlot> {
888        self.embeddings().iter().find(|e| e.name == name)
889    }
890
891    /// Update timestamp
892    fn touch(&mut self) {
893        self.updated_at = current_unix_secs();
894    }
895
896    /// Check if entity is stale (not updated in given seconds)
897    pub fn is_stale(&self, max_age_secs: u64) -> bool {
898        let now = current_unix_secs();
899        now.saturating_sub(self.updated_at) > max_age_secs
900    }
901}
902
903/// A cross-reference between entities
904#[derive(Debug, Clone, PartialEq)]
905pub struct CrossRef {
906    /// Source entity ID (the entity that holds this reference)
907    pub source: EntityId,
908    /// Target entity ID
909    pub target: EntityId,
910    /// Target collection name
911    pub target_collection: String,
912    /// Type of reference
913    pub ref_type: RefType,
914    /// Reference weight/strength (0.0-1.0)
915    pub weight: f32,
916    /// When this reference was created
917    pub created_at: u64,
918}
919
920impl CrossRef {
921    /// Create a new cross-reference
922    pub fn new(
923        source: EntityId,
924        target: EntityId,
925        target_collection: impl Into<String>,
926        ref_type: RefType,
927    ) -> Self {
928        Self {
929            source,
930            target,
931            target_collection: target_collection.into(),
932            ref_type,
933            weight: 1.0,
934            created_at: current_unix_secs(),
935        }
936    }
937
938    /// Create with weight
939    pub fn with_weight(
940        source: EntityId,
941        target: EntityId,
942        target_collection: impl Into<String>,
943        ref_type: RefType,
944        weight: f32,
945    ) -> Self {
946        let mut cr = Self::new(source, target, target_collection, ref_type);
947        cr.weight = weight;
948        cr
949    }
950}
951
952/// Types of cross-references between entities
953#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
954pub enum RefType {
955    // Table ↔ Graph
956    RowToNode, // Table row represents a graph node
957    RowToEdge, // Table row represents a graph edge
958    NodeToRow, // Node links back to source row
959
960    // Table ↔ Vector
961    RowToVector, // Table row has embeddings
962    VectorToRow, // Vector search → source row
963
964    // Graph ↔ Vector
965    NodeToVector, // Node has embeddings
966    EdgeToVector, // Edge has embeddings
967    VectorToNode, // Vector search → source node
968
969    // Semantic links (discovered)
970    SimilarTo,   // Discovered by vector similarity
971    RelatedTo,   // Domain-specific relationship
972    DerivesFrom, // Data lineage
973    Mentions,    // Text mentions another entity
974    Contains,    // Structural containment
975    DependsOn,   // Dependency relationship
976}
977
978impl RefType {
979    /// Get the inverse reference type (for bidirectional tracking)
980    pub fn inverse(&self) -> Option<Self> {
981        match self {
982            Self::RowToNode => Some(Self::NodeToRow),
983            Self::NodeToRow => Some(Self::RowToNode),
984            Self::RowToVector => Some(Self::VectorToRow),
985            Self::VectorToRow => Some(Self::RowToVector),
986            Self::NodeToVector => Some(Self::VectorToNode),
987            Self::VectorToNode => Some(Self::NodeToVector),
988            Self::SimilarTo => Some(Self::SimilarTo), // Symmetric
989            Self::RelatedTo => Some(Self::RelatedTo), // Symmetric
990            _ => None,                                // One-directional references
991        }
992    }
993
994    /// Check if this is a symmetric reference type
995    pub fn is_symmetric(&self) -> bool {
996        matches!(self, Self::SimilarTo | Self::RelatedTo)
997    }
998
999    /// Convert RefType to byte for binary serialization
1000    pub fn to_byte(&self) -> u8 {
1001        match self {
1002            Self::RowToNode => 0,
1003            Self::RowToEdge => 1,
1004            Self::NodeToRow => 2,
1005            Self::RowToVector => 3,
1006            Self::VectorToRow => 4,
1007            Self::NodeToVector => 5,
1008            Self::EdgeToVector => 6,
1009            Self::VectorToNode => 7,
1010            Self::SimilarTo => 8,
1011            Self::RelatedTo => 9,
1012            Self::DerivesFrom => 10,
1013            Self::Mentions => 11,
1014            Self::Contains => 12,
1015            Self::DependsOn => 13,
1016        }
1017    }
1018
1019    /// Create RefType from byte (binary deserialization)
1020    pub fn from_byte(byte: u8) -> Self {
1021        match byte {
1022            0 => Self::RowToNode,
1023            1 => Self::RowToEdge,
1024            2 => Self::NodeToRow,
1025            3 => Self::RowToVector,
1026            4 => Self::VectorToRow,
1027            5 => Self::NodeToVector,
1028            6 => Self::EdgeToVector,
1029            7 => Self::VectorToNode,
1030            8 => Self::SimilarTo,
1031            9 => Self::RelatedTo,
1032            10 => Self::DerivesFrom,
1033            11 => Self::Mentions,
1034            12 => Self::Contains,
1035            13 => Self::DependsOn,
1036            _ => Self::RelatedTo, // Default fallback
1037        }
1038    }
1039}
1040
1041/// Convert Vec<Value> to RowData
1042impl From<Vec<Value>> for RowData {
1043    fn from(columns: Vec<Value>) -> Self {
1044        RowData::new(columns)
1045    }
1046}
1047
1048/// Convert HashMap to NodeData
1049impl From<HashMap<String, Value>> for NodeData {
1050    fn from(properties: HashMap<String, Value>) -> Self {
1051        NodeData::with_properties(properties)
1052    }
1053}
1054
1055/// Convert dense vector to VectorData
1056impl From<Vec<f32>> for VectorData {
1057    fn from(dense: Vec<f32>) -> Self {
1058        VectorData::new(dense)
1059    }
1060}
1061
1062/// Convert tuple (dense, sparse) to VectorData
1063impl From<(Vec<f32>, SparseVector)> for VectorData {
1064    fn from((dense, sparse): (Vec<f32>, SparseVector)) -> Self {
1065        VectorData::with_sparse(dense, sparse)
1066    }
1067}
1068
1069// Helper trait for uniform entity creation
1070impl UnifiedEntity {
1071    /// Create a graph node entity from properties map
1072    pub fn from_properties(
1073        id: EntityId,
1074        label: impl Into<String>,
1075        node_type: impl Into<String>,
1076        properties: impl IntoIterator<Item = (impl Into<String>, Value)>,
1077    ) -> Self {
1078        let props: HashMap<String, Value> =
1079            properties.into_iter().map(|(k, v)| (k.into(), v)).collect();
1080        Self::graph_node(id, label, node_type, props)
1081    }
1082
1083    /// Convert entity to row data if applicable
1084    pub fn into_row(self) -> Option<RowData> {
1085        match self.data {
1086            EntityData::Row(r) => Some(r),
1087            _ => None,
1088        }
1089    }
1090
1091    /// Convert entity to node data if applicable
1092    pub fn into_node(self) -> Option<NodeData> {
1093        match self.data {
1094            EntityData::Node(n) => Some(n),
1095            _ => None,
1096        }
1097    }
1098
1099    /// Convert entity to edge data if applicable
1100    pub fn into_edge(self) -> Option<EdgeData> {
1101        match self.data {
1102            EntityData::Edge(e) => Some(e),
1103            _ => None,
1104        }
1105    }
1106
1107    /// Convert entity to vector data if applicable
1108    pub fn into_vector(self) -> Option<VectorData> {
1109        match self.data {
1110            EntityData::Vector(v) => Some(v),
1111            _ => None,
1112        }
1113    }
1114}
1115
1116#[cfg(test)]
1117mod tests {
1118    use super::*;
1119
1120    #[test]
1121    fn test_entity_creation() {
1122        let id = EntityId::new(1);
1123        let entity = UnifiedEntity::table_row(
1124            id,
1125            "users",
1126            100,
1127            vec![Value::text("alice".to_string()), Value::Integer(25)],
1128        );
1129
1130        assert!(entity.data.is_row());
1131        assert_eq!(entity.kind.storage_type(), "table");
1132        assert_eq!(entity.kind.collection(), "users");
1133    }
1134
1135    #[test]
1136    fn test_cross_refs() {
1137        let id1 = EntityId::new(1);
1138        let id2 = EntityId::new(2);
1139
1140        let cross_ref = CrossRef::new(id1, id2, "nodes", RefType::RowToNode);
1141        assert_eq!(cross_ref.source, id1);
1142        assert_eq!(cross_ref.target, id2);
1143        assert_eq!(cross_ref.ref_type.inverse(), Some(RefType::NodeToRow));
1144    }
1145
1146    #[test]
1147    fn test_sparse_vector() {
1148        let sparse = SparseVector::new(vec![0, 5, 10], vec![1.0, 2.0, 3.0], 100);
1149
1150        assert_eq!(sparse.nnz(), 3);
1151        assert_eq!(sparse.get(5), 2.0);
1152        assert_eq!(sparse.get(3), 0.0);
1153        assert!(sparse.sparsity() > 0.9);
1154    }
1155
1156    #[test]
1157    fn test_embedding_slots() {
1158        let mut entity = UnifiedEntity::table_row(
1159            EntityId::new(1),
1160            "documents",
1161            1,
1162            vec![Value::text("Hello world".to_string())],
1163        );
1164
1165        entity.add_embedding(EmbeddingSlot::new(
1166            "content",
1167            vec![0.1, 0.2, 0.3],
1168            "text-embedding-3-small",
1169        ));
1170
1171        assert_eq!(entity.embeddings().len(), 1);
1172        assert!(entity.get_embedding("content").is_some());
1173        assert!(entity.get_embedding("summary").is_none());
1174    }
1175}