A Claude-written graph database in Rust. Use at your own risk.
at main 6.0 kB view raw
1use gigabrain::{Graph}; 2use gigabrain::core::{PropertyValue, relationship::Direction}; 3use std::sync::Arc; 4 5mod dx_helpers; 6use dx_helpers::{GraphDxExt, NodeBuilder}; 7 8#[tokio::main] 9async fn main() -> Result<(), Box<dyn std::error::Error>> { 10 println!("🧠 GigaBrain Basic Example"); 11 println!("========================="); 12 13 // Create a new graph (wrapped in Arc for DX helpers) 14 let graph = Arc::new(Graph::new()); 15 16 // Setup schema 17 { 18 let mut schema = graph.schema().write(); 19 schema.get_or_create_label("Person"); 20 schema.get_or_create_label("Company"); 21 schema.get_or_create_property_key("name"); 22 schema.get_or_create_property_key("age"); 23 schema.get_or_create_property_key("email"); 24 schema.get_or_create_relationship_type("WORKS_FOR"); 25 schema.get_or_create_relationship_type("KNOWS"); 26 } 27 28 // Get schema IDs - they were already created above 29 let (person_label, company_label, name_prop, age_prop, email_prop, works_for_rel, knows_rel) = { 30 let mut schema = graph.schema().write(); 31 ( 32 schema.get_or_create_label("Person"), 33 schema.get_or_create_label("Company"), 34 schema.get_or_create_property_key("name"), 35 schema.get_or_create_property_key("age"), 36 schema.get_or_create_property_key("email"), 37 schema.get_or_create_relationship_type("WORKS_FOR"), 38 schema.get_or_create_relationship_type("KNOWS") 39 ) 40 }; 41 42 // Create some nodes 43 println!("\n📝 Creating nodes..."); 44 45 // Create Alice 46 let alice_id = graph.create_node(); 47 graph.update_node(alice_id, |node| { 48 node.add_label(person_label); 49 node.properties.insert(name_prop, PropertyValue::String("Alice".to_string())); 50 node.properties.insert(age_prop, PropertyValue::Integer(30)); 51 node.properties.insert(email_prop, PropertyValue::String("alice@example.com".to_string())); 52 })?; 53 println!("✅ Created Alice (Person)"); 54 55 // Create Bob 56 let bob_id = graph.create_node(); 57 graph.update_node(bob_id, |node| { 58 node.add_label(person_label); 59 node.properties.insert(name_prop, PropertyValue::String("Bob".to_string())); 60 node.properties.insert(age_prop, PropertyValue::Integer(25)); 61 node.properties.insert(email_prop, PropertyValue::String("bob@example.com".to_string())); 62 })?; 63 println!("✅ Created Bob (Person)"); 64 65 // Create TechCorp 66 let techcorp_id = graph.create_node(); 67 graph.update_node(techcorp_id, |node| { 68 node.add_label(company_label); 69 node.properties.insert(name_prop, PropertyValue::String("TechCorp".to_string())); 70 })?; 71 println!("✅ Created TechCorp (Company)"); 72 73 // Create relationships 74 println!("\n🔗 Creating relationships..."); 75 76 // Alice WORKS_FOR TechCorp 77 graph.create_relationship(alice_id, techcorp_id, works_for_rel)?; 78 println!("✅ Alice works for TechCorp"); 79 80 // Bob WORKS_FOR TechCorp 81 graph.create_relationship(bob_id, techcorp_id, works_for_rel)?; 82 println!("✅ Bob works for TechCorp"); 83 84 // Alice KNOWS Bob 85 graph.create_relationship(alice_id, bob_id, knows_rel)?; 86 println!("✅ Alice knows Bob"); 87 88 // Query the graph 89 println!("\n🔍 Querying the graph..."); 90 91 // Show all nodes 92 let all_nodes = graph.get_all_nodes(); 93 println!("📊 Total nodes: {}", all_nodes.len()); 94 95 // Find all people 96 let mut people = Vec::new(); 97 for node_id in &all_nodes { 98 if let Some(node) = graph.get_node(*node_id) { 99 if node.has_label(person_label) { 100 if let Some(PropertyValue::String(name)) = node.properties.get(&name_prop) { 101 if let Some(PropertyValue::Integer(age)) = node.properties.get(&age_prop) { 102 people.push((name.clone(), *age)); 103 } 104 } 105 } 106 } 107 } 108 109 println!("👥 People in the graph:"); 110 for (name, age) in &people { 111 println!(" - {} (age: {})", name, age); 112 } 113 114 // Find Alice's relationships 115 println!("\n🕸️ Alice's relationships:"); 116 let alice_rels = graph.get_node_relationships(alice_id, gigabrain::core::relationship::Direction::Outgoing, None); 117 let schema = graph.schema().read(); 118 for rel in alice_rels { 119 if let Some(target_node) = graph.get_node(rel.end_node) { 120 if let Some(PropertyValue::String(target_name)) = target_node.properties.get(&name_prop) { 121 if let Some(rel_name) = schema.get_relationship_type_name(rel.rel_type) { 122 println!(" - {} -> {}", rel_name, target_name); 123 } 124 } 125 } 126 } 127 drop(schema); 128 129 // Show graph statistics 130 println!("\n📈 Graph Statistics:"); 131 println!(" Nodes: {}", graph.get_all_nodes().len()); 132 133 let mut total_relationships = 0; 134 for node_id in &all_nodes { 135 let rels = graph.get_node_relationships(*node_id, gigabrain::core::relationship::Direction::Outgoing, None); 136 total_relationships += rels.len(); 137 } 138 println!(" Relationships: {}", total_relationships); 139 140 // Demonstrate DX improvements 141 println!("\n🚀 DX Helper Demonstration:"); 142 143 // Extract schema IDs once to avoid repeated lock acquisition 144 let schema_ids = graph.extract_schema_ids(); 145 146 // Use builder pattern for clean node creation 147 let charlie_id = NodeBuilder::new(graph.clone()) 148 .with_label(schema_ids.user_label)? 149 .with_property(schema_ids.username_prop, PropertyValue::String("charlie".to_string()))? 150 .build(); 151 152 println!("✅ Created charlie using NodeBuilder pattern"); 153 154 // Use ergonomic relationship querying 155 let alice_followers = graph.get_node_relationships_single(alice_id, Direction::Incoming, knows_rel); 156 println!("👥 Alice has {} people who know her", alice_followers.len()); 157 158 println!("\n✨ Example completed successfully!"); 159 160 Ok(()) 161}