Skip to content

Commit

Permalink
Make various triple iterators clonable
Browse files Browse the repository at this point in the history
  • Loading branch information
matko committed Nov 6, 2023
1 parent d0b7fad commit 4443c52
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 90 deletions.
23 changes: 12 additions & 11 deletions src/layer/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod subject_iterator;

use super::id_map::*;
use super::layer::*;
use crate::structure::util::ClonableIterator;
use crate::structure::*;

use std::collections::HashSet;
Expand Down Expand Up @@ -325,7 +326,7 @@ impl InternalLayer {
pub fn internal_triple_additions_s(
&self,
subject: u64,
) -> Box<dyn Iterator<Item = IdTriple> + Send> {
) -> Box<dyn ClonableIterator<Item = IdTriple> + Send> {
Box::new(
self.internal_triple_additions()
.seek_subject(subject)
Expand All @@ -336,7 +337,7 @@ impl InternalLayer {
pub fn internal_triple_removals_s(
&self,
subject: u64,
) -> Box<dyn Iterator<Item = IdTriple> + Send> {
) -> Box<dyn ClonableIterator<Item = IdTriple> + Send> {
Box::new(
self.internal_triple_removals()
.seek_subject(subject)
Expand All @@ -348,7 +349,7 @@ impl InternalLayer {
&self,
subject: u64,
predicate: u64,
) -> Box<dyn Iterator<Item = IdTriple> + Send> {
) -> Box<dyn ClonableIterator<Item = IdTriple> + Send> {
Box::new(
self.internal_triple_additions()
.seek_subject_predicate(subject, predicate)
Expand All @@ -360,7 +361,7 @@ impl InternalLayer {
&self,
subject: u64,
predicate: u64,
) -> Box<dyn Iterator<Item = IdTriple> + Send> {
) -> Box<dyn ClonableIterator<Item = IdTriple> + Send> {
Box::new(
self.internal_triple_removals()
.seek_subject_predicate(subject, predicate)
Expand Down Expand Up @@ -412,7 +413,7 @@ impl InternalLayer {
pub fn internal_triple_additions_o(
&self,
object: u64,
) -> Box<dyn Iterator<Item = IdTriple> + Send> {
) -> Box<dyn ClonableIterator<Item = IdTriple> + Send> {
Box::new(
self.internal_triple_additions_by_object()
.seek_object(object)
Expand All @@ -432,7 +433,7 @@ impl InternalLayer {
pub fn internal_triple_removals_o(
&self,
object: u64,
) -> Box<dyn Iterator<Item = IdTriple> + Send> {
) -> Box<dyn ClonableIterator<Item = IdTriple> + Send> {
Box::new(
self.internal_triple_removals_by_object()
.seek_object(object)
Expand Down Expand Up @@ -837,11 +838,11 @@ impl Layer for InternalLayer {
}
}

fn triples(&self) -> Box<dyn Iterator<Item = IdTriple> + Send> {
fn triples(&self) -> Box<dyn ClonableIterator<Item = IdTriple> + Send> {
Box::new(InternalTripleSubjectIterator::from_layer(self))
}

fn triples_s(&self, subject: u64) -> Box<dyn Iterator<Item = IdTriple> + Send> {
fn triples_s(&self, subject: u64) -> Box<dyn ClonableIterator<Item = IdTriple> + Send> {
Box::new(
InternalTripleSubjectIterator::from_layer(self)
.seek_subject(subject)
Expand All @@ -853,19 +854,19 @@ impl Layer for InternalLayer {
&self,
subject: u64,
predicate: u64,
) -> Box<dyn Iterator<Item = IdTriple> + Send> {
) -> Box<dyn ClonableIterator<Item = IdTriple> + Send> {
Box::new(
InternalTripleSubjectIterator::from_layer(self)
.seek_subject_predicate(subject, predicate)
.take_while(move |t| t.subject == subject && t.predicate == predicate),
)
}

fn triples_p(&self, predicate: u64) -> Box<dyn Iterator<Item = IdTriple> + Send> {
fn triples_p(&self, predicate: u64) -> Box<dyn ClonableIterator<Item = IdTriple> + Send> {
Box::new(InternalTriplePredicateIterator::from_layer(self, predicate))
}

fn triples_o(&self, object: u64) -> Box<dyn Iterator<Item = IdTriple> + Send> {
fn triples_o(&self, object: u64) -> Box<dyn ClonableIterator<Item = IdTriple> + Send> {
Box::new(
InternalTripleObjectIterator::from_layer(self)
.seek_object(object)
Expand Down
2 changes: 2 additions & 0 deletions src/layer/internal/object_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl Iterator for InternalLayerTripleObjectIterator {
}
}

#[derive(Clone)]
pub struct OptInternalLayerTripleObjectIterator(pub Option<InternalLayerTripleObjectIterator>);

impl OptInternalLayerTripleObjectIterator {
Expand Down Expand Up @@ -142,6 +143,7 @@ impl Iterator for OptInternalLayerTripleObjectIterator {
}
}

#[derive(Clone)]
pub struct InternalTripleObjectIterator {
positives: Vec<OptInternalLayerTripleObjectIterator>,
negatives: Vec<OptInternalLayerTripleObjectIterator>,
Expand Down
1 change: 1 addition & 0 deletions src/layer/internal/predicate_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl Iterator for OptInternalLayerTriplePredicateIterator {
}
}

#[derive(Clone)]
pub struct InternalTriplePredicateIterator {
positives: Vec<OptInternalLayerTriplePredicateIterator>,
negatives: Vec<OptInternalLayerTriplePredicateIterator>,
Expand Down
17 changes: 10 additions & 7 deletions src/layer/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::collections::HashMap;
use std::hash::Hash;

use crate::structure::{TdbDataType, TypedDictEntry};
use crate::structure::{util::ClonableIterator, TdbDataType, TypedDictEntry};

/// A layer containing dictionary entries and triples.
///
Expand Down Expand Up @@ -87,11 +87,14 @@ pub trait Layer: Send + Sync {
}

/// Iterator over all triples known to this layer.
fn triples(&self) -> Box<dyn Iterator<Item = IdTriple> + Send>;
fn triples(&self) -> Box<dyn ClonableIterator<Item = IdTriple> + Send>;

fn triples_s(&self, subject: u64) -> Box<dyn Iterator<Item = IdTriple> + Send>;
fn triples_sp(&self, subject: u64, predicate: u64)
-> Box<dyn Iterator<Item = IdTriple> + Send>;
fn triples_s(&self, subject: u64) -> Box<dyn ClonableIterator<Item = IdTriple> + Send>;
fn triples_sp(
&self,
subject: u64,
predicate: u64,
) -> Box<dyn ClonableIterator<Item = IdTriple> + Send>;

/// Convert a `ValueTriple` to an `IdTriple`, returning None if any of the strings in the triple could not be resolved.
fn value_triple_to_id(&self, triple: &ValueTriple) -> Option<IdTriple> {
Expand All @@ -110,9 +113,9 @@ pub trait Layer: Send + Sync {
})
}

fn triples_p(&self, predicate: u64) -> Box<dyn Iterator<Item = IdTriple> + Send>;
fn triples_p(&self, predicate: u64) -> Box<dyn ClonableIterator<Item = IdTriple> + Send>;

fn triples_o(&self, object: u64) -> Box<dyn Iterator<Item = IdTriple> + Send>;
fn triples_o(&self, object: u64) -> Box<dyn ClonableIterator<Item = IdTriple> + Send>;

/// Convert all known strings in the given string triple to ids.
fn value_triple_to_partially_resolved(&self, triple: ValueTriple) -> PartiallyResolvedTriple {
Expand Down
21 changes: 11 additions & 10 deletions src/storage/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::layer::*;
use crate::layer::*;
use crate::structure::util::ClonableIterator;
use crate::structure::{StringDict, TypedDict};
use async_trait::async_trait;
use std::collections::HashMap;
Expand Down Expand Up @@ -402,7 +403,7 @@ impl LayerStore for CachedLayerStore {
&self,
layer: [u32; 5],
subject: u64,
) -> io::Result<Box<dyn Iterator<Item = IdTriple> + Send>> {
) -> io::Result<Box<dyn ClonableIterator<Item = IdTriple> + Send>> {
if let Some(cached) = self.cache.get_layer_from_cache(layer) {
if !cached.is_rollup() {
return Ok(cached.internal_triple_additions_s(subject));
Expand All @@ -416,7 +417,7 @@ impl LayerStore for CachedLayerStore {
&self,
layer: [u32; 5],
subject: u64,
) -> io::Result<Box<dyn Iterator<Item = IdTriple> + Send>> {
) -> io::Result<Box<dyn ClonableIterator<Item = IdTriple> + Send>> {
if let Some(cached) = self.cache.get_layer_from_cache(layer) {
if !cached.is_rollup() {
return Ok(cached.internal_triple_removals_s(subject));
Expand All @@ -431,7 +432,7 @@ impl LayerStore for CachedLayerStore {
layer: [u32; 5],
subject: u64,
predicate: u64,
) -> io::Result<Box<dyn Iterator<Item = IdTriple> + Send>> {
) -> io::Result<Box<dyn ClonableIterator<Item = IdTriple> + Send>> {
if let Some(cached) = self.cache.get_layer_from_cache(layer) {
if !cached.is_rollup() {
return Ok(cached.internal_triple_additions_sp(subject, predicate));
Expand All @@ -448,7 +449,7 @@ impl LayerStore for CachedLayerStore {
layer: [u32; 5],
subject: u64,
predicate: u64,
) -> io::Result<Box<dyn Iterator<Item = IdTriple> + Send>> {
) -> io::Result<Box<dyn ClonableIterator<Item = IdTriple> + Send>> {
if let Some(cached) = self.cache.get_layer_from_cache(layer) {
if !cached.is_rollup() {
return Ok(cached.internal_triple_removals_sp(subject, predicate));
Expand All @@ -464,11 +465,11 @@ impl LayerStore for CachedLayerStore {
&self,
layer: [u32; 5],
predicate: u64,
) -> io::Result<Box<dyn Iterator<Item = IdTriple> + Send>> {
) -> io::Result<Box<dyn ClonableIterator<Item = IdTriple> + Send>> {
if let Some(cached) = self.cache.get_layer_from_cache(layer) {
if !cached.is_rollup() {
return Ok(Box::new(cached.internal_triple_additions_p(predicate))
as Box<dyn Iterator<Item = _> + Send>);
as Box<dyn ClonableIterator<Item = _> + Send>);
}
}

Expand All @@ -479,11 +480,11 @@ impl LayerStore for CachedLayerStore {
&self,
layer: [u32; 5],
predicate: u64,
) -> io::Result<Box<dyn Iterator<Item = IdTriple> + Send>> {
) -> io::Result<Box<dyn ClonableIterator<Item = IdTriple> + Send>> {
if let Some(cached) = self.cache.get_layer_from_cache(layer) {
if !cached.is_rollup() {
return Ok(Box::new(cached.internal_triple_removals_p(predicate))
as Box<dyn Iterator<Item = _> + Send>);
as Box<dyn ClonableIterator<Item = _> + Send>);
}
}

Expand All @@ -494,7 +495,7 @@ impl LayerStore for CachedLayerStore {
&self,
layer: [u32; 5],
object: u64,
) -> io::Result<Box<dyn Iterator<Item = IdTriple> + Send>> {
) -> io::Result<Box<dyn ClonableIterator<Item = IdTriple> + Send>> {
if let Some(cached) = self.cache.get_layer_from_cache(layer) {
if !cached.is_rollup() {
return Ok(cached.internal_triple_additions_o(object));
Expand All @@ -508,7 +509,7 @@ impl LayerStore for CachedLayerStore {
&self,
layer: [u32; 5],
object: u64,
) -> io::Result<Box<dyn Iterator<Item = IdTriple> + Send>> {
) -> io::Result<Box<dyn ClonableIterator<Item = IdTriple> + Send>> {
if let Some(cached) = self.cache.get_layer_from_cache(layer) {
if !cached.is_rollup() {
return Ok(cached.internal_triple_removals_o(object));
Expand Down
Loading

0 comments on commit 4443c52

Please sign in to comment.