hawdb-storage 0.40.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
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The resident row byte total is maintained incrementally
//! (nowledge-co/hawdb#1519). Every test compares it with the full O(rows)
//! fold after each kind of row mutation.

use super::*;
use crate::background::CheckpointDecodeContext;

fn assert_pages_exact(pages: &RelationalRowPages) {
    assert_eq!(pages.len(), pages.iter().count());
    assert_eq!(pages.entry_bytes(), pages.recomputed_entry_bytes());
}

fn assert_state_exact(state: &RelationalState) {
    for segment in state.segments.values() {
        assert_pages_exact(&segment.rows);
    }
    assert_eq!(
        state.estimated_materialized_row_bytes(),
        state.recomputed_materialized_row_bytes()
    );
}

fn key(id: i64) -> RelationalKey {
    RelationalKey(vec![RelationalValue::BigInt(id)])
}

fn row(id: i64, payload_bytes: usize) -> RelationalRow {
    RelationalRow::new(vec![
        RelationalValue::BigInt(id),
        RelationalValue::Text("p".repeat(payload_bytes)),
    ])
}

fn unmetered_context() -> CheckpointDecodeContext {
    CheckpointDecodeContext {
        work: Default::default(),
        memory: Default::default(),
    }
}

#[test]
fn row_pages_maintain_exact_entry_bytes_across_every_mutation() {
    let mut pages = RelationalRowPages::default();
    assert_eq!(pages.entry_bytes(), 0);

    // Insert into an empty map, then grow past the entry-count split.
    for id in 0..600 {
        assert!(pages.insert(key(id), row(id, (id % 17) as usize)).is_none());
        assert_pages_exact(&pages);
    }
    assert!(pages.page_count() > 1);

    // Replacement keeps the stored key and swaps only the row bytes.
    assert!(pages.insert(key(7), row(7, 4096)).is_some());
    assert_pages_exact(&pages);
    assert!(pages.insert(key(7), row(7, 0)).is_some());
    assert_pages_exact(&pages);

    // A row above the byte target splits its page by size.
    assert!(pages
        .insert(key(300), row(300, RELATIONAL_ROW_PAGE_TARGET_BYTES + 1))
        .is_some());
    assert_pages_exact(&pages);

    // Removal, including a missing key and emptying a whole page.
    assert!(pages.remove(&key(10_000)).is_none());
    assert_pages_exact(&pages);
    let before = pages.page_count();
    for id in 0..300 {
        assert!(pages.remove(&key(id)).is_some());
        assert_pages_exact(&pages);
    }
    assert!(pages.page_count() < before);

    // A COW clone keeps its own total while the source stays unchanged.
    let snapshot = pages.clone();
    let snapshot_bytes = snapshot.entry_bytes();
    pages.insert(key(5_000), row(5_000, 99));
    pages.remove(&key(301));
    assert_pages_exact(&pages);
    assert_pages_exact(&snapshot);
    assert_eq!(snapshot.entry_bytes(), snapshot_bytes);

    // Checkpoint-allocated insert, replacement and page copy.
    let context = unmetered_context();
    assert!(pages
        .insert_with_allocations(key(6_000), row(6_000, 5), Some(&context))
        .unwrap()
        .is_none());
    assert_pages_exact(&pages);
    assert!(pages
        .insert_with_allocations(key(6_000), row(6_000, 500), Some(&context))
        .unwrap()
        .is_some());
    assert_pages_exact(&pages);
    for id in 7_000..7_300 {
        pages
            .insert_with_allocations(key(id), row(id, 3), Some(&context))
            .unwrap();
    }
    assert_pages_exact(&pages);
    let shared = pages.clone();
    pages
        .prepare_checkpoint_copy_for_key(&key(7_100), &context.work)
        .unwrap();
    assert_pages_exact(&pages);
    assert_eq!(pages.entry_bytes(), shared.entry_bytes());

    // Schema rewrites change every row.
    let fill = RelationalValue::Text("default".into());
    pages.append_value_to_all(&fill);
    assert_pages_exact(&pages);
    pages
        .append_value_to_all_with_allocations(&RelationalValue::BigInt(1), &context)
        .unwrap();
    assert_pages_exact(&pages);
    assert_pages_exact(&shared);

    // Bulk construction.
    let map = pages
        .iter()
        .map(|(key, row)| (key.clone(), row.clone()))
        .collect::<BTreeMap<_, _>>();
    let rebuilt = RelationalRowPages::from_map(map);
    assert_pages_exact(&rebuilt);
    assert_eq!(rebuilt.entry_bytes(), pages.entry_bytes());
}

