121
121
self._get_parent_map = self._real_provider.get_parent_map
123
123
self._get_parent_map = get_parent_map
125
self._cache_misses = True
125
self.enable_cache(True)
127
127
def __repr__(self):
128
128
return "%s(%r)" % (self.__class__.__name__, self._real_provider)
133
133
raise AssertionError('Cache enabled when already enabled.')
135
135
self._cache_misses = cache_misses
136
self._missing_keys = set()
137
138
def disable_cache(self):
138
139
"""Disable and clear the cache."""
139
140
self._cache = None
141
self._cache_misses = None
142
self._missing_keys = set()
141
144
def get_cached_map(self):
142
145
"""Return any cached get_parent_map values."""
143
146
if self._cache is None:
145
return dict((k, v) for k, v in self._cache.items()
148
return dict(self._cache)
148
150
def get_parent_map(self, keys):
149
151
"""See _StackedParentsProvider.get_parent_map."""
150
# Hack to build up the caching logic.
151
ancestry = self._cache
153
# Caching is disabled.
154
missing_revisions = set(keys)
154
cache = self._get_parent_map(keys)
157
missing_revisions = set(key for key in keys if key not in ancestry)
158
if missing_revisions:
159
parent_map = self._get_parent_map(missing_revisions)
160
ancestry.update(parent_map)
161
if self._cache_misses:
162
# None is never a valid parents list, so it can be used to
164
ancestry.update(dict((k, None) for k in missing_revisions
165
if k not in parent_map))
166
present_keys = [k for k in keys if ancestry.get(k) is not None]
167
return dict((k, ancestry[k]) for k in present_keys)
156
# XXX: Full scan of cache, keeping a set of cached keys will scale
158
needed_revisions = set(key for key in keys if key not in cache)
159
# Do not ask for negatively cached keys
160
needed_revisions.difference_update(self._missing_keys)
162
parent_map = self._get_parent_map(needed_revisions)
163
cache.update(parent_map)
164
if self._cache_misses:
165
for key in needed_revisions:
166
if key not in parent_map:
167
self._missing_keys.add(key)
170
value = cache.get(key)
171
if value is not None:
170
176
class Graph(object):