hawdb 0.38.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
// Copyright 2026 Nowledge
// SPDX-License-Identifier: Apache-2.0

//! The read-only change batch probe and the typed bounded catch-up refusals
//! through the embedded handle (#1924).

use super::*;
use crate::{
    DatabaseReadTransaction, IncrementalSearchMaintenanceOptions, SearchProjectionChangeBatchProbe,
    SearchProjectionRelationalDelta,
};

fn resident_handle(name: &str) -> (std::path::PathBuf, EmbeddedStoreHandle) {
    resident_handle_over(name, Database::new())
}

fn resident_handle_over(name: &str, db: Database) -> (std::path::PathBuf, EmbeddedStoreHandle) {
    let root = unique_embedded_store_test_dir(name);
    let graph = EmbeddedGraph::from_database(db, GraphMode::WritableCutover);
    let projection = SearchProjection::from_index(SearchIndex::open(root.join("search")).unwrap());
    let handle = EmbeddedStoreHandle::new(EmbeddedStore::new(graph, Some(projection))).unwrap();
    (root, handle)
}

fn create(handle: &EmbeddedStoreHandle, ids: &[&str]) {
    for id in ids {
        handle
            .query_with_report(&format!("CREATE (:Memory {{id: '{id}', title: '{id}'}})"))
            .unwrap();
    }
}

fn graph_epoch(handle: &EmbeddedStoreHandle) -> u64 {
    handle
        .read_store()
        .unwrap()
        .graph
        .database()
        .commit_epoch()
        .unwrap()
}

fn durable_epoch(handle: &EmbeddedStoreHandle) -> Option<u64> {
    handle
        .runtime_status()
        .unwrap()
        .projection_freshness
        .unwrap()
        .durable_source_graph_commit_epoch
}

fn hydrate_nothing(
    _: &mut DatabaseReadTransaction,
    _: &crate::SearchProjectionChangeBatch,
) -> crate::Result<SearchProjectionRelationalDelta> {
    Ok(SearchProjectionRelationalDelta::default())
}

