@@ -123,9 +123,9 @@ def set_data(self, data: Optional['RelationshipData']) -> None:
123123 def get_data (self ) -> Optional ['RelationshipData' ]:
124124 return self ._data
125125
126- def set_value (self , relationship : 'Relationship' ) -> None :
126+ def set_value (self , relationship : 'Relationship' , traversal_id : str = "" ) -> None :
127127 """Set value by pushing match to collector."""
128- self ._matches .push (relationship )
128+ self ._matches .push (relationship , traversal_id )
129129 self ._value = self ._matches .value ()
130130
131131 def value (self ) -> Optional [Union [RelationshipMatchRecord , List [RelationshipMatchRecord ]]]:
@@ -139,11 +139,13 @@ def set_end_node(self, node: 'Node') -> None:
139139 """Set the end node for the current match."""
140140 self ._matches .end_node = node
141141
142+ def _left_id_or_right_id (self ) -> str :
143+ return "left_id" if self ._direction == "left" else "right_id"
144+
142145 async def find (self , left_id : str , hop : int = 0 ) -> None :
143146 """Find relationships starting from the given node ID."""
144147 # Save original source node
145148 original = self ._source
146- is_left = self ._direction == "left"
147149 if hop > 0 :
148150 # For hops greater than 0, the source becomes the target of the previous hop
149151 self ._source = self ._target
@@ -158,30 +160,26 @@ async def find(self, left_id: str, hop: int = 0) -> None:
158160 # No relationship match is pushed since no edge is traversed
159161 await self ._target .find (left_id , hop )
160162
161- def find_match (id_ : str , h : int ) -> bool :
162- if self ._data is None :
163- return False
164- if is_left :
165- return self ._data .find_reverse (id_ , h )
166- return self ._data .find (id_ , h )
167- follow_id = 'left_id' if is_left else 'right_id'
168- while self ._data and find_match (left_id , hop ):
163+ while self ._data and self ._data .find (left_id , hop , self ._direction ):
169164 data = self ._data .current (hop )
170- if data and self ._hops and hop + 1 >= self ._hops .min :
171- self .set_value (self )
165+ if data is None :
166+ continue
167+ id = data [self ._left_id_or_right_id ()]
168+ if hop + 1 >= self ._hops .min :
169+ self .set_value (self , left_id )
172170 if not self ._matches_properties (hop ):
173171 continue
174- if self ._target and follow_id in data :
175- await self ._target .find (data [follow_id ], hop )
176- if self ._matches .is_circular ():
177- raise ValueError ("Circular relationship detected" )
178- if self ._hops and hop + 1 < self ._hops .max :
179- await self .find (data [follow_id ], hop + 1 )
172+ if self ._target :
173+ await self ._target .find (id , hop )
174+ if hop + 1 < self ._hops .max :
175+ if self ._matches .is_circular (id ):
176+ self ._matches .pop ()
177+ continue
178+ await self .find (id , hop + 1 )
180179 self ._matches .pop ()
181- elif data and self . _hops :
180+ else :
182181 # Below minimum hops: traverse the edge without yielding a match
183- if follow_id in data :
184- await self .find (data [follow_id ], hop + 1 )
182+ await self .find (id , hop + 1 )
185183
186184 # Restore original source node
187185 self ._source = original
0 commit comments