Skip to content

Commit ffe84f9

Browse files
committed
feat: impl save to file
1 parent 56fa173 commit ffe84f9

3 files changed

Lines changed: 63 additions & 44 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.shuttle-storage
33
Secrets*.toml
44
.shuttle*
5+
KV/

src/config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ impl StartupConfig {
4848
}
4949

5050
impl SharedConfig {
51-
const KEY_VALUE_STORE_FOLDER: &str = "KV";
5251
pub fn try_new(secret_store: &SecretStore) -> anyhow::Result<&'static Self> {
5352
let auth_role_id = KeyName::AuthRoleId.get_non_secret_parse(secret_store)?;
5453
let channel_unranked = KeyName::ChannelUnrankedId.get_non_secret_parse(secret_store)?;

src/db.rs

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,67 @@
1+
use std::{fs, io::Write as _, path::PathBuf};
2+
3+
use tracing::error;
4+
5+
const KEY_VALUE_STORE_FOLDER: &str = "KV";
6+
7+
/// Returns a path if able to be created else logs error and returns `None`
8+
fn get_file_path(key: &str) -> Option<PathBuf> {
9+
let mut result = PathBuf::from(KEY_VALUE_STORE_FOLDER);
10+
match std::fs::create_dir_all(&result) {
11+
Ok(()) => {}
12+
Err(err_msg) => {
13+
error!(
14+
?err_msg,
15+
"Failed to create parent directory for key value store: {result:?}"
16+
);
17+
return None;
18+
}
19+
};
20+
result = result.join(key);
21+
if !result.add_extension("json") {
22+
error!("Unable to add json extension to path: {result:?}");
23+
}
24+
Some(result)
25+
}
26+
127
pub async fn save_kv(key: &str, value: String) {
2-
todo!("save to disk")
3-
// let query = sqlx::query!(
4-
// "\
5-
// INSERT INTO kv_store (id, content)
6-
// VALUES ($1, $2)
7-
// ON CONFLICT(id)
8-
// DO UPDATE SET
9-
// content = EXCLUDED.content;",
10-
// key,
11-
// value
12-
// );
13-
// match query.execute(pool).await {
14-
// Ok(query_result) => {
15-
// if query_result.rows_affected() == 1 {
16-
// debug!("Save completed for key: {key}");
17-
// } else {
18-
// error!(
19-
// ?key,
20-
// "Expected 1 row to be affected by save but got: {}",
21-
// query_result.rows_affected()
22-
// )
23-
// }
24-
// }
25-
// Err(err_msg) => error!(
26-
// ?err_msg,
27-
// "Failed to save content for key: {key} to kv store"
28-
// ),
29-
// }
28+
let Some(path) = get_file_path(key) else {
29+
return;
30+
};
31+
32+
let mut file = match std::fs::OpenOptions::new()
33+
.write(true)
34+
.create(true)
35+
.truncate(true)
36+
.open(path)
37+
{
38+
Ok(file) => file,
39+
Err(err_msg) => {
40+
error!(
41+
?err_msg,
42+
"Failed to save content for key: {key} to kv store (file creation failed)"
43+
);
44+
return;
45+
}
46+
};
47+
match file.write_all(value.as_bytes()) {
48+
Ok(_) => {}
49+
Err(err_msg) => {
50+
error!(
51+
?err_msg,
52+
"Failed to save content for key: {key} to kv store (write failed)"
53+
);
54+
}
55+
};
3056
}
3157

3258
pub async fn load_kv(key: &str) -> Option<String> {
33-
todo!("load from disk")
34-
// match sqlx::query!("SELECT content FROM kv_store where id = $1", key)
35-
// .fetch_optional(pool)
36-
// .await
37-
// {
38-
// Ok(Some(record)) => Some(record.content),
39-
// Ok(None) => {
40-
// info!("No content found in DB for key: {key}");
41-
// None
42-
// }
43-
// Err(err_msg) => {
44-
// error!(?err_msg, "Failed to get content for key: {key}");
45-
// None
46-
// }
47-
// }
59+
let path = get_file_path(key)?;
60+
match fs::read_to_string(path) {
61+
Ok(content) => Some(content),
62+
Err(err_msg) => {
63+
error!(?err_msg, "Failed to get content for key: {key}");
64+
None
65+
}
66+
}
4867
}

0 commit comments

Comments
 (0)