Skip to content

Commit c2eba5f

Browse files
committed
add howcreated
1 parent 2e459b1 commit c2eba5f

File tree

3 files changed

+79
-12
lines changed

3 files changed

+79
-12
lines changed

lib/src/config.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,24 @@ impl Config {
172172
Ok(config)
173173
}
174174
}
175+
176+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
177+
pub enum HowCreated {
178+
New,
179+
SameConfig,
180+
RecreatedDifferentConfig,
181+
RecreatedFlag,
182+
}
183+
184+
impl std::fmt::Display for HowCreated {
185+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
186+
match self {
187+
HowCreated::New => write!(f, "New Environment"),
188+
HowCreated::SameConfig => write!(f, "Same Config. Reusing existing environment."),
189+
HowCreated::RecreatedDifferentConfig => {
190+
write!(f, "Recreated environment due to different config")
191+
}
192+
HowCreated::RecreatedFlag => write!(f, "Recreated environment due to 'recreate' flag"),
193+
}
194+
}
195+
}

lib/src/lib.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub mod policy;
1010
pub mod util;
1111
pub mod transform;
1212

13-
use crate::config::Config;
13+
use crate::config::{Config, HowCreated};
1414
use crate::doctor::{Doctor, DuplicateOntology, OntologyDeclaration};
1515
use crate::ontology::{GraphIdentifier, Ontology, OntologyLocation};
1616
use anyhow::Result;
@@ -63,6 +63,8 @@ pub struct EnvironmentStatus {
6363
last_updated: Option<DateTime<Utc>>,
6464
// size of the oxigraph store, in bytes
6565
store_size: u64,
66+
// how this environment was last created
67+
how_created: HowCreated,
6668
}
6769

