summary refs log tree commit diff
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2018-01-30 10:17:55 +0000
committerRichard van der Hoff <richard@matrix.org>2018-02-05 16:23:25 +0000
commit225dc3b4cb8875fff52180d2f3b1e386dec26f4d (patch)
tree607df3a1d452fcfc60557b3b93b3fc269d1c3f2e
parentCheck that events being persisted have state_group (diff)
downloadsynapse-225dc3b4cb8875fff52180d2f3b1e386dec26f4d.tar.xz
Flatten _get_new_state_after_events
rejig the if statements to simplify the logic and reduce indentation
Diffstat (limited to '')
-rw-r--r--synapse/storage/events.py90
1 files changed, 46 insertions, 44 deletions
diff --git a/synapse/storage/events.py b/synapse/storage/events.py
index 9bceded7ba..1b5dffe1c7 100644
--- a/synapse/storage/events.py
+++ b/synapse/storage/events.py
@@ -503,6 +503,10 @@ class EventsStore(SQLBaseStore):
                 None if there are no changes to the room state, or
                 a dict of (type, state_key) -> event_id].
         """
+
+        if not new_latest_event_ids:
+            defer.returnValue({})
+
         state_sets = []
         state_groups = set()
         missing_event_ids = []
@@ -537,6 +541,9 @@ class EventsStore(SQLBaseStore):
                 was_updated = True
                 missing_event_ids.append(event_id)
 
+        if not was_updated:
+            return
+
         if missing_event_ids:
             # Now pull out the state for any missing events from DB
             event_to_groups = yield self._get_state_group_for_events(
@@ -549,54 +556,49 @@ class EventsStore(SQLBaseStore):
                 group_to_state = yield self._get_state_for_groups(groups)
                 state_sets.extend(group_to_state.itervalues())
 
-        if not new_latest_event_ids:
-            defer.returnValue({})
-        elif was_updated:
-            if len(state_sets) == 1:
-                # If there is only one state set, then we know what the current
-                # state is.
-                defer.returnValue(state_sets[0])
-            else:
-                # We work out the current state by passing the state sets to the
-                # state resolution algorithm. It may ask for some events, including
-                # the events we have yet to persist, so we need a slightly more
-                # complicated event lookup function than simply looking the events
-                # up in the db.
-
-                logger.info(
-                    "Resolving state with %i state sets", len(state_sets),
-                )
+        if len(state_sets) == 1:
+            # If there is only one state set, then we know what the current
+            # state is.
+            defer.returnValue(state_sets[0])
 
-                events_map = {ev.event_id: ev for ev, _ in events_context}
-
-                @defer.inlineCallbacks
-                def get_events(ev_ids):
-                    # We get the events by first looking at the list of events we
-                    # are trying to persist, and then fetching the rest from the DB.
-                    db = []
-                    to_return = {}
-                    for ev_id in ev_ids:
-                        ev = events_map.get(ev_id, None)
-                        if ev:
-                            to_return[ev_id] = ev
-                        else:
-                            db.append(ev_id)
+        # We work out the current state by passing the state sets to the
+        # state resolution algorithm. It may ask for some events, including
+        # the events we have yet to persist, so we need a slightly more
+        # complicated event lookup function than simply looking the events
+        # up in the db.
 
-                    if db:
-                        evs = yield self.get_events(
-                            ev_ids, get_prev_content=False, check_redacted=False,
-                        )
-                        to_return.update(evs)
-                    defer.returnValue(to_return)
+        logger.info(
+            "Resolving state with %i state sets", len(state_sets),
+        )
 
-                current_state = yield resolve_events_with_factory(
-                    state_sets,
-                    event_map={},
-                    state_map_factory=get_events,
+        events_map = {ev.event_id: ev for ev, _ in events_context}
+
+        @defer.inlineCallbacks
+        def get_events(ev_ids):
+            # We get the events by first looking at the list of events we
+            # are trying to persist, and then fetching the rest from the DB.
+            db = []
+            to_return = {}
+            for ev_id in ev_ids:
+                ev = events_map.get(ev_id, None)
+                if ev:
+                    to_return[ev_id] = ev
+                else:
+                    db.append(ev_id)
+
+            if db:
+                evs = yield self.get_events(
+                    ev_ids, get_prev_content=False, check_redacted=False,
                 )
-                defer.returnValue(current_state)
-        else:
-            return
+                to_return.update(evs)
+            defer.returnValue(to_return)
+
+        current_state = yield resolve_events_with_factory(
+            state_sets,
+            event_map={},
+            state_map_factory=get_events,
+        )
+        defer.returnValue(current_state)
 
     @defer.inlineCallbacks
     def _calculate_state_delta(self, room_id, current_state):