Skip to main content

grafeo_engine/
admin.rs

1//! Admin API types for database inspection, backup, and maintenance.
2//!
3//! These types support both LPG (Labeled Property Graph) and RDF (Resource Description Framework)
4//! data models.
5
6use std::collections::HashMap;
7use std::path::PathBuf;
8
9use serde::{Deserialize, Serialize};
10
11/// Database mode - either LPG (Labeled Property Graph) or RDF (Triple Store).
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
13#[serde(rename_all = "lowercase")]
14pub enum DatabaseMode {
15    /// Labeled Property Graph mode (nodes with labels and properties, typed edges).
16    Lpg,
17    /// RDF mode (subject-predicate-object triples).
18    Rdf,
19}
20
21impl std::fmt::Display for DatabaseMode {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            DatabaseMode::Lpg => write!(f, "lpg"),
25            DatabaseMode::Rdf => write!(f, "rdf"),
26        }
27    }
28}
29
30/// High-level database information returned by `db.info()`.
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct DatabaseInfo {
33    /// Database mode (LPG or RDF).
34    pub mode: DatabaseMode,
35    /// Number of nodes (LPG) or subjects (RDF).
36    pub node_count: usize,
37    /// Number of edges (LPG) or triples (RDF).
38    pub edge_count: usize,
39    /// Whether the database is backed by a file.
40    pub is_persistent: bool,
41    /// Database file path, if persistent.
42    pub path: Option<PathBuf>,
43    /// Whether WAL is enabled.
44    pub wal_enabled: bool,
45    /// Database version.
46    pub version: String,
47}
48
49/// Detailed database statistics returned by `db.stats()`.
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct DatabaseStats {
52    /// Number of nodes (LPG) or subjects (RDF).
53    pub node_count: usize,
54    /// Number of edges (LPG) or triples (RDF).
55    pub edge_count: usize,
56    /// Number of distinct labels (LPG) or classes (RDF).
57    pub label_count: usize,
58    /// Number of distinct edge types (LPG) or predicates (RDF).
59    pub edge_type_count: usize,
60    /// Number of distinct property keys.
61    pub property_key_count: usize,
62    /// Number of indexes.
63    pub index_count: usize,
64    /// Memory usage in bytes (approximate).
65    pub memory_bytes: usize,
66    /// Disk usage in bytes (if persistent).
67    pub disk_bytes: Option<usize>,
68}
69
70/// Schema information for LPG databases.
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct LpgSchemaInfo {
73    /// All labels used in the database.
74    pub labels: Vec<LabelInfo>,
75    /// All edge types used in the database.
76    pub edge_types: Vec<EdgeTypeInfo>,
77    /// All property keys used in the database.
78    pub property_keys: Vec<String>,
79}
80
81/// Information about a label.
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct LabelInfo {
84    /// The label name.
85    pub name: String,
86    /// Number of nodes with this label.
87    pub count: usize,
88}
89
90/// Information about an edge type.
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct EdgeTypeInfo {
93    /// The edge type name.
94    pub name: String,
95    /// Number of edges with this type.
96    pub count: usize,
97}
98
99/// Schema information for RDF databases.
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct RdfSchemaInfo {
102    /// All predicates used in the database.
103    pub predicates: Vec<PredicateInfo>,
104    /// All named graphs.
105    pub named_graphs: Vec<String>,
106    /// Number of distinct subjects.
107    pub subject_count: usize,
108    /// Number of distinct objects.
109    pub object_count: usize,
110}
111
112/// Information about an RDF predicate.
113#[derive(Debug, Clone, Serialize, Deserialize)]
114pub struct PredicateInfo {
115    /// The predicate IRI.
116    pub iri: String,
117    /// Number of triples using this predicate.
118    pub count: usize,
119}
120
121/// Combined schema information supporting both LPG and RDF.
122#[derive(Debug, Clone, Serialize, Deserialize)]
123#[serde(tag = "mode")]
124pub enum SchemaInfo {
125    /// LPG schema information.
126    #[serde(rename = "lpg")]
127    Lpg(LpgSchemaInfo),
128    /// RDF schema information.
129    #[serde(rename = "rdf")]
130    Rdf(RdfSchemaInfo),
131}
132
133/// Index information.
134#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct IndexInfo {
136    /// Index name.
137    pub name: String,
138    /// Index type (hash, btree, fulltext, etc.).
139    pub index_type: String,
140    /// Target (label:property for LPG, predicate for RDF).
141    pub target: String,
142    /// Whether the index is unique.
143    pub unique: bool,
144    /// Estimated cardinality.
145    pub cardinality: Option<usize>,
146    /// Size in bytes.
147    pub size_bytes: Option<usize>,
148}
149
150/// WAL (Write-Ahead Log) status.
151#[derive(Debug, Clone, Serialize, Deserialize)]
152pub struct WalStatus {
153    /// Whether WAL is enabled.
154    pub enabled: bool,
155    /// WAL file path.
156    pub path: Option<PathBuf>,
157    /// WAL size in bytes.
158    pub size_bytes: usize,
159    /// Number of WAL records.
160    pub record_count: usize,
161    /// Last checkpoint timestamp (Unix epoch seconds).
162    pub last_checkpoint: Option<u64>,
163    /// Current epoch/LSN.
164    pub current_epoch: u64,
165}
166
167/// Validation result.
168#[derive(Debug, Clone, Default, Serialize, Deserialize)]
169pub struct ValidationResult {
170    /// List of validation errors (empty = valid).
171    pub errors: Vec<ValidationError>,
172    /// List of validation warnings.
173    pub warnings: Vec<ValidationWarning>,
174}
175
176impl ValidationResult {
177    /// Returns true if validation passed (no errors).
178    #[must_use]
179    pub fn is_valid(&self) -> bool {
180        self.errors.is_empty()
181    }
182}
183
184/// A validation error.
185#[derive(Debug, Clone, Serialize, Deserialize)]
186pub struct ValidationError {
187    /// Error code.
188    pub code: String,
189    /// Human-readable error message.
190    pub message: String,
191    /// Optional context (e.g., affected entity ID).
192    pub context: Option<String>,
193}
194
195/// A validation warning.
196#[derive(Debug, Clone, Serialize, Deserialize)]
197pub struct ValidationWarning {
198    /// Warning code.
199    pub code: String,
200    /// Human-readable warning message.
201    pub message: String,
202    /// Optional context.
203    pub context: Option<String>,
204}
205
206/// Dump format for export operations.
207#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
208#[serde(rename_all = "lowercase")]
209pub enum DumpFormat {
210    /// Apache Parquet format (default for LPG).
211    Parquet,
212    /// RDF Turtle format (default for RDF).
213    Turtle,
214    /// JSON Lines format.
215    Json,
216}
217
218impl Default for DumpFormat {
219    fn default() -> Self {
220        DumpFormat::Parquet
221    }
222}
223
224impl std::fmt::Display for DumpFormat {
225    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
226        match self {
227            DumpFormat::Parquet => write!(f, "parquet"),
228            DumpFormat::Turtle => write!(f, "turtle"),
229            DumpFormat::Json => write!(f, "json"),
230        }
231    }
232}
233
234impl std::str::FromStr for DumpFormat {
235    type Err = String;
236
237    fn from_str(s: &str) -> Result<Self, Self::Err> {
238        match s.to_lowercase().as_str() {
239            "parquet" => Ok(DumpFormat::Parquet),
240            "turtle" | "ttl" => Ok(DumpFormat::Turtle),
241            "json" | "jsonl" => Ok(DumpFormat::Json),
242            _ => Err(format!("Unknown dump format: {}", s)),
243        }
244    }
245}
246
247/// Compaction statistics returned after a compact operation.
248#[derive(Debug, Clone, Serialize, Deserialize)]
249pub struct CompactionStats {
250    /// Bytes reclaimed.
251    pub bytes_reclaimed: usize,
252    /// Number of nodes compacted.
253    pub nodes_compacted: usize,
254    /// Number of edges compacted.
255    pub edges_compacted: usize,
256    /// Duration in milliseconds.
257    pub duration_ms: u64,
258}
259
260/// Metadata for dump files.
261#[derive(Debug, Clone, Serialize, Deserialize)]
262pub struct DumpMetadata {
263    /// Grafeo version that created the dump.
264    pub version: String,
265    /// Database mode.
266    pub mode: DatabaseMode,
267    /// Dump format.
268    pub format: DumpFormat,
269    /// Number of nodes.
270    pub node_count: usize,
271    /// Number of edges.
272    pub edge_count: usize,
273    /// Timestamp (ISO 8601).
274    pub created_at: String,
275    /// Additional metadata.
276    #[serde(default)]
277    pub extra: HashMap<String, String>,
278}