1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved.
//---------------------------------------------------------------
use {
crate::{DataStoreConnection, FactDomain, Namespaces, Parameters, Statement, Transaction},
indoc::formatdoc,
rdf_store_rs::{consts::LOG_TARGET_DATABASE, Graph, RDFStoreError},
std::{
fmt::{Display, Formatter},
path::Path,
sync::Arc,
time::Instant,
},
};
/// A `GraphConnection` is a wrapper around a
/// [`DataStoreConnection`](DataStoreConnection) with a specific
/// [`Graph`](Graph) and an optional ontology [`Graph`](Graph).
#[derive(Debug)]
pub struct GraphConnection {
pub data_store_connection: Arc<DataStoreConnection>,
started_at: Instant,
pub graph: Graph,
pub ontology_graph: Option<Graph>,
}
impl Display for GraphConnection {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"graph-connection to {:} (on {:})",
self.graph, self.data_store_connection
)
}
}
impl Drop for GraphConnection {
fn drop(&mut self) {
let duration = self.started_at.elapsed();
tracing::trace!(
target: LOG_TARGET_DATABASE,
duration = ?duration,
"Dropped {self}",
)
}
}
impl GraphConnection {
pub fn new(
data_store_connection: Arc<DataStoreConnection>,
graph: Graph,
ontology_graph: Option<Graph>,
) -> Arc<Self> {
let result = Self {
data_store_connection,
started_at: Instant::now(),
graph,
ontology_graph,
};
tracing::trace!("Created {result:}");
Arc::new(result)
}
/// Create a clone with another `DataStoreConnection`
pub fn with_data_store_connection(
&self,
data_store_connection: &Arc<DataStoreConnection>,
) -> Arc<Self> {
Arc::new(Self {
data_store_connection: data_store_connection.clone(),
started_at: self.started_at,
graph: self.graph.clone(),
ontology_graph: self.ontology_graph.clone(),
})
}
pub fn import_data_from_file<P>(&self, file: P) -> Result<(), RDFStoreError>
where P: AsRef<Path> {
self.data_store_connection
.import_data_from_file(file, &self.graph)
}
pub fn import_axioms(&self) -> Result<(), RDFStoreError> {
assert!(
self.ontology_graph.is_some(),
"no ontology graph specified"
);
self.data_store_connection
.import_axioms_from_triples(self.ontology_graph.as_ref().unwrap(), &self.graph)
}
/// Read all RDF files (currently it supports .ttl and .nt files) from
/// the given directory, applying ignore files like `.gitignore`.
///
/// Returns the number of loaded files.
///
/// TODO: Support all the types that RDFox supports (and more)
/// TODO: Support '*.gz' files
/// TODO: Parallelize appropriately in sync with number of threads that
/// RDFox uses
pub fn import_rdf_from_directory(&self, root: &Path) -> Result<u16, RDFStoreError> {
self.data_store_connection
.import_rdf_from_directory(root, &self.graph)
}
/// Get the number of triples using the given transaction.
///
/// TODO: Implement this with SPARQL COUNT (and compare performance)
pub fn get_triples_count(
&self,
tx: &Arc<Transaction>,
fact_domain: FactDomain,
) -> Result<usize, RDFStoreError> {
Statement::new(
&Namespaces::empty()?,
formatdoc!(
r##"
SELECT ?s ?p ?o
FROM {:}
WHERE {{
?s ?p ?o .
}}
"##,
self.graph.as_display_iri()
)
.into(),
)?
.cursor(
&self.data_store_connection,
&Parameters::empty()?.fact_domain(fact_domain)?,
)?
.count(tx)
}
// pub fn get_subjects_count(&self, fact_domain: FactDomain) ->
// Result<std::os::raw::c_ulong, RDFStoreError> { Statement::query(
// &Namespaces::default()?,
// indoc! {r##"
// SELECT DISTINCT ?subject
// WHERE {
// {
// GRAPH ?graph {
// ?subject ?p ?o
// }
// } UNION {
// ?subject ?p ?o .
// BIND("default" AS ?graph)
// }
// }
// "##},
// )?
// .cursor(self, &Parameters::empty()?.fact_domain(fact_domain)?)?
// .count()
// }
//
// pub fn get_predicates_count(&self, fact_domain: FactDomain) ->
// Result<std::os::raw::c_ulong, RDFStoreError> { Statement::query(
// &Namespaces::default()?,
// indoc! {r##"
// SELECT DISTINCT ?predicate
// WHERE {
// {
// GRAPH ?graph {
// ?s ?predicate ?o
// }
// } UNION {
// ?s ?predicate ?o .
// BIND("default" AS ?graph)
// }
// }
// "##},
// )?
// .cursor(self, &Parameters::empty()?.fact_domain(fact_domain)?)?
// .count()
// }
//
// pub fn get_ontologies_count(&self, fact_domain: FactDomain) ->
// Result<std::os::raw::c_ulong, RDFStoreError> { Statement::query(
// &Namespaces::default()?,
// indoc! {r##"
// SELECT DISTINCT ?ontology
// WHERE {
// {
// GRAPH ?graph {
// ?ontology a <http://www.w3.org/2002/07/owl#Ontology>
// }
// } UNION {
// ?ontology a <http://www.w3.org/2002/07/owl#Ontology>
// BIND("default" AS ?graph)
// }
// }
// "##},
// )?
// .cursor(self, &Parameters::empty()?.fact_domain(fact_domain)?)?
// .count()
// }
}