|
|
@ -2,10 +2,9 @@ |
|
|
|
|
|
|
|
|
|
|
|
use crate::error::UnwrapInfallible; |
|
|
|
use crate::error::UnwrapInfallible; |
|
|
|
use crate::model::*; |
|
|
|
use crate::model::*; |
|
|
|
use crate::sparql::{QueryOptions, QueryResult, SimplePreparedQuery}; |
|
|
|
use crate::sparql::{GraphPattern, QueryOptions, QueryResult, SimplePreparedQuery}; |
|
|
|
use crate::store::numeric_encoder::*; |
|
|
|
use crate::store::numeric_encoder::*; |
|
|
|
use crate::store::*; |
|
|
|
use crate::store::{load_dataset, load_graph, ReadableEncodedStore, WritableEncodedStore}; |
|
|
|
use crate::Result; |
|
|
|
|
|
|
|
use crate::{DatasetSyntax, GraphSyntax}; |
|
|
|
use crate::{DatasetSyntax, GraphSyntax}; |
|
|
|
use std::collections::hash_map::DefaultHasher; |
|
|
|
use std::collections::hash_map::DefaultHasher; |
|
|
|
use std::collections::{HashMap, HashSet}; |
|
|
|
use std::collections::{HashMap, HashSet}; |
|
|
@ -107,7 +106,7 @@ impl MemoryStore { |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
query: &str, |
|
|
|
query: &str, |
|
|
|
options: QueryOptions<'_>, |
|
|
|
options: QueryOptions<'_>, |
|
|
|
) -> Result<MemoryPreparedQuery> { |
|
|
|
) -> crate::Result<MemoryPreparedQuery> { |
|
|
|
Ok(MemoryPreparedQuery(SimplePreparedQuery::new( |
|
|
|
Ok(MemoryPreparedQuery(SimplePreparedQuery::new( |
|
|
|
self.clone(), |
|
|
|
self.clone(), |
|
|
|
query, |
|
|
|
query, |
|
|
@ -120,7 +119,7 @@ impl MemoryStore { |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
graph_pattern: &GraphPattern, |
|
|
|
graph_pattern: &GraphPattern, |
|
|
|
options: QueryOptions<'_>, |
|
|
|
options: QueryOptions<'_>, |
|
|
|
) -> Result<MemoryPreparedQuery> { |
|
|
|
) -> crate::Result<MemoryPreparedQuery> { |
|
|
|
Ok(MemoryPreparedQuery(SimplePreparedQuery::new_from_pattern( |
|
|
|
Ok(MemoryPreparedQuery(SimplePreparedQuery::new_from_pattern( |
|
|
|
self.clone(), |
|
|
|
self.clone(), |
|
|
|
graph_pattern, |
|
|
|
graph_pattern, |
|
|
@ -217,15 +216,16 @@ impl MemoryStore { |
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
pub fn transaction<'a>( |
|
|
|
pub fn transaction<'a>( |
|
|
|
&'a self, |
|
|
|
&'a self, |
|
|
|
f: impl FnOnce(&mut MemoryTransaction<'a>) -> Result<()>, |
|
|
|
f: impl FnOnce(&mut MemoryTransaction<'a>) -> crate::Result<()>, |
|
|
|
) -> Result<()> { |
|
|
|
) -> crate::Result<()> { |
|
|
|
let mut transaction = MemoryTransaction { |
|
|
|
let mut transaction = MemoryTransaction { |
|
|
|
store: self, |
|
|
|
store: self, |
|
|
|
ops: Vec::new(), |
|
|
|
ops: Vec::new(), |
|
|
|
strings: Vec::new(), |
|
|
|
strings: Vec::new(), |
|
|
|
}; |
|
|
|
}; |
|
|
|
f(&mut transaction)?; |
|
|
|
f(&mut transaction)?; |
|
|
|
transaction.commit() |
|
|
|
transaction.commit(); |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Loads a graph file (i.e. triples) into the store.
|
|
|
|
/// Loads a graph file (i.e. triples) into the store.
|
|
|
@ -253,7 +253,7 @@ impl MemoryStore { |
|
|
|
syntax: GraphSyntax, |
|
|
|
syntax: GraphSyntax, |
|
|
|
to_graph_name: &GraphName, |
|
|
|
to_graph_name: &GraphName, |
|
|
|
base_iri: Option<&str>, |
|
|
|
base_iri: Option<&str>, |
|
|
|
) -> Result<()> { |
|
|
|
) -> crate::Result<()> { |
|
|
|
let mut store = self; |
|
|
|
let mut store = self; |
|
|
|
load_graph(&mut store, reader, syntax, to_graph_name, base_iri) |
|
|
|
load_graph(&mut store, reader, syntax, to_graph_name, base_iri) |
|
|
|
} |
|
|
|
} |
|
|
@ -282,7 +282,7 @@ impl MemoryStore { |
|
|
|
reader: impl BufRead, |
|
|
|
reader: impl BufRead, |
|
|
|
syntax: DatasetSyntax, |
|
|
|
syntax: DatasetSyntax, |
|
|
|
base_iri: Option<&str>, |
|
|
|
base_iri: Option<&str>, |
|
|
|
) -> Result<()> { |
|
|
|
) -> crate::Result<()> { |
|
|
|
let mut store = self; |
|
|
|
let mut store = self; |
|
|
|
load_dataset(&mut store, reader, syntax, base_iri) |
|
|
|
load_dataset(&mut store, reader, syntax, base_iri) |
|
|
|
} |
|
|
|
} |
|
|
@ -600,7 +600,7 @@ impl MemoryStore { |
|
|
|
impl StrLookup for MemoryStore { |
|
|
|
impl StrLookup for MemoryStore { |
|
|
|
type Error = Infallible; |
|
|
|
type Error = Infallible; |
|
|
|
|
|
|
|
|
|
|
|
fn get_str(&self, id: StrHash) -> std::result::Result<Option<String>, Infallible> { |
|
|
|
fn get_str(&self, id: StrHash) -> Result<Option<String>, Infallible> { |
|
|
|
//TODO: avoid copy by adding a lifetime limit to get_str
|
|
|
|
//TODO: avoid copy by adding a lifetime limit to get_str
|
|
|
|
self.indexes().get_str(id) |
|
|
|
self.indexes().get_str(id) |
|
|
|
} |
|
|
|
} |
|
|
@ -609,7 +609,7 @@ impl StrLookup for MemoryStore { |
|
|
|
impl StrLookup for MemoryStoreIndexes { |
|
|
|
impl StrLookup for MemoryStoreIndexes { |
|
|
|
type Error = Infallible; |
|
|
|
type Error = Infallible; |
|
|
|
|
|
|
|
|
|
|
|
fn get_str(&self, id: StrHash) -> std::result::Result<Option<String>, Infallible> { |
|
|
|
fn get_str(&self, id: StrHash) -> Result<Option<String>, Infallible> { |
|
|
|
//TODO: avoid copy by adding a lifetime limit to get_str
|
|
|
|
//TODO: avoid copy by adding a lifetime limit to get_str
|
|
|
|
Ok(self.id2str.get(&id).cloned()) |
|
|
|
Ok(self.id2str.get(&id).cloned()) |
|
|
|
} |
|
|
|
} |
|
|
@ -618,7 +618,7 @@ impl StrLookup for MemoryStoreIndexes { |
|
|
|
impl StrContainer for MemoryStore { |
|
|
|
impl StrContainer for MemoryStore { |
|
|
|
type Error = Infallible; |
|
|
|
type Error = Infallible; |
|
|
|
|
|
|
|
|
|
|
|
fn insert_str(&mut self, key: StrHash, value: &str) -> std::result::Result<(), Infallible> { |
|
|
|
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { |
|
|
|
self.indexes_mut().insert_str(key, value) |
|
|
|
self.indexes_mut().insert_str(key, value) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -626,7 +626,7 @@ impl StrContainer for MemoryStore { |
|
|
|
impl<'a> StrContainer for &'a MemoryStore { |
|
|
|
impl<'a> StrContainer for &'a MemoryStore { |
|
|
|
type Error = Infallible; |
|
|
|
type Error = Infallible; |
|
|
|
|
|
|
|
|
|
|
|
fn insert_str(&mut self, key: StrHash, value: &str) -> std::result::Result<(), Infallible> { |
|
|
|
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { |
|
|
|
self.indexes_mut().insert_str(key, value) |
|
|
|
self.indexes_mut().insert_str(key, value) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -634,7 +634,7 @@ impl<'a> StrContainer for &'a MemoryStore { |
|
|
|
impl StrContainer for MemoryStoreIndexes { |
|
|
|
impl StrContainer for MemoryStoreIndexes { |
|
|
|
type Error = Infallible; |
|
|
|
type Error = Infallible; |
|
|
|
|
|
|
|
|
|
|
|
fn insert_str(&mut self, key: StrHash, value: &str) -> std::result::Result<(), Infallible> { |
|
|
|
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { |
|
|
|
self.id2str.entry(key).or_insert_with(|| value.to_owned()); |
|
|
|
self.id2str.entry(key).or_insert_with(|| value.to_owned()); |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
@ -647,7 +647,7 @@ impl<'a> ReadableEncodedStore for MemoryStore { |
|
|
|
predicate: Option<EncodedTerm>, |
|
|
|
predicate: Option<EncodedTerm>, |
|
|
|
object: Option<EncodedTerm>, |
|
|
|
object: Option<EncodedTerm>, |
|
|
|
graph_name: Option<EncodedTerm>, |
|
|
|
graph_name: Option<EncodedTerm>, |
|
|
|
) -> Box<dyn Iterator<Item = Result<EncodedQuad>> + 'b> { |
|
|
|
) -> Box<dyn Iterator<Item = crate::Result<EncodedQuad>> + 'b> { |
|
|
|
Box::new( |
|
|
|
Box::new( |
|
|
|
self.encoded_quads_for_pattern_inner(subject, predicate, object, graph_name) |
|
|
|
self.encoded_quads_for_pattern_inner(subject, predicate, object, graph_name) |
|
|
|
.into_iter() |
|
|
|
.into_iter() |
|
|
@ -659,11 +659,11 @@ impl<'a> ReadableEncodedStore for MemoryStore { |
|
|
|
impl WritableEncodedStore for MemoryStore { |
|
|
|
impl WritableEncodedStore for MemoryStore { |
|
|
|
type Error = Infallible; |
|
|
|
type Error = Infallible; |
|
|
|
|
|
|
|
|
|
|
|
fn insert_encoded(&mut self, quad: &EncodedQuad) -> std::result::Result<(), Infallible> { |
|
|
|
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { |
|
|
|
self.indexes_mut().insert_encoded(quad) |
|
|
|
self.indexes_mut().insert_encoded(quad) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn remove_encoded(&mut self, quad: &EncodedQuad) -> std::result::Result<(), Infallible> { |
|
|
|
fn remove_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { |
|
|
|
self.indexes_mut().remove_encoded(quad) |
|
|
|
self.indexes_mut().remove_encoded(quad) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -671,11 +671,11 @@ impl WritableEncodedStore for MemoryStore { |
|
|
|
impl<'a> WritableEncodedStore for &'a MemoryStore { |
|
|
|
impl<'a> WritableEncodedStore for &'a MemoryStore { |
|
|
|
type Error = Infallible; |
|
|
|
type Error = Infallible; |
|
|
|
|
|
|
|
|
|
|
|
fn insert_encoded(&mut self, quad: &EncodedQuad) -> std::result::Result<(), Infallible> { |
|
|
|
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { |
|
|
|
self.indexes_mut().insert_encoded(quad) |
|
|
|
self.indexes_mut().insert_encoded(quad) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn remove_encoded(&mut self, quad: &EncodedQuad) -> std::result::Result<(), Infallible> { |
|
|
|
fn remove_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { |
|
|
|
self.indexes_mut().remove_encoded(quad) |
|
|
|
self.indexes_mut().remove_encoded(quad) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -683,7 +683,7 @@ impl<'a> WritableEncodedStore for &'a MemoryStore { |
|
|
|
impl WritableEncodedStore for MemoryStoreIndexes { |
|
|
|
impl WritableEncodedStore for MemoryStoreIndexes { |
|
|
|
type Error = Infallible; |
|
|
|
type Error = Infallible; |
|
|
|
|
|
|
|
|
|
|
|
fn insert_encoded(&mut self, quad: &EncodedQuad) -> std::result::Result<(), Infallible> { |
|
|
|
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { |
|
|
|
insert_into_quad_map( |
|
|
|
insert_into_quad_map( |
|
|
|
&mut self.gosp, |
|
|
|
&mut self.gosp, |
|
|
|
quad.graph_name, |
|
|
|
quad.graph_name, |
|
|
@ -729,7 +729,7 @@ impl WritableEncodedStore for MemoryStoreIndexes { |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn remove_encoded(&mut self, quad: &EncodedQuad) -> std::result::Result<(), Infallible> { |
|
|
|
fn remove_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { |
|
|
|
remove_from_quad_map( |
|
|
|
remove_from_quad_map( |
|
|
|
&mut self.gosp, |
|
|
|
&mut self.gosp, |
|
|
|
&quad.graph_name, |
|
|
|
&quad.graph_name, |
|
|
@ -860,7 +860,7 @@ pub struct MemoryPreparedQuery(SimplePreparedQuery<MemoryStore>); |
|
|
|
|
|
|
|
|
|
|
|
impl MemoryPreparedQuery { |
|
|
|
impl MemoryPreparedQuery { |
|
|
|
/// Evaluates the query and returns its results
|
|
|
|
/// Evaluates the query and returns its results
|
|
|
|
pub fn exec(&self) -> Result<QueryResult<'_>> { |
|
|
|
pub fn exec(&self) -> crate::Result<QueryResult<'_>> { |
|
|
|
self.0.exec() |
|
|
|
self.0.exec() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -905,7 +905,7 @@ impl<'a> MemoryTransaction<'a> { |
|
|
|
syntax: GraphSyntax, |
|
|
|
syntax: GraphSyntax, |
|
|
|
to_graph_name: &GraphName, |
|
|
|
to_graph_name: &GraphName, |
|
|
|
base_iri: Option<&str>, |
|
|
|
base_iri: Option<&str>, |
|
|
|
) -> Result<()> { |
|
|
|
) -> crate::Result<()> { |
|
|
|
load_graph(self, reader, syntax, to_graph_name, base_iri) |
|
|
|
load_graph(self, reader, syntax, to_graph_name, base_iri) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -933,7 +933,7 @@ impl<'a> MemoryTransaction<'a> { |
|
|
|
reader: impl BufRead, |
|
|
|
reader: impl BufRead, |
|
|
|
syntax: DatasetSyntax, |
|
|
|
syntax: DatasetSyntax, |
|
|
|
base_iri: Option<&str>, |
|
|
|
base_iri: Option<&str>, |
|
|
|
) -> Result<()> { |
|
|
|
) -> crate::Result<()> { |
|
|
|
load_dataset(self, reader, syntax, base_iri) |
|
|
|
load_dataset(self, reader, syntax, base_iri) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -950,23 +950,22 @@ impl<'a> MemoryTransaction<'a> { |
|
|
|
self.remove_encoded(&quad).unwrap_infallible(); |
|
|
|
self.remove_encoded(&quad).unwrap_infallible(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn commit(self) -> Result<()> { |
|
|
|
fn commit(self) { |
|
|
|
let mut indexes = self.store.indexes_mut(); |
|
|
|
let mut indexes = self.store.indexes_mut(); |
|
|
|
indexes.id2str.extend(self.strings); |
|
|
|
indexes.id2str.extend(self.strings); |
|
|
|
for op in self.ops { |
|
|
|
for op in self.ops { |
|
|
|
match op { |
|
|
|
match op { |
|
|
|
TransactionOp::Insert(quad) => indexes.insert_encoded(&quad)?, |
|
|
|
TransactionOp::Insert(quad) => indexes.insert_encoded(&quad).unwrap_infallible(), |
|
|
|
TransactionOp::Delete(quad) => indexes.remove_encoded(&quad)?, |
|
|
|
TransactionOp::Delete(quad) => indexes.remove_encoded(&quad).unwrap_infallible(), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl StrContainer for MemoryTransaction<'_> { |
|
|
|
impl StrContainer for MemoryTransaction<'_> { |
|
|
|
type Error = Infallible; |
|
|
|
type Error = Infallible; |
|
|
|
|
|
|
|
|
|
|
|
fn insert_str(&mut self, key: StrHash, value: &str) -> std::result::Result<(), Infallible> { |
|
|
|
fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), Infallible> { |
|
|
|
self.strings.push((key, value.to_owned())); |
|
|
|
self.strings.push((key, value.to_owned())); |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
@ -975,12 +974,12 @@ impl StrContainer for MemoryTransaction<'_> { |
|
|
|
impl WritableEncodedStore for MemoryTransaction<'_> { |
|
|
|
impl WritableEncodedStore for MemoryTransaction<'_> { |
|
|
|
type Error = Infallible; |
|
|
|
type Error = Infallible; |
|
|
|
|
|
|
|
|
|
|
|
fn insert_encoded(&mut self, quad: &EncodedQuad) -> std::result::Result<(), Infallible> { |
|
|
|
fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { |
|
|
|
self.ops.push(TransactionOp::Insert(*quad)); |
|
|
|
self.ops.push(TransactionOp::Insert(*quad)); |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn remove_encoded(&mut self, quad: &EncodedQuad) -> std::result::Result<(), Infallible> { |
|
|
|
fn remove_encoded(&mut self, quad: &EncodedQuad) -> Result<(), Infallible> { |
|
|
|
self.ops.push(TransactionOp::Delete(*quad)); |
|
|
|
self.ops.push(TransactionOp::Delete(*quad)); |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|