1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
use crate::context::thread_safe::{RedisGILGuard, RedisLockIndicator};
use crate::{raw, CallOptionResp, CallOptionsBuilder, CallResult, RedisValue};
use crate::{Context, RedisError, RedisString};
use bitflags::bitflags;
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::os::raw::{c_char, c_int, c_longlong, c_void};
use std::sync::atomic::{AtomicBool, AtomicI64, Ordering};
use std::sync::Mutex;

bitflags! {
    /// Configuration options
    pub struct ConfigurationFlags : u32 {
        /// The default flags for a config. This creates a config that can be modified after startup.
        const DEFAULT = raw::REDISMODULE_CONFIG_DEFAULT;

        /// This config can only be provided loading time.
        const IMMUTABLE = raw::REDISMODULE_CONFIG_IMMUTABLE;

        /// The value stored in this config is redacted from all logging.
        const SENSITIVE = raw::REDISMODULE_CONFIG_SENSITIVE;

        /// The name is hidden from `CONFIG GET` with pattern matching.
        const HIDDEN = raw::REDISMODULE_CONFIG_HIDDEN;

        /// This config will be only be modifiable based off the value of enable-protected-configs.
        const PROTECTED = raw::REDISMODULE_CONFIG_PROTECTED;

        /// This config is not modifiable while the server is loading data.
        const DENY_LOADING = raw::REDISMODULE_CONFIG_DENY_LOADING;

        /// For numeric configs, this config will convert data unit notations into their byte equivalent.
        const MEMORY = raw::REDISMODULE_CONFIG_MEMORY;

        /// For enum configs, this config will allow multiple entries to be combined as bit flags.
        const BITFLAGS = raw::REDISMODULE_CONFIG_BITFLAGS;
    }
}

