projector.py 893 B

12345678910111213141516171819202122232425262728
  1. class ReferenceProjector:
  2. """Tiny transactional projection used to prove cursor/idempotency semantics."""
  3. def __init__(self) -> None:
  4. self.cursor = 0
  5. self.rows = {}
  6. async def load_cursor(self, application_ref, root_trace_id):
  7. del application_ref, root_trace_id
  8. return self.cursor
  9. async def project(self, event, expected_cursor):
  10. if expected_cursor != self.cursor:
  11. raise ValueError("stale projection cursor")
  12. self.rows.setdefault(event.event_key, event.payload)
  13. self.cursor = event.event_id
  14. async def advance_cursor(
  15. self,
  16. application_ref,
  17. root_trace_id,
  18. event_id,
  19. expected_cursor,
  20. ):
  21. del application_ref, root_trace_id
  22. if expected_cursor != self.cursor:
  23. raise ValueError("stale projection cursor")
  24. self.cursor = event_id