Skip to content

Commit 7f3662e

Browse files
committed
wip: continue db refactor
1 parent 44c42e0 commit 7f3662e

File tree

24 files changed

+526
-722
lines changed

24 files changed

+526
-722
lines changed

Cargo.lock

Lines changed: 18 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sandpolis-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ mod test_instance_id {
218218

219219
/// Groups have unique names and are shared across the entire cluster. Group
220220
/// names cannot be changed after they are created.
221-
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
221+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
222222
pub struct GroupName(String);
223223

224224
impl Display for GroupName {

sandpolis-database/Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ version = "0.0.1"
77

88
[dependencies]
99
anyhow = { workspace = true }
10-
clap = { workspace = true }
1110
chrono = { workspace = true }
12-
serde_cbor = { workspace = true }
13-
serde = { workspace = true }
14-
tracing = { workspace = true }
15-
tempfile = { workspace = true }
11+
clap = { workspace = true }
1612
native_db = { workspace = true }
1713
native_model = { workspace = true }
1814
sandpolis-core = { path = "../sandpolis-core", version = "0.0.1" }
1915
sandpolis-macros = { path = "../sandpolis-macros", version = "0.0.1" }
16+
serde_cbor = { workspace = true }
17+
serde = { workspace = true }
18+
tempfile = { workspace = true }
19+
tracing = { workspace = true }
20+
validator = { workspace = true }

sandpolis-database/src/config.rs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,59 @@
1-
use anyhow::{bail, Result};
1+
use anyhow::{Result, bail};
22
use serde::{Deserialize, Serialize};
33
use std::path::PathBuf;
4+
use validator::Validate;
45

56
#[derive(Serialize, Deserialize, Debug, Clone)]
67
pub struct DatabaseConfig {
7-
/// Storage directory
8-
pub storage: PathBuf,
8+
/// Override default storage directory
9+
pub storage: Option<PathBuf>,
10+
11+
/// Don't persist any data
12+
pub ephemeral: bool,
13+
}
14+
15+
impl Validate for DatabaseConfig {
16+
fn validate(&self) -> std::result::Result<(), validator::ValidationErrors> {
17+
if self.ephemeral && self.storage.is_some() {
18+
// TODO don't allow
19+
}
20+
21+
Ok(())
22+
}
923
}
1024

1125
impl Default for DatabaseConfig {
1226
fn default() -> Self {
1327
Self {
14-
storage: "/tmp".into(),
28+
// TODO platform specific
29+
storage: Some("/tmp".into()),
30+
ephemeral: false,
1531
}
1632
}
1733
}
1834

1935
impl DatabaseConfig {
2036
/// Create the storage directory if needed.
21-
pub fn create_storage_dir(&self) -> Result<()> {
22-
if !std::fs::exists(&self.storage)? {
23-
std::fs::create_dir_all(&self.storage)?;
37+
pub fn get_storage_dir(&self) -> Result<Option<PathBuf>> {
38+
if let Some(path) = self.storage.clone() {
39+
if !std::fs::exists(&path)? {
40+
std::fs::create_dir_all(&path)?;
41+
} else if !std::fs::metadata(&path)?.is_dir() {
42+
bail!("Storage directory must be a directory");
43+
}
44+
Ok(Some(path))
45+
} else {
46+
if self.ephemeral {
47+
Ok(None)
48+
} else {
49+
let path = Self::default().storage.unwrap();
50+
if !std::fs::exists(&path)? {
51+
std::fs::create_dir_all(&path)?;
52+
} else if !std::fs::metadata(&path)?.is_dir() {
53+
bail!("Storage directory must be a directory");
54+
}
55+
Ok(Some(path))
56+
}
2457
}
25-
if !std::fs::metadata(&self.storage)?.is_dir() {
26-
bail!("Storage directory must be a directory");
27-
}
28-
Ok(())
2958
}
3059
}

0 commit comments

Comments
 (0)