hawdb 0.18.0

An embedded Rust graph database for local data-plane workloads
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
// Copyright 2026 Nowledge
// SPDX-License-Identifier: Apache-2.0

//! Physical cuts in the fixed-projector graph/visibility publication closure.

use super::super::search_projection::same_physical_image;
use super::*;
use hawdb_storage::file_io::SourceDirectory;
use std::cell::Cell;
use std::rc::Rc;
use std::sync::Arc;

mod recovery;

const LEDGER: &str = "consumer-allocations.hawdb";

#[derive(Debug, Clone, Copy)]
enum Cut {
    Link,
    CandidateDirectory(ObservationBoundary),
    SelectorWrite,
    SelectorRename(ObservationBoundary),
    SelectorDirectory(ObservationBoundary),
}

fn ledger(root: &Path) -> serde_json::Value {
    let bytes = std::fs::read(root.join(LEDGER)).unwrap();
    let start = bytes.iter().position(|byte| *byte == b'\n').unwrap() + 1;
    serde_json::from_slice(&bytes[start..bytes.len() - 4]).unwrap()
}

fn is_candidate(path: &Path) -> bool {
    path.components().next().is_some_and(|component| {
        let component = component.as_os_str().to_string_lossy();
        component == "projection" || component.starts_with("hawdb-generation-")
    })
}

fn assert_preserved_artifacts(
    root: &Path,
    physical: &hawdb_storage::power_loss::image::CrashImage,
) {
    for path in physical.paths().filter(|path| is_candidate(path)) {
        let expected = physical.bytes(path).unwrap();
        let mut file = std::fs::File::open(root.join(path)).unwrap();
        assert_eq!(file.metadata().unwrap().len(), expected.len() as u64);
        // Windows locks can cover bytes past an empty lease file's EOF. Check
        // its length and read exactly the expected bytes without an EOF probe.
        let mut actual = vec![0; expected.len()];
        std::io::Read::read_exact(&mut file, &mut actual).unwrap();
        assert_eq!(
            actual, expected,
            "recovery changed Consumer evidence at {path:?}"
        );
    }
}

fn selected_ids(
    consumer: &SearchProjectionConsumer,
    permit: &hawdb_qos::RuntimePermit,
) -> Vec<String> {
    let pin = consumer.pin(permit).unwrap();
    let output = pin
        .query_vector_index(
            permit,
            Default::default(),
            hawdb_search::VectorIndexQuery {
                descriptor: consumer.vector_index_descriptor().unwrap().unwrap(),
                mode: hawdb_core::VectorIndexQueryMode::AnnWindow,
                eligible: None,
                embedding: &[1.0, 0.0],
                candidate_limit: NonZeroUsize::new(8).unwrap(),
                top_k: NonZeroUsize::new(8).unwrap(),
            },
        )
        .unwrap();
    output.hits().iter().map(|hit| hit.id.clone()).collect()
}

fn mutate(database: &mut Database, graph: bool) {
    let embedding = Value::Vector(Vector::new(&[0.0, 1.0]).unwrap());
    let inserted = Value::Vector(Vector::new(&[1.0, 0.0]).unwrap());
    let mut transaction = database.begin_transaction().unwrap();
    if graph {
        transaction
            .query_with_params(
                "MATCH (d:Document {id: 1}) SET d.embedding = $embedding",
                &BTreeMap::from([("embedding".into(), embedding)]),
            )
            .unwrap();
        transaction
            .query("MATCH (d:Document {id: 2}) DELETE d")
            .unwrap();
        transaction
            .query("MATCH (d:Document {id: 3}) SET d.embedding = NULL")
            .unwrap();
        transaction
            .query_with_params(
                "CREATE (:Document {id: 4, embedding: $embedding})",
                &BTreeMap::from([("embedding".into(), inserted)]),
            )
            .unwrap();
    } else {
        transaction
            .query_sql_with_params(
                "UPDATE vectors SET embedding = $1 WHERE id = 1",
                &[embedding],
            )
            .unwrap();
        transaction
            .query_sql("DELETE FROM vectors WHERE id = 2")
            .unwrap();
        transaction
            .query_sql("UPDATE vectors SET embedding = NULL WHERE id = 3")
            .unwrap();
        transaction
            .query_sql_with_params(
                "INSERT INTO vectors (id, embedding) VALUES (4, $1)",
                &[inserted],
            )
            .unwrap();
    }
    transaction.commit().unwrap();
}

