overgraph 0.11.0

An absurdly fast embedded graph database. Pure Rust, sub-microsecond reads.
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use crate::error::EngineError;
use crate::segment_writer::SEGMENT_FORMAT_VERSION;
use crate::types::{validate_label_token_name, ManifestState, LABEL_TOKEN_SCHEMA_VERSION};
use std::fs;
use std::io::Write;
use std::path::Path;

const MANIFEST_CURRENT: &str = "manifest.current";
const MANIFEST_TMP: &str = "manifest.tmp";
const MANIFEST_PREV: &str = "manifest.prev";

/// Persist a ManifestState atomically:
/// 1. Write to manifest.tmp + fsync file
/// 2. If manifest.current exists, rename to manifest.prev
/// 3. Rename manifest.tmp → manifest.current
/// 4. fsync the directory to make the rename durable
pub(crate) fn write_manifest(db_dir: &Path, state: &ManifestState) -> Result<(), EngineError> {
    let tmp_path = db_dir.join(MANIFEST_TMP);
    let current_path = db_dir.join(MANIFEST_CURRENT);
    let prev_path = db_dir.join(MANIFEST_PREV);

    // 1. Write to tmp + fsync
    let json = serde_json::to_string_pretty(state)
        .map_err(|e| EngineError::ManifestError(format!("serialize: {}", e)))?;
    let mut file = fs::File::create(&tmp_path)?;
    file.write_all(json.as_bytes())?;
    file.sync_all()?;
    drop(file);

    // 2. If current exists, rename to prev (for rollback safety)
    if current_path.exists() {
        if prev_path.exists() {
            fs::remove_file(&prev_path)?;
        }
        fs::rename(&current_path, &prev_path)?;
    }

    // 3. Atomic rename tmp → current
    fs::rename(&tmp_path, &current_path)?;

    // 4. fsync the directory to make the rename durable
    fsync_dir(db_dir)?;

    Ok(())
}

/// Load a ManifestState from disk. Recovery priority:
/// 1. manifest.current (normal path)
/// 2. manifest.tmp (crash between rename steps; tmp has the newest state)
/// 3. manifest.prev (fallback if current is corrupt)
pub(crate) fn load_manifest(db_dir: &Path) -> Result<Option<ManifestState>, EngineError> {
    let current_path = db_dir.join(MANIFEST_CURRENT);
    let tmp_path = db_dir.join(MANIFEST_TMP);
    let prev_path = db_dir.join(MANIFEST_PREV);

    // Try current
    if let Some(state) = try_load_manifest_file(&current_path)? {
        return Ok(Some(state));
    }

    // Try tmp (crash between step 2 and step 3 in write_manifest)
    if let Some(state) = try_load_manifest_file(&tmp_path)? {
        // Promote tmp to current
        fs::rename(&tmp_path, &current_path)?;
        fsync_dir(db_dir)?;
        return Ok(Some(state));
    }

    // Fall back to prev
    if let Some(state) = try_load_manifest_file(&prev_path)? {
        // Promote prev to current for consistency
        write_manifest(db_dir, &state)?;
        return Ok(Some(state));
    }

    Ok(None)
}

fn try_load_manifest_file(path: &Path) -> Result<Option<ManifestState>, EngineError> {
    if !path.exists() {
        return Ok(None);
    }

    let content = fs::read_to_string(path)?;
    match serde_json::from_str::<ManifestState>(&content) {
        Ok(state) => {
            validate_manifest_identity(&state)?;
            Ok(Some(state))
        }
        Err(e) => {
            eprintln!("warning: corrupt manifest at {}: {}", path.display(), e);
            Ok(None)
        }
    }
}

fn validate_manifest_identity(state: &ManifestState) -> Result<(), EngineError> {
    validate_label_token_manifest(state)?;
    for segment in &state.segments {
        if segment.segment_format_version != SEGMENT_FORMAT_VERSION
            || segment.segment_data_id == [0; 32]
        {
            return Err(EngineError::ManifestError(format!(
                "unsupported segment manifest entry for segment {}; rebuild the database",
                segment.id
            )));
        }
    }
    Ok(())
}

