103
104
if key in self._nodes and self._nodes[key][0] == '':
104
105
raise errors.BadIndexDuplicateKey(key, self)
105
106
self._nodes[key] = ('', tuple(node_refs), value)
107
if self._key_length > 1:
108
key_dict = self._nodes_by_key
109
if self.reference_lists:
110
key_value = key, value, tuple(node_refs)
112
key_value = key, value
113
# possibly should do this on-demand, but it seems likely it is
115
subkey = list(reversed(key[:-1]))
117
if subkey[-1] not in key_dict:
118
key_dict[subkey[-1]] = {}
119
key_dict = key_dict[subkey[-1]]
121
key_dict[key[-1]] = key_value
107
123
def finish(self):
108
124
lines = [_SIGNATURE]
581
597
yield key, node[2]
599
def iter_entries_prefix(self, keys):
600
"""Iterate over keys within the index using prefix matching.
602
Prefix matching is applied within the tuple of a key, not to within
603
the bytestring of each key element. e.g. if you have the keys ('foo',
604
'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
605
only the former key is returned.
607
:param keys: An iterable providing the key prefixes to be retrieved.
608
Each key prefix takes the form of a tuple the length of a key, but
609
with the last N elements 'None' rather than a regular bytestring.
610
The first element cannot be 'None'.
611
:return: An iterable as per iter_all_entries, but restricted to the
612
keys with a matching prefix to those supplied. No additional keys
613
will be returned, and every match that is in the index will be
616
# XXX: To much duplication with the GraphIndex class; consider finding
617
# a good place to pull out the actual common logic.
621
if self._key_length == 1:
625
raise errors.BadIndexKey(key)
626
if len(key) != self._key_length:
627
raise errors.BadIndexKey(key)
628
node = self._nodes[key]
631
if self.reference_lists:
632
yield key, node[2], node[1]
639
raise errors.BadIndexKey(key)
640
if len(key) != self._key_length:
641
raise errors.BadIndexKey(key)
642
# find what it refers to:
643
key_dict = self._nodes_by_key
645
# find the subdict to return
647
while len(elements) and elements[0] is not None:
648
key_dict = key_dict[elements[0]]
651
# a non-existant lookup.
656
key_dict = dicts.pop(-1)
657
# can't be empty or would not exist
658
item, value = key_dict.iteritems().next()
659
if type(value) == dict:
661
dicts.extend(key_dict.itervalues())
664
for value in key_dict.itervalues():
583
669
def validate(self):
584
670
"""In memory index's have no known corruption at the moment."""