Rename and comment tree_to_leaves_iterator

This commit is contained in:
Erik Johnston 2017-01-17 11:44:57 +00:00
parent 1ccd5676e3
commit d6c75cb7c2
2 changed files with 8 additions and 5 deletions

View file

@ -17,7 +17,7 @@ import logging
from synapse.util.async import ObservableDeferred from synapse.util.async import ObservableDeferred
from synapse.util import unwrapFirstError from synapse.util import unwrapFirstError
from synapse.util.caches.lrucache import LruCache from synapse.util.caches.lrucache import LruCache
from synapse.util.caches.treecache import TreeCache, popped_to_iterator from synapse.util.caches.treecache import TreeCache, iterate_tree_cache_entry
from synapse.util.logcontext import ( from synapse.util.logcontext import (
PreserveLoggingContext, preserve_context_over_deferred, preserve_context_over_fn PreserveLoggingContext, preserve_context_over_deferred, preserve_context_over_fn
) )
@ -181,7 +181,7 @@ class Cache(object):
val = self._pending_deferred_cache.pop(key, None) val = self._pending_deferred_cache.pop(key, None)
if val is not None: if val is not None:
entry_dict, _ = val entry_dict, _ = val
for entry in popped_to_iterator(entry_dict): for entry in iterate_tree_cache_entry(entry_dict):
entry.invalidate() entry.invalidate()
def invalidate_all(self): def invalidate_all(self):

View file

@ -65,16 +65,19 @@ class TreeCache(object):
return popped return popped
def values(self): def values(self):
return list(popped_to_iterator(self.root)) return list(iterate_tree_cache_entry(self.root))
def __len__(self): def __len__(self):
return self.size return self.size
def popped_to_iterator(d): 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.
"""
if isinstance(d, dict): if isinstance(d, dict):
for value_d in d.itervalues(): for value_d in d.itervalues():
for value in popped_to_iterator(value_d): for value in iterate_tree_cache_entry(value_d):
yield value yield value
else: else:
if isinstance(d, _Entry): if isinstance(d, _Entry):