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 Aug 16, 2024
1 parent 61f75ad commit 1a2b994
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions aries_cloudagent/storage/in_memory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Basic in-memory storage implementation (non-wallet)."""

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

from ..core.in_memory import InMemoryProfile
from .base import (
Expand Down Expand Up @@ -103,6 +104,8 @@ async def find_paginated_records(
tag_query: 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: Mapping = None,
order_by: Optional[str] = None,
descending: bool = False,
options: 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 1a2b994

Please sign in to comment.