fn assert_source(database: &mut Database, graph: bool, updated: bool) {
    let rows = if graph {
        database
            .query("MATCH (d:Document) RETURN d.id AS id, d.embedding AS embedding ORDER BY id")
            .unwrap()
    } else {
        database
            .query_sql("SELECT id, embedding FROM vectors ORDER BY id")
            .unwrap()
    }
    .rows;
    let expected = if updated {
        vec![(1, Some([0.0, 1.0])), (3, None), (4, Some([1.0, 0.0]))]
    } else {
        vec![
            (1, Some([1.0, 0.0])),
            (2, Some([0.0, 1.0])),
            (3, Some([-1.0, 0.0])),
        ]
    };
    assert_eq!(
        rows,
        expected
            .into_iter()
            .map(|(id, vector)| BTreeMap::from([
                ("id".into(), Value::Int(id)),
                (
                    "embedding".into(),
                    vector.map_or(Value::Null, |vector| Value::Vector(
                        Vector::new(&vector).unwrap()
                    ))
                ),
            ]))
            .collect::<Vec<_>>()
    );
}

fn plans(snapshot: &PowerLossSnapshot, cut: Cut) -> (Vec<CrashPlan>, usize) {
    plans_with_torn_writes(
        snapshot,
        matches!(cut, Cut::SelectorWrite).then_some("consumer-selector-"),
    )
}

fn plans_with_torn_writes(
    snapshot: &PowerLossSnapshot,
    prefix: Option<&str>,
) -> (Vec<CrashPlan>, usize) {
    let mut plans = publication_fault_plans(snapshot);
    let mut torn = 0;
    if let Some(prefix) = prefix {
        let path = snapshot.observed_path().unwrap();
        assert!(path
            .file_name()
            .unwrap()
            .to_str()
            .unwrap()
            .starts_with(prefix));
        let writes = snapshot.uncovered_writes(path).unwrap();
        assert!(!writes.is_empty());
        for write in writes {
            assert!(write.length > 1);
            for bytes in [0..write.length / 2, write.length / 2..write.length] {
                let mut plan = snapshot.persist_all_plan();
                let operation = plan
                    .persistence
                    .iter_mut()
                    .find(|operation| **operation == PersistOperation::Whole(write.operation))
                    .unwrap();
                *operation = PersistOperation::TornWrite {
                    operation: write.operation,
                    bytes,
                };
                plans.push(plan.clone());
                plan.persistence.reverse();
                plans.push(plan);
                torn += 2;
            }
        }
    }
    (plans, torn)
}

fn check(cut: Cut, durability: DurabilityPolicy, graph: bool) {
    check_recovery(cut, durability, graph, None, false);
}

