this repo has no description
1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use crate::{NodeId, RelationshipId, LabelId, PropertyKeyId, Result};
4
5pub mod graph;
6pub mod persistent_graph;
7pub mod node;
8pub mod relationship;
9pub mod property;
10pub mod schema_validation;
11
12pub use graph::Graph;
13pub use persistent_graph::PersistentGraph;
14pub use node::Node;
15pub use relationship::Relationship;
16pub use property::{Property, PropertyValue};
17pub use schema_validation::{SchemaValidator, Constraint, PropertyType};
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct GraphSchema {
21 pub labels: HashMap<String, LabelId>,
22 pub property_keys: HashMap<String, PropertyKeyId>,
23 pub relationship_types: HashMap<String, u32>,
24
25 next_label_id: LabelId,
26 next_property_key_id: PropertyKeyId,
27 next_relationship_type_id: u32,
28
29 /// Schema validator for enforcing constraints
30 #[serde(default)]
31 pub validator: SchemaValidator,
32}
33
34impl GraphSchema {
35 pub fn new() -> Self {
36 Self {
37 labels: HashMap::new(),
38 property_keys: HashMap::new(),
39 relationship_types: HashMap::new(),
40 next_label_id: LabelId(0),
41 next_property_key_id: PropertyKeyId(0),
42 next_relationship_type_id: 0,
43 validator: SchemaValidator::new(),
44 }
45 }
46
47 pub fn get_or_create_label(&mut self, label: &str) -> LabelId {
48 if let Some(&id) = self.labels.get(label) {
49 return id;
50 }
51
52 let id = self.next_label_id;
53 self.labels.insert(label.to_string(), id);
54 self.next_label_id = LabelId(self.next_label_id.0 + 1);
55 id
56 }
57
58 pub fn get_or_create_property_key(&mut self, key: &str) -> PropertyKeyId {
59 if let Some(&id) = self.property_keys.get(key) {
60 return id;
61 }
62
63 let id = self.next_property_key_id;
64 self.property_keys.insert(key.to_string(), id);
65 self.next_property_key_id = PropertyKeyId(self.next_property_key_id.0 + 1);
66 id
67 }
68
69 pub fn get_or_create_relationship_type(&mut self, rel_type: &str) -> u32 {
70 if let Some(&id) = self.relationship_types.get(rel_type) {
71 return id;
72 }
73
74 let id = self.next_relationship_type_id;
75 self.relationship_types.insert(rel_type.to_string(), id);
76 self.next_relationship_type_id += 1;
77 id
78 }
79
80 pub fn get_label_name(&self, label_id: LabelId) -> Option<String> {
81 self.labels.iter()
82 .find(|(_, &id)| id == label_id)
83 .map(|(name, _)| name.clone())
84 }
85
86 pub fn get_property_key_name(&self, key_id: PropertyKeyId) -> Option<String> {
87 self.property_keys.iter()
88 .find(|(_, &id)| id == key_id)
89 .map(|(name, _)| name.clone())
90 }
91
92 pub fn get_relationship_type_name(&self, type_id: u32) -> Option<String> {
93 self.relationship_types.iter()
94 .find(|(_, &id)| id == type_id)
95 .map(|(name, _)| name.clone())
96 }
97}