fn validate_label_token_manifest(state: &ManifestState) -> Result<(), EngineError> {
    if state.label_token_schema_version == 0 {
        return Err(EngineError::ManifestError(
            "database manifest is missing label token schema; recreate the database".to_string(),
        ));
    }
    if state.label_token_schema_version != LABEL_TOKEN_SCHEMA_VERSION {
        return Err(EngineError::ManifestError(format!(
            "unsupported label token schema version: expected {}, got {}",
            LABEL_TOKEN_SCHEMA_VERSION, state.label_token_schema_version
        )));
    }
    validate_token_namespace(
        "node label",
        &state.node_label_tokens,
        state.next_node_label_id,
    )?;
    validate_token_namespace(
        "edge label",
        &state.edge_label_tokens,
        state.next_edge_label_id,
    )?;
    Ok(())
}

fn validate_token_namespace(
    namespace: &str,
    tokens: &std::collections::BTreeMap<String, u32>,
    next_id: u32,
) -> Result<(), EngineError> {
    let mut ids = std::collections::BTreeMap::new();
    let mut max_id = 0u32;
    for (name, &label_id) in tokens {
        if let Err(error) = validate_label_token_name(name) {
            return Err(EngineError::ManifestError(format!(
                "{namespace} token name '{name}' is invalid: {error}"
            )));
        }
        if label_id == 0 {
            return Err(EngineError::ManifestError(format!(
                "{namespace} token '{name}' uses reserved label_id 0"
            )));
        }
        if let Some(existing_name) = ids.insert(label_id, name) {
            return Err(EngineError::ManifestError(format!(
                "{namespace} token conflict: label_id {label_id} is assigned to both '{existing_name}' and '{name}'"
            )));
        }
        max_id = max_id.max(label_id);
    }
    if next_id <= max_id {
        return Err(EngineError::ManifestError(format!(
            "{namespace} next token id {next_id} must be greater than max assigned id {max_id}"
        )));
    }
    Ok(())
}

/// fsync a directory to make rename operations durable.
/// No-op on Windows. NTFS doesn't support directory fsync via File::open().
fn fsync_dir(dir: &Path) -> Result<(), EngineError> {
    #[cfg(not(target_os = "windows"))]
    {
        let d = fs::File::open(dir)?;
        d.sync_all()?;
    }
    #[cfg(target_os = "windows")]
    let _ = dir;
    Ok(())
}

/// Diagnostic read-only manifest load.
///
/// This uses the same priority chain as `load_manifest` but never writes to
/// disk, so diagnostic tooling can inspect a live or crashed database without
/// side effects. The returned `ManifestState` is a raw manifest view and may
/// include internal numeric token IDs; ordinary graph APIs use names instead.
pub fn load_manifest_readonly(db_dir: &Path) -> Result<Option<ManifestState>, EngineError> {
    let current_path = db_dir.join(MANIFEST_CURRENT);
    let tmp_path = db_dir.join(MANIFEST_TMP);
    let prev_path = db_dir.join(MANIFEST_PREV);

    if let Some(state) = try_load_manifest_file(&current_path)? {
        return Ok(Some(state));
    }
    if let Some(state) = try_load_manifest_file(&tmp_path)? {
        return Ok(Some(state));
    }
    if let Some(state) = try_load_manifest_file(&prev_path)? {
        return Ok(Some(state));
    }
    Ok(None)
}

