nodedb-graph 0.4.0

Shared graph engine (CSR adjacency index + traversal) for NodeDB Origin and Lite
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// SPDX-License-Identifier: Apache-2.0

//! CSR checkpoint serialization via rkyv. On little-endian platforms
//! dense arrays are restored zero-copy by pointing `DenseArray` at the
//! archived buffer.
//!
//! Used by both Origin (via redb storage) and Lite (via embedded checkpoint).

use std::collections::HashMap;
use std::mem::size_of;

use nodedb_mem::EngineId;

use super::index::CsrIndex;
use crate::GraphError;

/// Magic header for rkyv-serialized CSR snapshots (6 bytes).
const RKYV_MAGIC: &[u8; 6] = b"RKCS2\0";
/// Current format version for rkyv-serialized CSR snapshots.
///
/// Bumped to `2` when per-edge collection tags were added (parallel
/// `out_collections` / `in_collections` arrays + collection interning). A v1
/// snapshot has no collection axis and is rejected; the CSR is instead rebuilt
/// from the collection-scoped durable edge store.
pub const CSR_FORMAT_VERSION: u8 = 2;

/// Errors during CSR checkpoint operations.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CsrCheckpointError {
    #[error("unsupported CSR checkpoint version {found}; expected {expected}")]
    UnsupportedVersion { found: u8, expected: u8 },
    #[error("CSR checkpoint rkyv deserialization failed")]
    RkyvDeserialize,
}

/// rkyv-serialized CSR snapshot for fast save/load.
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
struct CsrSnapshotRkyv {
    nodes: Vec<String>,
    labels: Vec<String>,
    collections: Vec<String>,
    out_offsets: Vec<u32>,
    out_targets: Vec<u32>,
    out_labels: Vec<u32>,
    out_collections: Vec<u32>,
    in_offsets: Vec<u32>,
    in_targets: Vec<u32>,
    in_labels: Vec<u32>,
    in_collections: Vec<u32>,
    buffer_out: Vec<Vec<(u32, u32)>>,
    buffer_in: Vec<Vec<(u32, u32)>>,
    buffer_out_collections: Vec<Vec<u32>>,
    buffer_in_collections: Vec<Vec<u32>>,
    /// Deleted-edge identities `(src, label, dst, collection)`. Collection is
    /// part of the key so per-collection copies of a shared triple tombstone
    /// independently. The v2 snapshot already carries the collection axis, so
    /// widening this key needs no new format version.
    deleted: Vec<(u32, u32, u32, u32)>,
    has_weights: bool,
    out_weights: Option<Vec<f64>>,
    in_weights: Option<Vec<f64>>,
    buffer_out_weights: Vec<Vec<f64>>,
    buffer_in_weights: Vec<Vec<f64>>,
}

