366
366
self._keys_by_offset = {}
367
367
# ready-to-return key:value or key:value, node_ref_lists
369
self._nodes_by_key = {}
369
self._nodes_by_key = None
371
371
pos = stream.tell()
372
372
lines = stream.read().split('\n')
382
382
node_value = value
383
383
self._nodes[key] = node_value
384
if self._key_length > 1:
385
# TODO: We may want to do this lazily, but if we are calling
386
# _buffer_all, we are likely to be doing
387
# iter_entries_prefix
388
key_dict = self._nodes_by_key
389
if self.node_ref_lists:
390
key_value = key, node_value[0], node_value[1]
392
key_value = key, node_value
393
# For a key of (foo, bar, baz) create
394
# _nodes_by_key[foo][bar][baz] = key_value
395
for subkey in key[:-1]:
396
key_dict = key_dict.setdefault(subkey, {})
397
key_dict[key[-1]] = key_value
398
384
# cache the keys for quick set intersections
399
385
self._keys = set(self._nodes)
400
386
if trailers != 1:
401
387
# there must be one line - the empty trailer line.
402
388
raise errors.BadIndexData(self)
390
def _get_nodes_by_key(self):
391
if self._nodes_by_key is None:
393
if self.node_ref_lists:
394
for key, (value, references) in self._nodes.iteritems():
395
key_dict = nodes_by_key
396
for subkey in key[:-1]:
397
key_dict = key_dict.setdefault(subkey, {})
398
key_dict[key[-1]] = key, value, references
400
for key, value in self._nodes.iteritems():
401
key_dict = nodes_by_key
402
for subkey in key[:-1]:
403
key_dict = key_dict.setdefault(subkey, {})
404
key_dict[key[-1]] = key, value
405
self._nodes_by_key = nodes_by_key
406
return self._nodes_by_key
404
408
def iter_all_entries(self):
405
409
"""Iterate over all keys within the index.
600
605
if len(key) != self._key_length:
601
606
raise errors.BadIndexKey(key)
602
607
# find what it refers to:
603
key_dict = self._nodes_by_key
608
key_dict = nodes_by_key
604
609
elements = list(key)
605
610
# find the subdict whose contents should be returned.
973
978
raise errors.BadIndexData(self)
974
979
# keys are tuples. Each element is a string that may occur many
975
980
# times, so we intern them to save space. AB, RC, 200807
976
key = tuple(intern(element) for element in elements[:self._key_length])
981
key = tuple([intern(element) for element in elements[:self._key_length]])
977
982
if first_key is None:
979
984
absent, references, value = elements[-3:]