Skip to content

Commit f053836

Browse files
authored
fix/fixing filtering by index for n by index (#332)
## Description <!-- Provide a brief description of the changes in this PR --> fixing filtering by index for n by index ## Related Issues <!-- Link to any related issues using #issue_number --> Closes # ## Checklist when merging to main <!-- Mark items with "x" when completed --> - [ ] No compiler warnings (if applicable) - [ ] Code is formatted with `rustfmt` - [ ] No useless or dead code (if applicable) - [ ] Code is easy to understand - [ ] Doc comments are used for all functions, enums, structs, and fields (where appropriate) - [ ] All tests pass - [ ] Performance has not regressed (assuming change was not to fix a bug) - [ ] Version number has been updated in `helix-cli/Cargo.toml` and `helixdb/Cargo.toml` - [ ] Lines are kept under 100 characters where possible - [ ] Code is good ## Additional Notes <!-- Add any additional information that would be helpful for reviewers -->
2 parents 38b9991 + 0c95960 commit f053836

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

helix-db/src/helix_engine/graph_core/ops/source/n_from_index.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::{
66
},
77
protocol::value::Value,
88
};
9+
use heed3::{RoTxn, byteorder::BE};
910
use helix_macros::debug_trace;
10-
use heed3::{byteorder::BE, RoTxn};
1111
use serde::Serialize;
1212
use std::sync::Arc;
1313

@@ -16,18 +16,25 @@ pub struct NFromIndex<'a> {
1616
heed3::RoPrefix<'a, heed3::types::Bytes, heed3::types::LazyDecode<heed3::types::U128<BE>>>,
1717
txn: &'a RoTxn<'a>,
1818
storage: Arc<HelixGraphStorage>,
19+
label: &'a str,
1920
}
2021

2122
impl<'a> Iterator for NFromIndex<'a> {
2223
type Item = Result<TraversalVal, GraphError>;
2324

2425
#[debug_trace("N_FROM_INDEX")]
2526
fn next(&mut self) -> Option<Self::Item> {
26-
if let Some(value) = self.iter.by_ref().next() {
27+
for value in self.iter.by_ref() {
2728
let (_, value) = value.unwrap();
2829
match value.decode() {
2930
Ok(value) => match self.storage.get_node(self.txn, &value) {
30-
Ok(node) => return Some(Ok(TraversalVal::Node(node))),
31+
Ok(node) => {
32+
if node.label == self.label {
33+
return Some(Ok(TraversalVal::Node(node)));
34+
} else {
35+
continue;
36+
}
37+
}
3138
Err(e) => {
3239
println!("{} Error getting node: {:?}", line!(), e);
3340
return Some(Err(GraphError::ConversionError(e.to_string())));
@@ -55,7 +62,7 @@ pub trait NFromIndexAdapter<'a, K: Into<Value> + Serialize>:
5562
///
5663
/// Note that both the `index` and `key` must be provided.
5764
/// The index must be a valid and existing secondary index and the key should match the type of the index.
58-
fn n_from_index(self, index: &'a str, key: &'a K) -> Self::OutputIter
65+
fn n_from_index(self, label: &'a str, index: &'a str, key: &'a K) -> Self::OutputIter
5966
where
6067
K: Into<Value> + Serialize + Clone;
6168
}
@@ -66,7 +73,7 @@ impl<'a, I: Iterator<Item = Result<TraversalVal, GraphError>>, K: Into<Value> +
6673
type OutputIter = RoTraversalIterator<'a, NFromIndex<'a>>;
6774

6875
#[inline]
69-
fn n_from_index(self, index: &'a str, key: &'a K) -> Self::OutputIter
76+
fn n_from_index(self, label: &'a str, index: &'a str, key: &'a K) -> Self::OutputIter
7077
where
7178
K: Into<Value> + Serialize + Clone,
7279
{
@@ -88,6 +95,7 @@ impl<'a, I: Iterator<Item = Result<TraversalVal, GraphError>>, K: Into<Value> +
8895
iter: res,
8996
txn: self.txn,
9097
storage: Arc::clone(&self.storage),
98+
label,
9199
};
92100

93101
RoTraversalIterator {

helix-db/src/helix_engine/graph_core/traversal_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,7 +2708,7 @@ fn test_update_of_secondary_indices() {
27082708
let txn = storage.graph_env.read_txn().unwrap();
27092709

27102710
let node = G::new(Arc::clone(&storage), &txn)
2711-
.n_from_index("name", &"Jane".to_string())
2711+
.n_from_index("person", "name", &"Jane".to_string())
27122712
.collect_to::<Vec<_>>();
27132713
assert_eq!(node.len(), 1);
27142714
assert_eq!(node[0].id(), node.id());
@@ -2722,7 +2722,7 @@ fn test_update_of_secondary_indices() {
27222722
}
27232723

27242724
let node = G::new(Arc::clone(&storage), &txn)
2725-
.n_from_index("name", &"John".to_string())
2725+
.n_from_index("person", "name", &"John".to_string())
27262726
.collect_to::<Vec<_>>();
27272727
assert_eq!(node.len(), 0);
27282728

helix-db/src/helixc/analyzer/methods/traversal_validation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ pub(crate) fn validate_traversal<'a>(
120120
};
121121
gen_traversal.source_step =
122122
Separator::Period(SourceStep::NFromIndex(NFromIndex {
123+
label: GenRef::Literal(node_type.clone()),
123124
index: GenRef::Literal(match *index {
124125
IdType::Identifier { value, loc: _ } => value,
125126
// would be caught by the parser

helix-db/src/helixc/generator/source_steps.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,12 @@ impl Display for SearchVector {
196196
pub struct NFromIndex {
197197
pub index: GenRef<String>,
198198
pub key: GeneratedValue,
199+
pub label: GenRef<String>,
199200
}
200201

201202
impl Display for NFromIndex {
202203
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
203-
write!(f, "n_from_index({}, {})", self.index, self.key)
204+
write!(f, "n_from_index({}, {}, {})", self.label, self.index, self.key)
204205
}
205206
}
206207

0 commit comments

Comments
 (0)