Skip to content

Commit

Permalink
✨ add ordering to InMemoryStorage scan and fetch_all methods
Browse files Browse the repository at this point in the history
Signed-off-by: ff137 <[email protected]>
  • Loading branch information
ff137 committed Oct 22, 2024
1 parent 746b013 commit dec3202
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions acapy_agent/storage/in_memory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Basic in-memory storage implementation (non-wallet)."""

from operator import attrgetter
from typing import Mapping, Optional, Sequence

from ..core.in_memory import InMemoryProfile
Expand Down Expand Up @@ -103,6 +104,8 @@ async def find_paginated_records(
tag_query: Optional[Mapping] = None,
limit: int = DEFAULT_PAGE_SIZE,
offset: int = 0,
order_by: Optional[str] = None,
descending: bool = False,
) -> Sequence[StorageRecord]:
"""Retrieve a page of records matching a particular type filter and tag query.
Expand All @@ -111,33 +114,52 @@ async def find_paginated_records(
tag_query: An optional dictionary of tag filter clauses
limit: The maximum number of records to retrieve
offset: The offset to start retrieving records from
order_by: An optional field by which to order the records.
descending: Whether to order the records in descending order.
Returns:
A sequence of StorageRecord matching the filter and query parameters.
"""
results = []
skipped = 0
collected = 0
for record in self.profile.records.values():
if record.type == type_filter and tag_query_match(record.tags, tag_query):
if skipped < offset:
skipped += 1
continue
if collected < limit:
collected += 1
results.append(record)
else:
break
return results
# Filter records based on type and tag_query
filtered_records = [
record
for record in self.profile.records.values()
if record.type == type_filter and tag_query_match(record.tags, tag_query)
]

# Sort records if order_by is specified
if order_by:
try:
filtered_records.sort(key=attrgetter(order_by), reverse=descending)
except AttributeError:
raise ValueError(f"Invalid order_by field: {order_by}")

# Apply pagination (offset and limit)
paginated_records = filtered_records[offset : offset + limit]

return paginated_records

async def find_all_records(
self,
type_filter: str,
tag_query: Optional[Mapping] = None,
order_by: Optional[str] = None,
descending: bool = False,
options: Optional[Mapping] = None,
):
"""Retrieve all records matching a particular type filter and tag query."""
results = []
for record in self.profile.records.values():
if record.type == type_filter and tag_query_match(record.tags, tag_query):
results.append(record)

# Sort records if order_by is specified
if order_by:
try:
results.sort(key=attrgetter(order_by), reverse=descending)
except AttributeError:
raise ValueError(f"Invalid order_by field: {order_by}")

return results

async def delete_all_records(
Expand Down

0 comments on commit dec3202

Please sign in to comment.