impl CsrIndex {
    /// Serialize the index to rkyv bytes (with magic header) for storage.
    ///
    /// # Errors
    ///
    /// Returns [`GraphError::MemoryBudget`] if a memory governor is installed
    /// and the serialization buffer would exceed the `Graph` engine budget.
    pub fn checkpoint_to_bytes(&self) -> Result<Vec<u8>, GraphError> {
        let snapshot = CsrSnapshotRkyv {
            nodes: self.id_to_node.clone(),
            labels: self.id_to_label.clone(),
            collections: self.id_to_collection.clone(),
            out_offsets: self.out_offsets.clone(),
            out_targets: self.out_targets.to_vec(),
            out_labels: self.out_labels.to_vec(),
            out_collections: self.out_collections.clone(),
            in_offsets: self.in_offsets.clone(),
            in_targets: self.in_targets.to_vec(),
            in_labels: self.in_labels.to_vec(),
            in_collections: self.in_collections.clone(),
            buffer_out: self.buffer_out.clone(),
            buffer_in: self.buffer_in.clone(),
            buffer_out_collections: self.buffer_out_collections.clone(),
            buffer_in_collections: self.buffer_in_collections.clone(),
            deleted: self.deleted_edges.iter().copied().collect(),
            has_weights: self.has_weights,
            out_weights: self.out_weights.as_ref().map(|w| w.to_vec()),
            in_weights: self.in_weights.as_ref().map(|w| w.to_vec()),
            buffer_out_weights: self.buffer_out_weights.clone(),
            buffer_in_weights: self.buffer_in_weights.clone(),
        };
        let rkyv_bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&snapshot)
            .expect("CSR rkyv serialization should not fail");
        let buf_capacity = RKYV_MAGIC.len() + 1 + rkyv_bytes.len();
        let _budget_guard = self
            .governor
            .as_ref()
            .map(|g| g.reserve(EngineId::Graph, buf_capacity * size_of::<u8>()))
            .transpose()?;
        let mut buf = Vec::with_capacity(buf_capacity);
        buf.extend_from_slice(RKYV_MAGIC);
        buf.push(CSR_FORMAT_VERSION);
        buf.extend_from_slice(&rkyv_bytes);
        Ok(buf)
    }

    /// Restore an index from a checkpoint snapshot.
    ///
    /// Returns:
    /// - `Ok(Some(index))` — successfully decoded.
    /// - `Ok(None)` — buffer does not start with the magic header (no legacy
    ///   format exists for CSR; callers should treat this as an invalid buffer).
    /// - `Err(CsrCheckpointError::UnsupportedVersion)` — magic matches but the
    ///   version byte is not `CSR_FORMAT_VERSION`.
    pub fn from_checkpoint(bytes: &[u8]) -> Result<Option<Self>, CsrCheckpointError> {
        let header_len = RKYV_MAGIC.len() + 1; // magic + version byte
        if bytes.len() > header_len && &bytes[..RKYV_MAGIC.len()] == RKYV_MAGIC {
            let version = bytes[RKYV_MAGIC.len()];
            if version != CSR_FORMAT_VERSION {
                return Err(CsrCheckpointError::UnsupportedVersion {
                    found: version,
                    expected: CSR_FORMAT_VERSION,
                });
            }
            return Ok(Self::from_rkyv_checkpoint(&bytes[header_len..]));
        }
        Ok(None)
    }

    /// Restore from rkyv-serialized bytes.
    ///
    /// On little-endian platforms (x86_64, ARM), dense arrays (targets, labels,
    /// weights) are zero-copy: DenseArray points directly into the archived
    /// buffer with no per-element parsing. On big-endian, falls back to full
    /// deserialization.
    fn from_rkyv_checkpoint(bytes: &[u8]) -> Option<Self> {
        let mut aligned = rkyv::util::AlignedVec::<16>::with_capacity(bytes.len());
        aligned.extend_from_slice(bytes);

        #[cfg(target_endian = "little")]
        {
            Self::from_rkyv_zero_copy(aligned)
        }
        #[cfg(not(target_endian = "little"))]
        {
            let snap: CsrSnapshotRkyv =
                rkyv::from_bytes::<CsrSnapshotRkyv, rkyv::rancor::Error>(&aligned).ok()?;
            Some(Self::from_snapshot_fields(snap))
        }
    }

    /// Zero-copy restore on little-endian platforms.
    ///
    /// SAFETY: On little-endian, rkyv's `u32_le`/`u16_le`/`f64_le` have
    /// identical memory layout to native `u32`/`u16`/`f64`. The pointer
    /// casts are sound because `ArchivedVec<T>` stores contiguous `T_le`
    /// values, and the `Arc<AlignedVec>` keeps the buffer alive.
    #[cfg(target_endian = "little")]
    fn from_rkyv_zero_copy(aligned: rkyv::util::AlignedVec) -> Option<Self> {
        use super::dense_array::DenseArray;

        let backing = std::sync::Arc::new(aligned);

        // Access archived data (zero-copy reference into the buffer).
        let archived =
            rkyv::access::<rkyv::Archived<CsrSnapshotRkyv>, rkyv::rancor::Error>(&backing).ok()?;

        // Zero-copy DenseArrays for dense CSR arrays.
        let out_targets = unsafe {
            let s = archived.out_targets.as_slice();
            DenseArray::zero_copy(backing.clone(), s.as_ptr().cast::<u32>(), s.len())
        };
        let out_labels = unsafe {
            let s = archived.out_labels.as_slice();
            DenseArray::zero_copy(backing.clone(), s.as_ptr().cast::<u32>(), s.len())
        };
        let in_targets = unsafe {
            let s = archived.in_targets.as_slice();
            DenseArray::zero_copy(backing.clone(), s.as_ptr().cast::<u32>(), s.len())
        };
        let in_labels = unsafe {
            let s = archived.in_labels.as_slice();
            DenseArray::zero_copy(backing.clone(), s.as_ptr().cast::<u32>(), s.len())
        };
        let out_weights = archived.out_weights.as_ref().map(|w| unsafe {
            let s = w.as_slice();
            DenseArray::zero_copy(backing.clone(), s.as_ptr().cast::<f64>(), s.len())
        });
        let in_weights = archived.in_weights.as_ref().map(|w| unsafe {
            let s = w.as_slice();
            DenseArray::zero_copy(backing.clone(), s.as_ptr().cast::<f64>(), s.len())
        });

        // Deserialize mutable/small fields (strings, buffers, offsets).
        let snap: CsrSnapshotRkyv =
            rkyv::from_bytes::<CsrSnapshotRkyv, rkyv::rancor::Error>(&backing).ok()?;

        let node_to_id: HashMap<String, u32> = snap
            .nodes
            .iter()
            .enumerate()
            .map(|(i, n)| (n.clone(), i as u32))
            .collect();
        let label_to_id: HashMap<String, u32> = snap
            .labels
            .iter()
            .enumerate()
            .map(|(i, l)| (l.clone(), i as u32))
            .collect();
        let node_count = snap.nodes.len();
        let access_counts = (0..node_count).map(|_| std::cell::Cell::new(0)).collect();
        let buffer_out_weights = if snap.buffer_out_weights.len() == node_count {
            snap.buffer_out_weights
        } else {
            vec![Vec::new(); node_count]
        };
        let buffer_in_weights = if snap.buffer_in_weights.len() == node_count {
            snap.buffer_in_weights
        } else {
            vec![Vec::new(); node_count]
        };
        let collection_to_id: HashMap<String, u32> = snap
            .collections
            .iter()
            .enumerate()
            .map(|(i, c)| (c.clone(), i as u32))
            .collect();
        let buffer_out_collections = if snap.buffer_out_collections.len() == node_count {
            snap.buffer_out_collections
        } else {
            vec![Vec::new(); node_count]
        };
        let buffer_in_collections = if snap.buffer_in_collections.len() == node_count {
            snap.buffer_in_collections
        } else {
            vec![Vec::new(); node_count]
        };

        Some(Self {
            node_to_id,
            id_to_node: snap.nodes,
            label_to_id,
            id_to_label: snap.labels,
            collection_to_id,
            id_to_collection: snap.collections,
            out_offsets: snap.out_offsets,
            out_targets,
            out_labels,
            out_collections: snap.out_collections,
            out_weights,
            in_offsets: snap.in_offsets,
            in_targets,
            in_labels,
            in_collections: snap.in_collections,
            in_weights,
            buffer_out: snap.buffer_out,
            buffer_in: snap.buffer_in,
            buffer_out_weights,
            buffer_in_weights,
            buffer_out_collections,
            buffer_in_collections,
            deleted_edges: snap.deleted.into_iter().collect(),
            has_weights: snap.has_weights,
            node_label_bits: vec![0; node_count],
            node_label_to_id: HashMap::new(),
            node_label_names: Vec::new(),
            // Surrogates are runtime-only and not persisted. After checkpoint
            // restore they start at zero and are repopulated by subsequent EdgePuts.
            node_surrogates: vec![0; node_count],
            surrogate_to_local: HashMap::new(),
            access_counts,
            query_epoch: 0,
            partition_tag: crate::csr::local_node_id::next_partition_tag(),
            // Checkpoint restore creates an ungoverned index; callers that
            // need budget enforcement should call `set_governor` afterwards.
            governor: None,
        })
    }

    /// Reconstruct CsrIndex from deserialized snapshot fields.
    #[cfg(not(target_endian = "little"))]
    fn from_snapshot_fields(snap: CsrSnapshotRkyv) -> Self {
        let node_to_id: HashMap<String, u32> = snap
            .nodes
            .iter()
            .enumerate()
            .map(|(i, n)| (n.clone(), i as u32))
            .collect();
        let label_to_id: HashMap<String, u32> = snap
            .labels
            .iter()
            .enumerate()
            .map(|(i, l)| (l.clone(), i as u32))
            .collect();

        let node_count = snap.nodes.len();
        let access_counts = (0..node_count).map(|_| std::cell::Cell::new(0)).collect();

        let buffer_out_weights = if snap.buffer_out_weights.len() == node_count {
            snap.buffer_out_weights
        } else {
            vec![Vec::new(); node_count]
        };
        let buffer_in_weights = if snap.buffer_in_weights.len() == node_count {
            snap.buffer_in_weights
        } else {
            vec![Vec::new(); node_count]
        };
        let collection_to_id: HashMap<String, u32> = snap
            .collections
            .iter()
            .enumerate()
            .map(|(i, c)| (c.clone(), i as u32))
            .collect();
        let buffer_out_collections = if snap.buffer_out_collections.len() == node_count {
            snap.buffer_out_collections
        } else {
            vec![Vec::new(); node_count]
        };
        let buffer_in_collections = if snap.buffer_in_collections.len() == node_count {
            snap.buffer_in_collections
        } else {
            vec![Vec::new(); node_count]
        };

        Self {
            node_to_id,
            id_to_node: snap.nodes,
            label_to_id,
            id_to_label: snap.labels,
            collection_to_id,
            id_to_collection: snap.collections,
            out_offsets: snap.out_offsets,
            out_targets: snap.out_targets.into(),
            out_labels: snap.out_labels.into(),
            out_collections: snap.out_collections,
            out_weights: snap.out_weights.map(Into::into),
            in_offsets: snap.in_offsets,
            in_targets: snap.in_targets.into(),
            in_labels: snap.in_labels.into(),
            in_collections: snap.in_collections,
            in_weights: snap.in_weights.map(Into::into),
            buffer_out: snap.buffer_out,
            buffer_in: snap.buffer_in,
            buffer_out_weights,
            buffer_in_weights,
            buffer_out_collections,
            buffer_in_collections,
            deleted_edges: snap.deleted.into_iter().collect(),
            has_weights: snap.has_weights,
            node_label_bits: vec![0; node_count],
            node_label_to_id: HashMap::new(),
            node_label_names: Vec::new(),
            // Surrogates are runtime-only and not persisted. After checkpoint
            // restore they start at zero and are repopulated by subsequent EdgePuts.
            node_surrogates: vec![0; node_count],
            surrogate_to_local: HashMap::new(),
            access_counts,
            query_epoch: 0,
            partition_tag: crate::csr::local_node_id::next_partition_tag(),
            // Checkpoint restore creates an ungoverned index.
            governor: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::csr::index::Direction;

    #[test]
    fn checkpoint_roundtrip_unweighted() {
        let mut csr = CsrIndex::new();
        csr.add_edge("a", "KNOWS", "b").unwrap();
        csr.add_edge("b", "KNOWS", "c").unwrap();
        csr.compact().expect("no governor, cannot fail");

        let bytes = csr.checkpoint_to_bytes().expect("no governor, cannot fail");
        let restored = CsrIndex::from_checkpoint(&bytes)
            .expect("roundtrip")
            .unwrap();
        assert_eq!(restored.node_count(), 3);
        assert_eq!(restored.edge_count(), 2);
        assert!(!restored.has_weights());

        let n = restored.neighbors("a", Some("KNOWS"), Direction::Out);
        assert_eq!(n.len(), 1);
        assert_eq!(n[0].1, "b");
    }

    #[test]
    fn checkpoint_roundtrip_weighted() {
        let mut csr = CsrIndex::new();
        csr.add_edge_weighted("a", "R", "b", 2.5).unwrap();
        csr.add_edge_weighted("b", "R", "c", 7.0).unwrap();
        csr.add_edge("c", "R", "d").unwrap();
        csr.compact().expect("no governor, cannot fail");

        let bytes = csr.checkpoint_to_bytes().expect("no governor, cannot fail");
        let restored = CsrIndex::from_checkpoint(&bytes)
            .expect("roundtrip")
            .unwrap();
        assert!(restored.has_weights());
        assert_eq!(restored.edge_weight("a", "R", "b"), Some(2.5));
        assert_eq!(restored.edge_weight("b", "R", "c"), Some(7.0));
        assert_eq!(restored.edge_weight("c", "R", "d"), Some(1.0));
    }

    #[test]
    fn checkpoint_roundtrip_with_buffer() {
        let mut csr = CsrIndex::new();
        csr.add_edge("a", "L", "b").unwrap();
        // Don't compact — edges in buffer.
        let bytes = csr.checkpoint_to_bytes().expect("no governor, cannot fail");
        let restored = CsrIndex::from_checkpoint(&bytes)
            .expect("roundtrip")
            .unwrap();
        assert_eq!(restored.edge_count(), 1);
    }

    #[test]
    fn golden_header_layout() {
        let mut csr = CsrIndex::new();
        csr.add_edge("a", "KNOWS", "b").unwrap();
        let bytes = csr.checkpoint_to_bytes().expect("no governor, cannot fail");
        // Magic at bytes[0..6].
        assert_eq!(&bytes[0..6], b"RKCS2\0");
        // Version byte at bytes[6].
        assert_eq!(bytes[6], super::CSR_FORMAT_VERSION);
        // rkyv payload follows immediately.
        assert!(bytes.len() > 7);
    }

    #[test]
    fn version_mismatch_returns_error() {
        let mut csr = CsrIndex::new();
        csr.add_edge("a", "KNOWS", "b").unwrap();
        let mut bytes = csr.checkpoint_to_bytes().expect("no governor, cannot fail");
        // Corrupt the version byte to an unsupported value.
        bytes[6] = 0;
        match CsrIndex::from_checkpoint(&bytes) {
            Err(CsrCheckpointError::UnsupportedVersion { found, expected }) => {
                assert_eq!(found, 0);
                assert_eq!(expected, super::CSR_FORMAT_VERSION);
            }
            Err(other) => panic!("unexpected error: {other}"),
            Ok(_) => panic!("expected UnsupportedVersion error, got Ok"),
        }
    }
}