nexus-common 0.4.1

Nexus common utils
Documentation
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
use crate::db::get_redis_conn;
use crate::types::DynError;
use deadpool_redis::redis::Script;
use deadpool_redis::redis::{AsyncCommands, JsonAsyncCommands};
use serde::{de::DeserializeOwned, Serialize};
use tracing::{debug, trace};

#[derive(Clone, Debug)]
pub enum JsonAction {
    Increment(i64),
    Decrement(i64),
}

pub struct ValueRange {
    min: i64,
    max: i64,
}

impl Default for ValueRange {
    fn default() -> Self {
        Self {
            min: 0,
            max: u32::MAX as i64,
        }
    }
}

/// Sets a value in Redis, supporting both JSON objects and boolean values.
///
/// This function stores JSON objects using RedisJSON and booleans as integers (0 or 1).
/// An optional expiration time can be set for both types.
///
/// # Arguments
///
/// * `prefix` - A string slice representing the prefix for the Redis key.
/// * `key` - A string slice representing the key under which the value is stored.
/// * `value` - A reference to the value to be stored. If the value is a boolean, it will be stored as 0 or 1. For other types, it must implement `Serialize`.
/// * `path` - An optional string slice representing the JSON path where the value should be set for JSON objects. Defaults to the root path "$".
/// * `expiration` - An optional expiration time in seconds. If provided, the key will expire after this duration.
///
/// # Errors
///
/// Returns an error if the operation fails.
pub async fn put<T: Serialize + Send + Sync>(
    prefix: &str,
    key: &str,
    value: &T,
    path: Option<&str>,
    expiration: Option<i64>,
) -> Result<(), DynError> {
    let index_key = format!("{prefix}:{key}");

    match serde_json::to_value(value)? {
        serde_json::Value::Bool(boolean_value) => {
            handle_put_boolean(&index_key, boolean_value, expiration).await?;
        }
        _ => {
            handle_put_json(&index_key, value, path, expiration).await?;
        }
    }

    debug!(
        "Set key: {} with optional expiration: {:?}",
        index_key, expiration
    );
    Ok(())
}

/// Modifies a numeric field in a Redis JSON object by either incrementing or decrementing it.
/// Uses LUA to ensure the value is never negative
///
/// This function uses the RedisJSON `JSON.NUMINCRBY` command to either increment or decrement a numeric field at a given path
/// based on the `JsonAction` provided.
///
/// # Arguments
///
/// * `prefix` - A string slice representing the prefix for the Redis key.
/// * `key` - A string slice representing the Redis key where the JSON object is stored.
/// * `field` - A string slice representing the field to be modified in the JSON object.
/// * `action` - A `JsonAction` enum that specifies whether to increment or decrement the field.
///
/// # Errors
///
/// Returns an error if the operation fails or if the field does not exist or is not numeric.
pub async fn modify_json_field(
    prefix: &str,
    key: &str,
    field: &str,
    action: JsonAction,
    range: Option<ValueRange>,
) -> Result<(), DynError> {
    let mut redis_conn = get_redis_conn().await?;
    let index_key = format!("{prefix}:{key}");
    let json_path = format!("$.{field}"); // Access the field using JSON path

    // Determine the action to take (increment or decrement)
    let amount = match action {
        JsonAction::Increment(value) => value,
        JsonAction::Decrement(value) => -value, // Negate the value for decrement
    };

    // Use default range if None provided
    let range = range.unwrap_or_default();

    // Lua script to safely increment/decrement without going below zero
    let script = Script::new(
        r#"
        local path = ARGV[1]
        local amount = tonumber(ARGV[2])
        local min_value = tonumber(ARGV[3])
        local max_value = tonumber(ARGV[4])
        local current = 0

        -- Fetch the current value as a JSON string
        local current_value = redis.call('JSON.GET', KEYS[1], path)

        if current_value ~= nil then
            -- Decode the JSON string into a Lua table
            local decoded = cjson.decode(current_value)
            
            if type(decoded) == 'table' then
                -- If the decoded value is an array, extract the first element
                if #decoded > 0 then
                    current = tonumber(decoded[1]) or 0
                end
            elseif type(decoded) == 'number' then
                -- If the decoded value is a number, use it directly
                current = decoded
            end
        end

        local new_value = current + amount

        -- Enforce min and max boundaries
        if new_value < min_value then
            new_value = min_value
        elseif new_value > max_value then
            new_value = max_value
        end

        -- Set the new value
        redis.call('JSON.SET', KEYS[1], path, new_value)
        return new_value
    "#,
    );

    debug!(
        "Modifiying field: {} in key: {} by {}",
        field, index_key, amount
    );

    let _: i64 = script
        .key(index_key)
        .arg(json_path)
        .arg(amount.to_string())
        .arg(range.min.to_string())
        .arg(range.max.to_string())
        .invoke_async(&mut redis_conn)
        .await?;

    Ok(())
}

