graphrag_core/persistence/mod.rs
1//! Persistence layer for GraphRAG knowledge graphs
2//!
3//! This module provides storage backends for persisting knowledge graphs to disk
4//! and loading them back into memory for fast querying.
5//!
6//! ## Supported Formats
7//!
8//! - **Parquet**: Columnar format for entities, relationships, chunks (Apache Arrow ecosystem)
9//! - **LanceDB**: Vector storage for embeddings (Lance columnar format)
10//! - **JSON**: Human-readable backup format (already implemented in core)
11//! - **GraphML**: Export format for visualization tools (already implemented in core)
12//!
13//! ## Architecture
14//!
15//! ```text
16//! workspace/
17//! ├── default/
18//! │ ├── entities.parquet
19//! │ ├── relationships.parquet
20//! │ ├── chunks.parquet
21//! │ ├── documents.parquet
22//! │ ├── vectors.lance/
23//! │ ├── graph.json
24//! │ └── metadata.toml
25//! └── project_a/
26//! └── ...
27//! ```
28//!
29//! ## Example
30//!
31//! ```no_run
32//! use graphrag_core::{KnowledgeGraph, persistence::WorkspaceManager};
33//!
34//! # fn example() -> graphrag_core::Result<()> {
35//! // Create workspace manager
36//! let workspace = WorkspaceManager::new("./workspace")?;
37//!
38//! // Save graph to default workspace
39//! let graph = KnowledgeGraph::new();
40//! workspace.save_graph(&graph, "default")?;
41//!
42//! // Load graph from workspace
43//! let loaded_graph = workspace.load_graph("default")?;
44//! # Ok(())
45//! # }
46//! ```
47
48use crate::core::Result;
49
50// Submodules
51pub mod workspace;
52
53#[cfg(feature = "persistent-storage")]
54pub mod parquet;
55
56#[cfg(feature = "lancedb")]
57pub mod lance;
58
59// Re-exports (workspace always available)
60pub use workspace::{WorkspaceInfo, WorkspaceManager, WorkspaceMetadata};
61
62#[cfg(feature = "persistent-storage")]
63pub use parquet::{ParquetConfig, ParquetPersistence};
64
65#[cfg(feature = "lancedb")]
66pub use lance::{DistanceMetric, IndexType, LanceConfig, LanceVectorStore, SearchResult};
67
68/// Persistence trait for knowledge graphs
69pub trait Persistence {
70 /// Save knowledge graph to storage
71 fn save(&self, path: &str) -> Result<()>;
72
73 /// Load knowledge graph from storage
74 fn load(path: &str) -> Result<Self>
75 where
76 Self: Sized;
77
78 /// Check if storage exists
79 fn exists(path: &str) -> bool;
80
81 /// Get storage size in bytes
82 fn size(path: &str) -> Result<u64>;
83}