summary refs log tree commit diff
path: root/synapse/storage
diff options
context:
space:
mode:
authorLuke Barnard <lukeb@openmarket.com>2017-04-11 17:34:09 +0100
committerLuke Barnard <lukeb@openmarket.com>2017-04-11 17:34:09 +0100
commit73880268ef7184d17ec369074d1d0d72de56f33c (patch)
treecdf1359a4c07f848c1a6edef94e8399f9ef4610d /synapse/storage
parentCopyright (diff)
downloadsynapse-73880268ef7184d17ec369074d1d0d72de56f33c.tar.xz
Refactor event ordering check to events store
Diffstat (limited to 'synapse/storage')
-rw-r--r--synapse/storage/events.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/synapse/storage/events.py b/synapse/storage/events.py
index 64fe937bdc..3c6df5c2d2 100644
--- a/synapse/storage/events.py
+++ b/synapse/storage/events.py
@@ -2159,6 +2159,34 @@ class EventsStore(SQLBaseStore):
             ]
         )
 
+    @defer.inlineCallbacks
+    def is_event_after(self, event_id1, event_id2):
+        is_after = True
+
+        to_1, so_1 = yield self._get_event_ordering(event_id1)
+        to_2, so_2 = yield self._get_event_ordering(event_id2)
+
+        # Prevent updating if the existing marker is ahead in the stream
+        if to_1 > to_2:
+            is_after = False
+        elif to_1 == to_2 and so_1 >= so_2:
+            is_after = False
+
+        defer.returnValue(is_after)
+
+    @defer.inlineCallbacks
+    def _get_event_ordering(self, event_id):
+        res = yield self._simple_select_one(
+            table="events",
+            retcols=["topological_ordering", "stream_ordering"],
+            keyvalues={"event_id": event_id},
+            allow_none=True
+        )
+
+        if not res:
+            raise SynapseError(404, "Could not find event %s" % (event_id,))
+
+        defer.returnValue((int(res["topological_ordering"]), int(res["stream_ordering"])))
 
 AllNewEventsResult = namedtuple("AllNewEventsResult", [
     "new_forward_events", "new_backfill_events",