#[test]
fn row_pages_entry_bytes_survive_a_deterministic_mutation_mix() {
    let mut pages = RelationalRowPages::default();
    let mut model = BTreeMap::new();
    let mut seed = 0x9e37_79b9_7f4a_7c15u64;
    for _ in 0..3_000 {
        seed ^= seed << 13;
        seed ^= seed >> 7;
        seed ^= seed << 17;
        let id = (seed % 700) as i64;
        let payload = ((seed >> 20) % 2_000) as usize;
        if seed.is_multiple_of(3) {
            assert_eq!(pages.remove(&key(id)), model.remove(&key(id)));
        } else {
            assert_eq!(
                pages.insert(key(id), row(id, payload)),
                model.insert(key(id), row(id, payload))
            );
        }
        assert_pages_exact(&pages);
    }
    assert_eq!(
        pages.entry_bytes(),
        RelationalRowPages::from_map(model).entry_bytes()
    );
}

#[test]
fn sparse_hydration_appends_maintain_entry_bytes() {
    let context = unmetered_context();
    let mut pages = RelationalRowPages::default();
    for id in 0..700 {
        assert!(pages
            .push_sorted_with_allocations(key(id), row(id, (id % 5) as usize), &context, 8)
            .unwrap()
            .is_none());
    }
    assert_pages_exact(&pages);
    // A repeated key is replaced (the caller reports it as corruption).
    assert!(pages
        .push_sorted_with_allocations(key(699), row(699, 4_000), &context, 8)
        .unwrap()
        .is_some());
    assert_pages_exact(&pages);
}

fn bigint_column(name: &str) -> RelationalColumnSchema {
    RelationalColumnSchema {
        name: name.to_string(),
        scalar_type: RelationalScalarType::BigInt,
        nullable: false,
        default: None,
    }
}

fn create_tables() -> RelationalTransaction {
    RelationalTransaction {
        writes: vec![
            create_upsert_table().writes.remove(0),
            // No primary key: rows use hidden row handles.
            RelationalWrite::CreateTable(RelationalTableSchema {
                materialized_view: None,
                name: "events".to_string(),
                columns: vec![bigint_column("at"), text_column("body", true)],
                primary_key: Vec::new(),
                unique_constraints: Vec::new(),
                foreign_keys: Vec::new(),
                indexes: Vec::new(),
            }),
        ],
    }
}

fn stage(state: &RelationalState, writes: Vec<RelationalWrite>) -> RelationalState {
    let next = state
        .stage_transaction(
            RelationalTransaction { writes },
            RelationalMutationLimits::default(),
            RelationalOverflowConfig::default(),
        )
        .expect("stage fixture transaction");
    assert_state_exact(&next);
    next
}

fn compare(column: &str, value: RelationalValue) -> RelationalPredicate {
    RelationalPredicate::Compare {
        column: column.to_string(),
        op: RelationalComparisonOp::Eq,
        value,
    }
}

