fusedb 0.3.0

A read-optimised binary database where many keys share one object — with zero duplication on disk.
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
// tests/interop.rs
// ──────────────────────────────────────────────────────────────────────────────
// Cross-language compatibility suite.
//
// These tests are the contract behind the project's central promise: there is
// one FuseDB format, not a Rust one and a Python one. Every test here drives
// both implementations over the *same file* and fails if they ever disagree.
//
// Running them needs the Python package importable (`maturin develop`). When it
// is not, they skip — unless FUSEDB_REQUIRE_INTEROP=1, which CI sets so a
// broken environment surfaces as a failure instead of a silent green run.

use std::path::{Path, PathBuf};
use std::process::Command;

use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use tempfile::TempDir;

use fusedb::{merge, FuseReader, FuseWriter};

// ── the canonical dataset, mirrored from tests/interop_helper.py ─────────────

/// `(key, company)` — the two Google aliases and the two Cloudflare aliases
/// share one object each, which is what makes deduplication observable.
const RECORDS: &[(&str, &str)] = &[
    ("google.com", "Google"),
    ("cloudflare.com", "Cloudflare"),
    ("8.8.8.8", "Google"),
    ("8.8.4.4", "Google"),
    ("1.1.1.1", "Cloudflare"),
];

fn record(company: &str) -> Value {
    match company {
        "Google" => json!({"asn": 15169, "cc": "US", "company": "Google"}),
        "Cloudflare" => json!({"asn": 13335, "cc": "US", "company": "Cloudflare"}),
        other => panic!("unknown company {other}"),
    }
}

