this repo has no description
1pub mod core;
2pub mod storage;
3pub mod persistence;
4pub mod cypher;
5pub mod algorithms;
6pub mod index;
7pub mod transaction;
8pub mod distributed;
9pub mod error;
10pub mod server;
11pub mod observability;
12pub mod cli;
13pub mod visualization;
14
15pub use core::{Graph, PersistentGraph, Node, Relationship, Property};
16pub use error::{GigabrainError, Result};
17pub use observability::{ObservabilitySystem, HealthLevel, HealthStatus};
18pub use persistence::{PersistentStorage, StorageBackend};
19pub use index::{IndexManager, IndexType, IndexQuery, IndexConfig};
20use serde::{Serialize, Deserialize};
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
23pub struct NodeId(pub u64);
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
26pub struct RelationshipId(pub u64);
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
29pub struct LabelId(pub u32);
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
32pub struct PropertyKeyId(pub u32);
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37 use crate::core::Graph;
38 use crate::cypher::{parse_cypher, QueryExecutor};
39 use crate::storage::MemoryStore;
40 use std::sync::Arc;
41
42 #[tokio::test]
43 async fn test_graph_creation() {
44 let graph = Graph::new();
45
46 // Create nodes
47 let alice = graph.create_node();
48 let bob = graph.create_node();
49
50 // Get schema
51 let mut schema = graph.schema().write();
52 let knows_rel = schema.get_or_create_relationship_type("KNOWS");
53 drop(schema);
54
55 // Create relationship
56 let rel = graph.create_relationship(alice, bob, knows_rel).unwrap();
57
58 // Verify
59 assert!(graph.get_node(alice).is_some());
60 assert!(graph.get_node(bob).is_some());
61 assert!(graph.get_relationship(rel).is_some());
62 }
63
64 #[tokio::test]
65 async fn test_cypher_parsing() {
66 let queries = vec![
67 "MATCH (n) RETURN n",
68 "CREATE (n:Person {name: 'Alice'})",
69 "MATCH (n:Person)-[:KNOWS]->(m) RETURN n, m",
70 // TODO: Add WHERE clause support
71 // "MATCH (n) WHERE n.age > 18 RETURN n",
72 ];
73
74 for query in queries {
75 let result = parse_cypher(query);
76 assert!(result.is_ok(), "Failed to parse: {}", query);
77 }
78 }
79
80 #[tokio::test]
81 async fn test_query_execution() {
82 let graph = Arc::new(Graph::new());
83 let executor = QueryExecutor::new(graph.clone());
84
85 // Test simple MATCH query
86 let query = parse_cypher("MATCH (n) RETURN n").unwrap();
87 let result = executor.execute_query(&query).await;
88 assert!(result.is_ok());
89 }
90
91 #[test]
92 fn test_memory_store() {
93 let store = MemoryStore::new();
94 // Basic storage operations would be tested here
95 // This is a placeholder for more comprehensive storage tests
96 }
97
98 #[tokio::test]
99 async fn test_node_relationships() {
100 let graph = Graph::new();
101
102 let node1 = graph.create_node();
103 let node2 = graph.create_node();
104 let node3 = graph.create_node();
105
106 let mut schema = graph.schema().write();
107 let knows = schema.get_or_create_relationship_type("KNOWS");
108 let follows = schema.get_or_create_relationship_type("FOLLOWS");
109 drop(schema);
110
111 let rel1 = graph.create_relationship(node1, node2, knows).unwrap();
112 let rel2 = graph.create_relationship(node1, node3, follows).unwrap();
113
114 let relationships = graph.get_node_relationships(
115 node1,
116 crate::core::relationship::Direction::Outgoing,
117 None
118 );
119
120 assert_eq!(relationships.len(), 2);
121 assert!(relationships.iter().any(|r| r.id == rel1));
122 assert!(relationships.iter().any(|r| r.id == rel2));
123 }
124
125 #[test]
126 fn test_property_value_hash() {
127 use crate::core::PropertyValue;
128 use std::collections::hash_map::DefaultHasher;
129 use std::hash::{Hash, Hasher};
130
131 let val1 = PropertyValue::String("test".to_string());
132 let val2 = PropertyValue::String("test".to_string());
133 let val3 = PropertyValue::Integer(42);
134
135 let mut hasher1 = DefaultHasher::new();
136 val1.hash(&mut hasher1);
137 let hash1 = hasher1.finish();
138
139 let mut hasher2 = DefaultHasher::new();
140 val2.hash(&mut hasher2);
141 let hash2 = hasher2.finish();
142
143 let mut hasher3 = DefaultHasher::new();
144 val3.hash(&mut hasher3);
145 let hash3 = hasher3.finish();
146
147 assert_eq!(hash1, hash2); // Same values should hash the same
148 assert_ne!(hash1, hash3); // Different values should hash differently
149 }
150
151 #[tokio::test]
152 async fn test_transaction_manager() {
153 use crate::transaction::{TransactionManager, IsolationLevel};
154
155 let tx_manager = TransactionManager::new();
156
157 let tx_id = tx_manager.begin(IsolationLevel::ReadCommitted).unwrap();
158 assert!(tx_manager.commit(tx_id).is_ok());
159
160 let tx_id2 = tx_manager.begin(IsolationLevel::Serializable).unwrap();
161 assert!(tx_manager.rollback(tx_id2).is_ok());
162 }
163
164 #[test]
165 fn test_distributed_sharding() {
166 use crate::distributed::ShardManager;
167
168 let shard_manager = ShardManager::new(4);
169
170 let node1 = NodeId(1);
171 let node2 = NodeId(2);
172 let node3 = NodeId(5); // Should hash to same shard as node1
173
174 let shard1 = shard_manager.get_shard_for_node(node1);
175 let shard2 = shard_manager.get_shard_for_node(node2);
176 let shard3 = shard_manager.get_shard_for_node(node3);
177
178 // Verify deterministic sharding
179 assert_eq!(shard1, shard_manager.get_shard_for_node(node1));
180 assert_eq!(shard3, shard_manager.get_shard_for_node(node3));
181
182 // Different nodes may or may not be on different shards
183 // depending on the hash function, but the behavior should be consistent
184 }
185}