#[test]
fn relational_state_row_bytes_track_transactions_rollback_and_recovery() {
    let empty = RelationalState::default();
    assert_state_exact(&empty);
    let base = stage(&empty, create_tables().writes);
    let base = stage(
        &base,
        vec![
            RelationalWrite::Insert {
                table: "documents".to_string(),
                rows: (0..400)
                    .map(|id| {
                        upsert_row(
                            &format!("id-{id:04}"),
                            &format!("owner-{id:04}"),
                            &"x".repeat(id % 23),
                        )
                    })
                    .collect(),
                mode: RelationalInsertMode::Error,
            },
            RelationalWrite::Insert {
                table: "events".to_string(),
                rows: (0..300)
                    .map(|at| {
                        RelationalRow::new(vec![
                            RelationalValue::BigInt(at),
                            RelationalValue::Text("e".repeat(at as usize % 11)),
                        ])
                    })
                    .collect(),
                mode: RelationalInsertMode::Error,
            },
        ],
    );
    let base_bytes = base.estimated_materialized_row_bytes();
    assert!(base_bytes > 0);

    let updated = stage(
        &base,
        vec![RelationalWrite::UpdateWhere {
            table: "documents".to_string(),
            assignments: vec![RelationalUpdateAssignment {
                column: "payload".to_string(),
                value: RelationalUpdateValue::Value(RelationalValue::Text("y".repeat(1_000))),
            }],
            predicate: compare("id", RelationalValue::Text("id-0007".into())),
        }],
    );
    assert!(updated.estimated_materialized_row_bytes() > base_bytes);
    // Staging is copy-on-write: the source state is untouched.
    assert_state_exact(&base);
    assert_eq!(base.estimated_materialized_row_bytes(), base_bytes);

    // Primary-key change moves a row to a new key.
    let rekeyed = stage(
        &updated,
        vec![RelationalWrite::UpdateWhere {
            table: "documents".to_string(),
            assignments: vec![RelationalUpdateAssignment {
                column: "id".to_string(),
                value: RelationalUpdateValue::Value(RelationalValue::Text(
                    "id-moved-to-a-longer-key".into(),
                )),
            }],
            predicate: compare("id", RelationalValue::Text("id-0008".into())),
        }],
    );

    let upserted = stage(
        &rekeyed,
        vec![
            RelationalWrite::Upsert {
                table: "documents".to_string(),
                rows: vec![
                    upsert_row("id-0009", "owner-0009", "short"),
                    upsert_row("id-9999", "owner-9999", "new"),
                ],
                conflict_columns: vec!["id".to_string()],
                action: RelationalConflictAction::Update(vec![RelationalUpsertAssignment {
                    column: "payload".to_string(),
                    value: RelationalUpsertValue::ExcludedColumn("payload".to_string()),
                }]),
            },
            RelationalWrite::Upsert {
                table: "documents".to_string(),
                rows: vec![upsert_row("id-0010", "owner-0010", "ignored")],
                conflict_columns: vec!["id".to_string()],
                action: RelationalConflictAction::DoNothing,
            },
        ],
    );

    let deleted = stage(
        &upserted,
        vec![
            RelationalWrite::DeleteByPrimaryKey {
                table: "documents".to_string(),
                keys: (20..120)
                    .map(|id| RelationalKey(vec![RelationalValue::Text(format!("id-{id:04}"))]))
                    .collect(),
            },
            RelationalWrite::DeleteWhere {
                table: "events".to_string(),
                predicate: compare("body", RelationalValue::Text(String::new())),
            },
            RelationalWrite::UpdateWhere {
                table: "events".to_string(),
                assignments: vec![RelationalUpdateAssignment {
                    column: "body".to_string(),
                    value: RelationalUpdateValue::Value(RelationalValue::Null),
                }],
                predicate: compare("at", RelationalValue::BigInt(5)),
            },
        ],
    );

    let altered = stage(
        &deleted,
        vec![
            RelationalWrite::AddColumn {
                table: "documents".to_string(),
                column: RelationalColumnSchema {
                    name: "note".to_string(),
                    scalar_type: RelationalScalarType::Text,
                    nullable: true,
                    default: Some(RelationalColumnDefault::Literal(RelationalValue::Text(
                        "note".into(),
                    ))),
                },
            },
            RelationalWrite::CreateIndex {
                table: "events".to_string(),
                index: RelationalIndexSchema {
                    name: "events_at".to_string(),
                    columns: vec!["at".to_string()],
                    included_columns: Vec::new(),
                    unique: false,
                    predicate: None,
                },
            },
        ],
    );

    // A rejected transaction leaves its source state, and total, unchanged.
    let altered_bytes = altered.estimated_materialized_row_bytes();
    assert!(altered
        .stage_transaction(
            RelationalTransaction {
                writes: vec![
                    RelationalWrite::Insert {
                        table: "documents".to_string(),
                        rows: vec![upsert_row("id-fresh", "owner-fresh", "z")],
                        mode: RelationalInsertMode::Error,
                    },
                    RelationalWrite::Insert {
                        table: "documents".to_string(),
                        rows: vec![upsert_row("id-0001", "owner-0001", "duplicate")],
                        mode: RelationalInsertMode::Error,
                    },
                ],
            },
            RelationalMutationLimits::default(),
            RelationalOverflowConfig::default(),
        )
        .is_err());
    assert_state_exact(&altered);
    assert_eq!(altered.estimated_materialized_row_bytes(), altered_bytes);

    // Checkpoint recovery rebuilds the same total from decoded rows.
    let encoded = encode_relational_checkpoint(3, &altered).expect("encode checkpoint");
    let recovered = decode_relational_checkpoint(&encoded, RelationalDecodeLimits::checkpoint())
        .expect("decode checkpoint")
        .state;
    assert_state_exact(&recovered);
    assert_eq!(recovered.estimated_materialized_row_bytes(), altered_bytes);

    // Detaching rows records the maintained total for checkpoint sizing.
    let mut detached = recovered.clone();
    detached.omit_materialized_rows();
    assert_eq!(detached.estimated_materialized_row_bytes(), 0);
    assert_eq!(
        detached.estimated_checkpoint_bytes(),
        recovered.estimated_checkpoint_bytes()
    );
    assert_state_exact(&recovered);
}

#[test]
fn checkpoint_size_estimate_does_not_walk_resident_rows() {
    let state = stage(&RelationalState::default(), create_tables().writes);
    let state = stage(
        &state,
        vec![RelationalWrite::Insert {
            table: "events".to_string(),
            rows: (0..5_000)
                .map(|at| {
                    RelationalRow::new(vec![
                        RelationalValue::BigInt(at),
                        RelationalValue::Text("body".into()),
                    ])
                })
                .collect(),
            mode: RelationalInsertMode::Error,
        }],
    );
    let before = row_entry_byte_probe::visits();
    let estimated = state.estimated_checkpoint_bytes();
    let materialized = state.estimated_materialized_row_bytes();
    assert_eq!(row_entry_byte_probe::visits(), before);
    assert_eq!(materialized, state.recomputed_materialized_row_bytes());
    assert!(estimated >= materialized);
    // Positive control: a real row walk is visible to the probe.
    let _ = state.estimated_checkpoint_admission_bytes_with_visit(&mut || {
        Ok::<_, std::convert::Infallible>(())
    });
    assert!(row_entry_byte_probe::visits() >= before + 5_000);
}