noxu-db 7.2.1

Noxu DB - An embedded transactional database engine
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
// Copyright (C) 2024-2025 Greg Burd.  Licensed under either of the
// Apache License, Version 2.0 or the MIT license, at your option.
// See LICENSE-APACHE and LICENSE-MIT at the root of this repository.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! End-to-end tests for the `noxu-admin` CLI (dump / load / print-log).
//!
//! These drive the real built binary as a subprocess, so they exercise arg
//! parsing, read-only env opening, the on-disk dump format, and error
//! handling exactly as a user would.  The binary path is injected by cargo
//! as `CARGO_BIN_EXE_noxu-admin`.
//!
//! Faithful to JE `DbDump` / `DbLoad` / `DbPrintLog` semantics.

use std::collections::BTreeSet;
use std::path::Path;
use std::process::Command;

use noxu_db::{DatabaseConfig, DatabaseEntry, Environment, EnvironmentConfig};

fn admin_bin() -> &'static str {
    env!("CARGO_BIN_EXE_noxu-admin")
}

/// A spread of records that stresses binary-safety:
/// - ordinary ASCII keys,
/// - keys/values containing non-printable bytes (0x00, 0x0a newline, 0xff),
/// - a backslash (the escape char) in both key and value,
/// - duplicate keys (same key, different data) in a dup-sort DB.
fn sample_records() -> Vec<(Vec<u8>, Vec<u8>)> {
    vec![
        (b"alpha".to_vec(), b"first".to_vec()),
        (b"beta".to_vec(), b"second".to_vec()),
        // Non-printable bytes in the value.
        (b"binval".to_vec(), vec![0x00, 0x0a, 0xff, 0x7f, 0x80]),
        // Non-printable bytes in the key.
        (vec![0x00, 0x01, 0x02, 0xfe], b"binkey".to_vec()),
        // Backslash and newline (the two characters JE's escape mechanism
        // treats specially) in both halves.
        (b"back\\slash".to_vec(), b"val\\with\\back".to_vec()),
        (vec![b'k', b'\n', b'e', b'y'], vec![b'v', b'\n', b'l']),
        // A long-ish value with the full byte range to be thorough.
        (b"allbytes".to_vec(), (0u8..=255).collect()),
    ]
}

fn populate(dir: &Path, db_name: &str, dup_sort: bool) {
    let env = Environment::open(
        EnvironmentConfig::new(dir.to_path_buf())
            .with_allow_create(true)
            .with_transactional(true),
    )
    .expect("open env");
    let db = env
        .open_database(
            None,
            db_name,
            &DatabaseConfig::new()
                .with_allow_create(true)
                .with_transactional(true)
                .with_sorted_duplicates(dup_sort),
        )
        .expect("open db");

    let txn = env.begin_transaction(None).expect("begin");
    for (k, v) in sample_records() {
        db.put_in(
            &txn,
            DatabaseEntry::from_bytes(&k),
            DatabaseEntry::from_bytes(&v),
        )
        .expect("put");
    }
    if dup_sort {
        // Add duplicate data for an existing key.
        db.put_in(
            &txn,
            DatabaseEntry::from_bytes(b"alpha"),
            DatabaseEntry::from_bytes(b"first-dup"),
        )
        .expect("put dup");
        db.put_in(
            &txn,
            DatabaseEntry::from_bytes(b"alpha"),
            DatabaseEntry::from_bytes(b"first-dup-2"),
        )
        .expect("put dup 2");
    }
    txn.commit().expect("commit");
    drop(db);
    env.close().expect("close");
}

/// Read every (key, data) pair from a database into a multiset so we can
/// compare two databases for exact equality regardless of any incidental
/// ordering differences (dup-sort iteration order is deterministic but we
/// compare as a set-of-pairs to be safe).
fn read_all(
    dir: &Path,
    db_name: &str,
    dup_sort: bool,
) -> BTreeSet<(Vec<u8>, Vec<u8>)> {
    let env = Environment::open(
        EnvironmentConfig::new(dir.to_path_buf()).with_read_only(true),
    )
    .expect("reopen env");
    let db = env
        .open_database(
            None,
            db_name,
            &DatabaseConfig::new()
                .with_read_only(true)
                .with_sorted_duplicates(dup_sort),
        )
        .expect("reopen db");
    let mut out = BTreeSet::new();
    for r in db.iter(None).expect("iter") {
        let (k, v) = r.expect("read");
        out.insert((k, v));
    }
    drop(db);
    env.close().expect("close");
    out
}

