summary refs log tree commit diff
path: root/synapse/util
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2016-01-28 16:37:41 +0000
committerErik Johnston <erik@matrix.org>2016-01-28 16:37:41 +0000
commit00cb3eb24b277bb37bd1b7d8449c08a37cb4b014 (patch)
tree9f22463af491df10fa08fe41281c8228e1d7af65 /synapse/util
parentEnsure keys to RoomStreamChangeCache are ints (diff)
downloadsynapse-00cb3eb24b277bb37bd1b7d8449c08a37cb4b014.tar.xz
Cache tags and account data
Diffstat (limited to 'synapse/util')
-rw-r--r--synapse/util/caches/room_change_cache.py92
-rw-r--r--synapse/util/caches/stream_change_cache.py95
2 files changed, 95 insertions, 92 deletions
diff --git a/synapse/util/caches/room_change_cache.py b/synapse/util/caches/room_change_cache.py
deleted file mode 100644
index e8bfedd72f..0000000000
--- a/synapse/util/caches/room_change_cache.py
+++ /dev/null
@@ -1,92 +0,0 @@
-# -*- coding: utf-8 -*-
-# Copyright 2016 OpenMarket 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.util.caches import cache_counter, caches_by_name
-
-
-from blist import sorteddict
-import logging
-
-
-logger = logging.getLogger(__name__)
-
-
-class RoomStreamChangeCache(object):
-    """Keeps track of the stream_id of the latest change in rooms.
-
-    Given a list of rooms and stream key, it will give a subset of rooms that
-    may have changed since that key. If the key is too old then the cache
-    will simply return all rooms.
-    """
-    def __init__(self, name, current_key, size_of_cache=10000):
-        self._size_of_cache = size_of_cache
-        self._room_to_key = {}
-        self._cache = sorteddict()
-        self._earliest_known_key = current_key
-        self.name = name
-        caches_by_name[self.name] = self._cache
-
-    def get_room_has_changed(self, room_id, key):
-        assert type(key) is int
-
-        if key <= self._earliest_known_key:
-            return True
-
-        room_key = self._room_to_key.get(room_id, None)
-        if room_key is None:
-            return True
-
-        if key < room_key:
-            return True
-
-        return False
-
-    def get_rooms_changed(self, room_ids, key):
-        """Returns subset of room ids that have had new things since the
-        given key. If the key is too old it will just return the given list.
-        """
-        assert type(key) is int
-
-        if key > self._earliest_known_key:
-            keys = self._cache.keys()
-            i = keys.bisect_right(key)
-
-            result = set(
-                self._cache[k] for k in keys[i:]
-            ).intersection(room_ids)
-
-            cache_counter.inc_hits(self.name)
-        else:
-            result = room_ids
-            cache_counter.inc_misses(self.name)
-
-        return result
-
-    def room_has_changed(self, room_id, key):
-        """Informs the cache that the room has been changed at the given key.
-        """
-        assert type(key) is int
-
-        if key > self._earliest_known_key:
-            old_key = self._room_to_key.get(room_id, None)
-            if old_key:
-                key = max(key, old_key)
-                self._cache.pop(old_key, None)
-            self._cache[key] = room_id
-
-            while len(self._cache) > self._size_of_cache:
-                k, r = self._cache.popitem()
-                self._earliest_key = max(k, self._earliest_key)
-                self._room_to_key.pop(r, None)
diff --git a/synapse/util/caches/stream_change_cache.py b/synapse/util/caches/stream_change_cache.py
new file mode 100644
index 0000000000..33b37f7f29
--- /dev/null
+++ b/synapse/util/caches/stream_change_cache.py
@@ -0,0 +1,95 @@
+# -*- coding: utf-8 -*-
+# Copyright 2016 OpenMarket 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.util.caches import cache_counter, caches_by_name
+
+
+from blist import sorteddict
+import logging
+
+
+logger = logging.getLogger(__name__)
+
+
+class StreamChangeCache(object):
+    """Keeps track of the stream positions of the latest change in a set of entities.
+
+    Typically the entity will be a room or user id.
+
+    Given a list of entities and a stream position, it will give a subset of
+    entities that may have changed since that position. If position key is too
+    old then the cache will simply return all given entities.
+    """
+    def __init__(self, name, current_stream_pos, max_size=10000):
+        self._max_size = max_size
+        self._entity_to_key = {}
+        self._cache = sorteddict()
+        self._earliest_known_stream_pos = current_stream_pos
+        self.name = name
+        caches_by_name[self.name] = self._cache
+
+    def get_entity_has_changed(self, entity, stream_pos):
+        assert type(stream_pos) is int
+
+        if stream_pos <= self._earliest_known_stream_pos:
+            return True
+
+        latest_entity_change_pos = self._entity_to_key.get(entity, None)
+        if latest_entity_change_pos is None:
+            return True
+
+        if stream_pos < latest_entity_change_pos:
+            return True
+
+        return False
+
+    def get_entities_changed(self, entities, stream_pos):
+        """Returns subset of entities that have had new things since the
+        given position. If the position is too old it will just return the given list.
+        """
+        assert type(stream_pos) is int
+
+        if stream_pos > self._earliest_known_stream_pos:
+            keys = self._cache.keys()
+            i = keys.bisect_right(stream_pos)
+
+            result = set(
+                self._cache[k] for k in keys[i:]
+            ).intersection(entities)
+
+            cache_counter.inc_hits(self.name)
+        else:
+            result = entities
+            cache_counter.inc_misses(self.name)
+
+        return result
+
+    def entity_has_changed(self, entitiy, stream_pos):
+        """Informs the cache that the entitiy has been changed at the given
+        position.
+        """
+        assert type(stream_pos) is int
+
+        if stream_pos > self._earliest_known_stream_pos:
+            old_pos = self._entity_to_key.get(entitiy, None)
+            if old_pos:
+                stream_pos = max(stream_pos, old_pos)
+                self._cache.pop(old_pos, None)
+            self._cache[stream_pos] = entitiy
+
+            while len(self._cache) > self._max_size:
+                k, r = self._cache.popitem()
+                self._earliest_known_stream_pos = max(k, self._earliest_known_stream_pos)
+                self._entity_to_key.pop(r, None)