#[macro_export]
macro_rules! enum_configuration {
    ($(#[$meta:meta])* $vis:vis enum $name:ident {
        $($(#[$vmeta:meta])* $vname:ident = $val:expr,)*
    }) => {
        use $crate::configuration::EnumConfigurationValue;
        $(#[$meta])*
        $vis enum $name {
            $($(#[$vmeta])* $vname = $val,)*
        }

        impl std::convert::TryFrom<i32> for $name {
            type Error = $crate::RedisError;

            fn try_from(v: i32) -> Result<Self, Self::Error> {
                match v {
                    $(x if x == $name::$vname as i32 => Ok($name::$vname),)*
                    _ => Err($crate::RedisError::Str("Value is not supported")),
                }
            }
        }

        impl std::convert::From<$name> for i32 {
            fn from(val: $name) -> Self {
                val as i32
            }
        }

        impl EnumConfigurationValue for $name {
            fn get_options(&self) -> (Vec<String>, Vec<i32>) {
                (vec![$(stringify!($vname).to_string(),)*], vec![$($val,)*])
            }
        }

        impl Clone for $name {
            fn clone(&self) -> Self {
                match self {
                    $($name::$vname => $name::$vname,)*
                }
            }
        }
    }
}

/// [`ConfigurationContext`] is used as a special context that indicate that we are
/// running with the Redis GIL is held but we should not perform all the regular
/// operation we can perfrom on the regular Context.
pub struct ConfigurationContext {
    _dummy: usize, // We set some none public vairable here so user will not be able to construct such object
}

impl ConfigurationContext {
    fn new() -> ConfigurationContext {
        ConfigurationContext { _dummy: 0 }
    }
}

unsafe impl RedisLockIndicator for ConfigurationContext {}

pub trait ConfigurationValue<T>: Sync + Send {
    fn get(&self, ctx: &ConfigurationContext) -> T;
    fn set(&self, ctx: &ConfigurationContext, val: T) -> Result<(), RedisError>;
}

pub trait EnumConfigurationValue: TryFrom<i32, Error = RedisError> + Into<i32> + Clone {
    fn get_options(&self) -> (Vec<String>, Vec<i32>);
}

impl<T: Clone> ConfigurationValue<T> for RedisGILGuard<T> {
    fn get(&self, ctx: &ConfigurationContext) -> T {
        let value = self.lock(ctx);
        value.clone()
    }
    fn set(&self, ctx: &ConfigurationContext, val: T) -> Result<(), RedisError> {
        let mut value = self.lock(ctx);
        *value = val;
        Ok(())
    }
}

impl<T: Clone + Send> ConfigurationValue<T> for Mutex<T> {
    fn get(&self, _ctx: &ConfigurationContext) -> T {
        let value = self.lock().unwrap();
        value.clone()
    }
    fn set(&self, _ctx: &ConfigurationContext, val: T) -> Result<(), RedisError> {
        let mut value = self.lock().unwrap();
        *value = val;
        Ok(())
    }
}

impl ConfigurationValue<i64> for AtomicI64 {
    fn get(&self, _ctx: &ConfigurationContext) -> i64 {
        self.load(Ordering::Relaxed)
    }
    fn set(&self, _ctx: &ConfigurationContext, val: i64) -> Result<(), RedisError> {
        self.store(val, Ordering::Relaxed);
        Ok(())
    }
}

impl ConfigurationValue<RedisString> for RedisGILGuard<String> {
    fn get(&self, ctx: &ConfigurationContext) -> RedisString {
        let value = self.lock(ctx);
        RedisString::create(None, value.as_str())
    }
    fn set(&self, ctx: &ConfigurationContext, val: RedisString) -> Result<(), RedisError> {
        let mut value = self.lock(ctx);
        *value = val.try_as_str()?.to_string();
        Ok(())
    }
}

impl ConfigurationValue<RedisString> for Mutex<String> {
    fn get(&self, _ctx: &ConfigurationContext) -> RedisString {
        let value = self.lock().unwrap();
        RedisString::create(None, value.as_str())
    }
    fn set(&self, _ctx: &ConfigurationContext, val: RedisString) -> Result<(), RedisError> {
        let mut value = self.lock().unwrap();
        *value = val.try_as_str()?.to_string();
        Ok(())
    }
}

impl ConfigurationValue<bool> for AtomicBool {
    fn get(&self, _ctx: &ConfigurationContext) -> bool {
        self.load(Ordering::Relaxed)
    }
    fn set(&self, _ctx: &ConfigurationContext, val: bool) -> Result<(), RedisError> {
        self.store(val, Ordering::Relaxed);
        Ok(())
    }
}

type OnUpdatedCallback<T> = Box<dyn Fn(&ConfigurationContext, &str, &'static T)>;

struct ConfigrationPrivateData<G, T: ConfigurationValue<G> + 'static> {
    variable: &'static T,
    on_changed: Option<OnUpdatedCallback<T>>,
    phantom: PhantomData<G>,
}

impl<G, T: ConfigurationValue<G> + 'static> ConfigrationPrivateData<G, T> {
    fn set_val(&self, name: *const c_char, val: G, err: *mut *mut raw::RedisModuleString) -> c_int {
        // we know the GIL is held so it is safe to use Context::dummy().
        let configuration_ctx = ConfigurationContext::new();
        if let Err(e) = self.variable.set(&configuration_ctx, val) {
            let error_msg = RedisString::create(None, e.to_string().as_str());
            unsafe { *err = error_msg.take() };
            return raw::REDISMODULE_ERR as i32;
        }
        let c_str_name = unsafe { CStr::from_ptr(name) };
        if let Some(v) = self.on_changed.as_ref() {
            v(
                &configuration_ctx,
                c_str_name.to_str().unwrap(),
                self.variable,
            )
        }
        raw::REDISMODULE_OK as i32
    }

    fn get_val(&self) -> G {
        self.variable.get(&ConfigurationContext::new())
    }
}

extern "C" fn i64_configuration_set<T: ConfigurationValue<i64> + 'static>(
    name: *const c_char,
    val: c_longlong,
    privdata: *mut c_void,
    err: *mut *mut raw::RedisModuleString,
) -> c_int {
    let private_data = unsafe { &*(privdata as *const ConfigrationPrivateData<i64, T>) };
    private_data.set_val(name, val, err)
}

extern "C" fn i64_configuration_get<T: ConfigurationValue<i64> + 'static>(
    _name: *const c_char,
    privdata: *mut c_void,
) -> c_longlong {
    let private_data = unsafe { &*(privdata as *const ConfigrationPrivateData<i64, T>) };
    private_data.get_val()
}

pub fn register_i64_configuration<T: ConfigurationValue<i64>>(
    ctx: &Context,
    name: &str,
    variable: &'static T,
    default: i64,
    min: i64,
    max: i64,
    flags: ConfigurationFlags,
    on_changed: Option<OnUpdatedCallback<T>>,
) {
    let name = CString::new(name).unwrap();
    let config_private_data = ConfigrationPrivateData {
        variable,
        on_changed,
        phantom: PhantomData::<i64>,
    };
    unsafe {
        raw::RedisModule_RegisterNumericConfig.unwrap()(
            ctx.ctx,
            name.as_ptr(),
            default,
            flags.bits(),
            min,
            max,
            Some(i64_configuration_get::<T>),
            Some(i64_configuration_set::<T>),
            None,
            Box::into_raw(Box::new(config_private_data)) as *mut c_void,
        );
    }
}

fn find_config_value<'a>(args: &'a [RedisString], name: &str) -> Option<&'a RedisString> {
    args.iter()
        .skip_while(|item| !item.as_slice().eq(name.as_bytes()))
        .nth(1)
}