/// HEADLINE: dump | load round-trip must reproduce the database exactly,
/// for both printable and hex formats, including binary and duplicate keys.
fn round_trip(printable: bool, dup_sort: bool) {
    let src = tempfile::tempdir().unwrap();
    let dst = tempfile::tempdir().unwrap();
    let dump_file = src.path().join("dump.txt");

    populate(src.path(), "data", dup_sort);

    // dump
    let mut dump_cmd = Command::new(admin_bin());
    dump_cmd
        .arg("dump")
        .arg("-h")
        .arg(src.path())
        .arg("-s")
        .arg("data")
        .arg("-f")
        .arg(&dump_file);
    if printable {
        dump_cmd.arg("-p");
    }
    if dup_sort {
        dump_cmd.arg("-D");
    }
    let dump_out = dump_cmd.output().expect("run dump");
    assert!(
        dump_out.status.success(),
        "dump failed: {}",
        String::from_utf8_lossy(&dump_out.stderr)
    );

    // Sanity: the dump file carries the right header for the chosen format.
    let dump_text = std::fs::read_to_string(&dump_file).unwrap();
    assert!(dump_text.starts_with("VERSION=3\n"));
    assert!(dump_text.contains(if printable {
        "format=print\n"
    } else {
        "format=bytevalue\n"
    }));
    assert!(
        dump_text
            .contains(&format!("dupsort={}\n", if dup_sort { 1 } else { 0 }))
    );
    assert!(dump_text.trim_end().ends_with("DATA=END"));

    // load into a fresh env
    let load_out = Command::new(admin_bin())
        .arg("load")
        .arg("-h")
        .arg(dst.path())
        .arg("-s")
        .arg("data")
        .arg("-f")
        .arg(&dump_file)
        .output()
        .expect("run load");
    assert!(
        load_out.status.success(),
        "load failed: {}",
        String::from_utf8_lossy(&load_out.stderr)
    );

    let original = read_all(src.path(), "data", dup_sort);
    let loaded = read_all(dst.path(), "data", dup_sort);
    assert_eq!(
        original, loaded,
        "round-trip mismatch (printable={printable}, dup_sort={dup_sort})"
    );
    // The all-bytes record proves binary safety end-to-end.
    assert!(loaded.contains(&(b"allbytes".to_vec(), (0u8..=255).collect())));
}

#[test]
fn dump_load_round_trip_printable_no_dups() {
    round_trip(true, false);
}

#[test]
fn dump_load_round_trip_hex_no_dups() {
    round_trip(false, false);
}

#[test]
fn dump_load_round_trip_printable_with_dups() {
    round_trip(true, true);
}

#[test]
fn dump_load_round_trip_hex_with_dups() {
    round_trip(false, true);
}

