-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
126 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by GNU Affero General Public License | ||
// that can be found in the LICENSE file. | ||
|
||
use crate::cmd::reply_frame::ReplyFrame; | ||
use crate::mem::db::{Db, MemObject}; | ||
|
||
/// Removes the specified fields from the hash stored at key. | ||
/// | ||
/// Specified fields that do not exist within this hash are ignored. | ||
/// If key does not exist, it is treated as an empty hash and this command returns 0. | ||
/// | ||
/// Reply: | ||
/// - Integer reply: The number of fields that were removed from the hash, | ||
/// excluding any specified but non-existing fields. | ||
pub fn delete( | ||
db: &mut Db, | ||
key: &str, | ||
field: &str, | ||
extra_fields: Option<Vec<String>>, | ||
) -> ReplyFrame { | ||
match db.get_mut(key) { | ||
Some(MemObject::Hash(old_hash)) => { | ||
let mut count = 0; | ||
if old_hash.remove(field).is_some() { | ||
count += 1; | ||
} | ||
if let Some(extra_fields) = extra_fields { | ||
for field in &extra_fields { | ||
if old_hash.remove(field).is_some() { | ||
count += 1; | ||
} | ||
} | ||
} | ||
|
||
ReplyFrame::Usize(count) | ||
} | ||
Some(_) => ReplyFrame::wrong_type_err(), | ||
None => ReplyFrame::zero(), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::cmd::reply_frame::ReplyFrame; | ||
use crate::mem::db::Db; | ||
use crate::mem::hash::delete::delete; | ||
use crate::mem::hash::set::set; | ||
|
||
#[test] | ||
fn test_delete() { | ||
let mut db = Db::new(); | ||
let key = "myhash".to_owned(); | ||
let reply = set( | ||
&mut db, | ||
key.clone(), | ||
"field1".to_owned(), | ||
b"foo".to_vec(), | ||
None, | ||
); | ||
assert_eq!(reply, ReplyFrame::one()); | ||
let reply = delete(&mut db, &key, "field1", None); | ||
assert_eq!(reply, ReplyFrame::one()); | ||
let reply = delete(&mut db, &key, "field2", None); | ||
assert_eq!(reply, ReplyFrame::zero()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters