summary refs log tree commit diff
path: root/synapse/storage/stream.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2014-08-15 15:53:06 +0100
committerErik Johnston <erik@matrix.org>2014-08-15 15:53:06 +0100
commit8d1f7632095b949d7726dd72fce10224764f3c11 (patch)
treef8ff59114b36c711e51466e431526546a6296c7b /synapse/storage/stream.py
parentCorrectly return new token when returning events. Serialize events correctly. (diff)
downloadsynapse-8d1f7632095b949d7726dd72fce10224764f3c11.tar.xz
Fix pagination to work with new db schema
Diffstat (limited to 'synapse/storage/stream.py')
-rw-r--r--synapse/storage/stream.py31
1 files changed, 24 insertions, 7 deletions
diff --git a/synapse/storage/stream.py b/synapse/storage/stream.py
index 1300aee8b0..6bfa00d59a 100644
--- a/synapse/storage/stream.py
+++ b/synapse/storage/stream.py
@@ -53,18 +53,35 @@ class StreamStore(SQLBaseStore):
         else:
             limit = 1000
 
+        # From and to keys should be integers from ordering.
+        from_key = int(from_key)
+        to_key = int(to_key)
+
+        if from_key == to_key:
+            defer.returnValue(([], to_key))
+            return
+
+
         sql = (
             "SELECT * FROM events as e WHERE "
             "((room_id IN (%(current)s)) OR "
             "(event_id IN (%(invites)s))) "
-            " AND e.ordering > ? AND e.ordering < ? "
-            "ORDER BY ordering ASC LIMIT %(limit)d"
         ) % {
             "current": current_room_membership_sql,
             "invites": invites_sql,
-            "limit": limit,
         }
 
+        if from_key < to_key:
+            sql += (
+                "AND e.ordering > ? AND e.ordering < ? "
+                "ORDER BY ordering ASC LIMIT %(limit)d "
+            ) % {"limit": limit}
+        else:
+            sql += (
+                "AND e.ordering < ? AND e.ordering > ? "
+                "ORDER BY ordering DESC LIMIT %(limit)d "
+            ) % {"limit": int(limit)}
+
         rows = yield self._execute_and_decode(
             sql,
             user_id, user_id, Membership.INVITE, from_key, to_key
@@ -72,12 +89,12 @@ class StreamStore(SQLBaseStore):
 
         ret = [self._parse_event_from_row(r) for r in rows]
 
-        if ret:
-            max_id = max([r["ordering"] for r in rows])
+        if from_key < to_key:
+            key = max([r["ordering"] for r in rows])
         else:
-            max_id = to_key
+            key = min([r["ordering"] for r in rows])
 
-        defer.returnValue((ret, max_id))
+        defer.returnValue((ret, key))
 
     @defer.inlineCallbacks
     def get_recent_events_for_room(self, room_id, limit, with_feedback=False):