/// HEADLINE: print-log on an env with known writes emits entries for those
/// writes — TxnCommit and LN puts — with their LSNs and types.
#[test]
fn print_log_shows_commits_and_lns() {
    let dir = tempfile::tempdir().unwrap();
    populate(dir.path(), "data", false);

    let out = Command::new(admin_bin())
        .arg("print-log")
        .arg("-h")
        .arg(dir.path())
        .output()
        .expect("run print-log");
    assert!(
        out.status.success(),
        "print-log failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let text = String::from_utf8_lossy(&out.stdout);

    // Every line is "lsn=... type=... size=...".
    assert!(text.contains("lsn="), "no lsn fields in output:\n{text}");
    // LogEntryType's Display uses short names ("Commit", "INS_LN_TX").
    assert!(text.contains("type=Commit"), "expected a commit entry:\n{text}");
    // The committed put is a transactional insert LN.
    assert!(
        text.contains("type=INS_LN_TX") || text.contains("type=INS_LN"),
        "expected an insert LN entry:\n{text}"
    );
    // LN lines carry key/data sizes.
    assert!(text.contains("keylen="), "LN entries should show keylen=");
}

/// print-log -S prints a per-type summary including a TxnCommit count.
#[test]
fn print_log_summary() {
    let dir = tempfile::tempdir().unwrap();
    populate(dir.path(), "data", false);

    let out = Command::new(admin_bin())
        .arg("print-log")
        .arg("-h")
        .arg(dir.path())
        .arg("-S")
        .output()
        .expect("run print-log -S");
    assert!(out.status.success());
    let text = String::from_utf8_lossy(&out.stdout);
    assert!(text.contains("Log summary:"), "summary header missing:\n{text}");
    assert!(text.contains("total entries:"));
    assert!(text.contains("Commit"), "summary should tally commit entries");
}

/// dump -l lists database names.
#[test]
fn dump_list_databases() {
    let dir = tempfile::tempdir().unwrap();
    populate(dir.path(), "data", false);
    populate(dir.path(), "other", false);

    let out = Command::new(admin_bin())
        .arg("dump")
        .arg("-h")
        .arg(dir.path())
        .arg("-l")
        .output()
        .expect("run dump -l");
    assert!(out.status.success());
    let text = String::from_utf8_lossy(&out.stdout);
    assert!(text.contains("data"), "missing 'data' in list:\n{text}");
    assert!(text.contains("other"), "missing 'other' in list:\n{text}");
}

// ── Graceful error handling: bad path, missing db, malformed dump ──────────

#[test]
fn dump_missing_env_fails_cleanly() {
    let out = Command::new(admin_bin())
        .arg("dump")
        .arg("-h")
        .arg("/nonexistent/path/to/env")
        .arg("-s")
        .arg("data")
        .output()
        .expect("run dump");
    assert!(!out.status.success(), "should fail on missing env");
    assert!(
        out.stdout.is_empty() || !out.stderr.is_empty(),
        "expected an error message on stderr"
    );
    // Must be a clean message, not a panic backtrace.
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(err.contains("noxu-admin:"), "expected clean error, got:\n{err}");
    assert!(!err.contains("panicked"), "must not panic:\n{err}");
}

#[test]
fn load_malformed_dump_fails_cleanly() {
    let dir = tempfile::tempdir().unwrap();
    let bad = dir.path().join("bad.txt");
    // Header without HEADER=END terminator.
    std::fs::write(&bad, "VERSION=3\nformat=print\n").unwrap();

    let out = Command::new(admin_bin())
        .arg("load")
        .arg("-h")
        .arg(dir.path())
        .arg("-s")
        .arg("data")
        .arg("-f")
        .arg(&bad)
        .output()
        .expect("run load");
    assert!(!out.status.success(), "should fail on malformed dump");
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(err.contains("noxu-admin:"), "expected clean error, got:\n{err}");
    assert!(!err.contains("panicked"));
}

#[test]
fn load_missing_db_name_fails_cleanly() {
    let dir = tempfile::tempdir().unwrap();
    let dump = dir.path().join("d.txt");
    // Valid header + one record, but no -s and no database= header line.
    std::fs::write(
        &dump,
        "VERSION=3\nformat=print\ntype=btree\ndupsort=0\nHEADER=END\n k\n v\nDATA=END\n",
    )
    .unwrap();

    let out = Command::new(admin_bin())
        .arg("load")
        .arg("-h")
        .arg(dir.path())
        .arg("-f")
        .arg(&dump)
        .output()
        .expect("run load");
    assert!(!out.status.success(), "should fail without a db name");
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(err.contains("noxu-admin:"));
}

/// load with `database=` header line (no -s) picks up the name from the dump.
#[test]
fn load_db_name_from_header() {
    let dir = tempfile::tempdir().unwrap();
    let dump = dir.path().join("d.txt");
    std::fs::write(
        &dump,
        "VERSION=3\nformat=print\ntype=btree\ndupsort=0\ndatabase=fromheader\nHEADER=END\n key1\n val1\nDATA=END\n",
    )
    .unwrap();

    let out = Command::new(admin_bin())
        .arg("load")
        .arg("-h")
        .arg(dir.path())
        .arg("-f")
        .arg(&dump)
        .output()
        .expect("run load");
    assert!(
        out.status.success(),
        "load failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let loaded = read_all(dir.path(), "fromheader", false);
    assert!(loaded.contains(&(b"key1".to_vec(), b"val1".to_vec())));
}

/// no-overwrite mode (-n) reports key-exists rather than clobbering.
#[test]
fn load_no_overwrite_keeps_existing() {
    let dir = tempfile::tempdir().unwrap();
    // Pre-populate with a different value for "alpha".
    {
        let env = Environment::open(
            EnvironmentConfig::new(dir.path().to_path_buf())
                .with_allow_create(true)
                .with_transactional(true),
        )
        .unwrap();
        let db = env
            .open_database(
                None,
                "data",
                &DatabaseConfig::new()
                    .with_allow_create(true)
                    .with_transactional(true),
            )
            .unwrap();
        let txn = env.begin_transaction(None).unwrap();
        db.put_in(
            &txn,
            DatabaseEntry::from_bytes(b"alpha"),
            DatabaseEntry::from_bytes(b"PRESERVE"),
        )
        .unwrap();
        txn.commit().unwrap();
        drop(db);
        env.close().unwrap();
    }

    let dump = dir.path().join("d.txt");
    std::fs::write(
        &dump,
        "VERSION=3\nformat=print\ntype=btree\ndupsort=0\nHEADER=END\n alpha\n CLOBBER\nDATA=END\n",
    )
    .unwrap();

    let out = Command::new(admin_bin())
        .arg("load")
        .arg("-h")
        .arg(dir.path())
        .arg("-s")
        .arg("data")
        .arg("-f")
        .arg(&dump)
        .arg("-n")
        .output()
        .expect("run load -n");
    assert!(out.status.success());

    // The existing value must survive.
    let env = Environment::open(
        EnvironmentConfig::new(dir.path().to_path_buf()).with_read_only(true),
    )
    .unwrap();
    let db = env
        .open_database(
            None,
            "data",
            &DatabaseConfig::new().with_read_only(true),
        )
        .unwrap();
    let key = DatabaseEntry::from_bytes(b"alpha");
    let mut val = DatabaseEntry::new();
    let status = db.get_into(None, &key, &mut val).unwrap();
    assert!(status);
    assert_eq!(val.data_opt(), Some(b"PRESERVE".as_ref()));
    drop(db);
    env.close().unwrap();
}