use sqlitegraph::backend::GraphBackend;
use sqlitegraph::backend::native::v3::V3Backend;
use std::time::Instant;
#[test]
fn test_index_persistence_on_flush() {
let temp_dir = tempfile::tempdir().unwrap();
let db_path = temp_dir.path().join("test.db");
let backend = V3Backend::create(&db_path).unwrap();
for i in 0..100 {
let kind = format!("Kind{}", i % 3);
let name = format!("node_{}", i);
backend
.insert_node(sqlitegraph::backend::NodeSpec {
kind,
name,
file_path: None,
data: serde_json::json!({}),
})
.unwrap();
}
backend.flush_to_disk().unwrap();
let index_path = db_path.with_extension("v3index");
assert!(index_path.exists(), "Index file should exist after flush");
let _backend = V3Backend::open(&db_path).unwrap();
}
#[test]
fn test_open_fallback_when_index_missing() {
let temp_dir = tempfile::tempdir().unwrap();
let db_path = temp_dir.path().join("test_fallback.db");
let backend = V3Backend::create(&db_path).unwrap();
for i in 0..50 {
let kind = format!("Kind{}", i % 2);
let name = format!("node_{}", i);
backend
.insert_node(sqlitegraph::backend::NodeSpec {
kind,
name,
file_path: None,
data: serde_json::json!({}),
})
.unwrap();
}
backend.flush_to_disk().unwrap();
let index_path = db_path.with_extension("v3index");
std::fs::remove_file(&index_path).unwrap();
let _backend = V3Backend::open(&db_path).unwrap();
}
#[test]
fn test_open_with_persisted_indexes_is_faster() {
let temp_dir = tempfile::tempdir().unwrap();
let db_path = temp_dir.path().join("perf_test.db");
let node_count = 10000;
let backend = V3Backend::create(&db_path).unwrap();
for i in 0..node_count {
let kind = format!("Kind{}", i % 10);
let name = format!("node_{:05}", i);
backend
.insert_node(sqlitegraph::backend::NodeSpec {
kind,
name,
file_path: None,
data: serde_json::json!({}),
})
.unwrap();
}
backend.flush_to_disk().unwrap();
let index_path = db_path.with_extension("v3index");
assert!(index_path.exists(), "Index file should exist");
let start = Instant::now();
{
let _backend = V3Backend::open(&db_path).unwrap();
}
let open_with_index = start.elapsed();
std::fs::remove_file(&index_path).unwrap();
let start = Instant::now();
{
let _backend = V3Backend::open(&db_path).unwrap();
}
let open_with_rebuild = start.elapsed();
let speedup = open_with_rebuild.as_nanos() as f64 / open_with_index.as_nanos().max(1) as f64;
println!("Open with persisted index: {:?}", open_with_index);
println!("Open with rebuild: {:?}", open_with_rebuild);
println!("Speedup: {:.2}x", speedup);
assert!(
speedup >= 0.8, "Persisted indexes should not be significantly slower than rebuild, got {:.2}x speedup",
speedup
);
}