Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
55e5ea9
placeholder
ephraimfeldblum Aug 8, 2024
8dd5acc
work
ephraimfeldblum Aug 8, 2024
3b9eb5a
refactor
ephraimfeldblum Aug 11, 2024
1eb8567
into_both
ephraimfeldblum Aug 12, 2024
ca90719
some work on `do_op`
ephraimfeldblum Aug 13, 2024
2e108b9
delete useless test output
ephraimfeldblum Aug 13, 2024
d9ec32c
and arr_pop too
ephraimfeldblum Aug 13, 2024
7e9456e
do_op cannot reach the case where op_fun returns Ok(None) as none of …
ephraimfeldblum Aug 13, 2024
be1fa94
manage case where path is invalid
ephraimfeldblum Aug 13, 2024
e05eca6
work
ephraimfeldblum Aug 15, 2024
6b89dbe
ridding unwraps
ephraimfeldblum Aug 18, 2024
7637971
mset is concerning
ephraimfeldblum Aug 19, 2024
a949ea8
further reducing complexity
ephraimfeldblum Aug 19, 2024
3e576d4
work
ephraimfeldblum Aug 20, 2024
38b2211
simplify error handling
ephraimfeldblum Aug 20, 2024
4e1d180
.
ephraimfeldblum Aug 20, 2024
59af235
code dupe
ephraimfeldblum Aug 20, 2024
21779ba
hoist loop invariant
ephraimfeldblum Aug 20, 2024
3889230
ownership
ephraimfeldblum Aug 20, 2024
24951dc
and_then(Ok()) should be map()
ephraimfeldblum Aug 20, 2024
6d50274
ownership
ephraimfeldblum Aug 21, 2024
3343446
.
ephraimfeldblum Aug 21, 2024
4ecbe57
dedup
ephraimfeldblum Aug 21, 2024
e63af1e
update and replace are the same thing
ephraimfeldblum Aug 21, 2024
27822b5
removing dead code
ephraimfeldblum Aug 22, 2024
eb70f71
readability
ephraimfeldblum Aug 22, 2024
bf5cb8b
strong typing is good, why use enums where they're not needed
ephraimfeldblum Aug 22, 2024
3018e35
reduce number of wheels needing reinvention
ephraimfeldblum Aug 25, 2024
dd1e325
PathTracker is driving me insane
ephraimfeldblum Aug 25, 2024
a78b5b6
.
ephraimfeldblum Aug 26, 2024
db1ac87
remove test from refactoring
ephraimfeldblum Aug 26, 2024
93a4510
removing dead loc to make codecov happy
ephraimfeldblum Aug 26, 2024
b2a1505
fmt
ephraimfeldblum Aug 26, 2024
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
Prev Previous commit
Next Next commit
.
  • Loading branch information
ephraimfeldblum committed Aug 21, 2024
commit 33434461c2157aa5c28058f1a176bb6fcb6a62b5
10 changes: 6 additions & 4 deletions redis_json/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,11 @@ pub fn json_mset<M: Manager>(manager: M, ctx: &Context, args: Vec<RedisString>)
})?;

// Parse the input and validate it's valid JSON
let value = value
.try_as_str()
.and_then(|value| manager.from_str(value, Format::JSON, true).into_both())?;
let value = value.try_as_str().and_then(|value| {
manager
.from_str(value, Format::JSON, true)
.map_err(Into::into)
})?;

Ok((redis_key, update_info, value))
})
Expand Down Expand Up @@ -771,7 +773,7 @@ fn json_num_op_legacy<M: Manager>(
NumOp::Mult => redis_key.mult_by(p, number),
NumOp::Pow => redis_key.pow_by(p, number),
}
.into_both()
.map(Into::into)
})
.transpose()
.unwrap_or_else(|| {
Expand Down
3 changes: 1 addition & 2 deletions redis_json/src/ivalue_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,7 @@ impl<'a> WriteHolder<IValue, IValue> for IValueKeyHolderWrite<'a> {
if !(0..=len).contains(&index) {
return Err("ERR index out of bounds".into());
}
arr.reserve(args.len());
args.iter().for_each(|a| arr.push(a.clone()));
arr.extend(args.iter().map(IValue::clone));
arr.as_mut_slice()[index as _..].rotate_right(args.len());
Ok(arr.len())
})
Expand Down
116 changes: 59 additions & 57 deletions redis_json/src/key_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
err_msg_json_expected, err_msg_json_path_doesnt_exist_with_param, AddUpdateInfo,
SetUpdateInfo, UpdateInfo,
},
redisjson::{normalize_arr_indices, Path, ReplyFormat, ResultInto, SetOptions},
redisjson::{normalize_arr_indices, Path, ReplyFormat, SetOptions},
};

pub struct KeyValue<'a, V: SelectValue> {
Expand All @@ -31,25 +31,28 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
}

pub fn get_first<'b>(&'a self, path: &'b str) -> Result<&'a V, Error> {
self.get_values(path)?.first().copied().ok_or(
err_msg_json_path_doesnt_exist_with_param(path)
.as_str()
.into(),
)
self.get_values(path).and_then(|v| {
v.first().copied().ok_or_else(|| {
err_msg_json_path_doesnt_exist_with_param(path)
.as_str()
.into()
})
})
}

