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

Commit 4f6f50f

Browse files
committed
requested changes
1 parent c4646cb commit 4f6f50f

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

synapse/events/snapshot.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,7 @@ async def batch_persist_unpersisted_contexts(
360360
"""
361361
Takes a list of events and their associated unpersisted contexts and persists
362362
the unpersisted contexts, returning a list of events and persisted contexts.
363-
Note that all the events must be in a linear chain (ie a <- b <- c)
364-
and must be state events.
363+
Note that all the events must be in a linear chain (ie a <- b <- c).
365364
366365
Args:
367366
events_and_context: A list of events and their unpersisted contexts
@@ -375,15 +374,14 @@ async def batch_persist_unpersisted_contexts(
375374

376375
events_and_persisted_context = []
377376
for event, unpersisted_context in amended_events_and_context:
378-
assert unpersisted_context.partial_state is not None
379377
context = EventContext(
380378
storage=unpersisted_context._storage,
381379
state_group=unpersisted_context.state_group_after_event,
382380
state_group_before_event=unpersisted_context.state_group_before_event,
383381
state_delta_due_to_event=unpersisted_context.state_delta_due_to_event,
384382
partial_state=unpersisted_context.partial_state,
385-
prev_group=unpersisted_context.prev_group_for_state_group_after_event,
386-
delta_ids=unpersisted_context.delta_ids_to_state_group_after_event,
383+
prev_group=unpersisted_context.state_group_before_event,
384+
delta_ids=unpersisted_context.state_delta_due_to_event,
387385
)
388386
events_and_persisted_context.append((event, context))
389387
return events_and_persisted_context

synapse/storage/databases/state/store.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,7 @@ async def store_state_deltas_for_batched(
413413
prev_group: int,
414414
) -> List[Tuple[EventBase, UnpersistedEventContext]]:
415415
"""Generate and store state deltas for a group of events and contexts created to be
416-
batch persisted. Note that all the events must be in a linear chain (ie a <- b <- c)
417-
and must be state events.
416+
batch persisted. Note that all the events must be in a linear chain (ie a <- b <- c).
418417
419418
Args:
420419
events_and_context: the events to generate and store a state groups for
@@ -449,31 +448,32 @@ def insert_deltas_group_txn(
449448
% (prev_group,)
450449
)
451450

452-
num_state_groups = len(events_and_context)
451+
num_state_groups = 0
452+
for event, _ in events_and_context:
453+
if event.is_state():
454+
num_state_groups += 1
453455

454456
state_groups = self._state_group_seq_gen.get_next_mult_txn(
455457
txn, num_state_groups
456458
)
457459

460+
sg_before = prev_group
458461
for index, (event, context) in enumerate(events_and_context):
459-
context.state_group_after_event = state_groups[index]
460-
# The first prev_group will be the last persisted state group, which is passed in
461-
# else it will be the group most recently assigned
462-
if index > 0:
463-
context.prev_group_for_state_group_after_event = state_groups[
464-
index - 1
465-
]
466-
context.state_group_before_event = state_groups[index - 1]
467-
else:
468-
context.prev_group_for_state_group_after_event = prev_group
469-
context.state_group_before_event = prev_group
470-
context.delta_ids_to_state_group_after_event = {
462+
if not event.is_state():
463+
context.state_group_after_event = sg_before
464+
context.state_group_before_event = sg_before
465+
pass
466+
467+
sg_after = state_groups[index]
468+
context.state_group_after_event = sg_after
469+
context.state_group_before_event = sg_before
470+
context.delta_ids_to_state_group_before_event = {
471471
(event.type, event.state_key): event.event_id
472472
}
473473
context.state_delta_due_to_event = {
474474
(event.type, event.state_key): event.event_id
475475
}
476-
index += 1
476+
sg_before = sg_after
477477

478478
self.db_pool.simple_insert_many_txn(
479479
txn,
@@ -492,29 +492,35 @@ def insert_deltas_group_txn(
492492
values=[
493493
(
494494
context.state_group_after_event,
495-
context.prev_group_for_state_group_after_event,
495+
context.state_group_before_event,
496496
)
497497
for _, context in events_and_context
498498
],
499499
)
500500

501+
values = []
501502
for _, context in events_and_context:
502-
assert context.delta_ids_to_state_group_after_event is not None
503-
self.db_pool.simple_insert_many_txn(
504-
txn,
505-
table="state_groups_state",
506-
keys=("state_group", "room_id", "type", "state_key", "event_id"),
507-
values=[
503+
assert context.delta_ids_to_state_group_before_event is not None
504+
for (
505+
key,
506+
state_id,
507+
) in context.delta_ids_to_state_group_before_event.items():
508+
values.append(
508509
(
509510
context.state_group_after_event,
510511
room_id,
511512
key[0],
512513
key[1],
513514
state_id,
514515
)
515-
for key, state_id in context.delta_ids_to_state_group_after_event.items()
516-
],
517-
)
516+
)
517+
518+
self.db_pool.simple_insert_many_txn(
519+
txn,
520+
table="state_groups_state",
521+
keys=("state_group", "room_id", "type", "state_key", "event_id"),
522+
values=values,
523+
)
518524
return events_and_context
519525

520526
return await self.db_pool.runInteraction(

tests/push/test_bulk_push_rule_evaluator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def test_suppress_edits(self) -> None:
379379
bulk_evaluator = BulkPushRuleEvaluator(self.hs)
380380

381381
# Create & persist an event to use as the parent of the relation.
382-
event, context = self.get_success(
382+
event, unpersisted_context = self.get_success(
383383
self.event_creation_handler.create_event(
384384
self.requester,
385385
{
@@ -393,6 +393,7 @@ def test_suppress_edits(self) -> None:
393393
},
394394
)
395395
)
396+
context = self.get_success(unpersisted_context.persist(event))
396397
self.get_success(
397398
self.event_creation_handler.handle_new_client_event(
398399
self.requester, events_and_context=[(event, context)]

0 commit comments

Comments
 (0)