6870
// impl Display pretty print for EnvironmentStatus
@@ -81,9 +83,11 @@ impl std::fmt::Display for EnvironmentStatus {
8183
write!(
8284
f,
8385
"Environment Status\n\
86+
How Created: {}\n\
8487
Number of Ontologies: {}\n\
8588
Last Updated: {}\n\
8689
Store Size: {} bytes",
90+
self.how_created,
8791
self.num_ontologies,
8892
last_updated,
8993
pretty_bytes(self.store_size as f64),
@@ -99,6 +103,7 @@ pub struct OntoEnv {
99103
dependency_graph: DiGraph<GraphIdentifier, (), petgraph::Directed>,
100104
#[serde(skip)]
101105
read_only: bool,
106+
how_created: HowCreated,
102107
}
103108

104109
// probably need some graph "identifier" that incorporates location and version..
@@ -111,27 +116,38 @@ impl OntoEnv {
111116
// is created
112117
let ontoenv_dir = config.root.join(".ontoenv");
113118
let config_path = ontoenv_dir.join("ontoenv.json");
119+
let mut how_created = HowCreated::New;
120+
info!("Creating OntoEnv with config: {:?}", config);
114121

115122
// test if the config in the ontoenv_dir is different from the current config.
116123
// If it is, replace the config with the current config and turn 'recreate' on
117-
if ontoenv_dir.exists() && config_path.exists() {
118-
println!(
124+
if ontoenv_dir.exists() && config_path.exists() && !recreate {
125+
info!(
119126
"OntoEnv directory exists: {:?}. Checking config",
120127
ontoenv_dir
121128
);
129+
how_created = HowCreated::SameConfig;
122130
let file = std::fs::File::open(&config_path)?;
123131
let reader = BufReader::new(file);
124-
let env: OntoEnv = serde_json::from_reader(reader)?;
132+
let mut env: OntoEnv = serde_json::from_reader(reader)?;
125133
// print old and new config
126-
println!("Old config: {:?}", env.config);
127-
println!("New config: {:?}", config);
128134
if env.config != config {
129135
info!("OntoEnv configuration has changed. Recreating environment.");
130136
fs::remove_dir_all(&ontoenv_dir)?;
131-
return Self::new(config, true);
137+
env = Self::new(config, true)?;
138+
env.how_created = HowCreated::RecreatedDifferentConfig;
139+
return Ok(env);
132140
}
133141
}
134142

143+
if recreate {
144+
info!("Recreating environment");
145+
if ontoenv_dir.exists() {
146+
fs::remove_dir_all(&ontoenv_dir)?;
147+
}
148+
how_created = HowCreated::RecreatedFlag;
149+
}
150+
135151
// if recreate is False, raise an error if the directory already exists
136152
//if ontoenv_dir.exists() && !recreate {
137153
// return Err(anyhow::anyhow!(
@@ -148,6 +164,7 @@ impl OntoEnv {
148164
ontologies: HashMap::new(),
149165
dependency_graph: DiGraph::new(),
150166
read_only: false,
167+
how_created,
151168
})
152169
}
153170

@@ -178,6 +195,10 @@ impl OntoEnv {
178195
Ok(size)
179196
}
180197

198+
pub fn get_how_created(&self) -> HowCreated {
199+
self.how_created.clone()
200+
}
201+
181202
/// Calculates and returns the environment status
182203
pub fn status(&self) -> Result<EnvironmentStatus> {
183204
let store = self.store()?;
@@ -191,6 +212,7 @@ impl OntoEnv {
191212
num_ontologies,
192213
last_updated: Some(last_updated),
193214
store_size: size,
215+
how_created: self.how_created.clone(),
194216
})
195217
}
196218

lib/tests/ontoenv_test.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::Result;
2-
use ontoenv::config::Config;
2+
use ontoenv::config::{Config, HowCreated};
33
use ontoenv::ontology::OntologyLocation;
44
use ontoenv::OntoEnv;
55
use oxigraph::model::NamedNodeRef;
@@ -81,6 +81,20 @@ fn default_config(dir: &TempDir) -> Config {
8181
.unwrap()
8282
}
8383

84+
fn default_config_ttl_only(dir: &TempDir) -> Config {
85+
Config::new(
86+
dir.path().into(),
87+
Some(vec![dir.path().into()]),
88+
&["*.ttl"],
89+
&[""],
90+
false,
91+
true,
92+
true,
93+
"default".to_string(),
94+
)
95+
.unwrap()
96+
}
97+
8498
fn default_config_with_subdir(dir: &TempDir, path: &str) -> Config {
8599
Config::new(
86100
dir.path().into(),
@@ -210,13 +224,23 @@ fn test_recreate() -> Result<()> {
210224
"fixtures/ont4.ttl" => "ont4.ttl" });
211225
let cfg = default_config(&dir);
212226
let env = OntoEnv::new(cfg, false)?;
213-
// create a new env, like above, but make sure it raises an error
227+
env.save_to_directory()?;
228+
assert_eq!(env.get_how_created(), HowCreated::New);
229+
// create a new env with the same config. This should still work.
214230
let cfg = default_config(&dir);
215-
let env = OntoEnv::new(cfg, false);
216-
assert!(env.is_err());
217-
231+
let env = OntoEnv::new(cfg, false)?;
232+
env.save_to_directory()?;
233+
assert_eq!(env.get_how_created(), HowCreated::SameConfig);
234+
// change the config; this should trigger a recreation of the environment
235+
let cfg = default_config_ttl_only(&dir);
236+
let env = OntoEnv::new(cfg, false)?;
237+
env.save_to_directory()?;
238+
assert_eq!(env.get_how_created(), HowCreated::RecreatedDifferentConfig);
239+
// now try to recreate the env with the same config but with recreate set to true
218240
let cfg = default_config(&dir);
219241
let env = OntoEnv::new(cfg, true)?;
242+
env.save_to_directory()?;
243+
assert_eq!(env.get_how_created(), HowCreated::RecreatedFlag);
220244

221245
teardown(dir);
222246
Ok(())

0 commit comments

Comments
 (0)