Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ serde_json = "1.0"
serde = "1.0"
libc = "0.2"
jsonpath_lib = { git="https://github.com/RedisJSON/jsonpath.git", branch="public-parser" }
redis-module = { version="0.14", features = ["experimental-api"]}
redis-module = { version="0.15", features = ["experimental-api"]}

[features]
# Workaround to allow cfg(feature = "test") in redismodue-rs dependencies:
Expand Down
72 changes: 52 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[macro_use]
extern crate redis_module;

use redis_module::native_types::RedisType;
use redis_module::raw::RedisModuleTypeMethods;
use redis_module::{native_types::RedisType, NotifyEvent};
use redis_module::{raw as rawmod, NextArg};
use redis_module::{Context, RedisError, RedisResult, RedisValue, REDIS_OK};
use serde_json::{Number, Value};
Expand Down Expand Up @@ -41,6 +41,11 @@ static REDIS_JSON_TYPE: RedisType = RedisType::new(
aux_load: None,
aux_save: None,
aux_save_triggers: rawmod::Aux::Before as i32,

free_effort: None,
unlink: None,
copy: None,
defrag: None,
},
);

Expand Down Expand Up @@ -69,15 +74,16 @@ fn json_del(ctx: &Context, args: Vec<String>) -> RedisResult {
let key = args.next_string()?;
let path = backwards_compat_path(args.next_string()?);

let key = ctx.open_key_writable(&key);
let deleted = match key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)? {
let redis_key = ctx.open_key_writable(&key);
let deleted = match redis_key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)? {
Some(doc) => {
let res = if path == "$" {
key.delete()?;
redis_key.delete()?;
1
} else {
doc.delete_path(&path)?
};
ctx.notify_keyspace_event(NotifyEvent::MODULE, "json_del", key.as_str());
ctx.replicate_verbatim();
res
}
Expand Down Expand Up @@ -116,6 +122,7 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
match (current, set_option) {
(Some(ref mut doc), ref op) => {
if doc.set_value(&value, &path, op, format)? {
ctx.notify_keyspace_event(NotifyEvent::MODULE, "json_set", key.as_str());
ctx.replicate_verbatim();
REDIS_OK
} else {
Expand All @@ -127,6 +134,7 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
let doc = RedisJSON::from_str(&value, format)?;
if path == "$" {
redis_key.set_value(&REDIS_JSON_TYPE, doc)?;
ctx.notify_keyspace_event(NotifyEvent::MODULE, "json_set", key.as_str());
ctx.replicate_verbatim();
REDIS_OK
} else {
Expand Down Expand Up @@ -256,24 +264,36 @@ fn json_type(ctx: &Context, args: Vec<String>) -> RedisResult {
/// JSON.NUMINCRBY <key> <path> <number>
///
fn json_num_incrby(ctx: &Context, args: Vec<String>) -> RedisResult {
json_num_op(ctx, args, |i1, i2| i1 + i2, |f1, f2| f1 + f2)
json_num_op(ctx, "json_incrby", args, |i1, i2| i1 + i2, |f1, f2| f1 + f2)
}

///
/// JSON.NUMMULTBY <key> <path> <number>
///
fn json_num_multby(ctx: &Context, args: Vec<String>) -> RedisResult {
json_num_op(ctx, args, |i1, i2| i1 * i2, |f1, f2| f1 * f2)
json_num_op(ctx, "json_multby", args, |i1, i2| i1 * i2, |f1, f2| f1 * f2)
}

///
/// JSON.NUMPOWBY <key> <path> <number>
///
fn json_num_powby(ctx: &Context, args: Vec<String>) -> RedisResult {
json_num_op(ctx, args, |i1, i2| i1.pow(i2 as u32), |f1, f2| f1.powf(f2))
json_num_op(
ctx,
"json_numpowby",
args,
|i1, i2| i1.pow(i2 as u32),
|f1, f2| f1.powf(f2),
)
}

fn json_num_op<I, F>(ctx: &Context, args: Vec<String>, op_i64: I, op_f64: F) -> RedisResult
fn json_num_op<I, F>(
ctx: &Context,
cmd: &str,
args: Vec<String>,
op_i64: I,
op_f64: F,
) -> RedisResult
where
I: Fn(i64, i64) -> i64,
F: Fn(f64, f64) -> f64,
Expand All @@ -284,15 +304,17 @@ where
let path = backwards_compat_path(args.next_string()?);
let number = args.next_string()?;

let key = ctx.open_key_writable(&key);
let redis_key = ctx.open_key_writable(&key);

key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
redis_key
.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
.ok_or_else(RedisError::nonexistent_key)
.and_then(|doc| {
doc.value_op(&path, |value| {
do_json_num_op(&number, value, &op_i64, &op_f64)
})
.map(|v| {
ctx.notify_keyspace_event(NotifyEvent::MODULE, cmd, key.as_str());
ctx.replicate_verbatim();
v.to_string().into()
})
Expand Down Expand Up @@ -360,13 +382,15 @@ fn json_str_append(ctx: &Context, args: Vec<String>) -> RedisResult {
json = path_or_json;
}

let key = ctx.open_key_writable(&key);
let redis_key = ctx.open_key_writable(&key);

key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
redis_key
.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
.ok_or_else(RedisError::nonexistent_key)
.and_then(|doc| {
doc.value_op(&path, |value| do_json_str_append(&json, value))
.map(|v| {
ctx.notify_keyspace_event(NotifyEvent::MODULE, "json_strappend", key.as_str());
ctx.replicate_verbatim();
v.as_str().map_or(usize::MAX, |v| v.len()).into()
})
Expand Down Expand Up @@ -401,13 +425,15 @@ fn json_arr_append(ctx: &Context, args: Vec<String>) -> RedisResult {
// We require at least one JSON item to append
args.peek().ok_or(RedisError::WrongArity)?;

let key = ctx.open_key_writable(&key);
let redis_key = ctx.open_key_writable(&key);

key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
redis_key
.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
.ok_or_else(RedisError::nonexistent_key)
.and_then(|doc| {
doc.value_op(&path, |value| do_json_arr_append(args.clone(), value))
.map(|v| {
ctx.notify_keyspace_event(NotifyEvent::MODULE, "json_arrappend", key.as_str());
ctx.replicate_verbatim();
v.as_array().map_or(usize::MAX, |v| v.len()).into()
})
Expand Down Expand Up @@ -470,15 +496,17 @@ fn json_arr_insert(ctx: &Context, args: Vec<String>) -> RedisResult {
// We require at least one JSON item to append
args.peek().ok_or(RedisError::WrongArity)?;

let key = ctx.open_key_writable(&key);
let redis_key = ctx.open_key_writable(&key);

key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
redis_key
.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
.ok_or_else(RedisError::nonexistent_key)
.and_then(|doc| {
doc.value_op(&path, |value| {
do_json_arr_insert(args.clone(), index, value)
})
.map(|v| {
ctx.notify_keyspace_event(NotifyEvent::MODULE, "json_arrinsert", key.as_str());
ctx.replicate_verbatim();
v.as_array().map_or(usize::MAX, |v| v.len()).into()
})
Expand Down Expand Up @@ -537,14 +565,16 @@ fn json_arr_pop(ctx: &Context, args: Vec<String>) -> RedisResult {
})
.unwrap_or(("$".to_string(), i64::MAX));

let key = ctx.open_key_writable(&key);
let redis_key = ctx.open_key_writable(&key);
let mut res = Value::Null;

key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
redis_key
.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
.ok_or_else(RedisError::nonexistent_key)
.and_then(|doc| {
doc.value_op(&path, |value| do_json_arr_pop(index, &mut res, value))
.map(|v| {
ctx.notify_keyspace_event(NotifyEvent::MODULE, "json_arrpop", key.as_str());
ctx.replicate_verbatim();
v
})
Expand Down Expand Up @@ -587,13 +617,15 @@ fn json_arr_trim(ctx: &Context, args: Vec<String>) -> RedisResult {
let start = args.next_i64()?;
let stop = args.next_i64()?;

let key = ctx.open_key_writable(&key);
let redis_key = ctx.open_key_writable(&key);

key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
redis_key
.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
.ok_or_else(RedisError::nonexistent_key)
.and_then(|doc| {
doc.value_op(&path, |value| do_json_arr_trim(start, stop, &value))
.map(|v| {
ctx.notify_keyspace_event(NotifyEvent::MODULE, "json_arrtrim", key.as_str());
ctx.replicate_verbatim();
v.as_array().map_or(usize::MAX, |v| v.len()).into()
})
Expand Down