Skip to content

Commit 2a992ee

Browse files
authored
Merge pull request #15 from CoLearn-Dev/bugfix-storage
- bugfix: delete after create - bugfix: delete non-exist entry
2 parents 47bdc96 + 09b0a84 commit 2a992ee

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "colink-server"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
edition = "2021"
55

66
[dependencies]

src/storage/basic.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ impl crate::storage::common::Storage for BasicStorage {
3131
let key_path_created = format!("{}::{}@{}", user_id, key_name, timestamp);
3232
let user_id_key_name = format!("{}::{}", user_id, key_name);
3333
debug!("{}", user_id_key_name);
34-
if map.contains_key(&user_id_key_name) {
34+
if map.contains_key(&user_id_key_name)
35+
&& map.contains_key(&format!(
36+
"{}@{}",
37+
user_id_key_name,
38+
String::from_utf8(map.get(&user_id_key_name).unwrap().to_vec()).unwrap()
39+
))
40+
{
3541
return Err(format!("Key name already exists: {}", user_id_key_name));
3642
}
3743
map.insert(key_path_created.clone(), value.to_vec());
@@ -65,7 +71,7 @@ impl crate::storage::common::Storage for BasicStorage {
6571
let value = map.get(key_path);
6672
match value {
6773
Some(v) => result.insert(key_path.clone(), v.clone()),
68-
None => return Err(format!("Key not found: {}", key_path)),
74+
None => return Err(format!("Key path not found: {}", key_path)),
6975
};
7076
}
7177
Ok(result)
@@ -90,7 +96,7 @@ impl crate::storage::common::Storage for BasicStorage {
9096
let value = map.get(&key_path);
9197
match value {
9298
Some(v) => result.insert(key_path, v.clone()),
93-
None => return Err(format!("Key path not found: {}", key_path)),
99+
None => return Err(format!("Entry have been deleted: {}", key_path)),
94100
};
95101
}
96102
Ok(result)
@@ -152,7 +158,17 @@ impl crate::storage::common::Storage for BasicStorage {
152158
let timestamp = Utc::now().timestamp();
153159
let mut map = self.map.lock().await;
154160
let user_id_key_name = format!("{}::{}", user_id, key_name);
155-
map.insert(user_id_key_name, timestamp.to_string().into_bytes());
156-
Ok(format!("{}::{}@{}", user_id, key_name, timestamp))
161+
if map.contains_key(&user_id_key_name)
162+
&& map.contains_key(&format!(
163+
"{}@{}",
164+
user_id_key_name,
165+
String::from_utf8(map.get(&user_id_key_name).unwrap().to_vec()).unwrap()
166+
))
167+
{
168+
map.insert(user_id_key_name, timestamp.to_string().into_bytes());
169+
Ok(format!("{}::{}@{}", user_id, key_name, timestamp))
170+
} else {
171+
Err(format!("Key name not found: {}", key_name))
172+
}
157173
}
158174
}

0 commit comments

Comments
 (0)