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
16 changes: 16 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ serde = "1.0"
libc = "0.2"
jsonpath_lib = { git = "https://github.com/RedisJSON/jsonpath.git", branch = "generic_json_path" }
redis-module = { version="0.24", features = ["experimental-api"]}
itertools = "0.10.1"
[features]
# Workaround to allow cfg(feature = "test") in redismodue-rs dependencies:
# https://github.com/RedisLabsModules/redismodule-rs/pull/68
Expand Down
44 changes: 43 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::redisjson::{normalize_arr_indices, Format, Path};
use jsonpath_lib::select::select_value::{SelectValue, SelectValueType};
use redis_module::{Context, RedisValue};
use redis_module::{NextArg, RedisError, RedisResult, RedisString, REDIS_OK};
use std::cmp::Ordering;

use jsonpath_lib::select::Selector;

Expand All @@ -15,8 +16,11 @@ use crate::redisjson::SetOptions;

use serde_json::{Number, Value};

use itertools::FoldWhile::{Continue, Done};
use itertools::Itertools;
use serde::{Serialize, Serializer};
use std::collections::HashMap;

const JSON_ROOT_PATH: &str = "$";
const JSON_ROOT_PATH_LEGACY: &str = ".";
const CMD_ARG_NOESCAPE: &str = "NOESCAPE";
Expand Down Expand Up @@ -742,6 +746,43 @@ where
.collect::<Vec<Value>>()
}

/// Sort the paths so higher indices precede lower indices on the same on the same array
/// And objects with higher hierarchy (closer to the top-level) preceded objects with deeper hierarchy
fn prepare_paths_for_deletion(paths: &mut Vec<Vec<String>>) {
paths.sort_by(|v1, v2| match (v1.len(), v2.len()) {
(l1, l2) if l1 < l2 => Ordering::Less, // Shorter paths before longer paths
(l1, l2) if l1 > l2 => Ordering::Greater, // Shorter paths before longer paths
_ => v1
.iter()
.zip(v2.iter())
.fold_while(Ordering::Equal, |_acc, (p1, p2)| {
let i1 = p1.parse::<usize>();
let i2 = p2.parse::<usize>();
match (i1, i2) {
(Err(_), Err(_)) => match p1.cmp(p2) {
// String compare
Ordering::Less => Done(Ordering::Less),
Ordering::Equal => Continue(Ordering::Equal),
Ordering::Greater => Done(Ordering::Greater),
},
(Ok(_), Err(_)) => Done(Ordering::Greater), //String before Numeric
(Err(_), Ok(_)) => Done(Ordering::Less), //String before Numeric
(Ok(i1), Ok(i2)) => {
// Numeric compare - higher indices before lower ones
if i1 < i2 {
Done(Ordering::Greater)
} else if i2 < i1 {
Done(Ordering::Less)
} else {
Continue(Ordering::Equal)
}
}
}
})
.into_inner(),
});
}

pub fn command_json_del<M: Manager>(
manager: M,
ctx: &Context,
Expand All @@ -762,7 +803,8 @@ pub fn command_json_del<M: Manager>(
redis_key.delete()?;
1
} else {
let paths = find_paths(path.get_path(), doc, |_| true)?;
let mut paths = find_paths(path.get_path(), doc, |_| true)?;
prepare_paths_for_deletion(&mut paths);
let mut changed = 0;
for p in paths {
if redis_key.delete_path(p)? {
Expand Down
16 changes: 14 additions & 2 deletions tests/pytest/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,23 @@ def testDelCommand(env):

r.assertOk(r.execute_command('JSON.SET', 'doc2', '$', '[1, 2, 3]'))
res = r.execute_command('JSON.DEL', 'doc2', '$[*]')
r.assertGreater(res, 0)
r.assertEqual(res, 3)

r.assertOk(r.execute_command('JSON.SET', 'doc2', '$', '[1, 2, 3]'))
res = r.execute_command('JSON.DEL', 'doc2', '$[2,1,0]')
r.assertGreater(res, 0)
r.assertEqual(res, 3)

r.assertOk(r.execute_command('JSON.SET', 'doc2', '$', '{"b": [1,2,3], "a": {"b": [1, 2, 3], "c": [1, 2, 3]}, "x": {"b": [1, 2, 3], "c": [1, 2, 3]}}'))
res = r.execute_command('JSON.DEL', 'doc2', '$..x.b[*]')
r.assertEqual(res, 3)
res = r.execute_command('JSON.GET', 'doc2', '$')
r.assertEqual(json.loads(res), [{"b": [1, 2, 3], "a": {"b": [1, 2, 3], "c": [1, 2, 3]}, "x": {"b": [], "c": [1, 2, 3]}}])

r.assertOk(r.execute_command('JSON.SET', 'doc2', '$', '{"b": [1,2,3], "a": {"b": [1, 2, 3], "c": [1, 2, 3]}, "x": {"b": [1, 2, 3], "c": [1, 2, 3]}}'))
res = r.execute_command('JSON.DEL', 'doc2', '$..x.b[1,0,2]')
r.assertEqual(res, 3)
res = r.execute_command('JSON.GET', 'doc2', '$')
r.assertEqual(json.loads(res), [{"b": [1, 2, 3], "a": {"b": [1, 2, 3], "c": [1, 2, 3]}, "x": {"b": [], "c": [1, 2, 3]}}])

# Test deleting a null value
r.assertOk(r.execute_command('JSON.SET', 'doc2', '$', '[ true, { "answer": 42}, null ]'))
Expand Down