Skip to content

Commit fa328ef

Browse files
committed
Add compile-time check for cacheable nodes.
1 parent f065f93 commit fa328ef

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
**Breaking changes**
66

77
- Changed the `JSONPathMatch.parts` representation of the non-standard _keys_ selector (default `~`) to be `~` followed by the key name. It used to be two "parts", `~` and key index.
8-
- All `FilterExpression` instances must now implement `children()` and `set_children()` methods. These methods facilitate filter expression inspection and caching.
8+
- All `FilterExpression` subclasses must now implement `children()` and `set_children()`. These methods facilitate filter expression inspection and caching.
99

1010
**Fixes**
1111

‎jsonpath/filter.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ def __init__(self, expression: FilterExpression):
381381
super().__init__()
382382

383383
def cache_tree(self) -> BooleanExpression:
384+
"""Return a copy of _self.expression_ augmented with caching nodes."""
385+
384386
def _cache_tree(expr: FilterExpression) -> FilterExpression:
385387
children = expr.children()
386388
if expr.volatile:
@@ -394,6 +396,13 @@ def _cache_tree(expr: FilterExpression) -> FilterExpression:
394396

395397
return BooleanExpression(_cache_tree(copy.copy(self.expression)))
396398

399+
def cacheable_nodes(self) -> bool:
400+
"""Return `True` if there are any cacheable nodes in this expression tree."""
401+
return any(
402+
isinstance(node, CachingFilterExpression)
403+
for node in walk(self.cache_tree())
404+
)
405+
397406
def __str__(self) -> str:
398407
return str(self.expression)
399408

‎jsonpath/selectors.py‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ async def resolve_async(
490490
class Filter(JSONPathSelector):
491491
"""A filter selector."""
492492

493-
__slots__ = ("expression",)
493+
__slots__ = ("expression", "cacheable_nodes")
494494

495495
def __init__(
496496
self,
@@ -501,16 +501,20 @@ def __init__(
501501
) -> None:
502502
super().__init__(env=env, token=token)
503503
self.expression = expression
504+
# Compile-time check for cacheable nodes.
505+
self.cacheable_nodes = self.expression.cacheable_nodes()
504506

505507
def __str__(self) -> str:
506508
return f"[?({self.expression})]"
507509

508510
def resolve( # noqa: PLR0912
509511
self, matches: Iterable[JSONPathMatch]
510512
) -> Iterable[JSONPathMatch]:
511-
expr = (
512-
self.expression.cache_tree() if self.env.filter_caching else self.expression
513-
)
513+
if self.cacheable_nodes and self.env.filter_caching:
514+
expr = self.expression.cache_tree()
515+
else:
516+
expr = self.expression
517+
514518
for match in matches:
515519
if isinstance(match.obj, Mapping):
516520
for key, val in match.obj.items():
@@ -567,9 +571,11 @@ def resolve( # noqa: PLR0912
567571
async def resolve_async( # noqa: PLR0912
568572
self, matches: AsyncIterable[JSONPathMatch]
569573
) -> AsyncIterable[JSONPathMatch]:
570-
expr = (
571-
self.expression.cache_tree() if self.env.filter_caching else self.expression
572-
)
574+
if self.cacheable_nodes and self.env.filter_caching:
575+
expr = self.expression.cache_tree()
576+
else:
577+
expr = self.expression
578+
573579
async for match in matches:
574580
if isinstance(match.obj, Mapping):
575581
for key, val in match.obj.items():

0 commit comments

Comments
 (0)