use std::fs;
use std::path::Path;
use crate::Database;
use anyhow::{Context, Result};
pub fn load_named_graph(name: &str) -> Result<Database> {
let graphs_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("tck")
.join("graphs");
let path = {
let nested = graphs_dir.join(name).join(format!("{name}.cypher"));
if nested.exists() {
nested
} else {
graphs_dir.join(format!("{name}.cypher"))
}
};
let source = fs::read_to_string(&path)
.with_context(|| format!("reading named graph {name}: {}", path.display()))?;
let mut db = Database::open_memory().context("opening in-memory database")?;
{
let tx = db.write_tx().context("begin_write")?;
for stmt in split_cypher_statements(&source) {
let stmt = stmt.trim();
if stmt.is_empty() {
continue;
}
tx.query(stmt)
.with_context(|| format!("seeding named graph {name}: {stmt}"))?;
}
tx.commit().context("commit named-graph seed")?;
}
Ok(db)
}
fn split_cypher_statements(source: &str) -> Vec<String> {
source
.split(';')
.map(|s| s.to_string())
.filter(|s| !s.trim().is_empty())
.collect()
}