From 3e8b9e6a953c32028ed0d59e85eb5e1731b8f91e Mon Sep 17 00:00:00 2001 From: Prashanth Pai Date: Tue, 2 Mar 2021 19:27:37 +0530 Subject: [PATCH] Add support to delete hash fields --- src/key.rs | 4 ++++ src/raw.rs | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/key.rs b/src/key.rs index 83bb533b..edc83ba7 100644 --- a/src/key.rs +++ b/src/key.rs @@ -173,6 +173,10 @@ impl RedisKeyWritable { raw::hash_set(self.key_inner, field, value.inner) } + pub fn hash_del(&self, field: &str) -> raw::Status { + raw::hash_del(self.key_inner, field) + } + pub fn hash_get(&self, field: &str) -> Result, RedisError> { Ok(hash_mget_key(self.ctx, self.key_inner, &[field])? .pop() diff --git a/src/raw.rs b/src/raw.rs index 3123bd44..4f5789bf 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -157,6 +157,11 @@ extern "C" { pub const FMT: *const c_char = b"v\0".as_ptr() as *const c_char; +// REDISMODULE_HASH_DELETE is defined explicitly here because bindgen cannot +// parse typecasts in C macro constants yet. +// See https://github.com/rust-lang/rust-bindgen/issues/316 +pub const REDISMODULE_HASH_DELETE: *const RedisModuleString = 1 as *const RedisModuleString; + // Helper functions for the raw bindings. pub fn call_reply_type(reply: *mut RedisModuleCallReply) -> ReplyType { @@ -348,6 +353,25 @@ pub fn hash_set(key: *mut RedisModuleKey, field: &str, value: *mut RedisModuleSt } } +pub fn hash_del(key: *mut RedisModuleKey, field: &str) -> Status { + let field = CString::new(field).unwrap(); + + // TODO: Add hash_del_multi() + // Support to pass multiple fields is desired but is complicated. + // See hash_get_multi() and https://github.com/redis/redis/issues/7860 + + unsafe { + RedisModule_HashSet.unwrap()( + key, + REDISMODULE_HASH_CFIELDS as i32, + field.as_ptr(), + REDISMODULE_HASH_DELETE, + ptr::null::(), + ) + .into() + } +} + // Returns pointer to the C string, and sets len to its length pub fn string_ptr_len(s: *mut RedisModuleString, len: *mut size_t) -> *const c_char { unsafe { RedisModule_StringPtrLen.unwrap()(s, len) }