use hakodb::config::{DurabilityMode, HakoConfig};
use hakodb::document::hako_doc::HakoDoc;
use hakodb::document::value::Value;
use hakodb::engine::{BatchMutation, Hako};
#[test]
fn write_path_nofsync_modes() {
for mode in [
DurabilityMode::Interval,
DurabilityMode::Manual,
DurabilityMode::OnCommit,
] {
let dir = std::env::temp_dir().join(format!(
"fl-test-wpm-{:?}-{}",
mode,
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let mut cfg = HakoConfig::default();
cfg.durability_mode = mode;
let db = Hako::open(&dir, cfg).expect("open");
let mut mutations = Vec::with_capacity(100);
for i in 0..100 {
let mut doc = HakoDoc::default();
doc.insert("v", Value::Int(i as i64));
mutations.push(BatchMutation::Put {
collection: "bench".into(),
doc_id: format!("k_{}", i),
doc,
});
}
db.write_batch(mutations).expect("write");
for i in 0..100 {
let got = db.get("bench", &format!("k_{}", i)).expect("get");
assert!(got.is_some(), "doc {i} missing after write in {mode:?}");
}
std::fs::remove_dir_all(&dir).ok();
}
}
#[test]
fn blob_write_path() {
let dir = std::env::temp_dir().join(format!(
"fl-test-blob-{}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let mut cfg = HakoConfig::default();
cfg.durability_mode = DurabilityMode::Manual;
cfg.value_blob_threshold_bytes = 1024;
let db = Hako::open(&dir, cfg).expect("open");
let mut small = HakoDoc::default();
small.insert("k", Value::Int(1));
db.write_batch(vec![BatchMutation::Put {
collection: "bench".into(),
doc_id: "small_doc".into(),
doc: small,
}])
.expect("small write");
assert!(db.get("bench", "small_doc").expect("get").is_some(), "small_doc visible");
let mut doc = HakoDoc::default();
doc.insert("blob", Value::String("x".repeat(4096)));
db.write_batch(vec![BatchMutation::Put {
collection: "bench".into(),
doc_id: "blob_doc".into(),
doc: doc.clone(),
}])
.expect("blob write");
std::thread::sleep(std::time::Duration::from_millis(500));
let small = db.get("bench", "small_doc").expect("get").expect("small_doc still present after sleep");
assert_eq!(small.get("k"), Some(&Value::Int(1)), "small_doc value preserved");
let blob = db.get("bench", "blob_doc").expect("get").expect("blob_doc present");
match blob.get("blob") {
Some(Value::String(s)) => assert_eq!(s.len(), 4096, "blob string round-trip length"),
other => panic!("expected resolved String after worker flush, got {:?}", other),
}
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn tx_get_put_commit() {
let dir = std::env::temp_dir().join(format!(
"fl-test-tx-{}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let mut cfg = HakoConfig::default();
cfg.durability_mode = DurabilityMode::Manual;
let db = Hako::open(&dir, cfg).expect("open");
let mut initial = HakoDoc::default();
initial.insert("counter", Value::Int(0));
db.write_batch(vec![BatchMutation::Put {
collection: "bench".into(),
doc_id: "target".into(),
doc: initial,
}])
.expect("seed");
let mut tx = db.begin_serializable_transaction();
let doc = tx.get(&db, "bench", "target").expect("tx get").expect("present");
let mut next = doc.clone();
next.insert("counter", Value::Int(42));
tx.put("bench", "target", next);
tx.commit(&db).expect("tx commit");
let after = db.get("bench", "target").expect("get").expect("present");
assert_eq!(after.get("counter"), Some(&Value::Int(42)));
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn wal_snapshot_reclaims_stale_history() {
let dir = std::env::temp_dir().join(format!(
"fl-test-walcompact-{}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let mut cfg = HakoConfig::default();
cfg.durability_mode = DurabilityMode::Interval;
cfg.auto_compaction_threshold_bytes = 256 * 1024;
cfg.wal_reserve_bytes = 65536;
let db = Hako::open(&dir, cfg).expect("open");
for round in 0..200 {
for i in 0..20 {
let mut doc = HakoDoc::default();
doc.insert("v", Value::Int(round));
doc.insert("pad", Value::String("x".repeat(500)));
db.put("bench", &format!("k_{i}"), &doc).expect("put");
}
}
let wal_path = dir.join("bench").join("wal.log");
let before = std::fs::metadata(&wal_path).expect("wal").len();
assert!(before > 500_000, "test setup too small: {before} bytes");
db.compact().expect("compact");
let after = std::fs::metadata(&wal_path).expect("wal").len();
assert!(
after < before / 10,
"WAL not reclaimed: {before} -> {after} bytes"
);
for i in 0..20 {
let got = db.get("bench", &format!("k_{i}")).expect("get").expect("present");
assert_eq!(got.get("v"), Some(&Value::Int(199)));
}
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn wal_reserve_skipped_for_internal_collections() {
let dir = std::env::temp_dir().join(format!(
"fl-test-reserve-{}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let mut cfg = HakoConfig::default();
cfg.durability_mode = DurabilityMode::Always;
cfg.wal_reserve_bytes = 4 * 1024 * 1024;
let db = Hako::open(&dir, cfg).expect("open");
let mut doc = HakoDoc::default();
doc.insert("v", Value::Int(1));
db.put("user_data", "a", &doc).expect("put");
db.put("__hako_system", "probe", &doc).expect("put");
db.flush().ok();
let wal_len = |col: &str| {
std::fs::metadata(dir.join(col).join("wal.log"))
.map(|m| m.len())
.unwrap_or(u64::MAX)
};
assert!(wal_len("user_data") >= 4 * 1024 * 1024, "user shard lost its reserve");
assert!(
wal_len("__hako_system") < 1024 * 1024,
"system shard carries phantom reserve: {} bytes",
wal_len("__hako_system")
);
std::fs::remove_dir_all(&dir).ok();
}