pub fn get_i64_default_config_value(
    args: &[RedisString],
    name: &str,
    default: i64,
) -> Result<i64, RedisError> {
    find_config_value(args, name).map_or(Ok(default), |arg| {
        arg.try_as_str()?
            .parse::<i64>()
            .map_err(|e| RedisError::String(e.to_string()))
    })
}

extern "C" fn string_configuration_set<T: ConfigurationValue<RedisString> + 'static>(
    name: *const c_char,
    val: *mut raw::RedisModuleString,
    privdata: *mut c_void,
    err: *mut *mut raw::RedisModuleString,
) -> c_int {
    let new_val = RedisString::new(None, val);
    let private_data = unsafe { &*(privdata as *const ConfigrationPrivateData<RedisString, T>) };
    private_data.set_val(name, new_val, err)
}

extern "C" fn string_configuration_get<T: ConfigurationValue<RedisString> + 'static>(
    _name: *const c_char,
    privdata: *mut c_void,
) -> *mut raw::RedisModuleString {
    let private_data = unsafe { &*(privdata as *const ConfigrationPrivateData<RedisString, T>) };
    // we know the GIL is held so it is safe to use Context::dummy().
    private_data
        .variable
        .get(&ConfigurationContext::new())
        .take()
}

pub fn register_string_configuration<T: ConfigurationValue<RedisString>>(
    ctx: &Context,
    name: &str,
    variable: &'static T,
    default: &str,
    flags: ConfigurationFlags,
    on_changed: Option<OnUpdatedCallback<T>>,
) {
    let name = CString::new(name).unwrap();
    let default = CString::new(default).unwrap();
    let config_private_data = ConfigrationPrivateData {
        variable,
        on_changed,
        phantom: PhantomData::<RedisString>,
    };
    unsafe {
        raw::RedisModule_RegisterStringConfig.unwrap()(
            ctx.ctx,
            name.as_ptr(),
            default.as_ptr(),
            flags.bits(),
            Some(string_configuration_get::<T>),
            Some(string_configuration_set::<T>),
            None,
            Box::into_raw(Box::new(config_private_data)) as *mut c_void,
        );
    }
}

pub fn get_string_default_config_value<'a>(
    args: &'a [RedisString],
    name: &str,
    default: &'a str,
) -> Result<&'a str, RedisError> {
    find_config_value(args, name).map_or(Ok(default), |arg| arg.try_as_str())
}

extern "C" fn bool_configuration_set<T: ConfigurationValue<bool> + 'static>(
    name: *const c_char,
    val: i32,
    privdata: *mut c_void,
    err: *mut *mut raw::RedisModuleString,
) -> c_int {
    let private_data = unsafe { &*(privdata as *const ConfigrationPrivateData<bool, T>) };
    private_data.set_val(name, val != 0, err)
}

extern "C" fn bool_configuration_get<T: ConfigurationValue<bool> + 'static>(
    _name: *const c_char,
    privdata: *mut c_void,
) -> c_int {
    let private_data = unsafe { &*(privdata as *const ConfigrationPrivateData<bool, T>) };
    private_data.get_val() as i32
}

pub fn register_bool_configuration<T: ConfigurationValue<bool>>(
    ctx: &Context,
    name: &str,
    variable: &'static T,
    default: bool,
    flags: ConfigurationFlags,
    on_changed: Option<OnUpdatedCallback<T>>,
) {
    let name = CString::new(name).unwrap();
    let config_private_data = ConfigrationPrivateData {
        variable,
        on_changed,
        phantom: PhantomData::<bool>,
    };
    unsafe {
        raw::RedisModule_RegisterBoolConfig.unwrap()(
            ctx.ctx,
            name.as_ptr(),
            default as i32,
            flags.bits(),
            Some(bool_configuration_get::<T>),
            Some(bool_configuration_set::<T>),
            None,
            Box::into_raw(Box::new(config_private_data)) as *mut c_void,
        );
    }
}

pub fn get_bool_default_config_value(
    args: &[RedisString],
    name: &str,
    default: bool,
) -> Result<bool, RedisError> {
    find_config_value(args, name).map_or(Ok(default), |arg| Ok(arg.try_as_str()? == "yes"))
}

extern "C" fn enum_configuration_set<
    G: EnumConfigurationValue,
    T: ConfigurationValue<G> + 'static,
