From ccb931dbc84d0eef8871e4fcb88fb4b4cf465ce3 Mon Sep 17 00:00:00 2001 From: Matthijs van Otterdijk Date: Tue, 19 Sep 2023 12:02:29 +0200 Subject: [PATCH] lookup in array by index --- src/layer/internal/mod.rs | 31 +++++++++++++++++++++++++++++++ src/layer/layer.rs | 7 +++++++ src/layer/simple_builder.rs | 1 + src/store/mod.rs | 30 +++++++++++++++++++++--------- src/store/sync.rs | 12 +++++++++++- 5 files changed, 71 insertions(+), 10 deletions(-) diff --git a/src/layer/internal/mod.rs b/src/layer/internal/mod.rs index 2045ff6d..82575267 100644 --- a/src/layer/internal/mod.rs +++ b/src/layer/internal/mod.rs @@ -8,6 +8,7 @@ mod subject_iterator; use super::id_map::*; use super::layer::*; +use crate::structure::indexed_property::IndexedPropertyCollection; use crate::structure::*; use std::collections::HashSet; @@ -218,6 +219,14 @@ impl InternalLayer { } } + pub fn indexed_property_collection(&self) -> Option<&IndexedPropertyCollection> { + match self { + Base(b) => b.indexed_property_collection.as_ref(), + Child(c) => c.indexed_property_collection.as_ref(), + Rollup(r) => r.internal.indexed_property_collection(), + } + } + pub fn predicate_dict_get(&self, id: usize) -> Option { self.predicate_dictionary().get(id) } @@ -944,6 +953,28 @@ impl Layer for InternalLayer { } } } + fn indexed_property_si(&self, subject: u64, index: usize) -> Option { + let mut cur = self; + loop { + if let Some(ip) = cur.indexed_property_collection() { + if let Some(object) = ip.lookup_index(subject, index) { + return Some(IndexIdTriple::new(subject, index, object)); + } + } + if let Some(parent) = self.immediate_parent() { + cur = parent; + } else { + break; + } + } + None + } + fn indexed_property_s(&self, subject: u64) -> Box + Send> { + todo!(); + } + fn indexed_properties(&self) -> Box + Send> { + todo!(); + } } impl From for InternalLayer { diff --git a/src/layer/layer.rs b/src/layer/layer.rs index 006548ef..0e12bb55 100644 --- a/src/layer/layer.rs +++ b/src/layer/layer.rs @@ -163,6 +163,13 @@ pub trait Layer: Send + Sync { } fn single_triple_sp(&self, subject: u64, predicate: u64) -> Option; + + fn indexed_property_si(&self, subject: u64, index: usize) -> Option; + fn indexed_property_exists(&self, subject: u64, index: usize) -> bool { + self.indexed_property_si(subject, index).is_some() + } + fn indexed_property_s(&self, subject: u64) -> Box + Send>; + fn indexed_properties(&self) -> Box + Send>; } pub struct LayerCounts { diff --git a/src/layer/simple_builder.rs b/src/layer/simple_builder.rs index 7c949581..d4976220 100644 --- a/src/layer/simple_builder.rs +++ b/src/layer/simple_builder.rs @@ -459,6 +459,7 @@ impl LayerBuilder for SimpleLayerBuil builder.add_id_triples(id_additions).await?; builder.remove_id_triples(id_removals).await?; + // TODO: while setting index triples for this layer, we need to make sure we're not duplicating assignments that were already present from the parent. This is currently not yet done bedcause the query logic has not been written yet. builder.set_index_triples(index_id_additions); builder.finalize().await diff --git a/src/store/mod.rs b/src/store/mod.rs index 53c883dd..3a6365b5 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -6,7 +6,9 @@ pub mod sync; use std::path::{Path, PathBuf}; use std::sync::{Arc, RwLock}; -use crate::layer::{IdTriple, Layer, LayerBuilder, LayerCounts, ObjectType, ValueTriple}; +use crate::layer::{ + IdTriple, IndexIdTriple, Layer, LayerBuilder, LayerCounts, ObjectType, ValueTriple, +}; use crate::storage::archive::{ArchiveLayerStore, DirectoryArchiveBackend, LruArchiveBackend}; use crate::storage::directory::{DirectoryLabelStore, DirectoryLayerStore}; use crate::storage::memory::{MemoryLabelStore, MemoryLayerStore}; @@ -624,6 +626,14 @@ impl Layer for StoreLayer { self.layer.id_object_is_node(id) } + fn all_counts(&self) -> LayerCounts { + self.layer.all_counts() + } + + fn clone_boxed(&self) -> Box { + Box::new(self.clone()) + } + fn triple_exists(&self, subject: u64, predicate: u64, object: u64) -> bool { self.layer.triple_exists(subject, predicate, object) } @@ -652,10 +662,6 @@ impl Layer for StoreLayer { self.layer.triples_o(object) } - fn clone_boxed(&self) -> Box { - Box::new(self.clone()) - } - fn triple_addition_count(&self) -> usize { self.layer.triple_addition_count() } @@ -664,13 +670,19 @@ impl Layer for StoreLayer { self.layer.triple_removal_count() } - fn all_counts(&self) -> LayerCounts { - self.layer.all_counts() - } - fn single_triple_sp(&self, subject: u64, predicate: u64) -> Option { self.layer.single_triple_sp(subject, predicate) } + + fn indexed_property_si(&self, subject: u64, index: usize) -> Option { + self.layer.indexed_property_si(subject, index) + } + fn indexed_property_s(&self, subject: u64) -> Box + Send> { + self.layer.indexed_property_s(subject) + } + fn indexed_properties(&self) -> Box + Send> { + self.layer.indexed_properties() + } } /// A named graph in terminus-store. diff --git a/src/store/sync.rs b/src/store/sync.rs index c4b78862..e30f318b 100644 --- a/src/store/sync.rs +++ b/src/store/sync.rs @@ -11,7 +11,7 @@ use tokio::runtime::Runtime; use std::io; use std::path::{Path, PathBuf}; -use crate::layer::{IdTriple, Layer, LayerCounts, ObjectType, ValueTriple}; +use crate::layer::{IdTriple, IndexIdTriple, Layer, LayerCounts, ObjectType, ValueTriple}; use crate::store::{ open_directory_store, open_memory_store, NamedGraph, Store, StoreLayer, StoreLayerBuilder, }; @@ -452,6 +452,16 @@ impl Layer for SyncStoreLayer { fn single_triple_sp(&self, subject: u64, predicate: u64) -> Option { self.inner.single_triple_sp(subject, predicate) } + + fn indexed_property_si(&self, subject: u64, index: usize) -> Option { + self.inner.indexed_property_si(subject, index) + } + fn indexed_property_s(&self, subject: u64) -> Box + Send> { + self.inner.indexed_property_s(subject) + } + fn indexed_properties(&self) -> Box + Send> { + self.inner.indexed_properties() + } } /// A named graph in terminus-store.