/// Field order matters: `to_vec_named` emits struct fields in declaration
/// order, and this order matches the literal in `interop_helper.py`.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Mixed {
    bool: bool,
    float: f64,
    int: u32,
    list: Vec<u8>,
    nested: Nested,
    neg: i32,
    null: Option<u8>,
    text: String,
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Nested {
    a: Inner,
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Inner {
    b: String,
}

fn mixed() -> Mixed {
    Mixed {
        bool: true,
        float: 1.5,
        int: 42,
        list: vec![1, 2, 3],
        nested: Nested {
            a: Inner { b: "c".into() },
        },
        neg: -7,
        null: None,
        text: "héllo wörld".into(),
    }
}

/// Build the canonical database with the Rust API, mirroring `cmd_write`.
fn write_canonical(path: &Path) {
    let mut w = FuseWriter::new();
    let mut by_company: std::collections::HashMap<&str, usize> = Default::default();

    for (key, company) in RECORDS {
        let oid = match by_company.get(company) {
            Some(&oid) => oid,
            None => {
                let oid = w.add_object(&record(company)).expect("encode record");
                by_company.insert(company, oid);
                oid
            }
        };
        w.add_key(key, oid).expect("add_key");
    }
    w.add("mixed", &mixed()).expect("add mixed");
    w.build(path).expect("build");
}

// ── Python harness ────────────────────────────────────────────────────────────

struct Python {
    exe: PathBuf,
    helper: PathBuf,
}

impl Python {
    /// Locate an interpreter that can `import fusedb`, preferring the repo venv.
    fn find() -> Option<Self> {
        let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let helper = root.join("tests").join("interop_helper.py");

        let venv = if cfg!(windows) {
            root.join(".venv").join("Scripts").join("python.exe")
        } else {
            root.join(".venv").join("bin").join("python")
        };

        let mut candidates: Vec<PathBuf> = Vec::new();
        if let Ok(explicit) = std::env::var("FUSEDB_PYTHON") {
            candidates.push(PathBuf::from(explicit));
        }
        candidates.push(venv);
        candidates.push(PathBuf::from("python3"));
        candidates.push(PathBuf::from("python"));

        candidates.into_iter().find_map(|exe| {
            let ok = Command::new(&exe)
                .args(["-c", "import fusedb"])
                .output()
                .map(|o| o.status.success())
                .unwrap_or(false);
            ok.then(|| Self {
                exe,
                helper: helper.clone(),
            })
        })
    }

    /// Skip the test unless CI demanded interop coverage.
    fn find_or_skip(test: &str) -> Option<Self> {
        match Self::find() {
            Some(py) => Some(py),
            None => {
                assert!(
                    std::env::var("FUSEDB_REQUIRE_INTEROP").as_deref() != Ok("1"),
                    "FUSEDB_REQUIRE_INTEROP=1 but no interpreter can `import fusedb` \
                     — run `maturin develop` first"
                );
                eprintln!("skipping {test}: python `fusedb` package not importable");
                None
            }
        }
    }

    fn run(&self, args: &[&str]) -> String {
        let out = Command::new(&self.exe)
            .arg(&self.helper)
            .args(args)
            // The helper writes UTF-8 to `sys.stdout.buffer` explicitly; this is
            // belt-and-braces for anything that reaches stdout another way.
            // Without it, Python on Windows encodes with the ANSI code page and
            // "héllo wörld" comes back as cp1252, which is not valid UTF-8.
            .env("PYTHONIOENCODING", "utf-8")
            .env("PYTHONUTF8", "1")
            .output()
            .unwrap_or_else(|e| panic!("spawning {:?} failed: {e}", self.exe));

        assert!(
            out.status.success(),
            "python {args:?} exited {:?}\nstderr:\n{}",
            out.status.code(),
            String::from_utf8_lossy(&out.stderr),
        );
        String::from_utf8(out.stdout).unwrap_or_else(|e| {
            panic!(
                "python stdout is not UTF-8 at byte {}: {:?}",
                e.utf8_error().valid_up_to(),
                String::from_utf8_lossy(e.as_bytes()),
            )
        })
    }
}

/// The same canonical-JSON shape `cmd_read` prints, produced from Rust.
fn rust_read_report(path: &Path) -> Value {
    let db = FuseReader::open(path).expect("open");
    let stats = db.stats();

    let mut items = serde_json::Map::new();
    for (key, value) in db.items::<Value>().expect("items") {
        items.insert(key, value);
    }

    json!({
        "keys": db.keys(),
        "items": Value::Object(items),
        "num_keys": stats.num_keys,
        "num_objects": stats.num_objects,
        "unique_objects": db.objects_raw().expect("objects").len(),
        "prefix_8_8": db.prefix_iter(b"8.8.")
            .map(|e| e.expect("prefix entry").0)
            .collect::<Vec<_>>(),
        "index_offset": stats.index_offset,
        "data_offset": stats.data_offset,
        "file_crc32": format!("{:#010x}", stats.file_crc32),
        "version": stats.version,
        "verified": db.verify().is_ok(),
    })
}

fn expected_report() -> Value {
    // Keys as the index stores them: sorted by raw bytes.
    let mut keys: Vec<&str> = RECORDS.iter().map(|(k, _)| *k).collect();
    keys.push("mixed");
    keys.sort_unstable();

    let mut items = serde_json::Map::new();
    for (key, company) in RECORDS {
        items.insert((*key).into(), record(company));
    }
    items.insert(
        "mixed".into(),
        serde_json::to_value(mixed()).expect("mixed to json"),
    );

    json!({
        "keys": keys,
        "items": Value::Object(items),
        "num_keys": 6,
        "num_objects": 3,          // Google, Cloudflare, mixed
        "unique_objects": 3,
        "prefix_8_8": ["8.8.4.4", "8.8.8.8"],
        "verified": true,
        "version": 2,
    })
}

/// Compare only the keys present in `expected` — the offsets and CRC in a full
/// report are file-specific and get asserted separately.
fn assert_subset(actual: &Value, expected: &Value, context: &str) {
    for (key, want) in expected.as_object().expect("expected is an object") {
        let got = actual.get(key).unwrap_or_else(|| {
            panic!("{context}: report is missing `{key}`\nfull report: {actual}")
        });
        assert_eq!(got, want, "{context}: `{key}` differs");
    }
}

// ── 1. Rust writes → Python reads ─────────────────────────────────────────────

#[test]
fn rust_writes_python_reads() {
    let Some(py) = Python::find_or_skip("rust_writes_python_reads") else {
        return;
    };
    let dir = TempDir::new().unwrap();
    let db = dir.path().join("rust.fsdb");

    write_canonical(&db);

    let report: Value =
        serde_json::from_str(&py.run(&["read", db.to_str().unwrap()])).expect("python report");

    assert_subset(&report, &expected_report(), "python reading a Rust file");
}

// ── 2. Python writes → Rust reads ─────────────────────────────────────────────

#[test]
fn python_writes_rust_reads() {
    let Some(py) = Python::find_or_skip("python_writes_rust_reads") else {
        return;
    };
    let dir = TempDir::new().unwrap();
    let db = dir.path().join("python.fsdb");

    py.run(&["write", db.to_str().unwrap()]);

    assert_subset(
        &rust_read_report(&db),
        &expected_report(),
        "Rust reading a Python file",
    );

    // Typed decode of a Python-written value into a concrete Rust struct.
    let reader = FuseReader::open(&db).unwrap();
    assert_eq!(
        reader.get::<Mixed>("mixed").unwrap().unwrap(),
        mixed(),
        "Python-encoded msgpack must decode into the Rust struct"
    );
}

// ── 3. Both implementations agree on the exact bytes ──────────────────────────

#[test]
fn both_report_identical_metadata() {
    let Some(py) = Python::find_or_skip("both_report_identical_metadata") else {
        return;
    };
    let dir = TempDir::new().unwrap();

    for (label, path) in [
        ("rust-written", dir.path().join("r.fsdb")),
        ("python-written", dir.path().join("p.fsdb")),
    ] {
        if label == "rust-written" {
            write_canonical(&path);
        } else {
            py.run(&["write", path.to_str().unwrap()]);
        }

        let from_python: Value =
            serde_json::from_str(&py.run(&["read", path.to_str().unwrap()])).unwrap();
        let from_rust = rust_read_report(&path);

        for field in [
            "num_keys",
            "num_objects",
            "unique_objects",
            "index_offset",
            "data_offset",
            "file_crc32",
            "version",
            "keys",
            "items",
            "prefix_8_8",
        ] {
            assert_eq!(
                from_rust[field], from_python[field],
                "{label}: `{field}` differs between implementations",
            );
        }
    }
}

// ── 4. CRC validation is the same check on both sides ─────────────────────────

#[test]
fn crc_validation_matches() {
    let Some(py) = Python::find_or_skip("crc_validation_matches") else {
        return;
    };
    let dir = TempDir::new().unwrap();
    let db = dir.path().join("crc.fsdb");

    write_canonical(&db);
    assert_eq!(py.run(&["verify", db.to_str().unwrap()]), "ok");
    assert!(FuseReader::open(&db).unwrap().verify().is_ok());

    // Flip one payload byte. Both implementations must reject the file, and
    // both must reject it at open() — the whole-file CRC covers everything
    // after the 40-byte header.
    let mut bytes = std::fs::read(&db).unwrap();
    let victim = bytes.len() - 1;
    bytes[victim] ^= 0xff;
    let corrupt = dir.path().join("corrupt.fsdb");
    std::fs::write(&corrupt, &bytes).unwrap();

    let py_result = py.run(&["verify", corrupt.to_str().unwrap()]);
    assert!(
        py_result.starts_with("fail:"),
        "python accepted a corrupted file: {py_result}"
    );
    assert!(
        FuseReader::open(&corrupt).is_err(),
        "Rust accepted a corrupted file that Python rejected"
    );
}

// ── 5. Deduplication survives the round-trip ──────────────────────────────────

#[test]
fn deduplication_matches() {
    let Some(py) = Python::find_or_skip("deduplication_matches") else {
        return;
    };
    let dir = TempDir::new().unwrap();
    let rust_db = dir.path().join("r.fsdb");
    let py_db = dir.path().join("p.fsdb");

    write_canonical(&rust_db);
    py.run(&["write", py_db.to_str().unwrap()]);

    for path in [&rust_db, &py_db] {
        let db = FuseReader::open(path).unwrap();
        assert_eq!(db.len(), 6, "{path:?}: key count");
        assert_eq!(db.stats().num_objects, 3, "{path:?}: stored object count");
        assert_eq!(
            db.objects_raw().unwrap().len(),
            3,
            "{path:?}: four aliased keys must resolve to two shared objects",
        );
        // The aliases really are the same object, not two equal copies.
        assert_eq!(
            db.get_raw("8.8.8.8").unwrap(),
            db.get_raw("google.com").unwrap()
        );
    }
}

// ── 6. merge() produces the same file in both languages ───────────────────────

#[test]
fn merge_is_byte_identical() {
    let Some(py) = Python::find_or_skip("merge_is_byte_identical") else {
        return;
    };
    let dir = TempDir::new().unwrap();

    // Two overlapping sources: `b` re-states one of `a`'s records and adds one.
    let a = dir.path().join("a.fsdb");
    let b = dir.path().join("b.fsdb");
    write_canonical(&a);

    let mut w = FuseWriter::new();
    let oid = w.add_object(&record("Cloudflare")).unwrap();
    w.add_key("1.0.0.1", oid).unwrap();
    w.add_key("1.1.1.1", oid).unwrap();
    w.add("fastly.com", &json!({"company": "Fastly"})).unwrap();
    w.build(&b).unwrap();

    let rust_out = dir.path().join("merged_rust.fsdb");
    let py_out = dir.path().join("merged_py.fsdb");

    merge(&[&a, &b], &rust_out).unwrap();
    py.run(&[
        "merge",
        py_out.to_str().unwrap(),
        a.to_str().unwrap(),
        b.to_str().unwrap(),
    ]);

    assert_eq!(
        std::fs::read(&rust_out).unwrap(),
        std::fs::read(&py_out).unwrap(),
        "merge() must be byte-identical across implementations",
    );

    let merged = FuseReader::open(&rust_out).unwrap();
    assert!(merged.exists("fastly.com") && merged.exists("google.com"));
    assert_eq!(
        merged.stats().num_objects,
        4,
        "Cloudflare appears in both sources and must be stored once",
    );
}

// ── 7. The value encoders agree byte for byte ─────────────────────────────────

#[test]
fn msgpack_encoding_is_identical() {
    let Some(py) = Python::find_or_skip("msgpack_encoding_is_identical") else {
        return;
    };

    let from_python = py.run(&["pack-mixed"]);
    let from_rust = fusedb::encode(&mixed())
        .unwrap()
        .iter()
        .map(|b| format!("{b:02x}"))
        .collect::<String>();

    assert_eq!(
        from_rust, from_python,
        "rmp_serde::to_vec_named must match msgpack.packb(use_bin_type=True)",
    );
}

// ── 8. Versions stay in lockstep ──────────────────────────────────────────────

#[test]
fn crate_and_package_versions_match() {
    let Some(py) = Python::find_or_skip("crate_and_package_versions_match") else {
        return;
    };
    assert_eq!(
        py.run(&["version"]),
        fusedb::CRATE_VERSION,
        "python __version__ and Cargo package version have drifted",
    );
}