linkrs-query 0.1.0

Query engine, parser, and executor for Linkrs
Documentation
use crate::storage::StorageReader;
use linkrs_core::types::VertexId;
use linkrs_core::{Edge, Vertex};
use parking_lot::RwLock;
use std::sync::Arc;

pub struct GraphStorageRef {
    pub storage: Arc<RwLock<dyn StorageReader>>,
    pub space: String,
}

impl GraphStorageRef {
    pub fn new(storage: Arc<RwLock<dyn StorageReader>>, space: String) -> Self {
        Self { storage, space }
    }

    pub fn get_neighbors(&self, node_id: &VertexId) -> Result<Vec<(VertexId, Edge)>, String> {
        use linkrs_core::types::EdgeDirection;
        let reader = self.storage.read();
        let edges = reader
            .get_node_edges(&self.space, node_id, EdgeDirection::Both, &[])
            .map_err(|e| format!("Storage error: {}", e))?;
        let neighbors: Vec<(VertexId, Edge)> = edges
            .into_iter()
            .map(|e| {
                let neighbor_id = if e.src == *node_id { e.dst } else { e.src };
                (neighbor_id, e)
            })
            .collect();
        Ok(neighbors)
    }

    pub fn get_vertex(&self, tag: &str, id: &VertexId) -> Result<Option<Vertex>, String> {
        let reader = self.storage.read();
        reader
            .get_vertex(&self.space, tag, id)
            .map_err(|e| format!("Storage error: {}", e))
    }
}