-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Store state groups separately from events #2784
Changes from 13 commits
cb222e2
0f18d58
c8c8f53
1b2af72
2fdf245
e66f2c3
693ea27
a742398
90cd2e6
c695725
22be520
2694524
7018b68
a6b415c
4a72132
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,8 +183,15 @@ def get_current_hosts_in_room(self, room_id, latest_event_ids=None): | |
def compute_event_context(self, event, old_state=None): | ||
"""Build an EventContext structure for the event. | ||
|
||
This works out what the current state should be for the event, and | ||
generates a new state group if necessary. | ||
|
||
Args: | ||
event (synapse.events.EventBase): | ||
old_state (dict|None): The state at the event if it can't be | ||
calculated from existing events. This is normally only specified | ||
when receiving an event from federation where we don't have the | ||
prev events for, e.g. when backfilling. | ||
Returns: | ||
synapse.events.snapshot.EventContext: | ||
""" | ||
|
@@ -208,15 +215,22 @@ def compute_event_context(self, event, old_state=None): | |
context.current_state_ids = {} | ||
context.prev_state_ids = {} | ||
context.prev_state_events = [] | ||
context.state_group = self.store.get_next_state_group() | ||
|
||
# We don't store state for outliers, so we don't generate a state | ||
# froup for it. | ||
context.state_group = None | ||
|
||
defer.returnValue(context) | ||
|
||
if old_state: | ||
# We already have the state, so we don't need to calculate it. | ||
# Let's just correctly fill out the context and create a | ||
# new state group for it. | ||
|
||
context = EventContext() | ||
context.prev_state_ids = { | ||
(s.type, s.state_key): s.event_id for s in old_state | ||
} | ||
context.state_group = self.store.get_next_state_group() | ||
|
||
if event.is_state(): | ||
key = (event.type, event.state_key) | ||
|
@@ -229,6 +243,14 @@ def compute_event_context(self, event, old_state=None): | |
else: | ||
context.current_state_ids = context.prev_state_ids | ||
|
||
context.state_group = yield self.store.store_state_group( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to create a new state_group if it's not a state event? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. Basically, we've been explicitly told the state at this point ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
event.event_id, | ||
event.room_id, | ||
prev_group=None, | ||
delta_ids=None, | ||
current_state_ids=context.current_state_ids, | ||
) | ||
|
||
context.prev_state_events = [] | ||
defer.returnValue(context) | ||
|
||
|
@@ -242,7 +264,8 @@ def compute_event_context(self, event, old_state=None): | |
context = EventContext() | ||
context.prev_state_ids = curr_state | ||
if event.is_state(): | ||
context.state_group = self.store.get_next_state_group() | ||
# If this is a state event then we need to create a new state | ||
# group for the state after this event. | ||
|
||
key = (event.type, event.state_key) | ||
if key in context.prev_state_ids: | ||
|
@@ -253,23 +276,42 @@ def compute_event_context(self, event, old_state=None): | |
context.current_state_ids[key] = event.event_id | ||
|
||
if entry.state_group: | ||
# If the state at the event has a state group assigned then | ||
# we can use that as the prev group | ||
context.prev_group = entry.state_group | ||
context.delta_ids = { | ||
key: event.event_id | ||
} | ||
elif entry.prev_group: | ||
# If the state at the event only has a prev group, then we can | ||
# use that as a prev group too. | ||
context.prev_group = entry.prev_group | ||
context.delta_ids = dict(entry.delta_ids) | ||
context.delta_ids[key] = event.event_id | ||
|
||
context.state_group = yield self.store.store_state_group( | ||
event.event_id, | ||
event.room_id, | ||
prev_group=context.prev_group, | ||
delta_ids=context.delta_ids, | ||
current_state_ids=context.current_state_ids, | ||
) | ||
else: | ||
context.current_state_ids = context.prev_state_ids | ||
context.prev_group = entry.prev_group | ||
context.delta_ids = entry.delta_ids | ||
|
||
if entry.state_group is None: | ||
entry.state_group = self.store.get_next_state_group() | ||
entry.state_group = yield self.store.store_state_group( | ||
event.event_id, | ||
event.room_id, | ||
prev_group=entry.prev_group, | ||
delta_ids=entry.delta_ids, | ||
current_state_ids=context.current_state_ids, | ||
) | ||
entry.state_id = entry.state_group | ||
|
||
context.state_group = entry.state_group | ||
context.current_state_ids = context.prev_state_ids | ||
context.prev_group = entry.prev_group | ||
context.delta_ids = entry.delta_ids | ||
|
||
context.prev_state_events = [] | ||
defer.returnValue(context) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2018 New Vector Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from synapse.storage.engines import PostgresEngine | ||
|
||
|
||
def run_create(cur, database_engine, *args, **kwargs): | ||
if isinstance(database_engine, PostgresEngine): | ||
# if we already have some state groups, we want to start making new | ||
# ones with a higher id. | ||
cur.execute("SELECT max(id) FROM state_groups") | ||
row = cur.fetchone() | ||
|
||
if row[0] is None: | ||
start_val = 1 | ||
else: | ||
start_val = row[0] + 1 | ||
|
||
cur.execute( | ||
"CREATE SEQUENCE state_group_id_seq START WITH %s", | ||
(start_val, ), | ||
) | ||
|
||
|
||
def run_upgrade(*args, **kwargs): | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. the
state_group
field on EventContext is documented as 'state_group (int): state group id', so if it can now be None as well as an int that needs updating, please.