fn check_recovery(
    cut: Cut,
    durability: DurabilityPolicy,
    graph: bool,
    recovery: Option<recovery::Cut>,
    lose_source: bool,
) {
    let root = std::fs::canonicalize(std::env::temp_dir())
        .unwrap()
        .join(format!(
            "hawdb-vector-publication-power-{}",
            hawdb_core::generate_uuidv7().unwrap()
        ));
    let original = MaterializedImage(root.clone());
    let project = ProjectFileDescriptors::acquire_with_capacity_limit(&root, 32, true).unwrap();
    let model = Arc::new(PowerLossModel::attach(&project, ImageLimits::default()).unwrap());
    let mut database = open(&root, durability);
    let field = create_source(&mut database, graph);
    for (id, vector) in [(1, [1.0, 0.0]), (2, [0.0, 1.0]), (3, [-1.0, 0.0])] {
        insert(&mut database, graph, id, vector);
    }
    let target = root.join("projection");
    let id = SearchProjectionConsumerId::new("vector-power").unwrap();
    let mut consumer = database
        .create_vector_index(
            "vector-power",
            &field,
            &target,
            SearchProjectionConsumerOptions::new(NonZeroU64::new(100).unwrap()),
            Default::default(),
        )
        .unwrap();
    let c = database.commit_epoch().unwrap();
    let initial_operation = consumer
        .projection()
        .stationary_receipt()
        .unwrap()
        .binding()
        .operation
        .get();
    let before = source_keys(&mut database, graph);
    let permit = database
        .search_projection_consumer_read_permit(
            RuntimeWorkRequest::foreground_query(64 << 20, 1 << 20).with_io_wave_slots(1),
        )
        .unwrap();
    let old = database
        .begin_read_transaction()
        .unwrap()
        .bind_vector_index(&consumer, &permit)
        .unwrap();
    mutate(&mut database, graph);
    let e = database.commit_epoch().unwrap();
    let after = source_keys(&mut database, graph);
    let armed = Rc::new(Cell::new(false));
    let observed = Rc::clone(&armed);
    let recorder = Arc::clone(&model);
    let observed_root = root.clone();
    SourceDirectory::observe_control_renames(
        move || {
            if observed.get() {
                return;
            }
            let value = ledger(&observed_root);
            let Some(row) = value["pending"]
                .as_array()
                .unwrap()
                .iter()
                .find(|row| row["operation"].as_u64().unwrap() > initial_operation)
            else {
                return;
            };
            let selected = value["selected"]
                .as_array()
                .unwrap()
                .iter()
                .any(|selected| selected["operation"] == row["operation"]);
            let (event, relative_path, boundary, include_descendants) = match cut {
                Cut::Link | Cut::CandidateDirectory(_)
                    if row["native_birth"]["state"] == "Recorded"
                        && (!matches!(cut, Cut::CandidateDirectory(_))
                            || row["candidate_seal"].is_object()) =>
                {
                    let parent = Path::new(row["native_birth"]["parent_locator"].as_str().unwrap());
                    let path = parent
                        .join(row["native_birth"]["generation_name"].as_str().unwrap())
                        .strip_prefix(&observed_root)
                        .unwrap()
                        .to_path_buf();
                    match cut {
                        Cut::Link => (
                            IoEvent::HardLink,
                            path.join("search_projection.out_of_core.manifest.hawdb"),
                            ObservationBoundary::After,
                            false,
                        ),
                        Cut::CandidateDirectory(boundary) => {
                            (IoEvent::DirectorySync, path, boundary, false)
                        }
                        _ => unreachable!(),
                    }
                }
                Cut::SelectorWrite if row["publication_intent"].is_object() => (
                    IoEvent::Write,
                    PathBuf::new(),
                    ObservationBoundary::After,
                    true,
                ),
                Cut::SelectorRename(boundary) if row["publication_intent"].is_object() => {
                    (IoEvent::Rename, PathBuf::new(), boundary, true)
                }
                Cut::SelectorDirectory(boundary) if selected => {
                    (IoEvent::DirectorySync, PathBuf::new(), boundary, false)
                }
                _ => return,
            };
            recorder
                .observe(ObservationPoint {
                    event,
                    relative_path,
                    boundary,
                    skip_matches: 0,
                    include_descendants,
                    keep_last: false,
                })
                .unwrap();
            observed.set(true);
        },
        || {
            database
                .catch_up_vector_index(&mut consumer, Default::default())
                .unwrap()
        },
    )
    .unwrap();
    assert!(armed.get(), "{cut:?}");
    let acknowledged = model.capture().unwrap();
    let snapshot = model
        .take_observation()
        .unwrap()
        .expect("the actual native publication boundary must execute");
    if matches!(cut, Cut::SelectorRename(_)) {
        assert!(snapshot
            .observed_path()
            .unwrap()
            .file_name()
            .unwrap()
            .to_str()
            .unwrap()
            .starts_with("consumer-selector-"));
    }
    assert_eq!(
        ids(&old, &permit),
        vec![before[&1].clone(), before[&2].clone(), before[&3].clone()]
    );
    assert_eq!(
        current_ids(&mut database, &consumer),
        vec![after[&4].clone(), after[&1].clone()]
    );
    if let Cut::CandidateDirectory(boundary) = cut {
        let manifest = snapshot
            .observed_path()
            .unwrap()
            .join("search_projection.out_of_core.manifest.hawdb");
        assert!(
            snapshot
                .crash(&snapshot.persist_all_plan())
                .unwrap()
                .bytes(&manifest)
                .is_some(),
            "candidate directory cut must retain its sealed artifacts: {boundary:?}"
        );
    }
    let (mut schedules, torn) = if recovery.is_some() {
        // Each recovery case begins at one exact surviving physical image.
        // The ordinary family above still evaluates every first-crash plan.
        (
            vec![if lose_source {
                CrashPlan::default()
            } else {
                snapshot.persist_all_plan()
            }],
            0,
        )
    } else {
        plans(&snapshot, cut)
    };
    assert_eq!(
        torn > 0,
        recovery.is_none() && matches!(cut, Cut::SelectorWrite)
    );
    eprintln!("canonical-vector-power-v1 cut={cut:?} durability={durability:?} graph={graph} plans={} torn={torn}", schedules.len());
    let complete = acknowledged.crash(&CrashPlan::default()).unwrap();
    drop(old);
    drop(permit);
    drop(consumer);
    drop(database);
    assert_eq!(project.metrics().admitted_runtimes, 0);
    drop(model);
    drop(project);
    drop(original);
    if recovery.is_none() {
        schedules.push(CrashPlan::default());
    }
    let total = schedules.len();
    let mut seen = Vec::new();
    let mut replayed = 0;
    for (ordinal, plan) in schedules.into_iter().enumerate() {
        let acknowledged = recovery.is_none() && ordinal + 1 == total;
        let physical = if acknowledged {
            complete.clone()
        } else {
            snapshot.crash(&plan).unwrap()
        };
        // Every plan is evaluated. Full namespace, inode-alias and byte
        // equality means the same reopen input, including private evidence.
        if !acknowledged
            && seen
                .iter()
                .any(|previous| same_physical_image(previous, &physical))
        {
            continue;
        }
        seen.push(physical.clone());
        let image = MaterializedImage(root.clone());
        physical.materialize(&image).unwrap();
        let recovery_project = recovery
            .map(|_| ProjectFileDescriptors::acquire_with_capacity_limit(&root, 32, true).unwrap());
        let recovery_model = recovery_project.as_ref().map(|project| {
            PowerLossModel::resume_from_image(project, &physical, ImageLimits::default()).unwrap()
        });
        replayed += 1;
        let mut recovered = open(&image, durability);
        if let Some(recovery_cut) = recovery {
            recovery_cut.observe(recovery_model.as_ref().unwrap(), matches!(cut, Cut::Link));
        }
        let source_epoch = recovered.commit_epoch().unwrap();
        if lose_source {
            assert_eq!(durability, DurabilityPolicy::SyncOnCheckpoint);
            assert_eq!(
                source_epoch, c,
                "the relaxed E transaction must actually be lost"
            );
        }
        assert!(
            source_epoch == c || source_epoch == e,
            "{cut:?}: source={source_epoch} C={c} E={e}"
        );
        // A private selector is sealed during preparation, before the host
        // synchronizes canonical E. Only selection follows that source barrier.
        if durability == DurabilityPolicy::SyncOnEveryWrite
            || acknowledged
            || matches!(cut, Cut::SelectorRename(_) | Cut::SelectorDirectory(_))
            || plan == snapshot.persist_all_plan()
        {
            assert_eq!(source_epoch, e);
        }
        assert_source(&mut recovered, graph, source_epoch == e);
        let mut consumer = recovered
            .open_search_projection_consumer(&id, &target)
            .unwrap_or_else(|error| {
                panic!("{cut:?} {durability:?} graph={graph} plan={plan:?}: {error:?}")
            });
        let index_epoch = consumer.canonical_vector_source_epoch().unwrap().unwrap();
        let receipt = consumer.projection().stationary_receipt().unwrap().clone();
        let recovery_snapshot = recovery.map(|_| {
            let model = recovery_model.as_ref().unwrap();
            // Complete native traversal proves recovery has no unrecorded IO.
            let acknowledged = model.capture().unwrap();
            (
                model
                    .take_observation()
                    .unwrap()
                    .expect("recovery must execute the actual publication boundary"),
                acknowledged,
            )
        });
        // Logical recovery must preserve every existing candidate byte. It
        // grants no right to adopt a partial producer or delete unknown files.
        assert_preserved_artifacts(&root, &physical);
        assert!(index_epoch == c || index_epoch == e);
        assert!(index_epoch <= source_epoch);
        if acknowledged || matches!(cut, Cut::SelectorDirectory(ObservationBoundary::After)) {
            assert_eq!(index_epoch, e);
        } else if matches!(
            cut,
            Cut::Link
                | Cut::CandidateDirectory(_)
                | Cut::SelectorWrite
                | Cut::SelectorRename(ObservationBoundary::Before)
        ) {
            assert_eq!(index_epoch, c);
        }
        let admission = recovered
            .search_projection_consumer_read_permit(
                RuntimeWorkRequest::foreground_query(64 << 20, 1 << 20).with_io_wave_slots(1),
            )
            .unwrap();
        let expected = if index_epoch == c {
            vec![before[&1].clone(), before[&2].clone(), before[&3].clone()]
        } else {
            vec![after[&4].clone(), after[&1].clone()]
        };
        assert_eq!(selected_ids(&consumer, &admission), expected);
        if index_epoch != source_epoch {
            assert!(matches!(
                recovered
                    .begin_read_transaction()
                    .unwrap()
                    .bind_vector_index(&consumer, &admission),
                Err(crate::VectorIndexReadError::SourceEpochMismatch { .. })
            ));
            recovered
                .catch_up_vector_index(&mut consumer, Default::default())
                .unwrap();
        }
        assert_eq!(
            current_ids(&mut recovered, &consumer),
            if source_epoch == c {
                vec![before[&1].clone(), before[&2].clone(), before[&3].clone()]
            } else {
                vec![after[&4].clone(), after[&1].clone()]
            }
        );
        drop(consumer);
        drop(recovered);
        drop(admission);
        drop(recovery_model);
        drop(recovery_project);
        drop(image);
        if let Some((snapshot, acknowledged)) = recovery_snapshot {
            recovery.unwrap().replay(
                &snapshot,
                &acknowledged,
                recovery::Context {
                    origin: cut,
                    root: &root,
                    target: &target,
                    id: &id,
                    durability,
                    graph,
                    source_epoch,
                    source_updated: source_epoch == e,
                    index_epoch,
                    expected: &expected,
                    original: &physical,
                    receipt: &receipt,
                },
                matches!(cut, Cut::Link),
            );
        }
    }
    eprintln!("canonical-vector-power-replay-v1 cut={cut:?} durability={durability:?} graph={graph} plans={total} distinct_reopens={replayed}");
}

fn qualify(cuts: &[Cut]) {
    for &cut in cuts {
        for durability in [
            DurabilityPolicy::SyncOnEveryWrite,
            DurabilityPolicy::SyncOnCheckpoint,
        ] {
            for graph in [false, true] {
                check(cut, durability, graph);
            }
        }
    }
}

#[test]
fn vector_index_incremental_power_loss_retains_the_linked_artifact_closure() {
    qualify(&[
        Cut::Link,
        Cut::CandidateDirectory(ObservationBoundary::Before),
        Cut::CandidateDirectory(ObservationBoundary::After),
    ]);
}

#[test]
fn vector_index_incremental_power_loss_rejects_torn_private_selectors() {
    qualify(&[Cut::SelectorWrite]);
}

#[test]
fn vector_index_incremental_power_loss_recovers_atomic_selector_and_root_barriers() {
    qualify(&[
        Cut::SelectorRename(ObservationBoundary::Before),
        Cut::SelectorRename(ObservationBoundary::After),
        Cut::SelectorDirectory(ObservationBoundary::Before),
        Cut::SelectorDirectory(ObservationBoundary::After),
    ]);
}