#[test]
fn probe_reports_no_backlog_a_covering_batch_and_a_prefix() {
    let (root, handle) = resident_handle("catch_up_probe_1924");
    assert_eq!(
        handle.search_projection_change_batch_probe(8).unwrap(),
        None
    );
    assert!(matches!(
        handle.search_projection_change_batch_probe(0),
        Err(HawDBError::Semantic(_))
    ));

    create(&handle, &["m1", "m2", "m3"]);
    let head = graph_epoch(&handle);
    let prefix = handle.search_projection_change_batch_probe(2).unwrap();
    assert_eq!(
        prefix,
        Some(SearchProjectionChangeBatchProbe {
            start_commit_epoch: 0,
            operation_count: 2,
            complete_through_commit_epoch: Some(head - 1),
            source_commit_epoch: head,
            covers_source: false,
        })
    );
    // The probe neither hydrates nor publishes.
    assert_eq!(durable_epoch(&handle), None);

    // A prefix probe predicts the typed catch-up refusal for that budget.
    for max_batches in [1, 4] {
        assert_eq!(
            handle
                .catch_up_search_projection(2, max_batches)
                .unwrap_err(),
            HawDBError::SearchProjectionPrefixBehindSource {
                complete_through_commit_epoch: head - 1,
                source_commit_epoch: head,
            }
        );
    }
    assert_eq!(durable_epoch(&handle), None);
    assert!(!handle.storage_handle_poisoned().unwrap());

    let covering = handle
        .search_projection_change_batch_probe(64)
        .unwrap()
        .unwrap();
    assert!(covering.covers_source);
    assert_eq!(covering.operation_count, 3);
    assert_eq!(covering.complete_through_commit_epoch, Some(head));
    assert!(
        handle
            .catch_up_search_projection(covering.operation_count, 1)
            .unwrap()
            .complete
    );
    assert_eq!(durable_epoch(&handle), Some(head));
    assert_eq!(
        handle.search_projection_change_batch_probe(8).unwrap(),
        None
    );
    drop(handle);
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn probe_refuses_a_single_commit_over_its_cap_with_the_typed_budget_error() {
    let mut db = Database::new();
    let mut transaction = db.begin_transaction().unwrap();
    for id in ["m1", "m2"] {
        transaction
            .query(&format!("CREATE (:Memory {{id: '{id}', title: '{id}'}})"))
            .unwrap();
    }
    transaction.commit().unwrap();
    let (root, handle) = resident_handle_over("catch_up_probe_commit_budget_1924", db);
    let commit = graph_epoch(&handle);
    let expected = HawDBError::SearchProjectionBatchBudgetExceeded {
        budget: crate::SearchProjectionBatchBudget::ChangeBatch,
        commit_epoch: Some(commit),
        required_operations: 2,
        limit: 1,
    };
    assert_eq!(
        handle.search_projection_change_batch_probe(1).unwrap_err(),
        expected
    );
    assert_eq!(
        handle
            .catch_up_search_projection_with_batch_hydrator(1, 8, 1, hydrate_nothing)
            .unwrap_err(),
        expected
    );
    assert!(!handle.storage_handle_poisoned().unwrap());
    assert_eq!(durable_epoch(&handle), None);
    let probe = handle
        .search_projection_change_batch_probe(2)
        .unwrap()
        .unwrap();
    assert!(probe.covers_source);
    assert!(
        handle
            .catch_up_search_projection_with_batch_hydrator(2, 8, 1, hydrate_nothing)
            .unwrap()
            .complete
    );
    drop(handle);
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn handle_entry_points_refuse_a_prefix_typed_and_a_fresh_probe_recovers_after_a_write() {
    let (root, handle) = resident_handle("catch_up_handle_prefix_1924");
    create(&handle, &["m1", "m2"]);
    let probe = handle
        .search_projection_change_batch_probe(64)
        .unwrap()
        .unwrap();
    assert!(probe.covers_source);
    // A write lands between the probe and the catch-up: the probed budget is
    // now a prefix.
    create(&handle, &["m3"]);
    let head = graph_epoch(&handle);
    let expected = HawDBError::SearchProjectionPrefixBehindSource {
        complete_through_commit_epoch: head - 1,
        source_commit_epoch: head,
    };
    let cap = probe.operation_count;
    assert_eq!(
        handle.catch_up_search_projection(cap, 4).unwrap_err(),
        expected
    );
    assert_eq!(
        handle
            .catch_up_search_projection_with_relational(cap, 4, hydrate_nothing)
            .unwrap_err(),
        expected
    );
    assert_eq!(
        handle
            .catch_up_search_projection_with_batch_hydrator(cap, cap, 4, hydrate_nothing)
            .unwrap_err(),
        expected
    );
    assert_eq!(
        handle
            .catch_up_search_projection_scheduled(cap, 4)
            .unwrap_err(),
        expected
    );
    assert_eq!(durable_epoch(&handle), None);
    assert!(!handle.storage_handle_poisoned().unwrap());

    let fresh = handle
        .search_projection_change_batch_probe(64)
        .unwrap()
        .unwrap();
    assert!(fresh.covers_source);
    assert!(
        handle
            .catch_up_search_projection_with_batch_hydrator(
                fresh.operation_count,
                fresh.operation_count,
                1,
                hydrate_nothing,
            )
            .unwrap()
            .complete
    );
    assert_eq!(durable_epoch(&handle), Some(head));
    drop(handle);
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn incremental_probe_and_catch_up_refusal_use_the_durable_watermark() {
    let (root, handle, _) = super::incremental_maintenance_fixture(
        "catch_up_probe_incremental_1924",
        IncrementalSearchMaintenanceOptions::default(),
    );
    handle
        .catch_up_search_projection_with_batch_hydrator(64, 64, 4, hydrate_nothing)
        .unwrap();
    let base = durable_epoch(&handle).unwrap();
    assert_eq!(
        handle.search_projection_change_batch_probe(8).unwrap(),
        None
    );
    create(&handle, &["m1", "m2", "m3"]);
    let head = graph_epoch(&handle);
    let prefix = handle
        .search_projection_change_batch_probe(1)
        .unwrap()
        .unwrap();
    assert_eq!(prefix.start_commit_epoch, base);
    assert!(!prefix.covers_source);
    assert_eq!(
        handle
            .catch_up_search_projection_with_batch_hydrator(1, 64, 4, hydrate_nothing)
            .unwrap_err(),
        HawDBError::SearchProjectionPrefixBehindSource {
            complete_through_commit_epoch: prefix.complete_through_commit_epoch.unwrap(),
            source_commit_epoch: head,
        }
    );
    assert_eq!(durable_epoch(&handle), Some(base));
    assert!(!handle.storage_handle_poisoned().unwrap());
    let covering = handle
        .search_projection_change_batch_probe(64)
        .unwrap()
        .unwrap();
    assert!(covering.covers_source);
    handle
        .catch_up_search_projection_with_batch_hydrator(
            covering.operation_count,
            64,
            1,
            hydrate_nothing,
        )
        .unwrap();
    assert_eq!(durable_epoch(&handle), Some(head));
    drop(handle);
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn incremental_host_derived_rows_over_the_projection_budget_are_typed() {
    let (root, handle, _) = super::incremental_maintenance_fixture(
        "catch_up_projection_budget_incremental_1924",
        IncrementalSearchMaintenanceOptions::default(),
    );
    handle
        .catch_up_search_projection_with_batch_hydrator(64, 64, 4, hydrate_nothing)
        .unwrap();
    let base = durable_epoch(&handle);
    create(&handle, &["m1"]);
    let derive_one_row = |_: &mut DatabaseReadTransaction,
                          _: &crate::SearchProjectionChangeBatch|
     -> crate::Result<SearchProjectionRelationalDelta> {
        Ok(SearchProjectionRelationalDelta {
            delta: crate::search::SearchProjectionDelta {
                upserts: vec![SearchProjectionRow {
                    kind: SearchProjectionKind::Memory,
                    external_id: "derived".into(),
                    title: "Derived".into(),
                    body: "Host row".into(),
                    embedding: None,
                    source_id: None,
                    metadata: BTreeMap::new(),
                }],
                ..Default::default()
            },
            processed_primary_key_count: 0,
        })
    };
    let error = handle
        .catch_up_search_projection_with_batch_hydrator(1, 1, 4, derive_one_row)
        .unwrap_err();
    assert_eq!(
        error,
        HawDBError::SearchProjectionBatchBudgetExceeded {
            budget: crate::SearchProjectionBatchBudget::ProjectionDelta,
            commit_epoch: None,
            required_operations: 2,
            limit: 1,
        }
    );
    assert_eq!(
        error.to_string(),
        "storage error: incremental projection update operation count 2 exceeded configured limit 1"
    );
    assert_eq!(durable_epoch(&handle), base);
    assert!(!handle.storage_handle_poisoned().unwrap());

    handle
        .catch_up_search_projection_with_batch_hydrator(1, 2, 1, derive_one_row)
        .unwrap();
    assert_eq!(durable_epoch(&handle), Some(graph_epoch(&handle)));
    drop(handle);
    std::fs::remove_dir_all(root).unwrap();
}

fn assert_not_attached(error: HawDBError) {
    assert!(
        matches!(&error, HawDBError::Semantic(message) if message.starts_with("search projection is not attached")),
        "{error:?}"
    );
}

#[test]
fn probe_and_readiness_without_a_projection_are_semantic_not_storage() {
    let graph = EmbeddedGraph::from_database(Database::new(), GraphMode::WritableCutover);
    let handle = EmbeddedStoreHandle::new(EmbeddedStore::new(graph, None)).unwrap();
    handle
        .query_with_report("CREATE (:Memory {id: 'm1', title: 'm1'})")
        .unwrap();
    assert_not_attached(handle.search_projection_change_batch_probe(8).unwrap_err());
    assert_not_attached(
        handle
            .search_projection_changefeed_readiness(false, Some(8))
            .unwrap_err(),
    );
    assert!(!handle.storage_handle_poisoned().unwrap());
}

#[test]
fn probe_and_readiness_after_a_failed_incremental_reattach_are_semantic() {
    let (root, handle, _) = super::incremental_maintenance_fixture(
        "catch_up_probe_detached_1924",
        IncrementalSearchMaintenanceOptions::default(),
    );
    handle
        .catch_up_search_projection_with_batch_hydrator(64, 64, 4, hydrate_nothing)
        .unwrap();
    create(&handle, &["m1"]);
    // Remove the durable selector so the next refresh cannot attach the head;
    // the store detaches its incremental reader rather than serve a stale one.
    let selector = root
        .join("search")
        .join("search_projection.out_of_core.manifest.hawdb");
    std::fs::remove_file(&selector).unwrap();
    handle
        .catch_up_search_projection_with_batch_hydrator(64, 64, 4, hydrate_nothing)
        .unwrap_err();
    assert_not_attached(handle.search_projection_change_batch_probe(8).unwrap_err());
    assert_not_attached(
        handle
            .search_projection_changefeed_readiness(false, Some(8))
            .unwrap_err(),
    );
    drop(handle);
    std::fs::remove_dir_all(root).unwrap();
}

/// No public constructor attaches both projections, and qualified read-only
/// out-of-core search is opened elsewhere; set the private fields directly to
/// cover the helper's remaining states.
#[test]
fn probe_and_readiness_report_ambiguous_and_unmaintained_projections_as_semantic() {
    let (root, handle, _) = super::incremental_maintenance_fixture(
        "catch_up_probe_states_1924",
        IncrementalSearchMaintenanceOptions::default(),
    );
    let semantic = |error: HawDBError, prefix: &str| {
        assert!(
            matches!(&error, HawDBError::Semantic(message) if message.starts_with(prefix)),
            "{error:?}"
        );
    };
    handle.write_store().unwrap().search_projection =
        Some(SearchProjection::from_index(SearchIndex::in_memory()));
    for error in [
        handle.search_projection_change_batch_probe(8).unwrap_err(),
        handle
            .search_projection_changefeed_readiness(false, Some(8))
            .unwrap_err(),
    ] {
        semantic(error, "search projection ownership is ambiguous");
    }
    {
        let mut store = handle.write_store().unwrap();
        store.search_projection = None;
        store
            .out_of_core_search_projection
            .as_mut()
            .unwrap()
            .maintenance = None;
    }
    for error in [
        handle.search_projection_change_batch_probe(8).unwrap_err(),
        handle
            .search_projection_changefeed_readiness(false, Some(8))
            .unwrap_err(),
    ] {
        semantic(error, "search projection has no catch-up maintenance");
    }
    assert!(!handle.storage_handle_poisoned().unwrap());
    drop(handle);
    std::fs::remove_dir_all(root).unwrap();
}