|
|
@ -1,6 +1,10 @@ |
|
|
|
|
|
|
|
use failure::Backtrace; |
|
|
|
use std::collections::BTreeMap; |
|
|
|
use std::collections::BTreeMap; |
|
|
|
use std::collections::BTreeSet; |
|
|
|
use std::collections::BTreeSet; |
|
|
|
|
|
|
|
use std::sync::PoisonError; |
|
|
|
use std::sync::RwLock; |
|
|
|
use std::sync::RwLock; |
|
|
|
|
|
|
|
use std::sync::RwLockReadGuard; |
|
|
|
|
|
|
|
use std::sync::RwLockWriteGuard; |
|
|
|
use store::encoded::*; |
|
|
|
use store::encoded::*; |
|
|
|
use store::numeric_encoder::*; |
|
|
|
use store::numeric_encoder::*; |
|
|
|
use Result; |
|
|
|
use Result; |
|
|
@ -71,8 +75,8 @@ impl BytesStore for MemoryStore { |
|
|
|
type BytesOutput = Vec<u8>; |
|
|
|
type BytesOutput = Vec<u8>; |
|
|
|
|
|
|
|
|
|
|
|
fn insert_bytes(&self, value: &[u8]) -> Result<u64> { |
|
|
|
fn insert_bytes(&self, value: &[u8]) -> Result<u64> { |
|
|
|
let mut id2str = self.id2str.write()?; |
|
|
|
let mut id2str = self.id2str.write().map_err(MemoryStorePoisonError::from)?; |
|
|
|
let mut str2id = self.str2id.write()?; |
|
|
|
let mut str2id = self.str2id.write().map_err(MemoryStorePoisonError::from)?; |
|
|
|
let id = str2id.entry(value.to_vec()).or_insert_with(|| { |
|
|
|
let id = str2id.entry(value.to_vec()).or_insert_with(|| { |
|
|
|
let id = id2str.len() as u64; |
|
|
|
let id = id2str.len() as u64; |
|
|
|
id2str.push(value.to_vec()); |
|
|
|
id2str.push(value.to_vec()); |
|
|
@ -83,7 +87,7 @@ impl BytesStore for MemoryStore { |
|
|
|
|
|
|
|
|
|
|
|
fn get_bytes(&self, id: u64) -> Result<Option<Vec<u8>>> { |
|
|
|
fn get_bytes(&self, id: u64) -> Result<Option<Vec<u8>>> { |
|
|
|
//TODO: use try_from when stable
|
|
|
|
//TODO: use try_from when stable
|
|
|
|
let id2str = self.id2str.read()?; |
|
|
|
let id2str = self.id2str.read().map_err(MemoryStorePoisonError::from)?; |
|
|
|
Ok(if id2str.len() as u64 <= id { |
|
|
|
Ok(if id2str.len() as u64 <= id { |
|
|
|
None |
|
|
|
None |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -114,7 +118,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
|
|
|
|
|
|
|
|
fn quads(&self) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
fn quads(&self) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
for (graph_name, graph) in self.graph_indexes.read()?.iter() { |
|
|
|
for (graph_name, graph) in self.graph_indexes()?.iter() { |
|
|
|
for (s, pos) in &graph.spo { |
|
|
|
for (s, pos) in &graph.spo { |
|
|
|
for (p, os) in pos.iter() { |
|
|
|
for (p, os) in pos.iter() { |
|
|
|
for o in os.iter() { |
|
|
|
for o in os.iter() { |
|
|
@ -131,7 +135,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
subject: EncodedTerm, |
|
|
|
subject: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
for (graph_name, graph) in self.graph_indexes.read()?.iter() { |
|
|
|
for (graph_name, graph) in self.graph_indexes()?.iter() { |
|
|
|
if let Some(pos) = graph.spo.get(&subject) { |
|
|
|
if let Some(pos) = graph.spo.get(&subject) { |
|
|
|
for (p, os) in pos.iter() { |
|
|
|
for (p, os) in pos.iter() { |
|
|
|
for o in os.iter() { |
|
|
|
for o in os.iter() { |
|
|
@ -149,7 +153,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
predicate: EncodedTerm, |
|
|
|
predicate: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
for (graph_name, graph) in self.graph_indexes.read()?.iter() { |
|
|
|
for (graph_name, graph) in self.graph_indexes()?.iter() { |
|
|
|
if let Some(pos) = graph.spo.get(&subject) { |
|
|
|
if let Some(pos) = graph.spo.get(&subject) { |
|
|
|
if let Some(os) = pos.get(&predicate) { |
|
|
|
if let Some(os) = pos.get(&predicate) { |
|
|
|
for o in os.iter() { |
|
|
|
for o in os.iter() { |
|
|
@ -168,7 +172,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
object: EncodedTerm, |
|
|
|
object: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
for (graph_name, graph) in self.graph_indexes.read()?.iter() { |
|
|
|
for (graph_name, graph) in self.graph_indexes()?.iter() { |
|
|
|
if let Some(pos) = graph.spo.get(&subject) { |
|
|
|
if let Some(pos) = graph.spo.get(&subject) { |
|
|
|
if let Some(os) = pos.get(&predicate) { |
|
|
|
if let Some(os) = pos.get(&predicate) { |
|
|
|
if os.contains(&object) { |
|
|
|
if os.contains(&object) { |
|
|
@ -191,7 +195,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
object: EncodedTerm, |
|
|
|
object: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
for (graph_name, graph) in self.graph_indexes.read()?.iter() { |
|
|
|
for (graph_name, graph) in self.graph_indexes()?.iter() { |
|
|
|
if let Some(sps) = graph.osp.get(&object) { |
|
|
|
if let Some(sps) = graph.osp.get(&object) { |
|
|
|
if let Some(ps) = sps.get(&subject) { |
|
|
|
if let Some(ps) = sps.get(&subject) { |
|
|
|
for p in ps.iter() { |
|
|
|
for p in ps.iter() { |
|
|
@ -208,7 +212,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
predicate: EncodedTerm, |
|
|
|
predicate: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
for (graph_name, graph) in self.graph_indexes.read()?.iter() { |
|
|
|
for (graph_name, graph) in self.graph_indexes()?.iter() { |
|
|
|
if let Some(oss) = graph.pos.get(&predicate) { |
|
|
|
if let Some(oss) = graph.pos.get(&predicate) { |
|
|
|
for (o, ss) in oss.iter() { |
|
|
|
for (o, ss) in oss.iter() { |
|
|
|
for s in ss.iter() { |
|
|
|
for s in ss.iter() { |
|
|
@ -226,7 +230,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
object: EncodedTerm, |
|
|
|
object: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
for (graph_name, graph) in self.graph_indexes.read()?.iter() { |
|
|
|
for (graph_name, graph) in self.graph_indexes()?.iter() { |
|
|
|
if let Some(oss) = graph.pos.get(&predicate) { |
|
|
|
if let Some(oss) = graph.pos.get(&predicate) { |
|
|
|
if let Some(ss) = oss.get(&object) { |
|
|
|
if let Some(ss) = oss.get(&object) { |
|
|
|
for s in ss.iter() { |
|
|
|
for s in ss.iter() { |
|
|
@ -243,7 +247,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
object: EncodedTerm, |
|
|
|
object: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
for (graph_name, graph) in self.graph_indexes.read()?.iter() { |
|
|
|
for (graph_name, graph) in self.graph_indexes()?.iter() { |
|
|
|
if let Some(sps) = graph.osp.get(&object) { |
|
|
|
if let Some(sps) = graph.osp.get(&object) { |
|
|
|
for (s, ps) in sps.iter() { |
|
|
|
for (s, ps) in sps.iter() { |
|
|
|
for p in ps.iter() { |
|
|
|
for p in ps.iter() { |
|
|
@ -260,7 +264,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
graph_name: EncodedTerm, |
|
|
|
graph_name: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
if let Some(graph) = self.graph_indexes.read()?.get(&graph_name) { |
|
|
|
if let Some(graph) = self.graph_indexes()?.get(&graph_name) { |
|
|
|
for (s, pos) in &graph.spo { |
|
|
|
for (s, pos) in &graph.spo { |
|
|
|
for (p, os) in pos.iter() { |
|
|
|
for (p, os) in pos.iter() { |
|
|
|
for o in os.iter() { |
|
|
|
for o in os.iter() { |
|
|
@ -278,7 +282,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
graph_name: EncodedTerm, |
|
|
|
graph_name: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
if let Some(graph) = self.graph_indexes.read()?.get(&graph_name) { |
|
|
|
if let Some(graph) = self.graph_indexes()?.get(&graph_name) { |
|
|
|
if let Some(pos) = graph.spo.get(&subject) { |
|
|
|
if let Some(pos) = graph.spo.get(&subject) { |
|
|
|
for (p, os) in pos.iter() { |
|
|
|
for (p, os) in pos.iter() { |
|
|
|
for o in os.iter() { |
|
|
|
for o in os.iter() { |
|
|
@ -297,7 +301,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
graph_name: EncodedTerm, |
|
|
|
graph_name: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
if let Some(graph) = self.graph_indexes.read()?.get(&graph_name) { |
|
|
|
if let Some(graph) = self.graph_indexes()?.get(&graph_name) { |
|
|
|
if let Some(pos) = graph.spo.get(&subject) { |
|
|
|
if let Some(pos) = graph.spo.get(&subject) { |
|
|
|
if let Some(os) = pos.get(&predicate) { |
|
|
|
if let Some(os) = pos.get(&predicate) { |
|
|
|
for o in os.iter() { |
|
|
|
for o in os.iter() { |
|
|
@ -316,7 +320,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
graph_name: EncodedTerm, |
|
|
|
graph_name: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
if let Some(graph) = self.graph_indexes.read()?.get(&graph_name) { |
|
|
|
if let Some(graph) = self.graph_indexes()?.get(&graph_name) { |
|
|
|
if let Some(sps) = graph.osp.get(&object) { |
|
|
|
if let Some(sps) = graph.osp.get(&object) { |
|
|
|
if let Some(ps) = sps.get(&subject) { |
|
|
|
if let Some(ps) = sps.get(&subject) { |
|
|
|
for p in ps.iter() { |
|
|
|
for p in ps.iter() { |
|
|
@ -334,7 +338,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
graph_name: EncodedTerm, |
|
|
|
graph_name: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
if let Some(graph) = self.graph_indexes.read()?.get(&graph_name) { |
|
|
|
if let Some(graph) = self.graph_indexes()?.get(&graph_name) { |
|
|
|
if let Some(oss) = graph.pos.get(&predicate) { |
|
|
|
if let Some(oss) = graph.pos.get(&predicate) { |
|
|
|
for (o, ss) in oss.iter() { |
|
|
|
for (o, ss) in oss.iter() { |
|
|
|
for s in ss.iter() { |
|
|
|
for s in ss.iter() { |
|
|
@ -353,7 +357,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
graph_name: EncodedTerm, |
|
|
|
graph_name: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
if let Some(graph) = self.graph_indexes.read()?.get(&graph_name) { |
|
|
|
if let Some(graph) = self.graph_indexes()?.get(&graph_name) { |
|
|
|
if let Some(oss) = graph.pos.get(&predicate) { |
|
|
|
if let Some(oss) = graph.pos.get(&predicate) { |
|
|
|
if let Some(ss) = oss.get(&object) { |
|
|
|
if let Some(ss) = oss.get(&object) { |
|
|
|
for s in ss.iter() { |
|
|
|
for s in ss.iter() { |
|
|
@ -371,7 +375,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
graph_name: EncodedTerm, |
|
|
|
graph_name: EncodedTerm, |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
) -> Result<<Vec<Result<EncodedQuad>> as IntoIterator>::IntoIter> { |
|
|
|
let mut result = Vec::default(); |
|
|
|
let mut result = Vec::default(); |
|
|
|
if let Some(graph) = self.graph_indexes.read()?.get(&graph_name) { |
|
|
|
if let Some(graph) = self.graph_indexes()?.get(&graph_name) { |
|
|
|
if let Some(sps) = graph.osp.get(&object) { |
|
|
|
if let Some(sps) = graph.osp.get(&object) { |
|
|
|
for (s, ps) in sps.iter() { |
|
|
|
for (s, ps) in sps.iter() { |
|
|
|
for p in ps.iter() { |
|
|
|
for p in ps.iter() { |
|
|
@ -385,8 +389,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
|
|
|
|
|
|
|
|
fn contains(&self, quad: &EncodedQuad) -> Result<bool> { |
|
|
|
fn contains(&self, quad: &EncodedQuad) -> Result<bool> { |
|
|
|
Ok(self |
|
|
|
Ok(self |
|
|
|
.graph_indexes |
|
|
|
.graph_indexes()? |
|
|
|
.read()? |
|
|
|
|
|
|
|
.get(&quad.graph_name) |
|
|
|
.get(&quad.graph_name) |
|
|
|
.map_or(false, |graph| { |
|
|
|
.map_or(false, |graph| { |
|
|
|
graph.spo.get(&quad.subject).map_or(false, |po| { |
|
|
|
graph.spo.get(&quad.subject).map_or(false, |po| { |
|
|
@ -397,7 +400,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn insert(&self, quad: &EncodedQuad) -> Result<()> { |
|
|
|
fn insert(&self, quad: &EncodedQuad) -> Result<()> { |
|
|
|
let mut graph_indexes = self.graph_indexes.write()?; |
|
|
|
let mut graph_indexes = self.graph_indexes_mut()?; |
|
|
|
let graph = graph_indexes |
|
|
|
let graph = graph_indexes |
|
|
|
.entry(quad.graph_name) |
|
|
|
.entry(quad.graph_name) |
|
|
|
.or_insert_with(MemoryGraphIndexes::default); |
|
|
|
.or_insert_with(MemoryGraphIndexes::default); |
|
|
@ -426,7 +429,7 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn remove(&self, quad: &EncodedQuad) -> Result<()> { |
|
|
|
fn remove(&self, quad: &EncodedQuad) -> Result<()> { |
|
|
|
let mut graph_indexes = self.graph_indexes.write()?; |
|
|
|
let mut graph_indexes = self.graph_indexes_mut()?; |
|
|
|
let mut empty_graph = false; |
|
|
|
let mut empty_graph = false; |
|
|
|
if let Some(graph) = graph_indexes.get_mut(&quad.graph_name) { |
|
|
|
if let Some(graph) = graph_indexes.get_mut(&quad.graph_name) { |
|
|
|
{ |
|
|
|
{ |
|
|
@ -491,3 +494,35 @@ impl EncodedQuadsStore for MemoryStore { |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl MemoryStore { |
|
|
|
|
|
|
|
fn graph_indexes(&self) -> Result<RwLockReadGuard<BTreeMap<EncodedTerm, MemoryGraphIndexes>>> { |
|
|
|
|
|
|
|
Ok(self |
|
|
|
|
|
|
|
.graph_indexes |
|
|
|
|
|
|
|
.read() |
|
|
|
|
|
|
|
.map_err(MemoryStorePoisonError::from)?) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn graph_indexes_mut( |
|
|
|
|
|
|
|
&self, |
|
|
|
|
|
|
|
) -> Result<RwLockWriteGuard<BTreeMap<EncodedTerm, MemoryGraphIndexes>>> { |
|
|
|
|
|
|
|
Ok(self |
|
|
|
|
|
|
|
.graph_indexes |
|
|
|
|
|
|
|
.write() |
|
|
|
|
|
|
|
.map_err(MemoryStorePoisonError::from)?) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Fail)] |
|
|
|
|
|
|
|
#[fail(display = "MemoryStore Mutex was poisoned")] |
|
|
|
|
|
|
|
pub struct MemoryStorePoisonError { |
|
|
|
|
|
|
|
backtrace: Backtrace, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<T> From<PoisonError<T>> for MemoryStorePoisonError { |
|
|
|
|
|
|
|
fn from(_: PoisonError<T>) -> Self { |
|
|
|
|
|
|
|
Self { |
|
|
|
|
|
|
|
backtrace: Backtrace::new(), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|