Skip to content

Commit 685e2a9

Browse files
committed
do not keep reopening the store
1 parent 1961d34 commit 685e2a9

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

lib/src/lib.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use std::fs;
2929
use std::io::{BufReader, Write};
3030
use std::path::Path;
3131
use walkdir::WalkDir;
32+
use std::fmt;
3233

3334
// custom derive for ontologies field as vec of Ontology
3435
fn ontologies_ser<S>(
@@ -97,7 +98,7 @@ impl std::fmt::Display for EnvironmentStatus {
9798
}
9899
}
99100

100-
#[derive(Serialize, Deserialize, Debug)]
101+
#[derive(Serialize, Deserialize)]
101102
pub struct OntoEnv {
102103
config: Config,
103104
#[serde(serialize_with = "ontologies_ser", deserialize_with = "ontologies_de")]
@@ -106,10 +107,23 @@ pub struct OntoEnv {
106107
#[serde(skip)]
107108
read_only: bool,
108109
how_created: HowCreated,
110+
#[serde(skip)]
111+
inner_store: Option<Store>,
109112
}
110113

111114
// probably need some graph "identifier" that incorporates location and version..
112115

116+
// format everything EXCEPT inner_store
117+
impl fmt::Debug for OntoEnv {
118+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
119+
write!(
120+
f,
121+
"OntoEnv {{ config: {:?}, ontologies: {:?}, dependency_graph: {:?}, read_only: {:?}, how_created: {:?} }}",
122+
self.config, self.ontologies, self.dependency_graph, self.read_only, self.how_created
123+
)
124+
}
125+
}
126+
113127
impl OntoEnv {
114128
/// Create a new OntoEnv with the given configuration. Will error if the
115129
/// environment already exists and recreate is false.
@@ -161,25 +175,36 @@ impl OntoEnv {
161175
std::fs::create_dir_all(&ontoenv_dir)?;
162176

163177
// create the store in the root/.ontoenv/store.db directory
164-
Ok(Self {
178+
let mut env = Self {
165179
config,
166180
ontologies: HashMap::new(),
167181
dependency_graph: DiGraph::new(),
168182
read_only: false,
169183
how_created,
170-
})
184+
inner_store: None,
185+
};
186+
env.inner_store = Some(env.get_store()?);
187+
Ok(env)
188+
}
189+
190+
/// Returns if the ontoenv instance is configured as read-only
191+
pub fn is_read_only(&self) -> bool {
192+
self.read_only
193+
}
194+
195+
fn store(&self) -> Store {
196+
self.inner_store.clone().unwrap()
171197
}
172198

173199
// TODO: add a read-only version? make this thread-safe?
174-
fn store(&self) -> Result<Store> {
200+
fn get_store(&self) -> Result<Store> {
175201
let ontoenv_dir = self.config.root.join(".ontoenv");
176202
std::fs::create_dir_all(&ontoenv_dir)?;
177203
if self.read_only {
178204
return Store::open_read_only(ontoenv_dir.join("store.db"))
179205
.map_err(|e| anyhow::anyhow!("Could not open store: {}", e));
180206
}
181207
Store::open(ontoenv_dir.join("store.db"))
182-
.or_else(|_| Store::open_read_only(ontoenv_dir.join("store.db")))
183208
.map_err(|e| anyhow::anyhow!("Could not open store: {}", e))
184209
}
185210

@@ -235,7 +260,7 @@ impl OntoEnv {
235260
/// Returns the number of triples in the environment
236261
pub fn num_triples(&self) -> Result<usize> {
237262
// this construction coerces the error the the correct type
238-
Ok(self.store()?.len()?)
263+
Ok(self.store().len()?)
239264
}
240265

241266
/// Returns an Ontology with the given name. Uses the provided policy to resolve
@@ -314,7 +339,7 @@ impl OntoEnv {
314339
None => self.ontologies.keys().cloned().collect(),
315340
};
316341
let mut seen: HashSet<GraphIdentifier> = HashSet::new();
317-
let store = self.store()?;
342+
let store = self.store();
318343

319344
info!("Using # updated ids: {:?}", stack.len());
320345

@@ -527,7 +552,7 @@ impl OntoEnv {
527552
// Step two: find all new and updated files
528553
let updated_files = self.get_updated_files()?;
529554

530-
let store = self.store()?;
555+
let store = self.store();
531556

532557
// Step three: add or update the ontologies from the new and updated files
533558
let updated_ids: Vec<GraphIdentifier> = if self.config.strict {
@@ -556,7 +581,7 @@ impl OntoEnv {
556581

557582
// optimize the store for storage + queries
558583
//if !self.read_only {
559-
// self.store()?.optimize()?;
584+
// self.store().optimize()?;
560585
//}
561586

562587
Ok(())
@@ -633,7 +658,7 @@ impl OntoEnv {
633658
/// Add the ontology from the given location to the environment. If the ontology
634659
/// already exists in the environment, it is overwritten.
635660
pub fn add(&mut self, location: OntologyLocation) -> Result<GraphIdentifier> {
636-
let store = self.store()?;
661+
let store = self.store();
637662
info!("Adding ontology from location: {:?}", location);
638663
self.add_or_update_ontology_from_location(location, &store)
639664
}
@@ -737,7 +762,7 @@ impl OntoEnv {
737762
pub fn get_graph(&self, id: &GraphIdentifier) -> Result<Graph> {
738763
let mut graph = Graph::new();
739764
let name = id.graphname()?;
740-
let store = self.store()?;
765+
let store = self.store();
741766
for quad in store.quads_for_pattern(None, None, None, Some(name.as_ref())) {
742767
graph.insert(quad?.as_ref());
743768
}
@@ -814,7 +839,7 @@ impl OntoEnv {
814839
) -> Result<Dataset> {
815840
// compute union of all graphs
816841
let mut union: Dataset = Dataset::new();
817-
let store = self.store()?;
842+
let store = self.store();
818843
for id in graph_ids {
819844
let graphname: NamedOrBlankNode = match id.graphname()? {
820845
GraphName::NamedNode(n) => NamedOrBlankNode::NamedNode(n),

python/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ impl OntoEnv {
233233
Ok(())
234234
}
235235

236+
fn is_read_only(&self) -> PyResult<bool> {
237+
let inner = self.inner.clone();
238+
let env = inner.lock().unwrap();
239+
Ok(env.is_read_only())
240+
}
241+
236242
fn __repr__(&self) -> PyResult<String> {
237243
let inner = self.inner.clone();
238244
let env = inner.lock().unwrap();

python/test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
brick.parse("../brick/Brick.ttl", format="turtle")
1717
env.import_dependencies(brick)
1818
print(len(brick))
19+
env.add("https://brickschema.org/schema/1.4/Brick.ttl")
1920

2021

2122
print("new env")

0 commit comments

Comments
 (0)