/// Handles storing a boolean value in Redis with an optional expiration.
///
/// This function sets a key in Redis to either `1` or `0`, depending on the boolean value provided.
/// Optionally, an expiration time can be set for the key.
///
/// # Arguments
///
/// * `key` - A string slice representing the Redis key.
/// * `value` - A boolean value to store. If `true`, `1` is stored; if `false`, `0` is stored.
/// * `expiration` - An optional expiration time in seconds. If provided, the key will expire after this duration.
async fn handle_put_boolean(
    key: &str,
    value: bool,
    expiration: Option<i64>,
) -> Result<(), DynError> {
    let mut redis_conn = get_redis_conn().await?;

    let int_value = if value { 1 } else { 0 };
    if let Some(exp) = expiration {
        let _: () = redis_conn.set_ex(key, int_value, exp as u64).await?;
    } else {
        let _: () = redis_conn.set(key, int_value).await?;
    }
    Ok(())
}

/// Handles storing a JSON object in Redis at a specified path, with optional expiration.
///
/// This function uses RedisJSON to store a JSON object under the provided key and path. An expiration time
/// can optionally be set for the key.
///
/// # Arguments
///
/// * `key` - A string slice representing the Redis key.
/// * `value` - A reference to the value to be stored, which must implement `Serialize`.
/// * `path` - An optional string slice representing the JSON path where the value should be set. Defaults to the root path "$".
/// * `expiration` - An optional expiration time in seconds. If provided, the key will expire after this duration.
async fn handle_put_json<T: Serialize + Send + Sync>(
    key: &str,
    value: &T,
    path: Option<&str>,
    expiration: Option<i64>,
) -> Result<(), DynError> {
    let mut redis_conn = get_redis_conn().await?;

    let json_path = path.unwrap_or("$");
    let _: () = redis_conn.json_set(key, json_path, value).await?;
    if let Some(exp) = expiration {
        let _: () = redis_conn.expire(key, exp).await?;
    }
    Ok(())
}