pub fn resp_serialize(&self, path: Path) -> RedisResult {
let res = if path.is_legacy() {
let v = self.get_first(path.get_path())?;
Self::resp_serialize_inner(v)
if path.is_legacy() {
self.get_first(path.get_path())
.map(Self::resp_serialize_inner)
} else {
self.get_values(path.get_path())?
.into_iter()
.map(|v| Self::resp_serialize_inner(v))
.collect_vec()
.into()
};
Ok(res)
self.get_values(path.get_path()).map(|v| {
v.into_iter()
.map(Self::resp_serialize_inner)
.collect_vec()
.into()
})
}
.map_err(Into::into)
}

fn resp_serialize_inner(v: &V) -> RedisValue {
Expand All @@ -69,7 +72,7 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {

SelectValueType::Array => {
let res = std::iter::once(RedisValue::SimpleStringStatic("["))
.chain(v.values().unwrap().map(|v| Self::resp_serialize_inner(v)))
.chain(v.values().unwrap().map(Self::resp_serialize_inner))
.collect();
RedisValue::Array(res)
}
Expand All @@ -89,9 +92,9 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
}

pub fn get_values<'b>(&'a self, path: &'b str) -> Result<Vec<&'a V>, Error> {
let query = compile(path)?;
let results = calc_once(query, self.val);
Ok(results)
compile(path)
.map(|query| calc_once(query, self.val))
.map_err(Into::into)
}

pub fn serialize_object<O: Serialize>(o: &O, format: ReplyFormatOptions) -> String {
Expand Down Expand Up @@ -180,12 +183,12 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
is_legacy: bool,
) -> Result<RedisValue, Error> {
if is_legacy {
self.to_string_single(path, format).into_both()
self.to_string_single(path, format).map(Into::into)
} else if format.is_resp3_reply() {
let values = self.get_values(path)?;
Ok(Self::values_to_resp3(values, format))
self.get_values(path)
.map(|values| Self::values_to_resp3(values, format))
} else {
self.to_string_multi(path, format).into_both()
self.to_string_multi(path, format).map(Into::into)
}
}

Expand Down Expand Up @@ -330,18 +333,17 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
path: &str,
format: ReplyFormatOptions,
) -> Result<String, Error> {
let result = self.get_first(path)?;
Ok(Self::serialize_object(&result, format))
self.get_first(path)
.map(|first| Self::serialize_object(first, format))
}

pub fn to_string_multi(&self, path: &str, format: ReplyFormatOptions) -> Result<String, Error> {
let results = self.get_values(path)?;
Ok(Self::serialize_object(&results, format))
self.get_values(path)
.map(|val| Self::serialize_object(&val, format))
}

pub fn get_type(&self, path: &str) -> Result<String, Error> {
let s = Self::value_name(self.get_first(path)?);
Ok(s.to_string())
self.get_first(path).map(Self::value_name).map(Into::into)
}

pub fn value_name(value: &V) -> &str {
Expand Down Expand Up @@ -377,17 +379,17 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
}

pub fn obj_len(&self, path: &str) -> Result<ObjectLen, Error> {
match self.get_first(path) {
Ok(first) => match first.get_type() {
SelectValueType::Object => Ok(ObjectLen::Len(first.len().unwrap())),
_ => Err(
err_msg_json_expected("object", self.get_type(path).unwrap().as_str())
.as_str()
.into(),
),
},
_ => Ok(ObjectLen::NoneExisting),
}
self.get_first(path)
.map_or(Ok(ObjectLen::NoneExisting), |first| {
match first.get_type() {
SelectValueType::Object => Ok(ObjectLen::Len(first.len().unwrap())),
_ => Err(err_msg_json_expected(
"object",
self.get_type(path).unwrap().as_str(),
)
.into()),
}
})
}

pub fn is_equal<T1: SelectValue, T2: SelectValue>(a: &T1, b: &T2) -> bool {
Expand Down Expand Up @@ -424,14 +426,13 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
start: i64,
end: i64,
) -> Result<RedisValue, Error> {
let res = self
.get_values(path)?
.into_iter()
.map(|value| {
RedisValue::from(Self::arr_first_index_single(value, &json_value, start, end))
})
.collect_vec();
Ok(res.into())
self.get_values(path).map(|v| {
v.into_iter()
.map(|value| Self::arr_first_index_single(value, &json_value, start, end))
.map(RedisValue::from)
.collect_vec()
.into()
})
}

pub fn arr_index_legacy(
Expand All @@ -441,14 +442,15 @@ impl<'a, V: SelectValue + 'a> KeyValue<'a, V> {
start: i64,
end: i64,
) -> Result<RedisValue, Error> {
let arr = self.get_first(path)?;
match Self::arr_first_index_single(arr, &json_value, start, end) {
FoundIndex::NotArray => Err(Error::from(err_msg_json_expected(
"array",
self.get_type(path).unwrap().as_str(),
))),
i => Ok(i.into()),
}
self.get_first(path).and_then(|arr| {
match Self::arr_first_index_single(arr, &json_value, start, end) {
FoundIndex::NotArray => Err(Error::from(err_msg_json_expected(
"array",
self.get_type(path).unwrap().as_str(),
))),
i => Ok(i.into()),
}
})
}

/// Returns first array index of `v` in `arr`, or `NotFound` if not found in `arr`, or `NotArray` if `arr` is not an array
Expand Down