|
23 | 23 |
|
24 | 24 | if TYPE_CHECKING: |
25 | 25 | from synapse.storage.controllers import StorageControllers |
| 26 | + from synapse.storage.databases import StateGroupDataStore |
26 | 27 | from synapse.storage.databases.main import DataStore |
27 | 28 | from synapse.types.state import StateFilter |
28 | 29 |
|
@@ -348,6 +349,43 @@ class UnpersistedEventContext(UnpersistedEventContextBase): |
348 | 349 | partial_state: bool |
349 | 350 | state_map_before_event: Optional[StateMap[str]] = None |
350 | 351 |
|
| 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 | + |
351 | 389 | async def get_prev_state_ids( |
352 | 390 | self, state_filter: Optional["StateFilter"] = None |
353 | 391 | ) -> StateMap[str]: |
|
0 commit comments