-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sorted_set_fetch_by_index (#148)
* feat: add sorted_set_fetch_by_index Adds a function to the simple cache client to fetch elements of a sorted set selecting them by index (rank).
- Loading branch information
Showing
3 changed files
with
236 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,62 @@ | ||
use momento_protos::cache_client::SortedSetElement; | ||
use crate::sorted_set::SortedSetElement; | ||
use crate::MomentoError; | ||
use core::convert::TryFrom; | ||
|
||
#[derive(Debug)] | ||
#[non_exhaustive] | ||
pub struct MomentoSortedSetFetchResponse { | ||
pub value: Option<Vec<SortedSetElement>>, | ||
} | ||
|
||
pub enum SortedSetFetch { | ||
Hit { elements: Vec<SortedSetElement> }, | ||
Miss, | ||
} | ||
|
||
impl TryFrom<SortedSetFetch> for Vec<(Vec<u8>, f64)> { | ||
type Error = MomentoError; | ||
|
||
fn try_from(value: SortedSetFetch) -> Result<Self, Self::Error> { | ||
match value { | ||
SortedSetFetch::Hit { mut elements } => { | ||
let result: Vec<(Vec<u8>, f64)> = | ||
elements.drain(..).map(|e| (e.value, e.score)).collect(); | ||
Ok(result) | ||
} | ||
SortedSetFetch::Miss => Err(MomentoError::Miss { | ||
description: std::borrow::Cow::Borrowed("sorted set was not found"), | ||
}), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<SortedSetFetch> for Vec<(String, f64)> { | ||
type Error = MomentoError; | ||
|
||
fn try_from(value: SortedSetFetch) -> Result<Self, Self::Error> { | ||
match value { | ||
SortedSetFetch::Hit { mut elements } => { | ||
let mut result = Vec::with_capacity(elements.len()); | ||
for element in elements.drain(..) { | ||
match String::from_utf8(element.value) { | ||
Ok(value) => { | ||
result.push((value, element.score)); | ||
} | ||
Err(e) => { | ||
return Err::<Self, Self::Error>(MomentoError::TypeError { | ||
description: std::borrow::Cow::Borrowed( | ||
"element value was not a valid utf-8 string", | ||
), | ||
source: Box::new(e), | ||
}) | ||
} | ||
} | ||
} | ||
Ok(result) | ||
} | ||
SortedSetFetch::Miss => Err(MomentoError::Miss { | ||
description: std::borrow::Cow::Borrowed("sorted set was not found"), | ||
}), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters