From e5ff71320778ed8e3f5e68d8797f54bde9b819a5 Mon Sep 17 00:00:00 2001 From: Jacobus Geluk Date: Fri, 19 Aug 2022 17:41:57 +0100 Subject: [PATCH] feat(rdfox): support for datastore params --- src/cursor/cursor.rs | 30 +++++++++++++++++++++++------- src/data_store.rs | 23 ++++++++++++----------- src/data_store_connection.rs | 18 +++++++++--------- src/graph_connection.rs | 19 ++++++++++++++++--- src/parameters.rs | 11 ++++++++++- src/server_connection.rs | 19 +++++++------------ src/streamer.rs | 2 +- src/transaction.rs | 2 +- 8 files changed, 79 insertions(+), 45 deletions(-) diff --git a/src/cursor/cursor.rs b/src/cursor/cursor.rs index b5dce77..76f2d72 100644 --- a/src/cursor/cursor.rs +++ b/src/cursor/cursor.rs @@ -18,7 +18,7 @@ use crate::{ pub struct Cursor<'a> { #[allow(dead_code)] pub inner: *mut CCursor, - pub(crate) connection: &'a DataStoreConnection, + pub(crate) connection: &'a DataStoreConnection<'a>, statement: Statement<'a>, } @@ -70,24 +70,40 @@ impl<'a> Cursor<'a> { Ok(cursor) } - pub fn count(&mut self) -> Result { self.execute_and_rollback(1000000000, |_row| Ok(())) } + pub fn count(&mut self) -> Result { + self.execute_and_rollback(1000000000, |_row| Ok(())) + } pub fn count_in_transaction(&mut self, tx: &mut Transaction) -> Result { self.consume(tx, 1000000000, |_row| Ok(())) } - pub fn consume(&mut self, tx: &mut Transaction, maxrow: u64, mut f: T) -> Result - where T: FnMut(CursorRow) -> Result<(), Error> { + pub fn consume( + &mut self, + tx: &mut Transaction, + maxrow: u64, + mut f: T, + ) -> Result + where + T: FnMut(CursorRow) -> Result<(), Error>, + { let (mut opened_cursor, mut multiplicity) = OpenedCursor::new(self, &tx)?; let mut rowid = 0_u64; let mut count = 0_u64; while multiplicity > 0 { if multiplicity >= maxrow { - return Err(Error::MultiplicityExceededMaximumNumberOfRows { maxrow, multiplicity, query: self.statement.text.clone() }) + return Err(Error::MultiplicityExceededMaximumNumberOfRows { + maxrow, + multiplicity, + query: self.statement.text.clone(), + }) } rowid += 1; if rowid >= maxrow { - return Err(Error::ExceededMaximumNumberOfRows { maxrow, query: self.statement.text.clone() }) + return Err(Error::ExceededMaximumNumberOfRows { + maxrow, + query: self.statement.text.clone(), + }) } count += multiplicity; let row = CursorRow { @@ -105,7 +121,7 @@ impl<'a> Cursor<'a> { pub fn update_and_commit(&mut self, maxrow: u64, f: T) -> Result where T: FnMut(CursorRow) -> Result<(), Error> { let mut tx = Transaction::begin_read_write(self.connection)?; - self.update_and_commit_in_transaction(&mut tx, maxrow,f) + self.update_and_commit_in_transaction(&mut tx, maxrow, f) } pub fn execute_and_rollback(&mut self, maxrow: u64, f: T) -> Result diff --git a/src/data_store.rs b/src/data_store.rs index 7c0462c..3b18e36 100644 --- a/src/data_store.rs +++ b/src/data_store.rs @@ -1,27 +1,28 @@ use std::fmt::{Display, Formatter}; -use crate::error::Error; -use crate::ServerConnection; +use crate::{error::Error, Parameters, ServerConnection}; #[derive(Debug, PartialEq, Clone)] -pub struct DataStore { - pub(crate) name: String, +pub struct DataStore<'a> { + pub name: String, + pub parameters: &'a Parameters, } -impl Display for DataStore { +impl<'a> Display for DataStore<'a> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "data store [{}]", self.name) } } -impl DataStore { - pub fn declare(name: &str) -> Self { - Self { +impl<'a> DataStore<'a> { + pub fn declare_with_parameters(name: &str, parameters: &'a Parameters) -> Result { + Ok(Self { name: name.to_string(), - } + parameters, + }) } - pub fn create(self, server_connection: &ServerConnection) -> Result { - server_connection.create_data_store(self) + pub fn create(self, server_connection: &'a ServerConnection) -> Result<(), Error> { + server_connection.create_data_store(&self).map(|_| ()) } } diff --git a/src/data_store_connection.rs b/src/data_store_connection.rs index 2b4dbb2..9b646f1 100644 --- a/src/data_store_connection.rs +++ b/src/data_store_connection.rs @@ -46,13 +46,13 @@ use crate::{ }; #[derive(Debug, PartialEq)] -pub struct DataStoreConnection { - pub data_store: DataStore, +pub struct DataStoreConnection<'a> { + pub data_store: &'a DataStore<'a>, pub(crate) inner: *mut CDataStoreConnection, started_at: Instant, } -impl Display for DataStoreConnection { +impl<'a> Display for DataStoreConnection<'a> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_str("connection").unwrap(); // match self.get_id() { @@ -68,15 +68,15 @@ impl Display for DataStoreConnection { } } -impl Drop for DataStoreConnection { +impl<'a> Drop for DataStoreConnection<'a> { fn drop(&mut self) { let duration = self.started_at.elapsed(); log::info!("dropped {self} after {:?}", duration) } } -impl DataStoreConnection { - pub(crate) fn new(data_store: DataStore, inner: *mut CDataStoreConnection) -> Self { +impl<'a> DataStoreConnection<'a> { + pub(crate) fn new(data_store: &'a DataStore<'a>, inner: *mut CDataStoreConnection) -> Self { Self { data_store, inner, @@ -224,9 +224,9 @@ impl DataStoreConnection { Ok(count) } - pub fn evaluate_update<'a>( + pub fn evaluate_update<'b>( &self, - statement: &'a Statement<'a>, + statement: &'b Statement<'b>, parameters: &Parameters, ) -> Result<(), Error> { let base_iri = ptr::null_mut(); @@ -249,7 +249,7 @@ impl DataStoreConnection { Ok(()) } - pub fn evaluate_to_stream<'a, W>( + pub fn evaluate_to_stream( &'a self, writer: W, statement: &'a Statement<'a>, diff --git a/src/graph_connection.rs b/src/graph_connection.rs index e19bc6d..292a6fa 100644 --- a/src/graph_connection.rs +++ b/src/graph_connection.rs @@ -9,10 +9,19 @@ use std::{ use indoc::formatdoc; -use crate::{error::Error, DataStoreConnection, FactDomain, Graph, Parameters, Prefixes, Statement, Transaction}; +use crate::{ + error::Error, + DataStoreConnection, + FactDomain, + Graph, + Parameters, + Prefixes, + Statement, + Transaction, +}; pub struct GraphConnection<'a> { - pub data_store_connection: &'a DataStoreConnection, + pub data_store_connection: &'a DataStoreConnection<'a>, started_at: Instant, pub graph: Graph, pub ontology_graph: Option, @@ -73,7 +82,11 @@ impl<'a> GraphConnection<'a> { .import_rdf_from_directory(root, &self.graph) } - pub fn get_triples_count(&self, tx: &mut Transaction, fact_domain: FactDomain) -> Result { + pub fn get_triples_count( + &self, + tx: &mut Transaction, + fact_domain: FactDomain, + ) -> Result { Statement::new( &Prefixes::default()?, formatdoc!( diff --git a/src/parameters.rs b/src/parameters.rs index da8f215..915ed91 100644 --- a/src/parameters.rs +++ b/src/parameters.rs @@ -30,7 +30,7 @@ pub enum FactDomain { pub enum PersistenceMode { File, FileSequence, - Off + Off, } impl Display for PersistenceMode { @@ -43,6 +43,7 @@ impl Display for PersistenceMode { } } +#[derive(Debug, Clone, PartialEq)] pub struct Parameters { pub(crate) inner: *mut CParameters, } @@ -128,6 +129,14 @@ impl Parameters { } } + pub fn import_rename_user_blank_nodes(self, setting: bool) -> Result { + self.set_string( + "import.rename-user-blank-nodes", + format!("{setting:?}").as_str(), + )?; + Ok(self) + } + /// If true, all API calls are recorded in a script that /// the shell can replay later. later. /// The default value is false. diff --git a/src/server_connection.rs b/src/server_connection.rs index a324410..db99448 100644 --- a/src/server_connection.rs +++ b/src/server_connection.rs @@ -7,7 +7,6 @@ use crate::{ database_call, error::Error, root::{ - CParameters_getEmptyParameters, CServerConnection, CServerConnection_createDataStore, CServerConnection_deleteDataStore, @@ -75,7 +74,7 @@ impl ServerConnection { Ok(()) } - pub fn create_data_store(&self, data_store: DataStore) -> Result { + pub fn create_data_store<'a>(&self, data_store: &'a DataStore<'a>) -> Result<(), Error> { log::debug!("Creating {data_store}"); let c_name = CString::new(data_store.name.as_str()).unwrap(); database_call!( @@ -83,23 +82,19 @@ impl ServerConnection { CServerConnection_createDataStore( self.inner, c_name.as_ptr(), - CParameters_getEmptyParameters(), + data_store.parameters.inner, ) )?; log::info!("Created {data_store}"); - Ok(data_store) - } - - pub fn create_data_store_named(&self, name: &str) -> Result { - self.create_data_store(DataStore::declare(name)) + Ok(()) } - pub fn connect_to_data_store( + pub fn connect_to_data_store<'a>( &self, - data_store: DataStore, - ) -> Result { + data_store: &'a DataStore<'a>, + ) -> Result, Error> { log::debug!("Connecting to {}", data_store); - let mut ds_connection = DataStoreConnection::new(data_store.clone(), ptr::null_mut()); + let mut ds_connection = DataStoreConnection::new(data_store, ptr::null_mut()); let c_name = CString::new(data_store.name.as_str()).unwrap(); database_call!( "creating a datastore connection", diff --git a/src/streamer.rs b/src/streamer.rs index ea6900f..1664700 100644 --- a/src/streamer.rs +++ b/src/streamer.rs @@ -39,7 +39,7 @@ impl<'a, W: 'a + Write + Debug> Drop for RefToSelf<'a, W> { /// to handle the various callbacks from the underlying C-API to RDFox. #[derive(PartialEq, Debug)] pub struct Streamer<'a, W: 'a + Write + Debug> { - pub connection: &'a DataStoreConnection, + pub connection: &'a DataStoreConnection<'a>, pub writer: W, pub statement: &'a Statement<'a>, pub mime_type: &'static Mime, diff --git a/src/transaction.rs b/src/transaction.rs index 1bc9686..603d3cc 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -15,7 +15,7 @@ use crate::{ #[derive(Debug)] pub struct Transaction<'a> { - pub connection: &'a DataStoreConnection, + pub connection: &'a DataStoreConnection<'a>, committed: bool, }