2019-10-02 06:29:01 -06:00
|
|
|
from typing import Dict
|
|
|
|
|
2018-05-31 03:03:47 -06:00
|
|
|
from six import itervalues
|
|
|
|
|
2016-01-21 12:16:25 -07:00
|
|
|
SENTINEL = object()
|
|
|
|
|
|
|
|
|
|
|
|
class TreeCache(object):
|
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.
|
|
|
|
"""
|
2019-06-20 03:32:02 -06:00
|
|
|
|
2016-01-21 12:16:25 -07:00
|
|
|
def __init__(self):
|
2016-01-29 03:11:21 -07:00
|
|
|
self.size = 0
|
2019-10-02 06:29:01 -06:00
|
|
|
self.root = {} # type: Dict
|
2016-01-21 12:16:25 -07:00
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
return self.set(key, value)
|
|
|
|
|
2016-01-22 04:49:59 -07:00
|
|
|
def __contains__(self, key):
|
|
|
|
return self.get(key, SENTINEL) is not SENTINEL
|
|
|
|
|
2016-01-21 12:16:25 -07:00
|
|
|
def set(self, key, value):
|
|
|
|
node = self.root
|
|
|
|
for k in key[:-1]:
|
|
|
|
node = node.setdefault(k, {})
|
2016-01-29 03:44:46 -07:00
|
|
|
node[key[-1]] = _Entry(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
|
2016-01-29 03:44:46 -07:00
|
|
|
return node.get(key[-1], _Entry(default)).value
|
2016-01-21 12:16:25 -07:00
|
|
|
|
|
|
|
def clear(self):
|
2016-01-29 03:11:21 -07:00
|
|
|
self.size = 0
|
2016-01-21 12:16:25 -07:00
|
|
|
self.root = {}
|
|
|
|
|
|
|
|
def pop(self, key, default=None):
|
|
|
|
nodes = []
|
|
|
|
|
|
|
|
node = self.root
|
|
|
|
for k in key[:-1]:
|
|
|
|
node = node.get(k, None)
|
|
|
|
nodes.append(node) # don't add the root node
|
|
|
|
if node is None:
|
|
|
|
return default
|
|
|
|
popped = node.pop(key[-1], SENTINEL)
|
|
|
|
if popped is SENTINEL:
|
|
|
|
return default
|
|
|
|
|
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
|
2016-02-02 10:18:50 -07:00
|
|
|
node_and_keys[i + 1][0].pop(k)
|
2016-01-21 12:16:25 -07:00
|
|
|
|
2016-01-29 03:44:46 -07:00
|
|
|
popped, cnt = _strip_and_count_entires(popped)
|
|
|
|
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):
|
2017-01-17 04:44:57 -07:00
|
|
|
return list(iterate_tree_cache_entry(self.root))
|
2016-08-19 04:18:26 -06:00
|
|
|
|
2016-01-29 03:11:21 -07:00
|
|
|
def __len__(self):
|
|
|
|
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.
|
|
|
|
"""
|
2017-01-17 04:18:13 -07:00
|
|
|
if isinstance(d, dict):
|
2018-05-31 03:03:47 -06:00
|
|
|
for value_d in itervalues(d):
|
2017-01-17 04:44:57 -07:00
|
|
|
for value in iterate_tree_cache_entry(value_d):
|
2017-01-17 04:18:13 -07:00
|
|
|
yield value
|
|
|
|
else:
|
|
|
|
if isinstance(d, _Entry):
|
|
|
|
yield d.value
|
|
|
|
else:
|
|
|
|
yield d
|
|
|
|
|
|
|
|
|
2016-01-29 03:44:46 -07:00
|
|
|
class _Entry(object):
|
|
|
|
__slots__ = ["value"]
|
|
|
|
|
|
|
|
def __init__(self, value):
|
2016-01-29 04:29:14 -07:00
|
|
|
self.value = value
|
2016-01-29 03:44:46 -07:00
|
|
|
|
|
|
|
|
|
|
|
def _strip_and_count_entires(d):
|
|
|
|
"""Takes an _Entry or dict with leaves of _Entry's, and either returns the
|
|
|
|
value or a dictionary with _Entry's replaced by their values.
|
|
|
|
|
|
|
|
Also returns the count of _Entry's
|
|
|
|
"""
|
|
|
|
if isinstance(d, dict):
|
|
|
|
cnt = 0
|
|
|
|
for key, value in d.items():
|
|
|
|
v, n = _strip_and_count_entires(value)
|
|
|
|
d[key] = v
|
|
|
|
cnt += n
|
|
|
|
return d, cnt
|
|
|
|
else:
|
|
|
|
return d.value, 1
|