2021-05-24 07:02:01 -06:00
|
|
|
# Copyright 2016-2021 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# 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.
|
2019-10-02 06:29:01 -06:00
|
|
|
|
2016-01-21 12:16:25 -07:00
|
|
|
SENTINEL = object()
|
|
|
|
|
|
|
|
|
2021-05-24 07:02:01 -06:00
|
|
|
class TreeCacheNode(dict):
|
|
|
|
"""The type of nodes in our tree.
|
|
|
|
|
|
|
|
Has its own type so we can distinguish it from real dicts that are stored at the
|
|
|
|
leaves.
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-09-04 04:54:56 -06:00
|
|
|
class TreeCache:
|
2016-01-22 04:47:22 -07:00
|
|
|
"""
|
|
|
|
Tree-based backing store for LruCache. Allows subtrees of data to be deleted
|
|
|
|
efficiently.
|
|
|
|
Keys must be tuples.
|
2021-05-24 07:02:01 -06:00
|
|
|
|
|
|
|
The data structure is a chain of TreeCacheNodes:
|
|
|
|
root = {key_1: {key_2: _value}}
|
2016-01-22 04:47:22 -07:00
|
|
|
"""
|
2019-06-20 03:32:02 -06:00
|
|
|
|
2021-09-10 10:03:18 -06:00
|
|
|
def __init__(self) -> None:
|
|
|
|
self.size: int = 0
|
2021-05-24 07:02:01 -06:00
|
|
|
self.root = TreeCacheNode()
|
2016-01-21 12:16:25 -07:00
|
|
|
|
2021-09-10 10:03:18 -06:00
|
|
|
def __setitem__(self, key, value) -> None:
|
|
|
|
self.set(key, value)
|
2016-01-21 12:16:25 -07:00
|
|
|
|
2021-09-10 10:03:18 -06:00
|
|
|
def __contains__(self, key) -> bool:
|
2016-01-22 04:49:59 -07:00
|
|
|
return self.get(key, SENTINEL) is not SENTINEL
|
|
|
|
|
2021-09-10 10:03:18 -06:00
|
|
|
def set(self, key, value) -> None:
|
2021-05-24 07:02:01 -06:00
|
|
|
if isinstance(value, TreeCacheNode):
|
|
|
|
# this would mean we couldn't tell where our tree ended and the value
|
|
|
|
# started.
|
|
|
|
raise ValueError("Cannot store TreeCacheNodes in a TreeCache")
|
|
|
|
|
2016-01-21 12:16:25 -07:00
|
|
|
node = self.root
|
|
|
|
for k in key[:-1]:
|
2021-05-24 07:02:01 -06:00
|
|
|
next_node = node.get(k, SENTINEL)
|
|
|
|
if next_node is SENTINEL:
|
|
|
|
next_node = node[k] = TreeCacheNode()
|
|
|
|
elif not isinstance(next_node, TreeCacheNode):
|
|
|
|
# this suggests that the caller is not being consistent with its key
|
|
|
|
# length.
|
|
|
|
raise ValueError("value conflicts with an existing subtree")
|
|
|
|
node = next_node
|
|
|
|
|
|
|
|
node[key[-1]] = value
|
2016-01-29 03:11:21 -07:00
|
|
|
self.size += 1
|
2016-01-21 12:16:25 -07:00
|
|
|
|
|
|
|
def get(self, key, default=None):
|
|
|
|
node = self.root
|
|
|
|
for k in key[:-1]:
|
|
|
|
node = node.get(k, None)
|
|
|
|
if node is None:
|
|
|
|
return default
|
2021-05-24 07:02:01 -06:00
|
|
|
return node.get(key[-1], default)
|
2016-01-21 12:16:25 -07:00
|
|
|
|
2021-09-10 10:03:18 -06:00
|
|
|
def clear(self) -> None:
|
2016-01-29 03:11:21 -07:00
|
|
|
self.size = 0
|
2021-05-24 07:02:01 -06:00
|
|
|
self.root = TreeCacheNode()
|
2016-01-21 12:16:25 -07:00
|
|
|
|
|
|
|
def pop(self, key, default=None):
|
2021-05-24 07:02:01 -06:00
|
|
|
"""Remove the given key, or subkey, from the cache
|
|
|
|
|
|
|
|
Args:
|
|
|
|
key: key or subkey to remove.
|
|
|
|
default: value to return if key is not found
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
If the key is not found, 'default'. If the key is complete, the removed
|
|
|
|
value. If the key is partial, the TreeCacheNode corresponding to the part
|
|
|
|
of the tree that was removed.
|
|
|
|
"""
|
2021-05-27 03:33:56 -06:00
|
|
|
if not isinstance(key, tuple):
|
|
|
|
raise TypeError("The cache key must be a tuple not %r" % (type(key),))
|
|
|
|
|
2021-05-24 07:02:01 -06:00
|
|
|
# a list of the nodes we have touched on the way down the tree
|
2016-01-21 12:16:25 -07:00
|
|
|
nodes = []
|
|
|
|
|
|
|
|
node = self.root
|
|
|
|
for k in key[:-1]:
|
|
|
|
node = node.get(k, None)
|
|
|
|
if node is None:
|
|
|
|
return default
|
2021-05-24 07:02:01 -06:00
|
|
|
if not isinstance(node, TreeCacheNode):
|
|
|
|
# we've gone off the end of the tree
|
|
|
|
raise ValueError("pop() key too long")
|
|
|
|
nodes.append(node) # don't add the root node
|
2016-01-21 12:16:25 -07:00
|
|
|
popped = node.pop(key[-1], SENTINEL)
|
|
|
|
if popped is SENTINEL:
|
|
|
|
return default
|
|
|
|
|
2021-05-24 07:02:01 -06:00
|
|
|
# working back up the tree, clear out any nodes that are now empty
|
2018-05-31 03:03:47 -06:00
|
|
|
node_and_keys = list(zip(nodes, key))
|
2016-01-21 12:16:25 -07:00
|
|
|
node_and_keys.reverse()
|
|
|
|
node_and_keys.append((self.root, None))
|
|
|
|
|
|
|
|
for i in range(len(node_and_keys) - 1):
|
2016-01-21 12:17:32 -07:00
|
|
|
n, k = node_and_keys[i]
|
2016-01-21 12:16:25 -07:00
|
|
|
|
|
|
|
if n:
|
|
|
|
break
|
2021-05-24 07:02:01 -06:00
|
|
|
# found an empty node: remove it from its parent, and loop.
|
2016-02-02 10:18:50 -07:00
|
|
|
node_and_keys[i + 1][0].pop(k)
|
2016-01-21 12:16:25 -07:00
|
|
|
|
2021-05-24 07:02:01 -06:00
|
|
|
cnt = sum(1 for _ in iterate_tree_cache_entry(popped))
|
2016-01-29 03:44:46 -07:00
|
|
|
self.size -= cnt
|
2016-01-21 12:17:32 -07:00
|
|
|
return popped
|
2016-01-29 03:11:21 -07:00
|
|
|
|
2016-08-19 04:18:26 -06:00
|
|
|
def values(self):
|
2021-05-24 07:02:01 -06:00
|
|
|
return iterate_tree_cache_entry(self.root)
|
2016-08-19 04:18:26 -06:00
|
|
|
|
2021-09-10 10:03:18 -06:00
|
|
|
def __len__(self) -> int:
|
2016-01-29 03:11:21 -07:00
|
|
|
return self.size
|
2016-01-29 03:44:46 -07:00
|
|
|
|
|
|
|
|
2017-01-17 04:44:57 -07:00
|
|
|
def iterate_tree_cache_entry(d):
|
|
|
|
"""Helper function to iterate over the leaves of a tree, i.e. a dict of that
|
|
|
|
can contain dicts.
|
|
|
|
"""
|
2021-05-24 07:02:01 -06:00
|
|
|
if isinstance(d, TreeCacheNode):
|
2020-06-15 05:03:36 -06:00
|
|
|
for value_d in d.values():
|
2021-07-19 08:28:05 -06:00
|
|
|
yield from iterate_tree_cache_entry(value_d)
|
2017-01-17 04:18:13 -07:00
|
|
|
else:
|
2021-05-24 07:02:01 -06:00
|
|
|
yield d
|