kotoba_query_engine/
types.rs

1//! Common types for the GQL query engine
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5use std::collections::HashMap;
6
7/// Vertex identifier
8pub type VertexId = String;
9
10/// Edge identifier
11pub type EdgeId = String;
12
13/// Graph vertex
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct Vertex {
16    pub id: VertexId,
17    pub labels: Vec<String>,
18    pub properties: HashMap<String, Value>,
19}
20
21/// Graph edge
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct Edge {
24    pub id: EdgeId,
25    pub label: String,
26    pub from_vertex: VertexId,
27    pub to_vertex: VertexId,
28    pub properties: HashMap<String, Value>,
29}
30
31/// Graph path (sequence of vertices and edges)
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct Path {
34    pub vertices: Vec<Vertex>,
35    pub edges: Vec<Edge>,
36}
37
38/// Vertex filter for scanning
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct VertexFilter {
41    pub labels: Option<Vec<String>>,
42    pub property_filters: HashMap<String, PropertyFilter>,
43}
44
45/// Edge filter for scanning
46#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct EdgeFilter {
48    pub labels: Option<Vec<String>>,
49    pub from_vertex: Option<VertexId>,
50    pub to_vertex: Option<VertexId>,
51    pub property_filters: HashMap<String, PropertyFilter>,
52}
53
54/// Property filter
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct PropertyFilter {
57    pub operator: FilterOperator,
58    pub value: Value,
59}
60
61/// Filter operators
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub enum FilterOperator {
64    Equal,
65    NotEqual,
66    LessThan,
67    LessThanOrEqual,
68    GreaterThan,
69    GreaterThanOrEqual,
70    Like,
71    Regex,
72    In,
73    Contains,
74}
75
76/// Path pattern for graph traversals
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct PathPattern {
79    pub start_vertex: VertexId,
80    pub edge_labels: Option<Vec<String>>,
81    pub direction: PathDirection,
82    pub min_length: Option<u32>,
83    pub max_length: Option<u32>,
84}
85
86/// Path direction
87#[derive(Debug, Clone, Serialize, Deserialize)]
88pub enum PathDirection {
89    Outgoing,
90    Incoming,
91    Both,
92}
93
94/// Query execution result
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct QueryExecutionResult {
97    pub query_id: String,
98    pub status: ExecutionStatus,
99    pub result: Option<QueryResult>,
100    pub error: Option<String>,
101    pub execution_time_ms: u64,
102    pub rows_processed: u64,
103}
104
105/// Execution status
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub enum ExecutionStatus {
108    Success,
109    Error,
110    Timeout,
111    Cancelled,
112}
113
114/// Query result data
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct QueryResult {
117    pub columns: Vec<String>,
118    pub rows: Vec<Vec<Value>>,
119    pub statistics: QueryStatistics,
120}
121
122/// Query execution statistics
123#[derive(Debug, Clone, Serialize, Deserialize)]
124pub struct QueryStatistics {
125    pub total_time_ms: u64,
126    pub planning_time_ms: u64,
127    pub execution_time_ms: u64,
128    pub rows_scanned: u64,
129    pub rows_returned: u64,
130    pub indices_used: Vec<String>,
131}
132
133/// Statement execution result
134#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct StatementResult {
136    pub success: bool,
137    pub message: String,
138    pub affected_rows: Option<u64>,
139    pub execution_time_ms: u64,
140}
141
142/// Index lookup result
143#[derive(Debug, Clone, Serialize, Deserialize)]
144pub struct IndexLookupResult {
145    pub index_name: String,
146    pub keys_searched: Vec<Value>,
147    pub results_found: u64,
148    pub lookup_time_ms: u64,
149}
150
151/// Cache entry
152#[derive(Debug, Clone, Serialize, Deserialize)]
153pub struct CacheEntry {
154    pub key: String,
155    pub value: Value,
156    pub created_at: chrono::DateTime<chrono::Utc>,
157    pub ttl: Option<std::time::Duration>,
158}
159
160/// Query plan cost
161#[derive(Debug, Clone, Serialize, Deserialize)]
162pub struct PlanCost {
163    pub cpu_cost: f64,
164    pub io_cost: f64,
165    pub network_cost: f64,
166    pub memory_cost: f64,
167    pub total_cost: f64,
168}
169
170/// Query optimization hint
171#[derive(Debug, Clone, Serialize, Deserialize)]
172pub struct OptimizationHint {
173    pub hint_type: HintType,
174    pub description: String,
175    pub suggested_action: String,
176}
177
178/// Hint types
179#[derive(Debug, Clone, Serialize, Deserialize)]
180pub enum HintType {
181    IndexSuggestion,
182    JoinOptimization,
183    FilterPushdown,
184    QueryRewrite,
185}
186
187/// Query execution context
188#[derive(Debug, Clone, Serialize, Deserialize)]
189pub struct ExecutionContext {
190    pub user_id: Option<String>,
191    pub session_id: String,
192    pub database_name: String,
193    pub query_timeout: std::time::Duration,
194    pub max_memory_mb: u64,
195    pub enable_tracing: bool,
196}
197
198/// Query compilation result
199#[derive(Debug, Clone, Serialize, Deserialize)]
200pub struct CompilationResult {
201    pub success: bool,
202    pub optimized_query: Option<String>,
203    pub execution_plan: Option<String>,
204    pub warnings: Vec<String>,
205    pub errors: Vec<String>,
206}
207
208/// Schema information
209#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct SchemaInfo {
211    pub vertex_labels: Vec<String>,
212    pub edge_labels: Vec<String>,
213    pub property_keys: Vec<String>,
214    pub indices: Vec<IndexInfo>,
215}
216
217/// Index information
218#[derive(Debug, Clone, Serialize, Deserialize)]
219pub struct IndexInfo {
220    pub name: String,
221    pub target_type: IndexTargetType,
222    pub properties: Vec<String>,
223    pub index_type: IndexType,
224}
225
226/// Index target type
227#[derive(Debug, Clone, Serialize, Deserialize)]
228pub enum IndexTargetType {
229    Vertex,
230    Edge,
231}
232
233/// Index type
234#[derive(Debug, Clone, Serialize, Deserialize)]
235pub enum IndexType {
236    BTree,
237    Hash,
238    FullText,
239    Spatial,
240}
241
242/// Query metrics for monitoring
243#[derive(Debug, Clone, Serialize, Deserialize)]
244pub struct QueryMetrics {
245    pub query_type: String,
246    pub execution_count: u64,
247    pub total_execution_time_ms: u64,
248    pub average_execution_time_ms: f64,
249    pub max_execution_time_ms: u64,
250    pub min_execution_time_ms: u64,
251    pub cache_hit_rate: f64,
252    pub error_count: u64,
253}
254
255#[cfg(test)]
256mod tests {
257    use super::*;
258
259    #[test]
260    fn test_vertex_creation() {
261        let mut properties = HashMap::new();
262        properties.insert("name".to_string(), Value::String("Alice".to_string()));
263        properties.insert("age".to_string(), Value::Number(30.into()));
264
265        let vertex = Vertex {
266            id: "v1".to_string(),
267            labels: vec!["Person".to_string()],
268            properties,
269        };
270
271        assert_eq!(vertex.id, "v1");
272        assert_eq!(vertex.labels, vec!["Person"]);
273        assert_eq!(vertex.properties["name"], Value::String("Alice".to_string()));
274    }
275
276    #[test]
277    fn test_edge_creation() {
278        let mut properties = HashMap::new();
279        properties.insert("since".to_string(), Value::Number(2020.into()));
280
281        let edge = Edge {
282            id: "e1".to_string(),
283            label: "FRIENDS_WITH".to_string(),
284            from_vertex: "v1".to_string(),
285            to_vertex: "v2".to_string(),
286            properties,
287        };
288
289        assert_eq!(edge.id, "e1");
290        assert_eq!(edge.label, "FRIENDS_WITH");
291        assert_eq!(edge.from_vertex, "v1");
292        assert_eq!(edge.to_vertex, "v2");
293    }
294}