/// Sets a list of keys and their corresponding values in Redis in a single operation.
///
/// This function handles both JSON and boolean values, using RedisJSON for JSON data and storing booleans
/// as integers. It uses multiple commands in a single request to set key-value pairs efficiently.
/// Each value must implement the `Serialize` trait for JSON or be a boolean.
///
/// # Arguments
///
/// * `prefix` - A string slice representing the prefix for the Redis keys.
/// * `data` - A slice of tuples where each tuple contains a key as a string slice and a value that implements `Serialize`.
///
/// # Returns
///
/// Returns a `Result` indicating success or an error if the operation fails.
///
/// # Errors
///
/// This function will return an error if there are issues connecting to Redis, or if serialization of the values fails.
///
/// # Examples
///
/// ```ignore
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct MyValue {
///     field1: String,
///     field2: i32,
/// }
///
/// let data = vec![
///     ("key1", MyValue { field1: "value1".to_string(), field2: 10 }),
///     ("key2", MyValue { field1: "value2".to_string(), field2: 20 }),
///     ("key3", true), // boolean value
/// ];
///
/// put_multiple::<MyValue>("prefix:", &data).await?;
/// ```
///
/// This example sets multiple key-value pairs with a common prefix in Redis.
pub async fn put_multiple<T: Serialize>(
    prefix: &str,
    data: &[(impl AsRef<str>, T)],
) -> Result<(), DynError> {
    let mut redis_conn = get_redis_conn().await?;

    // Create a pipeline-like command sequence
    let mut cmd = redis::pipe();

    for (key, value) in data {
        let full_key = format!("{}:{}", prefix, key.as_ref());

        // Check if the value is boolean
        match serde_json::to_value(value)? {
            serde_json::Value::Bool(boolean_value) => {
                let int_value = if boolean_value { 1 } else { 0 };
                cmd.set(&full_key, int_value);
            }
            _ => {
                // Handle other values as JSON
                cmd.json_set(&full_key, "$", value)?;
            }
        }
    }

    let _: () = cmd.query_async(&mut redis_conn).await?;
    Ok(())
}

/// Retrieves a JSON value from Redis at a specified path.
///
/// # Arguments
///
/// * `prefix` - A string slice that represents the prefix for the Redis key.
/// * `key` - A string slice that represents the key under which the value is stored.
/// * `path` - An optional string slice representing the JSON path from which the value should be retrieved. Defaults to the root path "$".
///
/// # Returns
///
/// Returns an `Option` containing the retrieved value if it exists, or `None` if it does not.
///
/// # Errors
///
/// Returns an error if the operation fails.
pub async fn get<T: DeserializeOwned + Send + Sync>(
    prefix: &str,
    key: &str,
    path: Option<&str>,
) -> Result<Option<T>, DynError> {
    let mut redis_conn = get_redis_conn().await?;
    let index_key = format!("{prefix}:{key}");
    let json_path = path.unwrap_or("$").to_string(); // Ensure path is a String

    // Use RedisJSON commands to get the value from the specified path
    if let Ok(indexed_value) = redis_conn
        .json_get::<String, String, String>(index_key.clone(), json_path)
        .await
    {
        //debug!("Restored key: {} with value: {}", index_key, indexed_value);
        let value: Vec<T> = serde_json::from_str(&indexed_value)?;
        return Ok(value.into_iter().next()); // Extract the first element from the Vec
    }

    Ok(None)
}

/// Retrieves a list of JSON values from Redis based on a list of keys.
///
/// This function fetches JSON objects from Redis using RedisJSON based on the provided keys.
///
/// # Arguments
///
/// * `prefix` - A string slice representing the prefix for the Redis keys.
/// * `keys` - A slice of strings representing the keys under which the values are stored.
/// * `path` - An optional string slice representing the JSON path from which the value should be retrieved. Defaults to the root path "$".
///
/// # Returns
///
/// Returns a vector of optional values corresponding to the provided keys.
///
/// # Errors
///
/// Returns an error if the operation fails.
pub async fn get_multiple<T: DeserializeOwned + Send + Sync>(
    prefix: &str,
    keys: &[impl AsRef<str>],
    path: Option<&str>,
) -> Result<Vec<Option<T>>, DynError> {
    let mut redis_conn = get_redis_conn().await?;
    let json_path = path.unwrap_or("$");

    // Generate full keys with prefix
    let full_keys: Vec<String> = keys
        .iter()
        .map(|key| format!("{}:{}", prefix, key.as_ref()))
        .collect();

    // Fetch values as Option<String> to handle missing keys
    let indexed_values: Vec<Option<String>> = redis_conn.json_get(&full_keys, json_path).await?;

    // Check if indexed_values is empty. That's an edge case 1 element and it was not found, redis does not return None.
    let results: Vec<Option<T>> = if indexed_values.is_empty() {
        (0..keys.len()).map(|_| None).collect()
    } else {
        deserialize_values(indexed_values)?
    };

    Ok(results)
}

// Helper function to deserialize JSON strings to Vec<Option<T>>
fn deserialize_values<T: DeserializeOwned>(
    values: Vec<Option<String>>,
) -> Result<Vec<Option<T>>, DynError> {
    values
        .into_iter()
        .map(|value_str| match value_str {
            Some(value) => {
                let value: Vec<T> = serde_json::from_str(&value)?;
                Ok(value.into_iter().next())
            }
            None => Ok(None),
        })
        .collect()
}

/// Retrieves a boolean value from Redis.
///
/// # Arguments
///
/// * `prefix` - A string slice that represents the prefix for the Redis key.
/// * `key` - A string slice that represents the key under which the value is stored.
///
/// # Returns
///
/// Returns an `Option` containing the retrieved boolean value if it exists, or `None` if it does not.
///
/// # Errors
///
/// Returns an error if the operation fails.
pub async fn _get_bool(prefix: &str, key: &str) -> Result<Option<bool>, DynError> {
    let mut redis_conn = get_redis_conn().await?;
    let index_key = format!("{prefix}:{key}");

    if let Ok(indexed_value) = redis_conn.get::<_, i32>(&index_key).await {
        trace!(
            "Restored boolean key: {} with value: {}",
            index_key,
            indexed_value
        );
        let value = match indexed_value {
            1 => true,
            0 => false,
            _ => return Ok(None), // Invalid value in Redis
        };
        return Ok(Some(value));
    }

    Ok(None)
}

/// Deletes multiple keys from Redis.
///
/// This function removes the specified keys from Redis. If a key does not exist, no error is returned.
///
/// # Arguments
///
/// * `prefix` - A string slice representing the prefix for the Redis keys.
/// * `keys` - A slice of strings representing the keys to be deleted.
///
/// # Errors
///
/// Returns an error if the operation fails.
pub async fn del_multiple(prefix: &str, keys: &[impl AsRef<str>]) -> Result<(), DynError> {
    let mut redis_conn = get_redis_conn().await?;

    // Generate full keys with prefix
    let full_keys: Vec<String> = keys
        .iter()
        .map(|key| format!("{}:{}", prefix, key.as_ref()))
        .collect();

    let _: () = redis_conn.del(full_keys).await?;
    Ok(())
}