Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 8f4282c

Browse files
committed
add a function to batch persist unpersisted event contexts
1 parent a47ee14 commit 8f4282c

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

synapse/events/snapshot.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
if TYPE_CHECKING:
2525
from synapse.storage.controllers import StorageControllers
26+
from synapse.storage.databases import StateGroupDataStore
2627
from synapse.storage.databases.main import DataStore
2728
from synapse.types.state import StateFilter
2829

@@ -348,6 +349,43 @@ class UnpersistedEventContext(UnpersistedEventContextBase):
348349
partial_state: bool
349350
state_map_before_event: Optional[StateMap[str]] = None
350351

352+
@classmethod
353+
async def batch_persist_unpersisted_contexts(
354+
cls,
355+
events_and_context: List[Tuple[EventBase, "UnpersistedEventContextBase"]],
356+
room_id: str,
357+
last_known_state_group: int,
358+
datastore: "StateGroupDataStore",
359+
) -> List[Tuple[EventBase, EventContext]]:
360+
"""
361+
Takes a list of events and their associated unpersisted contexts and persists
362+
the unpersisted contexts, returning a list of events and persisted contexts.
363+
364+
Args:
365+
events_and_context: A list of events and their unpersisted contexts
366+
room_id: the room_id for the events
367+
last_known_state_group: the last persisted state group
368+
datastore: a state datastore
369+
"""
370+
amended_events_and_context = await datastore.store_state_deltas_for_batched(
371+
events_and_context, room_id, last_known_state_group
372+
)
373+
374+
events_and_persisted_context = []
375+
for event, unpersisted_context in amended_events_and_context:
376+
assert unpersisted_context.partial_state is not None
377+
context = EventContext(
378+
storage=unpersisted_context._storage,
379+
state_group=unpersisted_context.state_group_after_event,
380+
state_group_before_event=unpersisted_context.state_group_before_event,
381+
state_delta_due_to_event=unpersisted_context.state_delta_due_to_event,
382+
partial_state=unpersisted_context.partial_state,
383+
prev_group=unpersisted_context.prev_group_for_state_group_after_event,
384+
delta_ids=unpersisted_context.delta_ids_to_state_group_after_event,
385+
)
386+
events_and_persisted_context.append((event, context))
387+
return events_and_persisted_context
388+
351389
async def get_prev_state_ids(
352390
self, state_filter: Optional["StateFilter"] = None
353391
) -> StateMap[str]:

0 commit comments

Comments
 (0)