valkey_module/
raw.rs

1// Allow dead code in here in case I want to publish it as a crate at some
2// point.
3#![allow(dead_code)]
4
5extern crate enum_primitive_derive;
6extern crate libc;
7extern crate num_traits;
8
9use std::cmp::Ordering;
10use std::ffi::{c_ulonglong, CStr, CString};
11use std::os::raw::{c_char, c_double, c_int, c_long, c_longlong, c_void};
12use std::ptr;
13use std::slice;
14
15use crate::ValkeyResult;
16use bitflags::bitflags;
17use enum_primitive_derive::Primitive;
18use libc::size_t;
19use num_traits::FromPrimitive;
20
21use crate::error::Error;
22pub use crate::redisraw::bindings::*;
23use crate::{context::StrCallArgs, Context, ValkeyString};
24use crate::{RedisBuffer, ValkeyError};
25
26const GENERIC_ERROR_MESSAGE: &str = "Generic error.";
27
28bitflags! {
29    pub struct KeyMode: c_int {
30        const READ = REDISMODULE_READ as c_int;
31        const WRITE = REDISMODULE_WRITE as c_int;
32    }
33}
34
35bitflags! {
36    pub struct ModuleOptions: c_int {
37        const HANDLE_IO_ERRORS = REDISMODULE_OPTIONS_HANDLE_IO_ERRORS as c_int;
38        const NO_IMPLICIT_SIGNAL_MODIFIED = REDISMODULE_OPTION_NO_IMPLICIT_SIGNAL_MODIFIED as c_int;
39        const HANDLE_REPL_ASYNC_LOAD = REDISMODULE_OPTIONS_HANDLE_REPL_ASYNC_LOAD as c_int;
40        const ALLOW_NESTED_KEYSPACE_NOTIFICATIONS = REDISMODULE_OPTIONS_ALLOW_NESTED_KEYSPACE_NOTIFICATIONS as c_int;
41    }
42}
43
44#[derive(Primitive, Debug, PartialEq, Eq)]
45pub enum KeyType {
46    Empty = REDISMODULE_KEYTYPE_EMPTY,
47    String = REDISMODULE_KEYTYPE_STRING,
48    List = REDISMODULE_KEYTYPE_LIST,
49    Hash = REDISMODULE_KEYTYPE_HASH,
50    Set = REDISMODULE_KEYTYPE_SET,
51    ZSet = REDISMODULE_KEYTYPE_ZSET,
52    Module = REDISMODULE_KEYTYPE_MODULE,
53    Stream = REDISMODULE_KEYTYPE_STREAM,
54}
55
56impl From<c_int> for KeyType {
57    fn from(v: c_int) -> Self {
58        Self::from_i32(v).unwrap()
59    }
60}
61
62#[derive(Primitive, Debug, PartialEq, Eq)]
63pub enum Where {
64    ListHead = REDISMODULE_LIST_HEAD,
65    ListTail = REDISMODULE_LIST_TAIL,
66}
67
68#[derive(Primitive, Debug, PartialEq, Eq)]
69pub enum ReplyType {
70    Unknown = REDISMODULE_REPLY_UNKNOWN,
71    String = REDISMODULE_REPLY_STRING,
72    Error = REDISMODULE_REPLY_ERROR,
73    Integer = REDISMODULE_REPLY_INTEGER,
74    Array = REDISMODULE_REPLY_ARRAY,
75    Null = REDISMODULE_REPLY_NULL,
76    Map = REDISMODULE_REPLY_MAP,
77    Set = REDISMODULE_REPLY_SET,
78    Bool = REDISMODULE_REPLY_BOOL,
79    Double = REDISMODULE_REPLY_DOUBLE,
80    BigNumber = REDISMODULE_REPLY_BIG_NUMBER,
81    VerbatimString = REDISMODULE_REPLY_VERBATIM_STRING,
82}
83
84impl From<c_int> for ReplyType {
85    fn from(v: c_int) -> Self {
86        Self::from_i32(v).unwrap()
87    }
88}
89
90#[derive(Primitive, Debug, PartialEq, Eq)]
91pub enum Aux {
92    Before = REDISMODULE_AUX_BEFORE_RDB,
93    After = REDISMODULE_AUX_AFTER_RDB,
94}
95
96#[derive(Primitive, Debug, PartialEq, Eq)]
97pub enum Status {
98    Ok = REDISMODULE_OK,
99    Err = REDISMODULE_ERR,
100}
101
102impl From<Status> for ValkeyResult<()> {
103    fn from(value: Status) -> Self {
104        match value {
105            Status::Ok => Ok(()),
106            Status::Err => Err(ValkeyError::Str(GENERIC_ERROR_MESSAGE)),
107        }
108    }
109}
110
111impl From<c_int> for Status {
112    fn from(v: c_int) -> Self {
113        Self::from_i32(v).unwrap()
114    }
115}
116
117impl From<Status> for Result<(), &str> {
118    fn from(s: Status) -> Self {
119        match s {
120            Status::Ok => Ok(()),
121            Status::Err => Err(GENERIC_ERROR_MESSAGE),
122        }
123    }
124}
125
126bitflags! {
127    #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
128    pub struct NotifyEvent : c_int {
129        const GENERIC = REDISMODULE_NOTIFY_GENERIC;
130        const STRING = REDISMODULE_NOTIFY_STRING;
131        const LIST = REDISMODULE_NOTIFY_LIST;
132        const SET = REDISMODULE_NOTIFY_SET;
133        const HASH = REDISMODULE_NOTIFY_HASH;
134        const ZSET = REDISMODULE_NOTIFY_ZSET;
135        const EXPIRED = REDISMODULE_NOTIFY_EXPIRED;
136        const EVICTED = REDISMODULE_NOTIFY_EVICTED;
137        const STREAM = REDISMODULE_NOTIFY_STREAM;
138        /// Available only starting from Redis `7.0.1`.
139        const NEW = REDISMODULE_NOTIFY_NEW;
140        const MODULE = REDISMODULE_NOTIFY_MODULE;
141        const LOADED = REDISMODULE_NOTIFY_LOADED;
142        const MISSED = REDISMODULE_NOTIFY_KEY_MISS;
143        /// Does not include the [`Self::MISSED`] and [`Self::NEW`].
144        ///
145        /// Includes [`Self::GENERIC`], [`Self::STRING`], [`Self::LIST`],
146        /// [`Self::SET`], [`Self::HASH`], [`Self::ZSET`], [`Self::EXPIRED`],
147        /// [`Self::EVICTED`], [`Self::STREAM`], [`Self::MODULE`].
148        const ALL = REDISMODULE_NOTIFY_ALL;
149        const TRIMMED = REDISMODULE_NOTIFY_TRIMMED;
150    }
151}
152
153#[derive(Debug)]
154pub enum CommandFlag {
155    Write,
156    Readonly,
157    Denyoom,
158    Admin,
159    Pubsub,
160    Noscript,
161    Random,
162    SortForScript,
163    Loading,
164    Stale,
165    SkipMonitor,
166    Asking,
167    Fast,
168    Movablekeys,
169}
170
171const fn command_flag_repr(flag: &CommandFlag) -> &'static str {
172    use crate::raw::CommandFlag::*;
173    match flag {
174        Write => "write",
175        Readonly => "readonly",
176        Denyoom => "denyoom",
177        Admin => "admin",
178        Pubsub => "pubsub",
179        Noscript => "noscript",
180        Random => "random",
181        SortForScript => "sort_for_script",
182        Loading => "loading",
183        Stale => "stale",
184        SkipMonitor => "skip_monitor",
185        Asking => "asking",
186        Fast => "fast",
187        Movablekeys => "movablekeys",
188    }
189}
190
191// This is the one static function we need to initialize a module.
192// bindgen does not generate it for us (probably since it's defined as static in redismodule.h).
193#[allow(improper_ctypes)]
194#[link(name = "redismodule", kind = "static")]
195extern "C" {
196    pub fn Export_RedisModule_Init(
197        ctx: *mut RedisModuleCtx,
198        module_name: *const c_char,
199        module_version: c_int,
200        api_version: c_int,
201    ) -> c_int;
202
203    pub fn Export_RedisModule_InitAPI(ctx: *mut RedisModuleCtx) -> c_void;
204}
205
206// This is the one static function we need to initialize a module.
207// bindgen does not generate it for us (probably since it's defined as static in valkeymodule.h).
208#[allow(improper_ctypes)]
209#[link(name = "valkeymodule", kind = "static")]
210extern "C" {
211    pub fn Export_ValkeyModule_Init(
212        ctx: *mut ValkeyModuleCtx,
213        module_name: *const c_char,
214        module_version: c_int,
215        api_version: c_int,
216    ) -> c_int;
217
218    pub fn Export_ValkeyModule_InitAPI(ctx: *mut ValkeyModuleCtx) -> c_void;
219}
220
221// Helper function to safely check whether the use-redismodule-api feature flag is enabled
222// without requiring consumers to include it in their Cargo.toml.
223pub fn use_redis_module_api() -> bool {
224    if cfg!(feature = "use-redismodule-api") {
225        true
226    } else {
227        false
228    }
229}
230
231///////////////////////////////////////////////////////////////
232
233pub const FMT: *const c_char = b"v\0".as_ptr().cast::<c_char>();
234
235// REDISMODULE_HASH_DELETE is defined explicitly here because bindgen cannot
236// parse typecasts in C macro constants yet.
237// See https://github.com/rust-lang/rust-bindgen/issues/316
238pub const REDISMODULE_HASH_DELETE: *const RedisModuleString = 1 as *const RedisModuleString;
239
240// Helper functions for the raw bindings.
241
242#[allow(clippy::not_unsafe_ptr_arg_deref)]
243pub fn call_reply_type(reply: *mut RedisModuleCallReply) -> ReplyType {
244    unsafe {
245        // TODO: Cache the unwrapped functions and use them instead of unwrapping every time?
246        RedisModule_CallReplyType.unwrap()(reply).into()
247    }
248}
249
250#[allow(clippy::not_unsafe_ptr_arg_deref)]
251pub fn free_call_reply(reply: *mut RedisModuleCallReply) {
252    unsafe { RedisModule_FreeCallReply.unwrap()(reply) }
253}
254
255#[allow(clippy::not_unsafe_ptr_arg_deref)]
256pub fn call_reply_integer(reply: *mut RedisModuleCallReply) -> c_longlong {
257    unsafe { RedisModule_CallReplyInteger.unwrap()(reply) }
258}
259
260/// # Panics
261///
262/// Panics if the Valkey server doesn't support replying with bool (since RESP3).
263#[allow(clippy::not_unsafe_ptr_arg_deref)]
264pub fn call_reply_bool(reply: *mut RedisModuleCallReply) -> bool {
265    (unsafe { RedisModule_CallReplyBool.unwrap()(reply) } != 0)
266}
267
268/// # Panics
269///
270/// Panics if the Valkey server doesn't support replying with bool (since RESP3).
271#[allow(clippy::not_unsafe_ptr_arg_deref)]
272pub fn call_reply_double(reply: *mut RedisModuleCallReply) -> f64 {
273    unsafe { RedisModule_CallReplyDouble.unwrap()(reply) }
274}
275
276/// # Panics
277///
278/// Panics if the Valkey server doesn't support replying with bool (since RESP3).
279#[allow(clippy::not_unsafe_ptr_arg_deref)]
280pub fn call_reply_big_number(reply: *mut RedisModuleCallReply) -> Option<String> {
281    unsafe {
282        let mut len: size_t = 0;
283        let reply_string: *mut u8 =
284            RedisModule_CallReplyBigNumber.unwrap()(reply, &mut len) as *mut u8;
285        if reply_string.is_null() {
286            return None;
287        }
288        String::from_utf8(slice::from_raw_parts(reply_string, len).to_vec()).ok()
289    }
290}
291
292/// # Panics
293///
294/// Panics if the Valkey server doesn't support replying with bool (since RESP3).
295#[allow(clippy::not_unsafe_ptr_arg_deref)]
296pub fn call_reply_verbatim_string(reply: *mut RedisModuleCallReply) -> Option<(String, Vec<u8>)> {
297    unsafe {
298        let mut len: size_t = 0;
299        let format: *const u8 = ptr::null();
300        let reply_string: *mut u8 =
301            RedisModule_CallReplyVerbatim.unwrap()(reply, &mut len, &mut (format as *const c_char))
302                as *mut u8;
303        if reply_string.is_null() {
304            return None;
305        }
306        Some((
307            String::from_utf8(slice::from_raw_parts(format, 3).to_vec()).ok()?,
308            slice::from_raw_parts(reply_string, len).to_vec(),
309        ))
310    }
311}
312
313#[allow(clippy::not_unsafe_ptr_arg_deref)]
314pub fn call_reply_array_element(
315    reply: *mut RedisModuleCallReply,
316    idx: usize,
317) -> *mut RedisModuleCallReply {
318    unsafe { RedisModule_CallReplyArrayElement.unwrap()(reply, idx) }
319}
320
321/// # Panics
322///
323/// Panics if the Valkey server doesn't support replying with bool (since RESP3).
324#[allow(clippy::not_unsafe_ptr_arg_deref)]
325pub fn call_reply_set_element(
326    reply: *mut RedisModuleCallReply,
327    idx: usize,
328) -> *mut RedisModuleCallReply {
329    unsafe { RedisModule_CallReplySetElement.unwrap()(reply, idx) }
330}
331
332/// # Panics
333///
334/// Panics if the Valkey server doesn't support replying with bool (since RESP3).
335#[allow(clippy::not_unsafe_ptr_arg_deref)]
336pub fn call_reply_map_element(
337    reply: *mut RedisModuleCallReply,
338    idx: usize,
339) -> (*mut RedisModuleCallReply, *mut RedisModuleCallReply) {
340    let mut key: *mut RedisModuleCallReply = ptr::null_mut();
341    let mut val: *mut RedisModuleCallReply = ptr::null_mut();
342    unsafe { RedisModule_CallReplyMapElement.unwrap()(reply, idx, &mut key, &mut val) };
343    (key, val)
344}
345
346#[allow(clippy::not_unsafe_ptr_arg_deref)]
347pub fn call_reply_length(reply: *mut RedisModuleCallReply) -> usize {
348    unsafe { RedisModule_CallReplyLength.unwrap()(reply) }
349}
350
351#[allow(clippy::not_unsafe_ptr_arg_deref)]
352pub fn call_reply_string_ptr(reply: *mut RedisModuleCallReply, len: *mut size_t) -> *const c_char {
353    unsafe { RedisModule_CallReplyStringPtr.unwrap()(reply, len) }
354}
355
356#[allow(clippy::not_unsafe_ptr_arg_deref)]
357pub fn call_reply_string(reply: *mut RedisModuleCallReply) -> Option<String> {
358    unsafe {
359        let mut len: size_t = 0;
360        let reply_string: *mut u8 =
361            RedisModule_CallReplyStringPtr.unwrap()(reply, &mut len) as *mut u8;
362        if reply_string.is_null() {
363            return None;
364        }
365        String::from_utf8(slice::from_raw_parts(reply_string, len).to_vec()).ok()
366    }
367}
368
369#[allow(clippy::not_unsafe_ptr_arg_deref)]
370#[inline]
371pub fn close_key(kp: *mut RedisModuleKey) {
372    unsafe { RedisModule_CloseKey.unwrap()(kp) }
373}
374
375#[allow(clippy::not_unsafe_ptr_arg_deref)]
376#[inline]
377pub fn open_key(
378    ctx: *mut RedisModuleCtx,
379    keyname: *mut RedisModuleString,
380    mode: KeyMode,
381) -> *mut RedisModuleKey {
382    unsafe { RedisModule_OpenKey.unwrap()(ctx, keyname, mode.bits()).cast::<RedisModuleKey>() }
383}
384
385#[allow(clippy::not_unsafe_ptr_arg_deref)]
386#[inline]
387pub(crate) fn open_key_with_flags(
388    ctx: *mut RedisModuleCtx,
389    keyname: *mut RedisModuleString,
390    mode: KeyMode,
391    flags: c_int,
392) -> *mut RedisModuleKey {
393    unsafe {
394        RedisModule_OpenKey.unwrap()(ctx, keyname, mode.bits() | flags).cast::<RedisModuleKey>()
395    }
396}
397
398#[allow(clippy::not_unsafe_ptr_arg_deref)]
399#[inline]
400pub fn reply_with_array(ctx: *mut RedisModuleCtx, len: c_long) -> Status {
401    unsafe { RedisModule_ReplyWithArray.unwrap()(ctx, len).into() }
402}
403
404#[allow(clippy::not_unsafe_ptr_arg_deref)]
405#[inline]
406pub fn reply_with_map(ctx: *mut RedisModuleCtx, len: c_long) -> Status {
407    unsafe {
408        RedisModule_ReplyWithMap
409            .map_or_else(
410                || RedisModule_ReplyWithArray.unwrap()(ctx, len * 2),
411                |f| f(ctx, len),
412            )
413            .into()
414    }
415}
416
417#[allow(clippy::not_unsafe_ptr_arg_deref)]
418#[inline]
419pub fn reply_with_set(ctx: *mut RedisModuleCtx, len: c_long) -> Status {
420    unsafe {
421        RedisModule_ReplyWithSet
422            .map_or_else(
423                || RedisModule_ReplyWithArray.unwrap()(ctx, len * 2),
424                |f| f(ctx, len),
425            )
426            .into()
427    }
428}
429
430#[allow(clippy::not_unsafe_ptr_arg_deref)]
431#[inline]
432pub fn reply_with_attribute(ctx: *mut RedisModuleCtx, len: c_long) -> Status {
433    unsafe { RedisModule_ReplyWithAttribute.unwrap()(ctx, len).into() }
434}
435
436#[allow(clippy::not_unsafe_ptr_arg_deref)]
437pub fn reply_with_error(ctx: *mut RedisModuleCtx, err: *const c_char) {
438    unsafe {
439        let msg = Context::str_as_legal_resp_string(CStr::from_ptr(err).to_str().unwrap());
440        RedisModule_ReplyWithError.unwrap()(ctx, msg.as_ptr());
441    }
442}
443
444#[allow(clippy::not_unsafe_ptr_arg_deref)]
445#[inline]
446pub fn reply_with_null(ctx: *mut RedisModuleCtx) -> Status {
447    unsafe { RedisModule_ReplyWithNull.unwrap()(ctx).into() }
448}
449
450#[allow(clippy::not_unsafe_ptr_arg_deref)]
451#[inline]
452pub fn reply_with_bool(ctx: *mut RedisModuleCtx, b: c_int) -> Status {
453    unsafe { RedisModule_ReplyWithBool.unwrap()(ctx, b).into() }
454}
455
456#[allow(clippy::not_unsafe_ptr_arg_deref)]
457#[inline]
458pub fn reply_with_long_long(ctx: *mut RedisModuleCtx, ll: c_longlong) -> Status {
459    unsafe { RedisModule_ReplyWithLongLong.unwrap()(ctx, ll).into() }
460}
461
462#[allow(clippy::not_unsafe_ptr_arg_deref)]
463#[inline]
464pub fn reply_with_double(ctx: *mut RedisModuleCtx, f: c_double) -> Status {
465    unsafe { RedisModule_ReplyWithDouble.unwrap()(ctx, f).into() }
466}
467
468#[allow(clippy::not_unsafe_ptr_arg_deref)]
469#[inline]
470pub fn reply_with_string(ctx: *mut RedisModuleCtx, s: *mut RedisModuleString) -> Status {
471    unsafe { RedisModule_ReplyWithString.unwrap()(ctx, s).into() }
472}
473
474#[allow(clippy::not_unsafe_ptr_arg_deref)]
475#[inline]
476pub fn reply_with_simple_string(ctx: *mut RedisModuleCtx, s: *const c_char) -> Status {
477    unsafe { RedisModule_ReplyWithSimpleString.unwrap()(ctx, s).into() }
478}
479
480#[allow(clippy::not_unsafe_ptr_arg_deref)]
481#[inline]
482pub fn reply_with_string_buffer(ctx: *mut RedisModuleCtx, s: *const c_char, len: size_t) -> Status {
483    unsafe { RedisModule_ReplyWithStringBuffer.unwrap()(ctx, s, len).into() }
484}
485
486#[allow(clippy::not_unsafe_ptr_arg_deref)]
487#[inline]
488pub fn reply_with_big_number(ctx: *mut RedisModuleCtx, s: *const c_char, len: size_t) -> Status {
489    unsafe { RedisModule_ReplyWithBigNumber.unwrap()(ctx, s, len).into() }
490}
491
492#[allow(clippy::not_unsafe_ptr_arg_deref)]
493#[inline]
494pub fn reply_with_verbatim_string(
495    ctx: *mut RedisModuleCtx,
496    s: *const c_char,
497    len: size_t,
498    format: *const c_char,
499) -> Status {
500    unsafe { RedisModule_ReplyWithVerbatimStringType.unwrap()(ctx, s, len, format).into() }
501}
502
503// Sets the expiry on a key.
504//
505// Expire is in milliseconds.
506#[allow(clippy::not_unsafe_ptr_arg_deref)]
507#[inline]
508pub fn set_expire(key: *mut RedisModuleKey, expire: c_longlong) -> Status {
509    unsafe { RedisModule_SetExpire.unwrap()(key, expire).into() }
510}
511
512#[allow(clippy::not_unsafe_ptr_arg_deref)]
513#[inline]
514pub fn string_dma(key: *mut RedisModuleKey, len: *mut size_t, mode: KeyMode) -> *mut c_char {
515    unsafe { RedisModule_StringDMA.unwrap()(key, len, mode.bits()) }
516}
517
518#[allow(clippy::not_unsafe_ptr_arg_deref)]
519#[inline]
520pub fn string_truncate(key: *mut RedisModuleKey, new_len: size_t) -> Status {
521    unsafe { RedisModule_StringTruncate.unwrap()(key, new_len).into() }
522}
523
524#[allow(clippy::not_unsafe_ptr_arg_deref)]
525pub fn hash_get_multi<T>(
526    key: *mut RedisModuleKey,
527    fields: &[T],
528    values: &mut [*mut RedisModuleString],
529) -> Result<(), ValkeyError>
530where
531    T: Into<Vec<u8>> + Clone,
532{
533    assert_eq!(fields.len(), values.len());
534
535    let fields = fields
536        .iter()
537        .map(|e| CString::new(e.clone()))
538        .collect::<Result<Vec<CString>, _>>()?;
539
540    let mut fi = fields.iter();
541    let mut vi = values.iter_mut();
542
543    macro_rules! rm {
544        () => { unsafe {
545            RedisModule_HashGet.unwrap()(key, REDISMODULE_HASH_CFIELDS as i32,
546                                         ptr::null::<c_char>())
547        }};
548        ($($args:expr)*) => { unsafe {
549            RedisModule_HashGet.unwrap()(
550                key, REDISMODULE_HASH_CFIELDS as i32,
551                $($args),*,
552                ptr::null::<c_char>()
553            )
554        }};
555    }
556    macro_rules! f {
557        () => {
558            fi.next().unwrap().as_ptr()
559        };
560    }
561    macro_rules! v {
562        () => {
563            vi.next().unwrap()
564        };
565    }
566
567    // This convoluted code is necessary since Redis only exposes a varargs API for HashGet
568    // to modules. Unfortunately there's no straightforward or portable way of calling a
569    // a varargs function with a variable number of arguments that is determined at runtime.
570    // See also the following Redis ticket: https://github.com/redis/redis/issues/7860
571    let res = Status::from(match fields.len() {
572        0 => rm! {},
573        1 => rm! {f!() v!()},
574        2 => rm! {f!() v!() f!() v!()},
575        3 => rm! {f!() v!() f!() v!() f!() v!()},
576        4 => rm! {f!() v!() f!() v!() f!() v!() f!() v!()},
577        5 => rm! {f!() v!() f!() v!() f!() v!() f!() v!() f!() v!()},
578        6 => rm! {f!() v!() f!() v!() f!() v!() f!() v!() f!() v!() f!() v!()},
579        7 => rm! {
580            f!() v!() f!() v!() f!() v!() f!() v!() f!() v!() f!() v!()
581            f!() v!()
582        },
583        8 => rm! {
584            f!() v!() f!() v!() f!() v!() f!() v!() f!() v!() f!() v!()
585            f!() v!() f!() v!()
586        },
587        9 => rm! {
588            f!() v!() f!() v!() f!() v!() f!() v!() f!() v!() f!() v!()
589            f!() v!() f!() v!() f!() v!()
590        },
591        10 => rm! {
592            f!() v!() f!() v!() f!() v!() f!() v!() f!() v!() f!() v!()
593            f!() v!() f!() v!() f!() v!() f!() v!()
594        },
595        11 => rm! {
596            f!() v!() f!() v!() f!() v!() f!() v!() f!() v!() f!() v!()
597            f!() v!() f!() v!() f!() v!() f!() v!() f!() v!()
598        },
599        12 => rm! {
600            f!() v!() f!() v!() f!() v!() f!() v!() f!() v!() f!() v!()
601            f!() v!() f!() v!() f!() v!() f!() v!() f!() v!() f!() v!()
602        },
603        _ => panic!("Unsupported length"),
604    });
605
606    match res {
607        Status::Ok => Ok(()),
608        Status::Err => Err(ValkeyError::Str("ERR key is not a hash value")),
609    }
610}
611
612#[allow(clippy::not_unsafe_ptr_arg_deref)]
613#[inline]
614pub fn hash_set(key: *mut RedisModuleKey, field: &str, value: *mut RedisModuleString) -> Status {
615    let field = CString::new(field).unwrap();
616
617    unsafe {
618        RedisModule_HashSet.unwrap()(
619            key,
620            REDISMODULE_HASH_CFIELDS as i32,
621            field.as_ptr(),
622            value,
623            ptr::null::<c_char>(),
624        )
625        .into()
626    }
627}
628
629#[allow(clippy::not_unsafe_ptr_arg_deref)]
630#[inline]
631pub fn hash_del(key: *mut RedisModuleKey, field: &str) -> Status {
632    let field = CString::new(field).unwrap();
633
634    // TODO: Add hash_del_multi()
635    // Support to pass multiple fields is desired but is complicated.
636    // See hash_get_multi() and https://github.com/redis/redis/issues/7860
637
638    unsafe {
639        RedisModule_HashSet.unwrap()(
640            key,
641            REDISMODULE_HASH_CFIELDS as i32,
642            field.as_ptr(),
643            REDISMODULE_HASH_DELETE,
644            ptr::null::<c_char>(),
645        )
646        .into()
647    }
648}
649
650#[allow(clippy::not_unsafe_ptr_arg_deref)]
651#[inline]
652pub fn list_push(
653    key: *mut RedisModuleKey,
654    list_where: Where,
655    element: *mut RedisModuleString,
656) -> Status {
657    unsafe { RedisModule_ListPush.unwrap()(key, list_where as i32, element).into() }
658}
659
660#[allow(clippy::not_unsafe_ptr_arg_deref)]
661#[inline]
662pub fn list_pop(key: *mut RedisModuleKey, list_where: Where) -> *mut RedisModuleString {
663    unsafe { RedisModule_ListPop.unwrap()(key, list_where as i32) }
664}
665
666// Returns pointer to the C string, and sets len to its length
667#[allow(clippy::not_unsafe_ptr_arg_deref)]
668#[inline]
669pub fn string_ptr_len(s: *const RedisModuleString, len: *mut size_t) -> *const c_char {
670    unsafe { RedisModule_StringPtrLen.unwrap()(s, len) }
671}
672
673#[allow(clippy::not_unsafe_ptr_arg_deref)]
674#[inline]
675pub fn string_retain_string(ctx: *mut RedisModuleCtx, s: *mut RedisModuleString) {
676    unsafe { RedisModule_RetainString.unwrap()(ctx, s) }
677}
678
679#[allow(clippy::not_unsafe_ptr_arg_deref)]
680#[inline]
681pub fn string_to_longlong(s: *const RedisModuleString, len: *mut i64) -> Status {
682    unsafe { RedisModule_StringToLongLong.unwrap()(s, len).into() }
683}
684
685#[allow(clippy::not_unsafe_ptr_arg_deref)]
686#[inline]
687pub fn string_to_double(s: *const RedisModuleString, len: *mut f64) -> Status {
688    unsafe { RedisModule_StringToDouble.unwrap()(s, len).into() }
689}
690
691#[allow(clippy::not_unsafe_ptr_arg_deref)]
692#[inline]
693pub fn string_set(key: *mut RedisModuleKey, s: *mut RedisModuleString) -> Status {
694    unsafe { RedisModule_StringSet.unwrap()(key, s).into() }
695}
696
697#[allow(clippy::not_unsafe_ptr_arg_deref)]
698#[inline]
699pub fn replicate_verbatim(ctx: *mut RedisModuleCtx) -> Status {
700    unsafe { RedisModule_ReplicateVerbatim.unwrap()(ctx).into() }
701}
702
703fn load<F, T>(rdb: *mut RedisModuleIO, f: F) -> Result<T, Error>
704where
705    F: FnOnce(*mut RedisModuleIO) -> T,
706{
707    let res = f(rdb);
708    if is_io_error(rdb) {
709        Err(ValkeyError::short_read().into())
710    } else {
711        Ok(res)
712    }
713}
714
715#[allow(clippy::not_unsafe_ptr_arg_deref)]
716pub fn load_unsigned(rdb: *mut RedisModuleIO) -> Result<u64, Error> {
717    unsafe { load(rdb, |rdb| RedisModule_LoadUnsigned.unwrap()(rdb)) }
718}
719
720#[allow(clippy::not_unsafe_ptr_arg_deref)]
721pub fn load_signed(rdb: *mut RedisModuleIO) -> Result<i64, Error> {
722    unsafe { load(rdb, |rdb| RedisModule_LoadSigned.unwrap()(rdb)) }
723}
724
725#[allow(clippy::not_unsafe_ptr_arg_deref)]
726pub fn load_string(rdb: *mut RedisModuleIO) -> Result<ValkeyString, Error> {
727    let p = unsafe { load(rdb, |rdb| RedisModule_LoadString.unwrap()(rdb))? };
728    Ok(ValkeyString::from_redis_module_string(ptr::null_mut(), p))
729}
730
731#[allow(clippy::not_unsafe_ptr_arg_deref)]
732pub fn load_string_buffer(rdb: *mut RedisModuleIO) -> Result<RedisBuffer, Error> {
733    unsafe {
734        let mut len = 0;
735        let buffer = load(rdb, |rdb| {
736            RedisModule_LoadStringBuffer.unwrap()(rdb, &mut len)
737        })?;
738        Ok(RedisBuffer::new(buffer, len))
739    }
740}
741
742#[allow(clippy::not_unsafe_ptr_arg_deref)]
743pub fn replicate<'a, T: Into<StrCallArgs<'a>>>(
744    ctx: *mut RedisModuleCtx,
745    command: &str,
746    args: T,
747) -> Status {
748    let mut call_args: StrCallArgs = args.into();
749    let final_args = call_args.args_mut();
750
751    let cmd = CString::new(command).unwrap();
752
753    unsafe {
754        RedisModule_Replicate.unwrap()(
755            ctx,
756            cmd.as_ptr(),
757            FMT,
758            final_args.as_ptr(),
759            final_args.len(),
760        )
761        .into()
762    }
763}
764
765#[allow(clippy::not_unsafe_ptr_arg_deref)]
766pub fn load_double(rdb: *mut RedisModuleIO) -> Result<f64, Error> {
767    unsafe { load(rdb, |rdb| RedisModule_LoadDouble.unwrap()(rdb)) }
768}
769
770#[allow(clippy::not_unsafe_ptr_arg_deref)]
771pub fn load_float(rdb: *mut RedisModuleIO) -> Result<f32, Error> {
772    unsafe { load(rdb, |rdb| RedisModule_LoadFloat.unwrap()(rdb)) }
773}
774
775#[allow(clippy::not_unsafe_ptr_arg_deref)]
776pub fn save_string(rdb: *mut RedisModuleIO, buf: &str) {
777    unsafe { RedisModule_SaveStringBuffer.unwrap()(rdb, buf.as_ptr().cast::<c_char>(), buf.len()) };
778}
779
780#[allow(clippy::not_unsafe_ptr_arg_deref)]
781/// Save the `RedisString` into the RDB
782pub fn save_redis_string(rdb: *mut RedisModuleIO, s: &ValkeyString) {
783    unsafe { RedisModule_SaveString.unwrap()(rdb, s.inner) };
784}
785
786#[allow(clippy::not_unsafe_ptr_arg_deref)]
787/// Save the `&[u8]` into the RDB
788pub fn save_slice(rdb: *mut RedisModuleIO, buf: &[u8]) {
789    unsafe { RedisModule_SaveStringBuffer.unwrap()(rdb, buf.as_ptr().cast::<c_char>(), buf.len()) };
790}
791
792#[allow(clippy::not_unsafe_ptr_arg_deref)]
793pub fn save_double(rdb: *mut RedisModuleIO, val: f64) {
794    unsafe { RedisModule_SaveDouble.unwrap()(rdb, val) };
795}
796
797#[allow(clippy::not_unsafe_ptr_arg_deref)]
798pub fn save_signed(rdb: *mut RedisModuleIO, val: i64) {
799    unsafe { RedisModule_SaveSigned.unwrap()(rdb, val) };
800}
801
802#[allow(clippy::not_unsafe_ptr_arg_deref)]
803pub fn save_float(rdb: *mut RedisModuleIO, val: f32) {
804    unsafe { RedisModule_SaveFloat.unwrap()(rdb, val) };
805}
806
807#[allow(clippy::not_unsafe_ptr_arg_deref)]
808pub fn save_unsigned(rdb: *mut RedisModuleIO, val: u64) {
809    unsafe { RedisModule_SaveUnsigned.unwrap()(rdb, val) };
810}
811
812#[allow(clippy::not_unsafe_ptr_arg_deref)]
813pub fn string_compare(a: *mut RedisModuleString, b: *mut RedisModuleString) -> Ordering {
814    unsafe { RedisModule_StringCompare.unwrap()(a, b).cmp(&0) }
815}
816
817#[allow(clippy::not_unsafe_ptr_arg_deref)]
818pub fn string_append_buffer(
819    ctx: *mut RedisModuleCtx,
820    s: *mut RedisModuleString,
821    buff: &str,
822) -> Status {
823    unsafe {
824        RedisModule_StringAppendBuffer.unwrap()(ctx, s, buff.as_ptr().cast::<c_char>(), buff.len())
825            .into()
826    }
827}
828
829#[allow(clippy::not_unsafe_ptr_arg_deref)]
830pub fn subscribe_to_server_event(
831    ctx: *mut RedisModuleCtx,
832    event: RedisModuleEvent,
833    callback: RedisModuleEventCallback,
834) -> Status {
835    unsafe { RedisModule_SubscribeToServerEvent.unwrap()(ctx, event, callback).into() }
836}
837
838#[allow(clippy::not_unsafe_ptr_arg_deref)]
839pub fn register_info_function(ctx: *mut RedisModuleCtx, callback: RedisModuleInfoFunc) -> Status {
840    unsafe { RedisModule_RegisterInfoFunc.unwrap()(ctx, callback).into() }
841}
842
843#[allow(clippy::not_unsafe_ptr_arg_deref)]
844pub fn add_info_section(ctx: *mut RedisModuleInfoCtx, name: Option<&str>) -> Status {
845    name.map(|n| CString::new(n).unwrap()).map_or_else(
846        || unsafe { RedisModule_InfoAddSection.unwrap()(ctx, ptr::null_mut()).into() },
847        |n| unsafe { RedisModule_InfoAddSection.unwrap()(ctx, n.as_ptr()).into() },
848    )
849}
850
851#[allow(clippy::not_unsafe_ptr_arg_deref)]
852pub fn add_info_field_str(ctx: *mut RedisModuleInfoCtx, name: &str, content: &str) -> Status {
853    let name = CString::new(name).unwrap();
854    let content = ValkeyString::create(None, content);
855    unsafe { RedisModule_InfoAddFieldString.unwrap()(ctx, name.as_ptr(), content.inner).into() }
856}
857
858#[allow(clippy::not_unsafe_ptr_arg_deref)]
859pub fn add_info_field_long_long(
860    ctx: *mut RedisModuleInfoCtx,
861    name: &str,
862    value: c_longlong,
863) -> Status {
864    let name = CString::new(name).unwrap();
865    unsafe { RedisModule_InfoAddFieldLongLong.unwrap()(ctx, name.as_ptr(), value).into() }
866}
867
868#[allow(clippy::not_unsafe_ptr_arg_deref)]
869pub fn add_info_field_unsigned_long_long(
870    ctx: *mut RedisModuleInfoCtx,
871    name: &str,
872    value: c_ulonglong,
873) -> Status {
874    let name = CString::new(name).unwrap();
875    unsafe { RedisModule_InfoAddFieldULongLong.unwrap()(ctx, name.as_ptr(), value).into() }
876}
877
878#[allow(clippy::not_unsafe_ptr_arg_deref)]
879pub fn add_info_field_double(ctx: *mut RedisModuleInfoCtx, name: &str, value: c_double) -> Status {
880    let name = CString::new(name).unwrap();
881    unsafe { RedisModule_InfoAddFieldDouble.unwrap()(ctx, name.as_ptr(), value).into() }
882}
883
884#[allow(clippy::not_unsafe_ptr_arg_deref)]
885pub fn add_info_begin_dict_field(ctx: *mut RedisModuleInfoCtx, name: &str) -> Status {
886    let name = CString::new(name).unwrap();
887    unsafe { RedisModule_InfoBeginDictField.unwrap()(ctx, name.as_ptr()).into() }
888}
889
890#[allow(clippy::not_unsafe_ptr_arg_deref)]
891pub fn add_info_end_dict_field(ctx: *mut RedisModuleInfoCtx) -> Status {
892    unsafe { RedisModule_InfoEndDictField.unwrap()(ctx).into() }
893}
894
895/// # Safety
896///
897/// This function is safe to use as it doesn't perform any work with
898/// the [RedisModuleCtx] pointer except for passing it to the valkey server.
899///
900/// # Panics
901///
902/// Panics when the [RedisModule_ExportSharedAPI] is unavailable.
903pub unsafe fn export_shared_api(
904    ctx: *mut RedisModuleCtx,
905    func: *const ::std::os::raw::c_void,
906    name: *const ::std::os::raw::c_char,
907) {
908    RedisModule_ExportSharedAPI.unwrap()(ctx, name, func as *mut ::std::os::raw::c_void);
909}
910
911/// # Safety
912///
913/// This function is safe to use as it doesn't perform any work with
914/// the [RedisModuleCtx] pointer except for passing it to the valkey server.
915///
916/// # Panics
917///
918/// Panics when the [RedisModule_NotifyKeyspaceEvent] is unavailable.
919pub unsafe fn notify_keyspace_event(
920    ctx: *mut RedisModuleCtx,
921    event_type: NotifyEvent,
922    event: &str,
923    keyname: &ValkeyString,
924) -> Status {
925    let event = CString::new(event).unwrap();
926    RedisModule_NotifyKeyspaceEvent.unwrap()(ctx, event_type.bits(), event.as_ptr(), keyname.inner)
927        .into()
928}
929
930/// # Panics
931///
932/// Panics when the [RedisModule_GetNotifyKeyspaceEvents] is unavailable.
933#[must_use]
934pub fn get_keyspace_events() -> NotifyEvent {
935    unsafe {
936        let events = RedisModule_GetNotifyKeyspaceEvents.unwrap()();
937        NotifyEvent::from_bits_truncate(events)
938    }
939}
940
941/// Returns all the available notification flags for key-space
942/// notifications.
943///
944/// # Safety
945///
946/// This function is safe to use as it doesn't perform any work with
947/// the [RedisModuleCtx] pointer except for passing it to the valkey server.
948///
949/// # Panics
950///
951/// Panics when the [RedisModule_GetKeyspaceNotificationFlagsAll] is
952/// unavailable.
953pub fn get_keyspace_notification_flags_all() -> NotifyEvent {
954    unsafe {
955        NotifyEvent::from_bits_truncate(RedisModule_GetKeyspaceNotificationFlagsAll.unwrap()())
956    }
957}
958
959#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
960pub struct Version {
961    pub major: i32,
962    pub minor: i32,
963    pub patch: i32,
964}
965
966impl From<c_int> for Version {
967    fn from(ver: c_int) -> Self {
968        // Expected format: 0x00MMmmpp for Major, minor, patch
969        Self {
970            major: (ver & 0x00FF_0000) >> 16,
971            minor: (ver & 0x0000_FF00) >> 8,
972            patch: (ver & 0x0000_00FF),
973        }
974    }
975}
976
977#[allow(clippy::not_unsafe_ptr_arg_deref)]
978pub fn is_io_error(rdb: *mut RedisModuleIO) -> bool {
979    unsafe { RedisModule_IsIOError.unwrap()(rdb) != 0 }
980}