use crate::graph::{
Entity, KnowledgeGraph, ObservationDeletion, ObservationInput, ObservationResult, Relation,
};
use crate::storage::Database;
use anyhow::{Context, Result};
use std::path::PathBuf;
use std::sync::Arc;
pub struct KnowledgeGraphManager {
db: Arc<Database>,
}
impl KnowledgeGraphManager {
pub fn new(db_path: PathBuf) -> Result<Self> {
let db = Database::open(&db_path)?;
Ok(Self { db: Arc::new(db) })
}
pub async fn create_entities(&self, entities: Vec<Entity>) -> Result<Vec<Entity>> {
let db = self.db.clone();
tokio::task::spawn_blocking(move || db.create_entities(&entities))
.await
.context("Task panicked")?
}
pub async fn create_relations(&self, relations: Vec<Relation>) -> Result<Vec<Relation>> {
let db = self.db.clone();
tokio::task::spawn_blocking(move || db.create_relations(&relations))
.await
.context("Task panicked")?
}
pub async fn add_observations(
&self,
inputs: Vec<ObservationInput>,
) -> Result<Vec<ObservationResult>> {
let db = self.db.clone();
tokio::task::spawn_blocking(move || db.add_observations(&inputs))
.await
.context("Task panicked")?
}
pub async fn delete_entities(&self, names: Vec<String>) -> Result<usize> {
let db = self.db.clone();
tokio::task::spawn_blocking(move || db.delete_entities(&names))
.await
.context("Task panicked")?
}
pub async fn delete_observations(&self, deletions: Vec<ObservationDeletion>) -> Result<()> {
let db = self.db.clone();
tokio::task::spawn_blocking(move || db.delete_observations(&deletions))
.await
.context("Task panicked")?
}
pub async fn delete_relations(&self, relations: Vec<Relation>) -> Result<usize> {
let db = self.db.clone();
tokio::task::spawn_blocking(move || db.delete_relations(&relations))
.await
.context("Task panicked")?
}
pub async fn read_graph(&self) -> Result<KnowledgeGraph> {
let db = self.db.clone();
tokio::task::spawn_blocking(move || db.read_graph())
.await
.context("Task panicked")?
}
pub async fn search_nodes(&self, query: Option<String>) -> Result<KnowledgeGraph> {
let db = self.db.clone();
tokio::task::spawn_blocking(move || db.search_nodes(query.as_deref()))
.await
.context("Task panicked")?
}
pub async fn open_nodes(&self, names: Vec<String>) -> Result<KnowledgeGraph> {
let db = self.db.clone();
tokio::task::spawn_blocking(move || db.open_nodes(&names))
.await
.context("Task panicked")?
}
}