>(
    name: *const c_char,
    val: i32,
    privdata: *mut c_void,
    err: *mut *mut raw::RedisModuleString,
) -> c_int {
    let private_data = unsafe { &*(privdata as *const ConfigrationPrivateData<G, T>) };
    let val: Result<G, _> = val.try_into();
    match val {
        Ok(val) => private_data.set_val(name, val, err),
        Err(e) => {
            let error_msg = RedisString::create(None, e.to_string().as_str());
            unsafe { *err = error_msg.take() };
            raw::REDISMODULE_ERR as i32
        }
    }
}

extern "C" fn enum_configuration_get<
    G: EnumConfigurationValue,
    T: ConfigurationValue<G> + 'static,
>(
    _name: *const c_char,
    privdata: *mut c_void,
) -> c_int {
    let private_data = unsafe { &*(privdata as *const ConfigrationPrivateData<G, T>) };
    private_data.get_val().into()
}

pub fn register_enum_configuration<G: EnumConfigurationValue, T: ConfigurationValue<G>>(
    ctx: &Context,
    name: &str,
    variable: &'static T,
    default: G,
    flags: ConfigurationFlags,
    on_changed: Option<OnUpdatedCallback<T>>,
) {
    let name = CString::new(name).unwrap();
    let (names, vals) = default.get_options();
    assert_eq!(names.len(), vals.len());
    let names: Vec<CString> = names
        .into_iter()
        .map(|v| CString::new(v).unwrap())
        .collect();
    let config_private_data = ConfigrationPrivateData {
        variable,
        on_changed,
        phantom: PhantomData::<G>,
    };
    unsafe {
        raw::RedisModule_RegisterEnumConfig.unwrap()(
            ctx.ctx,
            name.as_ptr(),
            default.into(),
            flags.bits(),
            names
                .iter()
                .map(|v| v.as_ptr())
                .collect::<Vec<*const c_char>>()
                .as_mut_ptr(),
            vals.as_ptr(),
            names.len() as i32,
            Some(enum_configuration_get::<G, T>),
            Some(enum_configuration_set::<G, T>),
            None,
            Box::into_raw(Box::new(config_private_data)) as *mut c_void,
        );
    }
}

pub fn get_enum_default_config_value<G: EnumConfigurationValue>(
    args: &[RedisString],
    name: &str,
    default: G,
) -> Result<G, RedisError> {
    find_config_value(args, name).map_or(Ok(default.clone()), |arg| {
        let (names, vals) = default.get_options();
        let (index, _name) = names
            .into_iter()
            .enumerate()
            .find(|(_index, item)| item.as_bytes().eq(arg.as_slice()))
            .ok_or(RedisError::String(format!(
                "Enum '{}' not exists",
                arg.to_string_lossy()
            )))?;
        G::try_from(vals[index])
    })
}

pub fn module_config_get(
    ctx: &Context,
    args: Vec<RedisString>,
    name: &str,
) -> Result<RedisValue, RedisError> {
    let mut args: Vec<String> = args
        .into_iter()
        .skip(1)
        .map(|e| format!("{}.{}", name, e.to_string_lossy()))
        .collect();
    args.insert(0, "get".into());
    let res: CallResult = ctx.call_ext(
        "config",
        &CallOptionsBuilder::new()
            .errors_as_replies()
            .resp(CallOptionResp::Auto)
            .build(),
        args.iter()
            .map(|v| v.as_str())
            .collect::<Vec<&str>>()
            .as_slice(),
    );
    let res = res.map_err(|e| {
        RedisError::String(
            e.to_utf8_string()
                .unwrap_or("Failed converting error to utf8".into()),
        )
    })?;
    Ok((&res).into())
}

pub fn module_config_set(
    ctx: &Context,
    args: Vec<RedisString>,
    name: &str,
) -> Result<RedisValue, RedisError> {
    let mut args: Vec<String> = args
        .into_iter()
        .skip(1)
        .enumerate()
        .map(|(index, e)| {
            if index % 2 == 0 {
                format!("{}.{}", name, e.to_string_lossy())
            } else {
                e.to_string_lossy()
            }
        })
        .collect();
    args.insert(0, "set".into());
    let res: CallResult = ctx.call_ext(
        "config",
        &CallOptionsBuilder::new()
            .errors_as_replies()
            .resp(CallOptionResp::Auto)
            .build(),
        args.iter()
            .map(|v| v.as_str())
            .collect::<Vec<&str>>()
            .as_slice(),
    );
    let res = res.map_err(|e| {
        RedisError::String(
            e.to_utf8_string()
                .unwrap_or("Failed converting error to utf8".into()),
        )
    })?;
    Ok((&res).into())
}