Skip to content

Commit 134fb50

Browse files
committed
For APIs that may be paged, entries, keys, and values, make the paging opt-in given the performance considerations.
1 parent f6be892 commit 134fb50

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/coherence/client.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ async def aggregate(
400400
"""
401401

402402
@abc.abstractmethod
403-
def values(self, filter: Optional[Filter] = None, comparator: Optional[Comparator] = None) -> AsyncIterator[V]:
403+
def values(self, filter: Optional[Filter] = None, comparator: Optional[Comparator] = None, by_page: bool = False) -> AsyncIterator[V]:
404404
"""
405405
Return a Set of the values contained in this map that satisfy the criteria expressed by the filter.
406406
If no filter or comparator is specified, it returns a Set view of the values contained in this map.The
@@ -411,22 +411,26 @@ def values(self, filter: Optional[Filter] = None, comparator: Optional[Comparato
411411
:param filter: the Filter object representing the criteria that the entries of this map should satisfy
412412
:param comparator: the Comparator object which imposes an ordering on entries in the resulting set; or null
413413
if the entries' natural ordering should be used
414+
:param by_page: returns the keys in pages (transparently to the caller). This option is only valid
415+
if no filter or comparator is provided.
414416
:return: an AsyncIterator of MapEntry instances resolving to the values that satisfy the specified criteria
415417
"""
416418

417419
@abc.abstractmethod
418-
def keys(self, filter: Optional[Filter] = None) -> AsyncIterator[K]:
420+
def keys(self, filter: Optional[Filter] = None, by_page: bool = False) -> AsyncIterator[K]:
419421
"""
420422
Return a set view of the keys contained in this map for entries that satisfy the criteria expressed by the
421423
filter.
422424
423425
:param filter: the Filter object representing the criteria that the entries of this map should satisfy
426+
:param by_page: returns the keys in pages (transparently to the caller). This option is only valid
427+
if no filter is provided.
424428
:return: an AsyncIterator of keys for entries that satisfy the specified criteria
425429
"""
426430

427431
@abc.abstractmethod
428432
def entries(
429-
self, filter: Optional[Filter] = None, comparator: Optional[Comparator] = None
433+
self, filter: Optional[Filter] = None, comparator: Optional[Comparator] = None, by_page: bool = False
430434
) -> AsyncIterator[MapEntry[K, V]]:
431435
"""
432436
Return a set view of the entries contained in this map that satisfy the criteria expressed by the filter.
@@ -435,6 +439,8 @@ def entries(
435439
:param filter: the Filter object representing the criteria that the entries of this map should satisfy
436440
:param comparator: the Comparator object which imposes an ordering on entries in the resulting set; or `None`
437441
if the entries' values natural ordering should be used
442+
:param by_page: returns the keys in pages (transparently to the caller). This option is only valid
443+
if no filter or comparator is provided.
438444
:return: an AsyncIterator of MapEntry instances that satisfy the specified criteria
439445
"""
440446

@@ -664,10 +670,8 @@ async def aggregate(
664670
return cast(R, value)
665671

666672
@_pre_call_cache
667-
def values(self, filter: Optional[Filter] = None, comparator: Optional[Comparator] = None) -> AsyncIterator[V]:
668-
# if there is no filter or no co, or the filter is an AlwaysFilter,
669-
# obtain results by-page
670-
if (comparator is None and filter is None) or (comparator is None and isinstance(filter, AlwaysFilter)):
673+
def values(self, filter: Optional[Filter] = None, comparator: Optional[Comparator] = None, by_page: bool = False) -> AsyncIterator[V]:
674+
if by_page and comparator is None and filter is None:
671675
return _PagedStream(self, _scalar_deserializer)
672676
else:
673677
r = self._request_factory.values_request(filter)
@@ -676,10 +680,8 @@ def values(self, filter: Optional[Filter] = None, comparator: Optional[Comparato
676680
return _Stream(self._request_factory.get_serializer(), stream, _scalar_producer)
677681

678682
@_pre_call_cache
679-
def keys(self, filter: Optional[Filter] = None) -> AsyncIterator[K]:
680-
# if there is no filter, or the filter is an AlwaysFilter,
681-
# obtain results by-page
682-
if filter is None or isinstance(filter, AlwaysFilter):
683+
def keys(self, filter: Optional[Filter] = None, by_page: bool = False) -> AsyncIterator[K]:
684+
if by_page and filter is None:
683685
return _PagedStream(self, _scalar_deserializer, True)
684686
else:
685687
r = self._request_factory.keys_request(filter)
@@ -689,11 +691,9 @@ def keys(self, filter: Optional[Filter] = None) -> AsyncIterator[K]:
689691

690692
@_pre_call_cache
691693
def entries(
692-
self, filter: Optional[Filter] = None, comparator: Optional[Comparator] = None
694+
self, filter: Optional[Filter] = None, comparator: Optional[Comparator] = None, by_page: bool = False
693695
) -> AsyncIterator[MapEntry[K, V]]:
694-
# if there is no filter and no comparator, or the filter is an AlwaysFilter,
695-
# obtain results by-page
696-
if (comparator is None and filter is None) or (comparator is None and isinstance(filter, AlwaysFilter)):
696+
if by_page and comparator is None and filter is None:
697697
return _PagedStream(self, _entry_deserializer)
698698
else:
699699
r = self._request_factory.entries_request(filter, comparator)

tests/test_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ async def test_keys_paged(setup_and_teardown: NamedCache[str, str]) -> None:
225225

226226
# Stream the keys and locally cache the results
227227
local_set: set[str] = set()
228-
async for e in cache.keys():
228+
async for e in cache.keys(by_page=True):
229229
local_set.add(e)
230230

231231
assert len(local_set) == num_entries
@@ -268,7 +268,7 @@ async def test_entries_paged(setup_and_teardown: NamedCache[str, str]) -> None:
268268

269269
# Stream the keys and locally cache the results
270270
local_dict: dict[str, str] = {}
271-
async for e in cache.entries():
271+
async for e in cache.entries(by_page=True):
272272
local_dict[e.key] = e.value
273273

274274
assert len(local_dict) == num_entries
@@ -308,7 +308,7 @@ async def test_values_paged(setup_and_teardown: NamedCache[str, str]) -> None:
308308

309309
# Stream the keys and locally cache the results
310310
local_list: list[str] = []
311-
async for e in cache.values():
311+
async for e in cache.values(by_page=True):
312312
local_list.append(e)
313313

314314
assert len(local_list) == num_entries

0 commit comments

Comments
 (0)