diff --git a/src/layer/layer.rs b/src/layer/layer.rs index a03437d3..006548ef 100644 --- a/src/layer/layer.rs +++ b/src/layer/layer.rs @@ -199,6 +199,23 @@ impl IdTriple { } } +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct IndexIdTriple { + pub subject: u64, + pub index: usize, + pub object: u64, +} + +impl IndexIdTriple { + pub fn new(subject: u64, index: usize, object: u64) -> Self { + IndexIdTriple { + subject, + index, + object, + } + } +} + /// A triple stored as strings. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ValueTriple { @@ -207,6 +224,13 @@ pub struct ValueTriple { pub object: ObjectType, } +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct IndexValueTriple { + pub subject: String, + pub index: usize, + pub object: ObjectType, +} + impl ValueTriple { /// Construct a triple with a node object. /// diff --git a/src/layer/simple_builder.rs b/src/layer/simple_builder.rs index 024a643a..cb4372a7 100644 --- a/src/layer/simple_builder.rs +++ b/src/layer/simple_builder.rs @@ -38,6 +38,8 @@ pub trait LayerBuilder: Send + Sync { fn remove_value_triple(&mut self, triple: ValueTriple); /// Remove an id triple fn remove_id_triple(&mut self, triple: IdTriple); + fn set_index_value_triple(&mut self, triple: IndexValueTriple); + fn set_index_id_triple(&mut self, triple: IndexIdTriple); /// Commit the layer to storage fn commit(self) -> Pin> + Send>>; /// Commit a boxed layer to storage @@ -55,6 +57,7 @@ pub struct SimpleLayerBuilder { files: LayerFiles, id_additions: Vec, id_removals: Vec, + index_id_additions: Vec, nodes_values_map: HashMap, predicates_map: HashMap, @@ -74,6 +77,7 @@ impl SimpleLayerBuilder { files: LayerFiles::Base(files), id_additions: Vec::with_capacity(0), id_removals: Vec::with_capacity(0), + index_id_additions: Vec::with_capacity(0), nodes_values_map: HashMap::new(), predicates_map: HashMap::new(), @@ -95,6 +99,7 @@ impl SimpleLayerBuilder { files: LayerFiles::Child(files), id_additions: Vec::new(), id_removals: Vec::new(), + index_id_additions: Vec::new(), nodes_values_map: HashMap::new(), predicates_map: HashMap::new(), @@ -106,11 +111,9 @@ impl SimpleLayerBuilder { } } - fn calculate_triple(&mut self, triple: ValueTriple) -> IdTriple { - let subject = ObjectType::Node(triple.subject); - let predicate = triple.predicate; - let object = triple.object; - let subject_id = if let Some(n) = self.nodes_values_map.get(&subject) { + fn calculate_triple_subject(&mut self, subject: String) -> u64 { + let subject = ObjectType::Node(subject); + if let Some(n) = self.nodes_values_map.get(&subject) { *n } else { let node_id = if let Some(node_id) = self @@ -127,9 +130,11 @@ impl SimpleLayerBuilder { self.nodes_values_map.insert(subject, node_id); node_id - }; + } + } - let predicate_id = if let Some(p) = self.predicates_map.get(&predicate) { + fn calculate_triple_predicate(&mut self, predicate: String) -> u64 { + if let Some(p) = self.predicates_map.get(&predicate) { *p } else { let predicate_id = if let Some(predicate_id) = self @@ -146,8 +151,11 @@ impl SimpleLayerBuilder { self.predicates_map.insert(predicate, predicate_id); predicate_id - }; - let object_id = if let Some(o) = self.nodes_values_map.get(&object) { + } + } + + fn calculate_triple_object(&mut self, object: ObjectType) -> u64 { + if let Some(o) = self.nodes_values_map.get(&object) { *o } else { match object { @@ -182,10 +190,23 @@ impl SimpleLayerBuilder { value_id } } - }; + } + } + + fn calculate_triple(&mut self, triple: ValueTriple) -> IdTriple { + let subject_id = self.calculate_triple_subject(triple.subject); + let predicate_id = self.calculate_triple_predicate(triple.predicate); + let object_id = self.calculate_triple_object(triple.object); IdTriple::new(subject_id, predicate_id, object_id) } + + fn calculate_index_triple(&mut self, triple: IndexValueTriple) -> IndexIdTriple { + let subject_id = self.calculate_triple_subject(triple.subject); + let object_id = self.calculate_triple_object(triple.object); + + IndexIdTriple::new(subject_id, triple.index, object_id) + } } impl LayerBuilder for SimpleLayerBuilder { @@ -215,9 +236,17 @@ impl LayerBuilder for SimpleLayerBuil self.id_removals.push(triple); } + fn set_index_value_triple(&mut self, triple: IndexValueTriple) { + let triple = self.calculate_index_triple(triple); + self.set_index_id_triple(triple); + } + + fn set_index_id_triple(&mut self, triple: IndexIdTriple) { + self.index_id_additions.push(triple); + } + fn commit(self) -> Pin> + Send>> { let SimpleLayerBuilder { - name: _, parent, files, mut id_additions, @@ -225,11 +254,10 @@ impl LayerBuilder for SimpleLayerBuil nodes_values_map, predicates_map, - nodes_values_map_count: _, - predicates_map_count: _, node_count, pred_count, val_count, + .. } = self; let parent_node_value_offset = parent .as_ref()