perf: index in-memory message history and measure limits
This commit is contained in:
+10
-6
@@ -3,6 +3,7 @@
|
||||
import base64
|
||||
import binascii
|
||||
import json
|
||||
from bisect import bisect_right, insort
|
||||
from datetime import datetime, timezone
|
||||
from threading import Lock
|
||||
from uuid import UUID, uuid4
|
||||
@@ -14,6 +15,10 @@ def now() -> str:
|
||||
return datetime.now(timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z")
|
||||
|
||||
|
||||
def message_key(item: dict) -> tuple[str, str]:
|
||||
return item["created_at"], item["id"]
|
||||
|
||||
|
||||
def encode_cursor(resource: str, item: dict) -> str:
|
||||
value = [resource, item["created_at"], item["id"]]
|
||||
return base64.urlsafe_b64encode(json.dumps(value, separators=(",", ":")).encode()).decode().rstrip("=")
|
||||
@@ -100,7 +105,7 @@ class Store:
|
||||
"id": str(uuid4()), "chat_id": chat_id, "sender_id": actor_id,
|
||||
"text": text, "created_at": now(),
|
||||
}
|
||||
self.messages[chat_id].append(message)
|
||||
insort(self.messages[chat_id], message, key=message_key)
|
||||
return message
|
||||
|
||||
def list_messages(self, actor_id: str, chat_id: str, limit: int, cursor: str | None) -> dict:
|
||||
@@ -109,11 +114,10 @@ class Store:
|
||||
if chat is None or actor_id not in chat["member_ids"]:
|
||||
raise HTTPException(404)
|
||||
after = decode_cursor(cursor, f"messages:{chat_id}")
|
||||
items = sorted(self.messages[chat_id], key=lambda item: (item["created_at"], item["id"]))
|
||||
if after is not None:
|
||||
items = [item for item in items if (item["created_at"], item["id"]) > after]
|
||||
page = items[:limit]
|
||||
next_cursor = encode_cursor(f"messages:{chat_id}", page[-1]) if len(items) > limit else None
|
||||
items = self.messages[chat_id]
|
||||
start = bisect_right(items, after, key=message_key) if after is not None else 0
|
||||
page = items[start:start + limit]
|
||||
next_cursor = encode_cursor(f"messages:{chat_id}", page[-1]) if len(items) > start + limit else None
|
||||
return {"items": page, "next_cursor": next_cursor}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user