Skip to content

Commit 549b814

Browse files
prashanthpaigavrie
authored andcommitted
Add support to delete hash fields
1 parent 4eb32d7 commit 549b814

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/key.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ impl RedisKeyWritable {
173173
raw::hash_set(self.key_inner, field, value.inner)
174174
}
175175

176+
pub fn hash_del(&self, field: &str) -> raw::Status {
177+
raw::hash_del(self.key_inner, field)
178+
}
179+
176180
pub fn hash_get(&self, field: &str) -> Result<Option<RedisString>, RedisError> {
177181
Ok(hash_mget_key(self.ctx, self.key_inner, &[field])?
178182
.pop()

src/raw.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ extern "C" {
157157

158158
pub const FMT: *const c_char = b"v\0".as_ptr() as *const c_char;
159159

160+
// REDISMODULE_HASH_DELETE is defined explicitly here because bindgen cannot
161+
// parse typecasts in C macro constants yet.
162+
// See https://github.com/rust-lang/rust-bindgen/issues/316
163+
pub const REDISMODULE_HASH_DELETE: *const RedisModuleString = 1 as *const RedisModuleString;
164+
160165
// Helper functions for the raw bindings.
161166

162167
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
348353
}
349354
}
350355

356+
pub fn hash_del(key: *mut RedisModuleKey, field: &str) -> Status {
357+
let field = CString::new(field).unwrap();
358+
359+
// TODO: Add hash_del_multi()
360+
// Support to pass multiple fields is desired but is complicated.
361+
// See hash_get_multi() and https://github.com/redis/redis/issues/7860
362+
363+
unsafe {
364+
RedisModule_HashSet.unwrap()(
365+
key,
366+
REDISMODULE_HASH_CFIELDS as i32,
367+
field.as_ptr(),
368+
REDISMODULE_HASH_DELETE,
369+
ptr::null::<c_char>(),
370+
)
371+
.into()
372+
}
373+
}
374+
351375
// Returns pointer to the C string, and sets len to its length
352376
pub fn string_ptr_len(s: *mut RedisModuleString, len: *mut size_t) -> *const c_char {
353377
unsafe { RedisModule_StringPtrLen.unwrap()(s, len) }

0 commit comments

Comments
 (0)