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
22 changes: 11 additions & 11 deletions src/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn json_api_open_key_internal<M: Manager>(
let ctx = Context::new(ctx);
if let Ok(h) = manager.open_key_read(&ctx, key) {
if let Ok(v) = h.get_value() {
if let Some(_) = v {
if v.is_some() {
return Box::into_raw(Box::new(h));
}
}
Expand All @@ -76,7 +76,7 @@ fn json_api_close_key_internal<M: Manager>(_: M, json: *mut c_void) {
}

#[no_mangle]
pub extern "C" fn JSONAPI_openKey<'a>(
pub extern "C" fn JSONAPI_openKey(
ctx: *mut rawmod::RedisModuleCtx,
key_str: *mut rawmod::RedisModuleString,
) -> *mut c_void {
Expand All @@ -90,7 +90,7 @@ pub extern "C" fn JSONAPI_openKey<'a>(
}

#[no_mangle]
pub extern "C" fn JSONAPI_openKeyFromStr<'a>(
pub extern "C" fn JSONAPI_openKeyFromStr(
ctx: *mut rawmod::RedisModuleCtx,
path: *const c_char,
) -> *mut c_void {
Expand All @@ -112,7 +112,7 @@ fn json_api_get_at<M: Manager>(
match json.get_type() {
SelectValueType::Array => match json.get_index(index) {
Some(v) => {
if jtype != null_mut() {
if !jtype.is_null() {
unsafe { *jtype = json_api_get_type_internal(v) as c_int };
}
v as *const M::V as *const c_void
Expand Down Expand Up @@ -198,7 +198,7 @@ pub extern "C" fn JSONAPI_getStringFromKey(
) -> c_int {
let mut t: c_int = 0;
let v = JSONAPI_get(key, path, &mut t);
if v != null() && t == JSONType::String as c_int {
if !v.is_null() && t == JSONType::String as c_int {
JSONAPI_getString(v, str, len)
} else {
Status::Err as c_int
Expand Down Expand Up @@ -234,7 +234,7 @@ pub extern "C" fn JSONAPI_getJSONFromKey(
) -> c_int {
let mut t: c_int = 0;
let v = JSONAPI_get(key, path, &mut t);
if v != null() {
if !v.is_null() {
JSONAPI_getJSON(v, ctx, str)
} else {
Status::Err as c_int
Expand Down Expand Up @@ -273,7 +273,7 @@ pub extern "C" fn JSONAPI_getIntFromKey(
) -> c_int {
let mut t: c_int = 0;
let v = JSONAPI_get(key, path, &mut t);
if v != null() && t == JSONType::Int as c_int {
if !v.is_null() && t == JSONType::Int as c_int {
JSONAPI_getInt(v, val)
} else {
Status::Err as c_int
Expand Down Expand Up @@ -304,7 +304,7 @@ pub extern "C" fn JSONAPI_getDoubleFromKey(
) -> c_int {
let mut t: c_int = 0;
let v = JSONAPI_get(key, path, &mut t);
if v != null() && t == JSONType::Double as c_int {
if !v.is_null() && t == JSONType::Double as c_int {
JSONAPI_getDouble(v, val)
} else {
Status::Err as c_int
Expand Down Expand Up @@ -335,7 +335,7 @@ pub extern "C" fn JSONAPI_getBooleanFromKey(
) -> c_int {
let mut t: c_int = 0;
let v = JSONAPI_get(key, path, &mut t);
if v != null() && t == JSONType::Bool as c_int {
if !v.is_null() && t == JSONType::Bool as c_int {
JSONAPI_getBoolean(v, val)
} else {
Status::Err as c_int
Expand Down Expand Up @@ -403,13 +403,13 @@ pub fn json_api_get<M: Manager>(
let mut selector = Selector::new();
selector.value(v);
let path = unsafe { CStr::from_ptr(path).to_str().unwrap() };
if let Err(_) = selector.str_path(path) {
if selector.str_path(path).is_err() {
return null();
}
match selector.select() {
Ok(s) => match s.first() {
Some(v) => {
if jtype != null_mut() {
if jtype.is_null() {
unsafe { *jtype = json_api_get_type_internal(*v) as c_int };
}
*v as *const M::V as *const c_void
Expand Down
51 changes: 24 additions & 27 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,11 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
let temp_doc = paths.drain(..).fold(HashMap::new(), |mut acc, path| {
let mut selector = Selector::new();
selector.value(self.val);
if let Err(_) = selector.str_path(path.get_path()) {
if selector.str_path(path.get_path()).is_err() {
return acc;
}
let value = match selector.select() {
Ok(s) => match s.first() {
Some(v) => Some(*v),
None => None,
},
Ok(s) => s.first().map(|v| *v),
Err(_) => None,
};
acc.insert(path.take_original(), value);
Expand Down Expand Up @@ -234,7 +231,7 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
// Adding to the root
Ok(vec![UpdateInfo::AUI(AddUpdateInfo {
path: Vec::new(),
key: key.to_string(),
key,
})])
} else {
// Adding somewhere in existing object, use jsonpath_lib::replace_with
Expand Down Expand Up @@ -282,7 +279,7 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
.str_path(path)?
.value(self.val)
.select_with_paths(|_| true)?;
if res.len() > 0 {
if !res.is_empty() {
return Ok(res
.drain(..)
.map(|v| UpdateInfo::SUI(SetUpdateInfo { path: v }))
Expand Down Expand Up @@ -527,7 +524,7 @@ pub fn command_json_set<M: Manager>(manager: M, ctx: &Context, args: Vec<String>
}
} else {
let mut update_info = KeyValue::new(*doc).find_paths(&path, op)?;
if update_info.len() > 0 {
if !update_info.is_empty() {
let mut res = false;
if update_info.len() == 1 {
res = match update_info.pop().unwrap() {
Expand Down Expand Up @@ -589,7 +586,7 @@ pub fn command_json_del<M: Manager>(manager: M, ctx: &Context, args: Vec<String>
let key = args.next_string()?;
let path = args
.next_string()
.map_or_else(|_| JSON_ROOT_PATH.to_string(), |v| backwards_compat_path(v));
.map_or_else(|_| JSON_ROOT_PATH.to_string(), backwards_compat_path);

let mut redis_key = manager.open_key_write(ctx, &key)?;
let deleted = match redis_key.get_value()? {
Expand Down Expand Up @@ -661,9 +658,9 @@ pub fn command_json_type<M: Manager>(manager: M, ctx: &Context, args: Vec<String
}

enum NumOp {
INCR,
MULT,
POW,
Incr,
Mult,
Pow,
}

fn command_json_num_op<M>(
Expand All @@ -690,13 +687,13 @@ where
let paths = find_paths(&path, root, |v| {
v.get_type() == SelectValueType::Double || v.get_type() == SelectValueType::Long
})?;
if paths.len() > 0 {
if !paths.is_empty() {
let mut res = None;
for p in paths {
res = Some(match op {
NumOp::INCR => redis_key.incr_by(p, &number)?,
NumOp::MULT => redis_key.mult_by(p, &number)?,
NumOp::POW => redis_key.pow_by(p, &number)?,
NumOp::Incr => redis_key.incr_by(p, &number)?,
NumOp::Mult => redis_key.mult_by(p, &number)?,
NumOp::Pow => redis_key.pow_by(p, &number)?,
});
}
redis_key.apply_changes(ctx, cmd)?;
Expand All @@ -714,23 +711,23 @@ pub fn command_json_num_incrby<M: Manager>(
ctx: &Context,
args: Vec<String>,
) -> RedisResult {
command_json_num_op(manager, ctx, args, "json.numincrby", NumOp::INCR)
command_json_num_op(manager, ctx, args, "json.numincrby", NumOp::Incr)
}

pub fn command_json_num_multby<M: Manager>(
manager: M,
ctx: &Context,
args: Vec<String>,
) -> RedisResult {
command_json_num_op(manager, ctx, args, "json.nummultby", NumOp::MULT)
command_json_num_op(manager, ctx, args, "json.nummultby", NumOp::Mult)
}

pub fn command_json_num_powby<M: Manager>(
manager: M,
ctx: &Context,
args: Vec<String>,
) -> RedisResult {
command_json_num_op(manager, ctx, args, "json.numpowby", NumOp::POW)
command_json_num_op(manager, ctx, args, "json.numpowby", NumOp::Pow)
}

pub fn command_json_bool_toggle<M: Manager>(
Expand All @@ -747,7 +744,7 @@ pub fn command_json_bool_toggle<M: Manager>(
.get_value()?
.ok_or_else(RedisError::nonexistent_key)?;
let paths = find_paths(&path, root, |v| v.get_type() == SelectValueType::Bool)?;
if paths.len() > 0 {
if !paths.is_empty() {
let mut res = None;
for p in paths {
res = Some(redis_key.bool_toggle(p)?);
Expand Down Expand Up @@ -791,7 +788,7 @@ pub fn command_json_str_append<M: Manager>(
.ok_or_else(RedisError::nonexistent_key)?;

let paths = find_paths(&path, root, |v| v.get_type() == SelectValueType::String)?;
if paths.len() > 0 {
if !paths.is_empty() {
let mut res = None;
for p in paths {
res = Some(redis_key.str_append(p, json.clone())?);
Expand Down Expand Up @@ -846,7 +843,7 @@ pub fn command_json_arr_append<M: Manager>(
.ok_or_else(RedisError::nonexistent_key)?;

let mut paths = find_paths(&path, root, |v| v.get_type() == SelectValueType::Array)?;
if paths.len() == 0 {
if paths.is_empty() {
Err(RedisError::String(format!(
"Path '{}' does not exist",
path
Expand Down Expand Up @@ -913,7 +910,7 @@ pub fn command_json_arr_insert<M: Manager>(
.ok_or_else(RedisError::nonexistent_key)?;

let paths = find_paths(&path, root, |v| v.get_type() == SelectValueType::Array)?;
if paths.len() > 0 {
if !paths.is_empty() {
let mut res = None;
for p in paths {
res = Some(redis_key.arr_insert(p, &args, index)?);
Expand Down Expand Up @@ -971,7 +968,7 @@ pub fn command_json_arr_pop<M: Manager>(
.ok_or_else(RedisError::nonexistent_key)?;

let paths = find_paths(&path, root, |v| v.get_type() == SelectValueType::Array)?;
if paths.len() > 0 {
if !paths.is_empty() {
let mut res = None;
for p in paths {
res = Some(redis_key.arr_pop(p, index)?);
Expand Down Expand Up @@ -1010,7 +1007,7 @@ pub fn command_json_arr_trim<M: Manager>(
.ok_or_else(RedisError::nonexistent_key)?;

let paths = find_paths(&path, root, |v| v.get_type() == SelectValueType::Array)?;
if paths.len() > 0 {
if !paths.is_empty() {
let mut res = None;
for p in paths {
res = Some(redis_key.arr_trim(p, start, stop)?);
Expand Down Expand Up @@ -1086,7 +1083,7 @@ pub fn command_json_clear<M: Manager>(manager: M, ctx: &Context, args: Vec<Strin
.ok_or_else(RedisError::nonexistent_key)?;

let paths = find_paths(path, root, |_v| true)?;
if paths.len() > 0 {
if !paths.is_empty() {
let mut res = None;
for p in paths {
res = Some(redis_key.clear(p)?);
Expand Down Expand Up @@ -1134,7 +1131,7 @@ pub fn command_json_resp<M: Manager>(manager: M, ctx: &Context, args: Vec<String
let key = args.next_string()?;
let path = args
.next_string()
.map_or_else(|_| JSON_ROOT_PATH.to_string(), |v| backwards_compat_path(v));
.map_or_else(|_| JSON_ROOT_PATH.to_string(), backwards_compat_path);

let key = manager.open_key_read(ctx, &key)?;
match key.get_value()? {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,13 @@ macro_rules! redis_json_module_create {(
}

#[cfg(not(feature = "as-library"))]
fn init(ctx: &Context, _args: &Vec<String>) -> Status {
fn init(ctx: &Context, _args: &[String]) -> Status {
export_shared_api(ctx);
Status::Ok
}

#[cfg(not(feature = "as-library"))]
fn pre_command(_ctx: &Context, _args: &Vec<String>) {}
fn pre_command(_ctx: &Context, _args: &[String]) {}

#[cfg(not(feature = "as-library"))]
redis_json_module_create! {
Expand Down
4 changes: 2 additions & 2 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl KeyHolderWrite {
where
F: FnMut(Value) -> Result<Option<Value>, Error>,
{
if paths.len() == 0 {
if paths.is_empty() {
// updating the root require special treatment
let root = self.get_value().unwrap().unwrap();
let res = (op_fun)(root.take())?;
Expand Down Expand Up @@ -489,7 +489,7 @@ impl Manager for RedisJsonKeyManager {

fn open_key_read(&self, ctx: &Context, key: &str) -> Result<KeyHolderRead, RedisError> {
let key = ctx.open_key(key);
Ok(KeyHolderRead { key: key })
Ok(KeyHolderRead { key })
}

fn open_key_write(&self, ctx: &Context, key: &str) -> Result<KeyHolderWrite, RedisError> {
Expand Down