use crate::graph::core::Graph;
use crate::utils::error::Result;
use oxigraph::store::Store;
use std::path::Path;
use std::sync::Arc;
pub struct GraphStore {
store: Arc<Store>,
}
impl GraphStore {
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
let store = Store::open(path.as_ref()).map_err(|e| {
crate::utils::error::Error::new(&format!("Failed to open store: {}", e))
})?;
Ok(Self {
store: Arc::new(store),
})
}
pub fn new() -> Result<Self> {
let store = Store::new().map_err(|e| {
crate::utils::error::Error::new(&format!("Failed to create store: {}", e))
})?;
Ok(Self {
store: Arc::new(store),
})
}
pub fn create_graph(&self) -> Result<Graph> {
Graph::from_store(Arc::clone(&self.store))
}
pub fn inner(&self) -> &Store {
&self.store
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_store_new() {
let store = GraphStore::new().unwrap();
let graph = store.create_graph().unwrap();
assert!(graph.is_empty());
}
#[test]
fn test_store_open_and_create_graph() {
let temp_dir = TempDir::new().unwrap();
let store_path = temp_dir.path().join("test_store");
let store = GraphStore::open(&store_path).unwrap();
let graph = store.create_graph().unwrap();
assert!(graph.is_empty());
assert!(store_path.exists() || !store_path.exists()); }
#[test]
fn test_store_create_graph_and_insert() {
let store = GraphStore::new().unwrap();
let graph = store.create_graph().unwrap();
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice a ex:Person .
"#,
)
.unwrap();
assert!(!graph.is_empty());
assert!(!graph.is_empty());
}
#[test]
fn test_store_multiple_graphs_share_data() {
let store = GraphStore::new().unwrap();
let graph1 = store.create_graph().unwrap();
graph1
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice a ex:Person .
"#,
)
.unwrap();
let graph2 = store.create_graph().unwrap();
assert!(!graph1.is_empty());
assert!(!graph2.is_empty());
assert_eq!(graph1.len(), graph2.len());
}
#[test]
fn test_store_persistent_storage() {
let temp_dir = TempDir::new().unwrap();
let store_path = temp_dir.path().join("persistent_store");
let store1 = GraphStore::open(&store_path).unwrap();
let graph1 = store1.create_graph().unwrap();
graph1
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice a ex:Person .
"#,
)
.unwrap();
let count1 = graph1.len();
drop(graph1);
drop(store1);
let store2 = GraphStore::open(&store_path).unwrap();
let graph2 = store2.create_graph().unwrap();
assert_eq!(graph2.len(), count1);
assert!(!graph2.is_empty());
}
#[test]
fn test_store_inner_access() {
let store = GraphStore::new().unwrap();
let inner = store.inner();
assert_eq!(inner.len().unwrap_or(0), 0);
}
#[test]
fn test_store_resource_cleanup() {
let temp_dir = TempDir::new().unwrap();
let store_path = temp_dir.path().join("cleanup_test");
{
let store = GraphStore::open(&store_path).unwrap();
let graph = store.create_graph().unwrap();
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice a ex:Person .
"#,
)
.unwrap();
}
let store2 = GraphStore::open(&store_path).unwrap();
let graph2 = store2.create_graph().unwrap();
assert!(!graph2.is_empty());
assert!(!graph2.is_empty());
}
}