|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union |
| 5 | +from typing import TYPE_CHECKING, Any, AsyncIterator, Dict, List, Optional, Union |
6 | 6 |
|
7 | 7 | from ..parsing.ast_node import ASTNode |
8 | 8 | from .hops import Hops |
@@ -151,44 +151,49 @@ def set_end_node(self, node: 'Node') -> None: |
151 | 151 | def _left_id_or_right_id(self) -> str: |
152 | 152 | return "left_id" if self._direction == "left" else "right_id" |
153 | 153 |
|
154 | | - async def find(self, left_id: str, hop: int = 0) -> None: |
| 154 | + async def find(self, left_id: str, hop: int = 0) -> AsyncIterator[None]: |
155 | 155 | """Find relationships starting from the given node ID.""" |
156 | 156 | # Save original source node |
157 | 157 | original = self._source |
158 | 158 | if hop > 0: |
159 | 159 | # For hops greater than 0, the source becomes the target of the previous hop |
160 | 160 | self._source = self._target |
161 | | - if hop == 0: |
162 | | - if self._data: |
163 | | - self._data.reset() |
164 | | - |
165 | | - # Handle zero-hop case: when min is 0 on a variable-length relationship, |
166 | | - # match source node as target (no traversal) |
167 | | - if self._hops and self._hops.multi() and self._hops.min == 0 and self._target: |
168 | | - # For zero-hop, target finds the same node as source (left_id) |
169 | | - # No relationship match is pushed since no edge is traversed |
170 | | - await self._target.find(left_id, hop) |
171 | | - |
172 | | - while self._data and self._data.find(left_id, hop, self._direction): |
173 | | - data = self._data.current(hop) |
174 | | - if data is None: |
175 | | - continue |
176 | | - id = data[self._left_id_or_right_id()] |
177 | | - if hop + 1 >= self._hops.min: |
178 | | - self.set_value(self, left_id) |
179 | | - if not self._matches_properties(hop): |
| 161 | + try: |
| 162 | + if hop == 0: |
| 163 | + if self._data: |
| 164 | + self._data.reset() |
| 165 | + |
| 166 | + # Handle zero-hop case: when min is 0 on a variable-length relationship, |
| 167 | + # match source node as target (no traversal) |
| 168 | + if self._hops and self._hops.multi() and self._hops.min == 0 and self._target: |
| 169 | + # For zero-hop, target finds the same node as source (left_id) |
| 170 | + # No relationship match is pushed since no edge is traversed |
| 171 | + async for _ in self._target.find(left_id, hop): |
| 172 | + yield |
| 173 | + |
| 174 | + while self._data and self._data.find(left_id, hop, self._direction): |
| 175 | + data = self._data.current(hop) |
| 176 | + if data is None: |
180 | 177 | continue |
181 | | - if self._target: |
182 | | - await self._target.find(id, hop) |
183 | | - if hop + 1 < self._hops.max: |
184 | | - if self._matches.is_circular(id): |
185 | | - self._matches.pop() |
| 178 | + id = data[self._left_id_or_right_id()] |
| 179 | + if hop + 1 >= self._hops.min: |
| 180 | + self.set_value(self, left_id) |
| 181 | + if not self._matches_properties(hop): |
186 | 182 | continue |
187 | | - await self.find(id, hop + 1) |
188 | | - self._matches.pop() |
189 | | - else: |
190 | | - # Below minimum hops: traverse the edge without yielding a match |
191 | | - await self.find(id, hop + 1) |
192 | | - |
193 | | - # Restore original source node |
194 | | - self._source = original |
| 183 | + if self._target: |
| 184 | + async for _ in self._target.find(id, hop): |
| 185 | + yield |
| 186 | + if hop + 1 < self._hops.max: |
| 187 | + if self._matches.is_circular(id): |
| 188 | + self._matches.pop() |
| 189 | + continue |
| 190 | + async for _ in self.find(id, hop + 1): |
| 191 | + yield |
| 192 | + self._matches.pop() |
| 193 | + else: |
| 194 | + # Below minimum hops: traverse the edge without yielding a match |
| 195 | + async for _ in self.find(id, hop + 1): |
| 196 | + yield |
| 197 | + finally: |
| 198 | + # Restore original source node |
| 199 | + self._source = original |
0 commit comments