Skip to content

Commit

Permalink
feat: test collect function
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyMatt committed Jun 6, 2024
1 parent 4f2ec1c commit ad7860c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/graph/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,13 @@ mod tests {

#[test]
fn test_query() {
let mut graph = open_test_graph("test_query_and_lazy_query");
let mut graph = open_test_graph("test_query");
let res = graph.inner.query("MATCH (a:actor) WITH a MATCH (b:actor) WHERE a.age = b.age AND a <> b RETURN a, collect(b) LIMIT 10").execute().expect("Could not execute query");
assert_eq!(res.data.collect::<Vec<_>>().len(), 10);

let mut res = graph.inner.query("MATCH (a:actor) WITH a MATCH (b:actor) WHERE a.age = b.age AND a <> b RETURN a, collect(b) LIMIT 10").execute().expect("Could not execute query");
assert!(res.data.next().is_some());
assert_eq!(res.data.collect::<Vec<_>>().len(), 9);
}

#[test]
Expand Down
20 changes: 4 additions & 16 deletions src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,15 @@ impl LazyResultSet {
))),
}
}

/// Consumes the entire iterator and returns a specified collection of type T.
/// Requires that type T implements [`FromIterator<FalkorValue>`] and [`Default`] (which most containers do).
/// This has a performance boost, but requires parsing the entire iterator, so should be avoided unless that is the specific purpose.
pub fn collect<T: FromIterator<FalkorValue> + Default>(self) -> T {
if self.data.is_empty() {
return T::default();
}
let mut graph_schema = self.graph_schema.lock();
self.data
.into_iter()
.filter_map(|current_result| {
parse_type(6, current_result, graph_schema.deref_mut()).ok()
})
.collect::<T>()
}
}

impl Iterator for LazyResultSet {
type Item = Vec<FalkorValue>;

// Initially I created a fast_collect() function which collected using only a single lock at the start of the function
// But it seems the compiler is smarter somehow, and optimizes this by a lot
// In small result sets the single lock won by ~50micros, but the more results there are the faster the regular collect worked
// Until it won in a landslide, so honestly I'm just letting the regular collect() function do its thing
fn next(&mut self) -> Option<Self::Item> {
self.data.pop_front().and_then(|current_result| {
parse_type(6, current_result, self.graph_schema.lock().deref_mut())
Expand Down

0 comments on commit ad7860c

Please sign in to comment.