Skip to content

Commit 6f33517

Browse files
committed
update
1 parent 905def0 commit 6f33517

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::path::PathBuf;
2+
3+
use microkv::MicroKV;
4+
use serde::de::DeserializeOwned;
5+
6+
use crate::util;
7+
8+
// 加密kv存储方案:
9+
pub struct KvEncryptedClient {
10+
pub cli: MicroKV,
11+
}
12+
13+
impl KvEncryptedClient {
14+
pub fn default() -> Self {
15+
Self::new(None, None)
16+
}
17+
18+
pub fn new(db_path: Option<PathBuf>, unsafe_pwd: Option<&str>) -> Self {
19+
let db_name = "app_test";
20+
let pwd = unsafe_pwd.unwrap_or("unsafe_pwd");
21+
22+
let db_path = db_path.unwrap_or_else(|| util::local_dir(None).join(db_name));
23+
let fp = db_path.as_path().to_str().expect("db_path to str failed");
24+
25+
let cli: MicroKV = MicroKV::open_with_base_path(fp, PathBuf::new())
26+
.expect("Failed to create MicroKV from a stored file or create MicroKV for this file")
27+
.set_auto_commit(true)
28+
.with_pwd_clear(pwd);
29+
30+
Self { cli }
31+
}
32+
}
33+
34+
#[cfg(test)]
35+
mod tests {
36+
use super::*;
37+
38+
#[test]
39+
fn test_new_kv_enc_client() {
40+
let mut kv = KvEncryptedClient::default();
41+
42+
let cases =
43+
[("k1", "v1"), ("k2", "v2"), ("k3", "v3"), ("k4", "v4"), ("k5", "v5"), ("k6", "v6")];
44+
45+
for (k, v) in cases.iter() {
46+
kv.cli.put(k, v).unwrap();
47+
let vv: String = kv.cli.get_unwrap(k).unwrap();
48+
println!("{}: {:?}", k, vv);
49+
}
50+
}
51+
}

crates/rs-pkg/src/x/storage/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pub use kv::*;
2+
pub use kv_enc::*;
23
pub use sql::*;
34

45
pub mod kv;
6+
pub mod kv_enc;
57
pub mod sql;

0 commit comments

Comments
 (0)