summary refs log tree commit diff
path: root/synapse/storage/_base.py
diff options
context:
space:
mode:
authorDavid Baker <dave@matrix.org>2016-04-07 16:35:22 +0100
committerDavid Baker <dave@matrix.org>2016-04-07 16:35:22 +0100
commit9c99ab4572623a8a9554e6c0292f34059252c6b7 (patch)
tree3b231aab16688d64bc6e30561ddd42949c2ab727 /synapse/storage/_base.py
parentRemove code that's now been obsoleted or moved elsewhere (diff)
parentMerge pull request #703 from matrix-org/erikj/member (diff)
downloadsynapse-9c99ab4572623a8a9554e6c0292f34059252c6b7.tar.xz
Merge remote-tracking branch 'origin/develop' into dbkr/pushers_use_event_actions
Diffstat (limited to 'synapse/storage/_base.py')
-rw-r--r--synapse/storage/_base.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/synapse/storage/_base.py b/synapse/storage/_base.py
index b75b79df36..04d7fcf6d6 100644
--- a/synapse/storage/_base.py
+++ b/synapse/storage/_base.py
@@ -816,6 +816,40 @@ class SQLBaseStore(object):
             self._next_stream_id += 1
             return i
 
+    def _get_cache_dict(self, db_conn, table, entity_column, stream_column,
+                        max_value):
+        # Fetch a mapping of room_id -> max stream position for "recent" rooms.
+        # It doesn't really matter how many we get, the StreamChangeCache will
+        # do the right thing to ensure it respects the max size of cache.
+        sql = (
+            "SELECT %(entity)s, MAX(%(stream)s) FROM %(table)s"
+            " WHERE %(stream)s > ? - 100000"
+            " GROUP BY %(entity)s"
+        ) % {
+            "table": table,
+            "entity": entity_column,
+            "stream": stream_column,
+        }
+
+        sql = self.database_engine.convert_param_style(sql)
+
+        txn = db_conn.cursor()
+        txn.execute(sql, (int(max_value),))
+        rows = txn.fetchall()
+        txn.close()
+
+        cache = {
+            row[0]: int(row[1])
+            for row in rows
+        }
+
+        if cache:
+            min_val = min(cache.values())
+        else:
+            min_val = max_value
+
+        return cache, min_val
+
 
 class _RollbackButIsFineException(Exception):
     """ This exception is used to rollback a transaction without implying