Skip to content

Commit

Permalink
[db] Fix edge_count condition stopping the search incorrectly #636 (#637
Browse files Browse the repository at this point in the history
)

* Update search_where_test.rs

* Update search_where_test.rs

* Update query_condition.rs

* Update db.rs
  • Loading branch information
michaelvlach authored Jul 9, 2023
1 parent c5d0509 commit 4f51aea
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 35 deletions.
32 changes: 16 additions & 16 deletions src/agdb/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,29 +762,29 @@ impl Db {
condition: &QueryConditionData,
) -> Result<SearchControl, DbError> {
match condition {
QueryConditionData::Distance(value) => Ok(value.compare(distance)),
QueryConditionData::Distance(value) => Ok(value.compare_distance(distance)),
QueryConditionData::Edge => Ok(SearchControl::Continue(index.is_edge())),
QueryConditionData::EdgeCount(value) => {
Ok(if let Some(node) = self.graph.node(index) {
QueryConditionData::EdgeCount(value) => Ok(SearchControl::Continue(
if let Some(node) = self.graph.node(index) {
value.compare(node.edge_count())
} else {
SearchControl::Continue(false)
})
}
QueryConditionData::EdgeCountFrom(value) => {
Ok(if let Some(node) = self.graph.node(index) {
false
},
)),
QueryConditionData::EdgeCountFrom(value) => Ok(SearchControl::Continue(
if let Some(node) = self.graph.node(index) {
value.compare(node.edge_count_from())
} else {
SearchControl::Continue(false)
})
}
QueryConditionData::EdgeCountTo(value) => {
Ok(if let Some(node) = self.graph.node(index) {
false
},
)),
QueryConditionData::EdgeCountTo(value) => Ok(SearchControl::Continue(
if let Some(node) = self.graph.node(index) {
value.compare(node.edge_count_to())
} else {
SearchControl::Continue(false)
})
}
false
},
)),
QueryConditionData::Ids(values) => {
Ok(SearchControl::Continue(values.iter().any(|id| {
index.0
Expand Down
49 changes: 30 additions & 19 deletions src/agdb/query/query_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub enum Comparison {
}

impl CountComparison {
pub(crate) fn compare(&self, right: u64) -> SearchControl {
pub(crate) fn compare_distance(&self, right: u64) -> SearchControl {
match self {
CountComparison::Equal(left) => match right.cmp(left) {
std::cmp::Ordering::Less => SearchControl::Continue(false),
Expand Down Expand Up @@ -182,6 +182,17 @@ impl CountComparison {
},
}
}

pub(crate) fn compare(&self, left: u64) -> bool {
match self {
CountComparison::Equal(right) => left == *right,
CountComparison::GreaterThan(right) => left > *right,
CountComparison::GreaterThanOrEqual(right) => left >= *right,
CountComparison::LessThan(right) => left < *right,
CountComparison::LessThanOrEqual(right) => left <= *right,
CountComparison::NotEqual(right) => left != *right,
}
}
}

impl Comparison {
Expand Down Expand Up @@ -271,23 +282,23 @@ mod tests {
use SearchControl::Continue;
use SearchControl::Stop;

assert_eq!(Equal(2).compare(3), Stop(false));
assert_eq!(Equal(2).compare(2), Stop(true));
assert_eq!(Equal(2).compare(1), Continue(false));
assert_eq!(NotEqual(2).compare(3), Continue(true));
assert_eq!(NotEqual(2).compare(2), Continue(false));
assert_eq!(NotEqual(2).compare(1), Continue(true));
assert_eq!(GreaterThan(2).compare(3), Continue(true));
assert_eq!(GreaterThan(2).compare(2), Continue(false));
assert_eq!(GreaterThan(2).compare(1), Continue(false));
assert_eq!(GreaterThanOrEqual(2).compare(3), Continue(true));
assert_eq!(GreaterThanOrEqual(2).compare(2), Continue(true));
assert_eq!(GreaterThanOrEqual(2).compare(1), Continue(false));
assert_eq!(LessThan(2).compare(3), Stop(false));
assert_eq!(LessThan(2).compare(2), Stop(false));
assert_eq!(LessThan(2).compare(1), Continue(true));
assert_eq!(LessThanOrEqual(2).compare(3), Stop(false));
assert_eq!(LessThanOrEqual(2).compare(2), Stop(true));
assert_eq!(LessThanOrEqual(2).compare(1), Continue(true));
assert_eq!(Equal(2).compare_distance(3), Stop(false));
assert_eq!(Equal(2).compare_distance(2), Stop(true));
assert_eq!(Equal(2).compare_distance(1), Continue(false));
assert_eq!(NotEqual(2).compare_distance(3), Continue(true));
assert_eq!(NotEqual(2).compare_distance(2), Continue(false));
assert_eq!(NotEqual(2).compare_distance(1), Continue(true));
assert_eq!(GreaterThan(2).compare_distance(3), Continue(true));
assert_eq!(GreaterThan(2).compare_distance(2), Continue(false));
assert_eq!(GreaterThan(2).compare_distance(1), Continue(false));
assert_eq!(GreaterThanOrEqual(2).compare_distance(3), Continue(true));
assert_eq!(GreaterThanOrEqual(2).compare_distance(2), Continue(true));
assert_eq!(GreaterThanOrEqual(2).compare_distance(1), Continue(false));
assert_eq!(LessThan(2).compare_distance(3), Stop(false));
assert_eq!(LessThan(2).compare_distance(2), Stop(false));
assert_eq!(LessThan(2).compare_distance(1), Continue(true));
assert_eq!(LessThanOrEqual(2).compare_distance(3), Stop(false));
assert_eq!(LessThanOrEqual(2).compare_distance(2), Stop(true));
assert_eq!(LessThanOrEqual(2).compare_distance(1), Continue(true));
}
}
40 changes: 40 additions & 0 deletions tests/search_where_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,46 @@ fn search_from_where_edge_count_test() {
.query(),
&[8, 7, 6],
);
db.exec_ids(
QueryBuilder::search()
.from("root")
.where_()
.edge_count(CountComparison::GreaterThanOrEqual(2))
.query(),
&[1, 3, 2, 8, 7, 6, 15, 14, 12],
);
db.exec_ids(
QueryBuilder::search()
.from("root")
.where_()
.edge_count(CountComparison::LessThanOrEqual(2))
.query(),
&[1, 8, 7, 6, 16, 15, 14, 13, 12],
);
db.exec_ids(
QueryBuilder::search()
.from("root")
.where_()
.edge_count(CountComparison::LessThan(2))
.query(),
&[16, 13],
);
db.exec_ids(
QueryBuilder::search()
.from("root")
.where_()
.edge_count(CountComparison::Equal(2))
.query(),
&[1, 8, 7, 6, 15, 14, 12],
);
db.exec_ids(
QueryBuilder::search()
.from("root")
.where_()
.edge_count(CountComparison::NotEqual(2))
.query(),
&[3, 2, 16, 13],
);
}

#[test]
Expand Down

0 comments on commit 4f51aea

Please sign in to comment.