/// Create a fresh default manifest state.
pub(crate) fn default_manifest() -> ManifestState {
    ManifestState {
        version: 1,
        label_token_schema_version: LABEL_TOKEN_SCHEMA_VERSION,
        node_label_tokens: std::collections::BTreeMap::new(),
        edge_label_tokens: std::collections::BTreeMap::new(),
        next_node_label_id: 1,
        next_edge_label_id: 1,
        segments: Vec::new(),
        next_node_id: 1,
        next_edge_id: 1,
        dense_vector: None,
        prune_policies: std::collections::BTreeMap::new(),
        next_engine_seq: 0,
        next_wal_generation_id: 0,
        active_wal_generation_id: 0,
        pending_flush_epochs: Vec::new(),
        secondary_indexes: Vec::new(),
        next_secondary_index_id: 1,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::SegmentInfo;
    use tempfile::TempDir;

    #[test]
    fn test_write_and_load_manifest() {
        let dir = TempDir::new().unwrap();
        let state = ManifestState {
            version: 1,
            segments: vec![SegmentInfo {
                id: 1,
                node_count: 100,
                edge_count: 200,
                segment_format_version: 10,
                segment_data_id: [1; 32],
            }],
            next_node_id: 101,
            next_edge_id: 201,
            ..default_manifest()
        };

        write_manifest(dir.path(), &state).unwrap();
        let loaded = load_manifest(dir.path()).unwrap().unwrap();

        assert_eq!(loaded.version, 1);
        assert_eq!(loaded.segments.len(), 1);
        assert_eq!(loaded.segments[0].id, 1);
        assert_eq!(loaded.next_node_id, 101);
        assert_eq!(loaded.next_edge_id, 201);
    }

    #[test]
    fn test_load_nonexistent_manifest() {
        let dir = TempDir::new().unwrap();
        let result = load_manifest(dir.path()).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_manifest_preserves_prev() {
        let dir = TempDir::new().unwrap();

        let state1 = ManifestState {
            next_node_id: 10,
            next_edge_id: 20,
            ..default_manifest()
        };
        write_manifest(dir.path(), &state1).unwrap();

        let state2 = ManifestState {
            next_node_id: 50,
            next_edge_id: 100,
            ..default_manifest()
        };
        write_manifest(dir.path(), &state2).unwrap();

        let loaded = load_manifest(dir.path()).unwrap().unwrap();
        assert_eq!(loaded.next_node_id, 50);
        assert!(dir.path().join(MANIFEST_PREV).exists());
    }

    #[test]
    fn test_manifest_fallback_to_prev() {
        let dir = TempDir::new().unwrap();

        let state = ManifestState {
            next_node_id: 42,
            next_edge_id: 84,
            ..default_manifest()
        };
        write_manifest(dir.path(), &state).unwrap();

        let state2 = ManifestState {
            next_node_id: 99,
            next_edge_id: 199,
            ..default_manifest()
        };
        write_manifest(dir.path(), &state2).unwrap();

        // Corrupt current
        let current_path = dir.path().join(MANIFEST_CURRENT);
        fs::write(&current_path, "NOT VALID JSON {{{").unwrap();

        let loaded = load_manifest(dir.path()).unwrap().unwrap();
        assert_eq!(loaded.next_node_id, 42);
    }

    #[test]
    fn test_manifest_recovery_from_tmp() {
        let dir = TempDir::new().unwrap();

        // Simulate crash: only manifest.tmp exists (crash between rename steps)
        let state = ManifestState {
            next_node_id: 77,
            next_edge_id: 88,
            ..default_manifest()
        };
        let json = serde_json::to_string_pretty(&state).unwrap();
        let tmp_path = dir.path().join(MANIFEST_TMP);
        fs::write(&tmp_path, &json).unwrap();

        let loaded = load_manifest(dir.path()).unwrap().unwrap();
        assert_eq!(loaded.next_node_id, 77);

        // tmp should be promoted to current
        assert!(dir.path().join(MANIFEST_CURRENT).exists());
        assert!(!dir.path().join(MANIFEST_TMP).exists());
    }

    #[test]
    fn test_default_manifest() {
        let m = default_manifest();
        assert_eq!(m.version, 1);
        assert_eq!(m.label_token_schema_version, LABEL_TOKEN_SCHEMA_VERSION);
        assert!(m.node_label_tokens.is_empty());
        assert!(m.edge_label_tokens.is_empty());
        assert_eq!(m.next_node_label_id, 1);
        assert_eq!(m.next_edge_label_id, 1);
        assert!(m.segments.is_empty());
        assert_eq!(m.next_node_id, 1);
        assert_eq!(m.next_edge_id, 1);
        assert!(m.dense_vector.is_none());
        assert!(m.prune_policies.is_empty());
        assert!(m.secondary_indexes.is_empty());
        assert_eq!(m.next_secondary_index_id, 1);
    }

    #[test]
    fn test_load_manifest_missing_dense_vector_defaults_to_none() {
        let dir = TempDir::new().unwrap();
        let legacy_json = r#"{
  "version": 1,
  "label_token_schema_version": 1,
  "node_label_tokens": {},
  "edge_label_tokens": {},
  "next_node_label_id": 1,
  "next_edge_label_id": 1,
  "segments": [],
  "next_node_id": 10,
  "next_edge_id": 20,
  "prune_policies": {}
}"#;
        fs::write(dir.path().join(MANIFEST_CURRENT), legacy_json).unwrap();

        let loaded = load_manifest(dir.path()).unwrap().unwrap();
        assert_eq!(loaded.next_node_id, 10);
        assert!(loaded.dense_vector.is_none());
    }

    #[test]
    fn test_load_manifest_readonly_does_not_write() {
        use super::load_manifest_readonly;

        let dir = TempDir::new().unwrap();

        // Simulate crash: only manifest.tmp exists
        let state = ManifestState {
            next_node_id: 77,
            next_edge_id: 88,
            ..default_manifest()
        };
        let json = serde_json::to_string_pretty(&state).unwrap();
        let tmp_path = dir.path().join(MANIFEST_TMP);
        fs::write(&tmp_path, &json).unwrap();

        // Read-only load should find it via tmp
        let loaded = load_manifest_readonly(dir.path()).unwrap().unwrap();
        assert_eq!(loaded.next_node_id, 77);

        // But should NOT have promoted tmp to current
        assert!(dir.path().join(MANIFEST_TMP).exists());
        assert!(!dir.path().join(MANIFEST_CURRENT).exists());
    }

    #[test]
    fn test_manifest_atomic_no_partial_write() {
        let dir = TempDir::new().unwrap();
        let state = ManifestState {
            next_node_id: 5,
            next_edge_id: 10,
            ..default_manifest()
        };
        write_manifest(dir.path(), &state).unwrap();

        assert!(!dir.path().join(MANIFEST_TMP).exists());
        assert!(dir.path().join(MANIFEST_CURRENT).exists());
    }

    #[test]
    fn test_load_manifest_missing_secondary_index_fields_defaults_cleanly() {
        let dir = TempDir::new().unwrap();
        let legacy_json = r#"{
  "version": 1,
  "label_token_schema_version": 1,
  "node_label_tokens": {},
  "edge_label_tokens": {},
  "next_node_label_id": 1,
  "next_edge_label_id": 1,
  "segments": [],
  "next_node_id": 10,
  "next_edge_id": 20,
  "prune_policies": {},
  "next_engine_seq": 0,
  "next_wal_generation_id": 0,
  "active_wal_generation_id": 0,
  "pending_flush_epochs": []
}"#;
        fs::write(dir.path().join(MANIFEST_CURRENT), legacy_json).unwrap();

        let loaded = load_manifest(dir.path()).unwrap().unwrap();
        assert!(loaded.secondary_indexes.is_empty());
        assert_eq!(loaded.next_secondary_index_id, 0);
    }

    #[test]
    fn test_load_manifest_missing_label_token_schema_rejected() {
        let dir = TempDir::new().unwrap();
        let legacy_json = r#"{
  "version": 1,
  "segments": [],
  "next_node_id": 10,
  "next_edge_id": 20,
  "prune_policies": {}
}"#;
        fs::write(dir.path().join(MANIFEST_CURRENT), legacy_json).unwrap();

        let err = load_manifest(dir.path()).unwrap_err();
        assert!(err.to_string().contains("missing label token schema"));
    }

    #[test]
    fn test_load_manifest_rejects_label_token_reverse_conflict() {
        let dir = TempDir::new().unwrap();
        let mut state = default_manifest();
        state.node_label_tokens.insert("Person".to_string(), 1);
        state.node_label_tokens.insert("Company".to_string(), 1);
        state.next_node_label_id = 2;
        write_manifest(dir.path(), &state).unwrap();

        let current_path = dir.path().join(MANIFEST_CURRENT);
        let err = try_load_manifest_file(&current_path).unwrap_err();
        assert!(err.to_string().contains("token conflict"));
    }

    #[test]
    fn test_load_manifest_rejects_label_token_next_id_not_above_max() {
        let dir = TempDir::new().unwrap();
        let mut state = default_manifest();
        state.edge_label_tokens.insert("KNOWS".to_string(), 3);
        state.next_edge_label_id = 3;
        write_manifest(dir.path(), &state).unwrap();

        let current_path = dir.path().join(MANIFEST_CURRENT);
        let err = try_load_manifest_file(&current_path).unwrap_err();
        assert!(err
            .to_string()
            .contains("must be greater than max assigned"));
    }

    #[test]
    fn test_load_manifest_rejects_invalid_label_token_names() {
        let dir = TempDir::new().unwrap();
        let mut state = default_manifest();
        state.node_label_tokens.insert(" Person".to_string(), 1);
        state.next_node_label_id = 2;
        write_manifest(dir.path(), &state).unwrap();

        let current_path = dir.path().join(MANIFEST_CURRENT);
        let err = try_load_manifest_file(&current_path).unwrap_err();
        assert!(err.to_string().contains("token name"));
        assert!(err.to_string().contains("invalid"));
    }

    #[test]
    fn test_manifest_round_trip_node_and_edge_secondary_indexes() {
        let dir = TempDir::new().unwrap();
        let state = ManifestState {
            next_secondary_index_id: 4,
            secondary_indexes: vec![
                crate::types::SecondaryIndexManifestEntry {
                    index_id: 0,
                    target: crate::types::SecondaryIndexTarget::NodeProperty {
                        label_id: 1,
                        prop_key: "color".to_string(),
                    },
                    kind: crate::types::SecondaryIndexKind::Equality,
                    state: crate::types::SecondaryIndexState::Building,
                    last_error: None,
                },
                crate::types::SecondaryIndexManifestEntry {
                    index_id: 1,
                    target: crate::types::SecondaryIndexTarget::EdgeProperty {
                        label_id: 2,
                        prop_key: "weight".to_string(),
                    },
                    kind: crate::types::SecondaryIndexKind::Range,
                    state: crate::types::SecondaryIndexState::Building,
                    last_error: None,
                },
                crate::types::SecondaryIndexManifestEntry {
                    index_id: 2,
                    target: crate::types::SecondaryIndexTarget::EdgeProperty {
                        label_id: 1,
                        prop_key: "label".to_string(),
                    },
                    kind: crate::types::SecondaryIndexKind::Equality,
                    state: crate::types::SecondaryIndexState::Ready,
                    last_error: None,
                },
            ],
            ..default_manifest()
        };
        write_manifest(dir.path(), &state).unwrap();

        let raw_manifest = fs::read_to_string(dir.path().join(MANIFEST_CURRENT)).unwrap();
        assert!(raw_manifest.contains("\"label_id\""));
        assert!(!raw_manifest.contains(concat!("\"", "type", "_id\"")));

        let loaded = load_manifest(dir.path()).unwrap().unwrap();
        assert_eq!(loaded.secondary_indexes.len(), 3);
        assert_eq!(loaded.next_secondary_index_id, 4);
        assert!(matches!(
            &loaded.secondary_indexes[0].target,
            crate::types::SecondaryIndexTarget::NodeProperty { label_id: 1, .. }
        ));
        assert!(matches!(
            &loaded.secondary_indexes[1].target,
            crate::types::SecondaryIndexTarget::EdgeProperty { label_id: 2, .. }
        ));
        assert_eq!(loaded.secondary_indexes[1].index_id, 1);
        assert!(matches!(
            loaded.secondary_indexes[1].kind,
            crate::types::SecondaryIndexKind::Range
        ));
        assert!(matches!(
            &loaded.secondary_indexes[2].target,
            crate::types::SecondaryIndexTarget::EdgeProperty { label_id: 1, .. }
        ));
        assert_eq!(
            loaded.secondary_indexes[2].state,
            crate::types::SecondaryIndexState::Ready
        );
    }

    #[test]
    fn test_load_manifest_rejects_legacy_domainful_range_index_kind() {
        let dir = TempDir::new().unwrap();
        let state = ManifestState {
            next_secondary_index_id: 2,
            secondary_indexes: vec![crate::types::SecondaryIndexManifestEntry {
                index_id: 1,
                target: crate::types::SecondaryIndexTarget::NodeProperty {
                    label_id: 1,
                    prop_key: "score".to_string(),
                },
                kind: crate::types::SecondaryIndexKind::Range,
                state: crate::types::SecondaryIndexState::Ready,
                last_error: None,
            }],
            ..default_manifest()
        };
        let json = serde_json::to_string_pretty(&state).unwrap();
        let legacy_json = json.replace(
            "\"kind\": \"Range\"",
            "\"kind\": { \"Range\": { \"domain\": \"Int\" } }",
        );
        assert_ne!(json, legacy_json);
        fs::write(dir.path().join(MANIFEST_CURRENT), legacy_json).unwrap();

        assert!(try_load_manifest_file(&dir.path().join(MANIFEST_CURRENT))
            .unwrap()
            .is_none());
    }
}