mindb 0.1.2

Lightweight embedded key–value store with write-ahead log and zstd compression.
Documentation
use std::error::Error;

use mindb::db::{Database, DatabaseOptions, SecondaryIndexConfig};
use mindb::observe::OperationKind;

#[test]
fn buffered_puts_share_single_wal_commit() -> Result<(), Box<dyn Error>> {
    let tempdir = tempfile::tempdir()?;

    let mut options = DatabaseOptions::new(tempdir.path());
    options.wal_direct_io = false;
    options.secondary_indexes = SecondaryIndexConfig::default();

    let db = Database::open(options)?;
    let metrics = db.metrics();

    let before = metrics
        .histogram(OperationKind::WalAppend)
        .expect("wal append histogram");
    assert_eq!(before.count, 0, "fresh database should have no WAL appends");

    db.put(b"one".to_vec(), b"alpha".to_vec())?;
    let after_first = metrics
        .histogram(OperationKind::WalAppend)
        .expect("wal append histogram");

    db.put(b"two".to_vec(), b"beta".to_vec())?;

    db.sync()?;

    let wal_hist = metrics
        .histogram(OperationKind::WalAppend)
        .expect("wal append histogram");
    let delta = wal_hist.count.saturating_sub(after_first.count);
    assert_eq!(delta / 2, 1, "expected a single WAL append observation");
    assert_eq!(
        after_first.count, before.count,
        "writes should be buffered until sync"
    );

    Ok(())
}