/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/index.py

  • Committer: Robert Collins
  • Date: 2007-10-11 04:54:04 UTC
  • mfrom: (2904 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2933.
  • Revision ID: robertc@robertcollins.net-20071011045404-lj5a81n4ripi01mt
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Indexing facilities."""
 
18
 
 
19
__all__ = [
 
20
    'CombinedGraphIndex',
 
21
    'GraphIndex',
 
22
    'GraphIndexBuilder',
 
23
    'GraphIndexPrefixAdapter',
 
24
    'InMemoryGraphIndex',
 
25
    ]
 
26
 
 
27
from bisect import bisect_right
 
28
from cStringIO import StringIO
 
29
import re
 
30
 
 
31
from bzrlib.lazy_import import lazy_import
 
32
lazy_import(globals(), """
 
33
from bzrlib import trace
 
34
from bzrlib.bisect_multi import bisect_multi_bytes
 
35
from bzrlib.trace import mutter
 
36
""")
 
37
from bzrlib import debug, errors
 
38
 
 
39
_OPTION_KEY_ELEMENTS = "key_elements="
 
40
_OPTION_LEN = "len="
 
41
_OPTION_NODE_REFS = "node_ref_lists="
 
42
_SIGNATURE = "Bazaar Graph Index 1\n"
 
43
 
 
44
 
 
45
_whitespace_re = re.compile('[\t\n\x0b\x0c\r\x00 ]')
 
46
_newline_null_re = re.compile('[\n\0]')
 
47
 
 
48
 
 
49
class GraphIndexBuilder(object):
 
50
    """A builder that can build a GraphIndex.
 
51
    
 
52
    The resulting graph has the structure:
 
53
    
 
54
    _SIGNATURE OPTIONS NODES NEWLINE
 
55
    _SIGNATURE     := 'Bazaar Graph Index 1' NEWLINE
 
56
    OPTIONS        := 'node_ref_lists=' DIGITS NEWLINE
 
57
    NODES          := NODE*
 
58
    NODE           := KEY NULL ABSENT? NULL REFERENCES NULL VALUE NEWLINE
 
59
    KEY            := Not-whitespace-utf8
 
60
    ABSENT         := 'a'
 
61
    REFERENCES     := REFERENCE_LIST (TAB REFERENCE_LIST){node_ref_lists - 1}
 
62
    REFERENCE_LIST := (REFERENCE (CR REFERENCE)*)?
 
63
    REFERENCE      := DIGITS  ; digits is the byte offset in the index of the
 
64
                              ; referenced key.
 
65
    VALUE          := no-newline-no-null-bytes
 
66
    """
 
67
 
 
68
    def __init__(self, reference_lists=0, key_elements=1):
 
69
        """Create a GraphIndex builder.
 
70
 
 
71
        :param reference_lists: The number of node references lists for each
 
72
            entry.
 
73
        :param key_elements: The number of bytestrings in each key.
 
74
        """
 
75
        self.reference_lists = reference_lists
 
76
        self._keys = set()
 
77
        self._nodes = {}
 
78
        self._nodes_by_key = {}
 
79
        self._key_length = key_elements
 
80
 
 
81
    def _check_key(self, key):
 
82
        """Raise BadIndexKey if key is not a valid key for this index."""
 
83
        if type(key) != tuple:
 
84
            raise errors.BadIndexKey(key)
 
85
        if self._key_length != len(key):
 
86
            raise errors.BadIndexKey(key)
 
87
        for element in key:
 
88
            if not element or _whitespace_re.search(element) is not None:
 
89
                raise errors.BadIndexKey(element)
 
90
 
 
91
    def add_node(self, key, value, references=()):
 
92
        """Add a node to the index.
 
93
 
 
94
        :param key: The key. keys are non-empty tuples containing
 
95
            as many whitespace-free utf8 bytestrings as the key length
 
96
            defined for this index.
 
97
        :param references: An iterable of iterables of keys. Each is a
 
98
            reference to another key.
 
99
        :param value: The value to associate with the key. It may be any
 
100
            bytes as long as it does not contain \0 or \n.
 
101
        """
 
102
        self._check_key(key)
 
103
        if _newline_null_re.search(value) is not None:
 
104
            raise errors.BadIndexValue(value)
 
105
        if len(references) != self.reference_lists:
 
106
            raise errors.BadIndexValue(references)
 
107
        node_refs = []
 
108
        for reference_list in references:
 
109
            for reference in reference_list:
 
110
                self._check_key(reference)
 
111
                if reference not in self._nodes:
 
112
                    self._nodes[reference] = ('a', (), '')
 
113
            node_refs.append(tuple(reference_list))
 
114
        if key in self._nodes and self._nodes[key][0] == '':
 
115
            raise errors.BadIndexDuplicateKey(key, self)
 
116
        self._nodes[key] = ('', tuple(node_refs), value)
 
117
        self._keys.add(key)
 
118
        if self._key_length > 1:
 
119
            key_dict = self._nodes_by_key
 
120
            if self.reference_lists:
 
121
                key_value = key, value, tuple(node_refs)
 
122
            else:
 
123
                key_value = key, value
 
124
            # possibly should do this on-demand, but it seems likely it is 
 
125
            # always wanted
 
126
            # For a key of (foo, bar, baz) create
 
127
            # _nodes_by_key[foo][bar][baz] = key_value
 
128
            for subkey in key[:-1]:
 
129
                key_dict = key_dict.setdefault(subkey, {})
 
130
            key_dict[key[-1]] = key_value
 
131
 
 
132
    def finish(self):
 
133
        lines = [_SIGNATURE]
 
134
        lines.append(_OPTION_NODE_REFS + str(self.reference_lists) + '\n')
 
135
        lines.append(_OPTION_KEY_ELEMENTS + str(self._key_length) + '\n')
 
136
        lines.append(_OPTION_LEN + str(len(self._keys)) + '\n')
 
137
        prefix_length = sum(len(x) for x in lines)
 
138
        # references are byte offsets. To avoid having to do nasty
 
139
        # polynomial work to resolve offsets (references to later in the 
 
140
        # file cannot be determined until all the inbetween references have
 
141
        # been calculated too) we pad the offsets with 0's to make them be
 
142
        # of consistent length. Using binary offsets would break the trivial
 
143
        # file parsing.
 
144
        # to calculate the width of zero's needed we do three passes:
 
145
        # one to gather all the non-reference data and the number of references.
 
146
        # one to pad all the data with reference-length and determine entry
 
147
        # addresses.
 
148
        # One to serialise.
 
149
        
 
150
        # forward sorted by key. In future we may consider topological sorting,
 
151
        # at the cost of table scans for direct lookup, or a second index for
 
152
        # direct lookup
 
153
        nodes = sorted(self._nodes.items())
 
154
        # if we do not prepass, we don't know how long it will be up front.
 
155
        expected_bytes = None
 
156
        # we only need to pre-pass if we have reference lists at all.
 
157
        if self.reference_lists:
 
158
            key_offset_info = []
 
159
            non_ref_bytes = prefix_length
 
160
            total_references = 0
 
161
            # TODO use simple multiplication for the constants in this loop.
 
162
            for key, (absent, references, value) in nodes:
 
163
                # record the offset known *so far* for this key:
 
164
                # the non reference bytes to date, and the total references to
 
165
                # date - saves reaccumulating on the second pass
 
166
                key_offset_info.append((key, non_ref_bytes, total_references))
 
167
                # key is literal, value is literal, there are 3 null's, 1 NL
 
168
                # key is variable length tuple, \x00 between elements
 
169
                non_ref_bytes += sum(len(element) for element in key)
 
170
                if self._key_length > 1:
 
171
                    non_ref_bytes += self._key_length - 1
 
172
                # value is literal bytes, there are 3 null's, 1 NL.
 
173
                non_ref_bytes += len(value) + 3 + 1
 
174
                # one byte for absent if set.
 
175
                if absent:
 
176
                    non_ref_bytes += 1
 
177
                elif self.reference_lists:
 
178
                    # (ref_lists -1) tabs
 
179
                    non_ref_bytes += self.reference_lists - 1
 
180
                    # (ref-1 cr's per ref_list)
 
181
                    for ref_list in references:
 
182
                        # how many references across the whole file?
 
183
                        total_references += len(ref_list)
 
184
                        # accrue reference separators
 
185
                        if ref_list:
 
186
                            non_ref_bytes += len(ref_list) - 1
 
187
            # how many digits are needed to represent the total byte count?
 
188
            digits = 1
 
189
            possible_total_bytes = non_ref_bytes + total_references*digits
 
190
            while 10 ** digits < possible_total_bytes:
 
191
                digits += 1
 
192
                possible_total_bytes = non_ref_bytes + total_references*digits
 
193
            expected_bytes = possible_total_bytes + 1 # terminating newline
 
194
            # resolve key addresses.
 
195
            key_addresses = {}
 
196
            for key, non_ref_bytes, total_references in key_offset_info:
 
197
                key_addresses[key] = non_ref_bytes + total_references*digits
 
198
            # serialise
 
199
            format_string = '%%0%sd' % digits
 
200
        for key, (absent, references, value) in nodes:
 
201
            flattened_references = []
 
202
            for ref_list in references:
 
203
                ref_addresses = []
 
204
                for reference in ref_list:
 
205
                    ref_addresses.append(format_string % key_addresses[reference])
 
206
                flattened_references.append('\r'.join(ref_addresses))
 
207
            string_key = '\x00'.join(key)
 
208
            lines.append("%s\x00%s\x00%s\x00%s\n" % (string_key, absent,
 
209
                '\t'.join(flattened_references), value))
 
210
        lines.append('\n')
 
211
        result = StringIO(''.join(lines))
 
212
        if expected_bytes and len(result.getvalue()) != expected_bytes:
 
213
            raise errors.BzrError('Failed index creation. Internal error:'
 
214
                ' mismatched output length and expected length: %d %d' %
 
215
                (len(result.getvalue()), expected_bytes))
 
216
        return StringIO(''.join(lines))
 
217
 
 
218
 
 
219
class GraphIndex(object):
 
220
    """An index for data with embedded graphs.
 
221
 
 
222
    The index maps keys to a list of key reference lists, and a value.
 
223
    Each node has the same number of key reference lists. Each key reference
 
224
    list can be empty or an arbitrary length. The value is an opaque NULL
 
225
    terminated string without any newlines. The storage of the index is 
 
226
    hidden in the interface: keys and key references are always tuples of
 
227
    bytestrings, never the internal representation (e.g. dictionary offsets).
 
228
 
 
229
    It is presumed that the index will not be mutated - it is static data.
 
230
 
 
231
    Successive iter_all_entries calls will read the entire index each time.
 
232
    Additionally, iter_entries calls will read the index linearly until the
 
233
    desired keys are found. XXX: This must be fixed before the index is
 
234
    suitable for production use. :XXX
 
235
    """
 
236
 
 
237
    def __init__(self, transport, name, size):
 
238
        """Open an index called name on transport.
 
239
 
 
240
        :param transport: A bzrlib.transport.Transport.
 
241
        :param name: A path to provide to transport API calls.
 
242
        :param size: The size of the index in bytes. This is used for bisection
 
243
            logic to perform partial index reads. While the size could be
 
244
            obtained by statting the file this introduced an additional round
 
245
            trip as well as requiring stat'able transports, both of which are
 
246
            avoided by having it supplied. If size is None, then bisection
 
247
            support will be disabled and accessing the index will just stream
 
248
            all the data.
 
249
        """
 
250
        self._transport = transport
 
251
        self._name = name
 
252
        # becomes a dict of key:(value, reference-list-byte-locations) 
 
253
        # used by the bisection interface to store parsed but not resolved
 
254
        # keys.
 
255
        self._bisect_nodes = None
 
256
        self._nodes = None
 
257
        # a sorted list of slice-addresses for the parsed bytes of the file.
 
258
        # e.g. (0,1) would mean that byte 0 is parsed.
 
259
        self._parsed_byte_map = []
 
260
        # a sorted list of keys matching each slice address for parsed bytes
 
261
        # e.g. (None, 'foo@bar') would mean that the first byte contained no
 
262
        # key, and the end byte of the slice is the of the data for 'foo@bar'
 
263
        self._parsed_key_map = []
 
264
        self._key_count = None
 
265
        self._keys_by_offset = None
 
266
        self._nodes_by_key = None
 
267
        self._size = size
 
268
 
 
269
    def __eq__(self, other):
 
270
        """Equal when self and otherwere created with the same parameters."""
 
271
        return (
 
272
            type(self) == type(other) and
 
273
            self._transport == other._transport and
 
274
            self._name == other._name and
 
275
            self._size == other._size)
 
276
 
 
277
    def __ne__(self, other):
 
278
        return not self.__eq__(other)
 
279
 
 
280
    def _buffer_all(self):
 
281
        """Buffer all the index data.
 
282
 
 
283
        Mutates self._nodes and self.keys_by_offset.
 
284
        """
 
285
        if 'index' in debug.debug_flags:
 
286
            mutter('Reading entire index %s', self._transport.abspath(self._name))
 
287
        stream = self._transport.get(self._name)
 
288
        self._read_prefix(stream)
 
289
        expected_elements = 3 + self._key_length
 
290
        line_count = 0
 
291
        # raw data keyed by offset
 
292
        self._keys_by_offset = {}
 
293
        # ready-to-return key:value or key:value, node_ref_lists
 
294
        self._nodes = {}
 
295
        self._nodes_by_key = {}
 
296
        trailers = 0
 
297
        pos = stream.tell()
 
298
        for line in stream.readlines():
 
299
            if line == '\n':
 
300
                trailers += 1
 
301
                continue
 
302
            elements = line.split('\0')
 
303
            if len(elements) != expected_elements:
 
304
                raise errors.BadIndexData(self)
 
305
            # keys are tuples
 
306
            key = tuple(elements[:self._key_length])
 
307
            absent, references, value = elements[-3:]
 
308
            value = value[:-1] # remove the newline
 
309
            ref_lists = []
 
310
            for ref_string in references.split('\t'):
 
311
                ref_lists.append(tuple([
 
312
                    int(ref) for ref in ref_string.split('\r') if ref
 
313
                    ]))
 
314
            ref_lists = tuple(ref_lists)
 
315
            self._keys_by_offset[pos] = (key, absent, ref_lists, value)
 
316
            pos += len(line)
 
317
        for key, absent, references, value in self._keys_by_offset.itervalues():
 
318
            if absent:
 
319
                continue
 
320
            # resolve references:
 
321
            if self.node_ref_lists:
 
322
                node_refs = []
 
323
                for ref_list in references:
 
324
                    node_refs.append(tuple([self._keys_by_offset[ref][0] for ref in ref_list]))
 
325
                node_value = (value, tuple(node_refs))
 
326
            else:
 
327
                node_value = value
 
328
            self._nodes[key] = node_value
 
329
            if self._key_length > 1:
 
330
                subkey = list(reversed(key[:-1]))
 
331
                key_dict = self._nodes_by_key
 
332
                if self.node_ref_lists:
 
333
                    key_value = key, node_value[0], node_value[1]
 
334
                else:
 
335
                    key_value = key, node_value
 
336
                # possibly should do this on-demand, but it seems likely it is 
 
337
                # always wanted
 
338
                # For a key of (foo, bar, baz) create
 
339
                # _nodes_by_key[foo][bar][baz] = key_value
 
340
                for subkey in key[:-1]:
 
341
                    key_dict = key_dict.setdefault(subkey, {})
 
342
                key_dict[key[-1]] = key_value
 
343
        # cache the keys for quick set intersections
 
344
        self._keys = set(self._nodes)
 
345
        if trailers != 1:
 
346
            # there must be one line - the empty trailer line.
 
347
            raise errors.BadIndexData(self)
 
348
 
 
349
    def iter_all_entries(self):
 
350
        """Iterate over all keys within the index.
 
351
 
 
352
        :return: An iterable of (index, key, value) or (index, key, value, reference_lists).
 
353
            The former tuple is used when there are no reference lists in the
 
354
            index, making the API compatible with simple key:value index types.
 
355
            There is no defined order for the result iteration - it will be in
 
356
            the most efficient order for the index.
 
357
        """
 
358
        if 'evil' in debug.debug_flags:
 
359
            trace.mutter_callsite(3,
 
360
                "iter_all_entries scales with size of history.")
 
361
        if self._nodes is None:
 
362
            self._buffer_all()
 
363
        if self.node_ref_lists:
 
364
            for key, (value, node_ref_lists) in self._nodes.iteritems():
 
365
                yield self, key, value, node_ref_lists
 
366
        else:
 
367
            for key, value in self._nodes.iteritems():
 
368
                yield self, key, value
 
369
 
 
370
    def _read_prefix(self, stream):
 
371
        signature = stream.read(len(self._signature()))
 
372
        if not signature == self._signature():
 
373
            raise errors.BadIndexFormatSignature(self._name, GraphIndex)
 
374
        options_line = stream.readline()
 
375
        if not options_line.startswith(_OPTION_NODE_REFS):
 
376
            raise errors.BadIndexOptions(self)
 
377
        try:
 
378
            self.node_ref_lists = int(options_line[len(_OPTION_NODE_REFS):-1])
 
379
        except ValueError:
 
380
            raise errors.BadIndexOptions(self)
 
381
        options_line = stream.readline()
 
382
        if not options_line.startswith(_OPTION_KEY_ELEMENTS):
 
383
            raise errors.BadIndexOptions(self)
 
384
        try:
 
385
            self._key_length = int(options_line[len(_OPTION_KEY_ELEMENTS):-1])
 
386
        except ValueError:
 
387
            raise errors.BadIndexOptions(self)
 
388
        options_line = stream.readline()
 
389
        if not options_line.startswith(_OPTION_LEN):
 
390
            raise errors.BadIndexOptions(self)
 
391
        try:
 
392
            self._key_count = int(options_line[len(_OPTION_LEN):-1])
 
393
        except ValueError:
 
394
            raise errors.BadIndexOptions(self)
 
395
 
 
396
    def _resolve_references(self, references):
 
397
        """Return the resolved key references for references."""
 
398
        node_refs = []
 
399
        for ref_list in references:
 
400
            node_refs.append(tuple([self._keys_by_offset[ref][0] for ref in ref_list]))
 
401
        return tuple(node_refs)
 
402
 
 
403
    def _find_index(self, range_map, key):
 
404
        """Helper for the _parsed_*_index calls.
 
405
 
 
406
        Given a range map - [(start, end), ...], finds the index of the range
 
407
        in the map for key if it is in the map, and if it is not there, the
 
408
        immediately preceeding range in the map.
 
409
        """
 
410
        result = bisect_right(range_map, key) - 1
 
411
        if result + 1 < len(range_map):
 
412
            # check the border condition, it may be in result + 1
 
413
            if range_map[result + 1][0] == key[0]:
 
414
                return result + 1
 
415
        return result
 
416
 
 
417
    def _parsed_byte_index(self, offset):
 
418
        """Return the index of the entry immediately before offset.
 
419
 
 
420
        e.g. if the parsed map has regions 0,10 and 11,12 parsed, meaning that
 
421
        there is one unparsed byte (the 11th, addressed as[10]). then:
 
422
        asking for 0 will return 0
 
423
        asking for 10 will return 0
 
424
        asking for 11 will return 1
 
425
        asking for 12 will return 1
 
426
        """
 
427
        key = (offset, 0)
 
428
        return self._find_index(self._parsed_byte_map, key)
 
429
 
 
430
    def _parsed_key_index(self, key):
 
431
        """Return the index of the entry immediately before key.
 
432
 
 
433
        e.g. if the parsed map has regions (None, 'a') and ('b','c') parsed,
 
434
        meaning that keys from None to 'a' inclusive, and 'b' to 'c' inclusive
 
435
        have been parsed, then:
 
436
        asking for '' will return 0
 
437
        asking for 'a' will return 0
 
438
        asking for 'b' will return 1
 
439
        asking for 'e' will return 1
 
440
        """
 
441
        search_key = (key, None)
 
442
        return self._find_index(self._parsed_key_map, search_key)
 
443
 
 
444
    def _is_parsed(self, offset):
 
445
        """Returns True if offset has been parsed."""
 
446
        index = self._parsed_byte_index(offset)
 
447
        if index == len(self._parsed_byte_map):
 
448
            return offset < self._parsed_byte_map[index - 1][1]
 
449
        start, end = self._parsed_byte_map[index]
 
450
        return offset >= start and offset < end
 
451
 
 
452
    def _iter_entries_from_total_buffer(self, keys):
 
453
        """Iterate over keys when the entire index is parsed."""
 
454
        keys = keys.intersection(self._keys)
 
455
        if self.node_ref_lists:
 
456
            for key in keys:
 
457
                value, node_refs = self._nodes[key]
 
458
                yield self, key, value, node_refs
 
459
        else:
 
460
            for key in keys:
 
461
                yield self, key, self._nodes[key]
 
462
 
 
463
    def iter_entries(self, keys):
 
464
        """Iterate over keys within the index.
 
465
 
 
466
        :param keys: An iterable providing the keys to be retrieved.
 
467
        :return: An iterable as per iter_all_entries, but restricted to the
 
468
            keys supplied. No additional keys will be returned, and every
 
469
            key supplied that is in the index will be returned.
 
470
        """
 
471
        # PERFORMANCE TODO: parse and bisect all remaining data at some
 
472
        # threshold of total-index processing/get calling layers that expect to
 
473
        # read the entire index to use the iter_all_entries  method instead.
 
474
        keys = set(keys)
 
475
        if not keys:
 
476
            return []
 
477
        if self._size is None and self._nodes is None:
 
478
            self._buffer_all()
 
479
        if self._nodes is not None:
 
480
            return self._iter_entries_from_total_buffer(keys)
 
481
        else:
 
482
            return (result[1] for result in bisect_multi_bytes(
 
483
                self.lookup_keys_via_location, self._size, keys))
 
484
 
 
485
    def iter_entries_prefix(self, keys):
 
486
        """Iterate over keys within the index using prefix matching.
 
487
 
 
488
        Prefix matching is applied within the tuple of a key, not to within
 
489
        the bytestring of each key element. e.g. if you have the keys ('foo',
 
490
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
 
491
        only the former key is returned.
 
492
 
 
493
        WARNING: Note that this method currently causes a full index parse
 
494
        unconditionally (which is reasonably appropriate as it is a means for
 
495
        thunking many small indices into one larger one and still supplies
 
496
        iter_all_entries at the thunk layer).
 
497
 
 
498
        :param keys: An iterable providing the key prefixes to be retrieved.
 
499
            Each key prefix takes the form of a tuple the length of a key, but
 
500
            with the last N elements 'None' rather than a regular bytestring.
 
501
            The first element cannot be 'None'.
 
502
        :return: An iterable as per iter_all_entries, but restricted to the
 
503
            keys with a matching prefix to those supplied. No additional keys
 
504
            will be returned, and every match that is in the index will be
 
505
            returned.
 
506
        """
 
507
        keys = set(keys)
 
508
        if not keys:
 
509
            return
 
510
        # load data - also finds key lengths
 
511
        if self._nodes is None:
 
512
            self._buffer_all()
 
513
        if self._key_length == 1:
 
514
            for key in keys:
 
515
                # sanity check
 
516
                if key[0] is None:
 
517
                    raise errors.BadIndexKey(key)
 
518
                if len(key) != self._key_length:
 
519
                    raise errors.BadIndexKey(key)
 
520
                if self.node_ref_lists:
 
521
                    value, node_refs = self._nodes[key]
 
522
                    yield self, key, value, node_refs
 
523
                else:
 
524
                    yield self, key, self._nodes[key]
 
525
            return
 
526
        for key in keys:
 
527
            # sanity check
 
528
            if key[0] is None:
 
529
                raise errors.BadIndexKey(key)
 
530
            if len(key) != self._key_length:
 
531
                raise errors.BadIndexKey(key)
 
532
            # find what it refers to:
 
533
            key_dict = self._nodes_by_key
 
534
            elements = list(key)
 
535
            # find the subdict whose contents should be returned.
 
536
            try:
 
537
                while len(elements) and elements[0] is not None:
 
538
                    key_dict = key_dict[elements[0]]
 
539
                    elements.pop(0)
 
540
            except KeyError:
 
541
                # a non-existant lookup.
 
542
                continue
 
543
            if len(elements):
 
544
                dicts = [key_dict]
 
545
                while dicts:
 
546
                    key_dict = dicts.pop(-1)
 
547
                    # can't be empty or would not exist
 
548
                    item, value = key_dict.iteritems().next()
 
549
                    if type(value) == dict:
 
550
                        # push keys 
 
551
                        dicts.extend(key_dict.itervalues())
 
552
                    else:
 
553
                        # yield keys
 
554
                        for value in key_dict.itervalues():
 
555
                            # each value is the key:value:node refs tuple
 
556
                            # ready to yield.
 
557
                            yield (self, ) + value
 
558
            else:
 
559
                # the last thing looked up was a terminal element
 
560
                yield (self, ) + key_dict
 
561
 
 
562
    def key_count(self):
 
563
        """Return an estimate of the number of keys in this index.
 
564
        
 
565
        For GraphIndex the estimate is exact.
 
566
        """
 
567
        if self._key_count is None:
 
568
            # really this should just read the prefix
 
569
            self._buffer_all()
 
570
        return self._key_count
 
571
 
 
572
    def lookup_keys_via_location(self, location_keys):
 
573
        """Public interface for implementing bisection.
 
574
 
 
575
        If _buffer_all has been called, then all the data for the index is in
 
576
        memory, and this method should not be called, as it uses a separate
 
577
        cache because it cannot pre-resolve all indices, which buffer_all does
 
578
        for performance.
 
579
 
 
580
        :param location_keys: A list of location, key tuples.
 
581
        :return: A list of (location_key, result) tuples as expected by
 
582
            bzrlib.bisect_multi.bisect_multi_bytes.
 
583
        """
 
584
        # Possible improvements:
 
585
        #  - only bisect lookup each key once
 
586
        #  - sort the keys first, and use that to reduce the bisection window
 
587
        # ----- 
 
588
        # this progresses in three parts:
 
589
        # read data
 
590
        # parse it
 
591
        # attempt to answer the question from the now in memory data.
 
592
        # build the readv request
 
593
        # for each location, ask for 800 bytes - much more than rows we've seen
 
594
        # anywhere.
 
595
        readv_ranges = []
 
596
        for location, key in location_keys:
 
597
            # can we answer from cache?
 
598
            # - if we know the answer - yes
 
599
            index = self._parsed_key_index(key)
 
600
            if (len(self._parsed_key_map) and 
 
601
                self._parsed_key_map[index][0] <= key and
 
602
                (self._parsed_key_map[index][1] > key or
 
603
                 # end of the file has been parsed
 
604
                 self._parsed_byte_map[index][1] == self._size)):
 
605
                # the key has been parsed, so no lookup is needed
 
606
                continue
 
607
            # - if we have examined this part of the file already - yes
 
608
            index = self._parsed_byte_index(location)
 
609
            if (len(self._parsed_byte_map) and 
 
610
                self._parsed_byte_map[index][0] <= location and
 
611
                self._parsed_byte_map[index][1] > location):
 
612
                # the byte region has been parsed, so no read is needed.
 
613
                continue
 
614
            length = 800
 
615
            if location + length > self._size:
 
616
                length = self._size - location
 
617
            # todo, trim out parsed locations.
 
618
            if length > 0:
 
619
                readv_ranges.append((location, length))
 
620
        # read the header if needed
 
621
        if self._bisect_nodes is None:
 
622
            readv_ranges.append((0, 200))
 
623
        self._read_and_parse(readv_ranges)
 
624
        # generate results:
 
625
        #  - figure out <, >, missing, present
 
626
        #  - result present references so we can return them.
 
627
        result = []
 
628
        # keys that we cannot answer until we resolve references
 
629
        pending_references = []
 
630
        pending_locations = set()
 
631
        for location, key in location_keys:
 
632
            # can we answer from cache?
 
633
            index = self._parsed_key_index(key)
 
634
            if (self._parsed_key_map[index][0] <= key and
 
635
                (self._parsed_key_map[index][1] > key or
 
636
                 # end of the file has been parsed
 
637
                 self._parsed_byte_map[index][1] == self._size)):
 
638
                # the key has been parsed, so no lookup is needed
 
639
                if key in self._bisect_nodes:
 
640
                    if self.node_ref_lists:
 
641
                        # the references may not have been all parsed.
 
642
                        value, refs = self._bisect_nodes[key]
 
643
                        wanted_locations = []
 
644
                        for ref_list in refs:
 
645
                            for ref in ref_list:
 
646
                                if ref not in self._keys_by_offset:
 
647
                                    wanted_locations.append(ref)
 
648
                        if wanted_locations:
 
649
                            pending_locations.update(wanted_locations)
 
650
                            pending_references.append((location, key))
 
651
                            continue
 
652
                        result.append(((location, key), (self, key,
 
653
                            value, self._resolve_references(refs))))
 
654
                    else:
 
655
                        result.append(((location, key),
 
656
                            (self, key, self._bisect_nodes[key])))
 
657
                else:
 
658
                    result.append(((location, key), False))
 
659
                continue
 
660
            # no, is the key above or below the probed location:
 
661
            # get the range of the probed & parsed location
 
662
            index = self._parsed_byte_index(location)
 
663
            # if the key is below the start of the range, its below
 
664
            if key < self._parsed_key_map[index][0]:
 
665
                direction = -1
 
666
            else:
 
667
                direction = +1
 
668
            result.append(((location, key), direction))
 
669
        readv_ranges = []
 
670
        # lookup data to resolve references
 
671
        for location in pending_locations:
 
672
            length = 800
 
673
            if location + length > self._size:
 
674
                length = self._size - location
 
675
            # TODO: trim out parsed locations (e.g. if the 800 is into the
 
676
            # parsed region trim it, and dont use the ajust_for_latency
 
677
            # facility)
 
678
            if length > 0:
 
679
                readv_ranges.append((location, length))
 
680
        self._read_and_parse(readv_ranges)
 
681
        for location, key in pending_references:
 
682
            # answer key references we had to look-up-late.
 
683
            index = self._parsed_key_index(key)
 
684
            value, refs = self._bisect_nodes[key]
 
685
            result.append(((location, key), (self, key,
 
686
                value, self._resolve_references(refs))))
 
687
        return result
 
688
 
 
689
    def _parse_header_from_bytes(self, bytes):
 
690
        """Parse the header from a region of bytes.
 
691
 
 
692
        :param bytes: The data to parse.
 
693
        :return: An offset, data tuple such as readv yields, for the unparsed
 
694
            data. (which may length 0).
 
695
        """
 
696
        signature = bytes[0:len(self._signature())]
 
697
        if not signature == self._signature():
 
698
            raise errors.BadIndexFormatSignature(self._name, GraphIndex)
 
699
        lines = bytes[len(self._signature()):].splitlines()
 
700
        options_line = lines[0]
 
701
        if not options_line.startswith(_OPTION_NODE_REFS):
 
702
            raise errors.BadIndexOptions(self)
 
703
        try:
 
704
            self.node_ref_lists = int(options_line[len(_OPTION_NODE_REFS):])
 
705
        except ValueError:
 
706
            raise errors.BadIndexOptions(self)
 
707
        options_line = lines[1]
 
708
        if not options_line.startswith(_OPTION_KEY_ELEMENTS):
 
709
            raise errors.BadIndexOptions(self)
 
710
        try:
 
711
            self._key_length = int(options_line[len(_OPTION_KEY_ELEMENTS):])
 
712
        except ValueError:
 
713
            raise errors.BadIndexOptions(self)
 
714
        options_line = lines[2]
 
715
        if not options_line.startswith(_OPTION_LEN):
 
716
            raise errors.BadIndexOptions(self)
 
717
        try:
 
718
            self._key_count = int(options_line[len(_OPTION_LEN):])
 
719
        except ValueError:
 
720
            raise errors.BadIndexOptions(self)
 
721
        # calculate the bytes we have processed
 
722
        header_end = (len(signature) + len(lines[0]) + len(lines[1]) +
 
723
            len(lines[2]) + 3)
 
724
        self._parsed_bytes(0, None, header_end, None)
 
725
        # setup parsing state
 
726
        self._expected_elements = 3 + self._key_length
 
727
        # raw data keyed by offset
 
728
        self._keys_by_offset = {}
 
729
        # keys with the value and node references
 
730
        self._bisect_nodes = {}
 
731
        return header_end, bytes[header_end:]
 
732
 
 
733
    def _parse_region(self, offset, data):
 
734
        """Parse node data returned from a readv operation.
 
735
 
 
736
        :param offset: The byte offset the data starts at.
 
737
        :param data: The data to parse.
 
738
        """
 
739
        # trim the data.
 
740
        # end first:
 
741
        end = offset + len(data)
 
742
        high_parsed = offset
 
743
        while True:
 
744
            # Trivial test - if the current index's end is within the
 
745
            # low-matching parsed range, we're done.
 
746
            index = self._parsed_byte_index(high_parsed)
 
747
            if end < self._parsed_byte_map[index][1]:
 
748
                return
 
749
            # print "[%d:%d]" % (offset, end), \
 
750
            #     self._parsed_byte_map[index:index + 2]
 
751
            high_parsed, last_segment = self._parse_segment(
 
752
                offset, data, end, index)
 
753
            if last_segment:
 
754
                return
 
755
 
 
756
    def _parse_segment(self, offset, data, end, index):
 
757
        """Parse one segment of data.
 
758
 
 
759
        :param offset: Where 'data' begins in the file.
 
760
        :param data: Some data to parse a segment of.
 
761
        :param end: Where data ends
 
762
        :param index: The current index into the parsed bytes map.
 
763
        :return: True if the parsed segment is the last possible one in the
 
764
            range of data.
 
765
        :return: high_parsed_byte, last_segment.
 
766
            high_parsed_byte is the location of the highest parsed byte in this
 
767
            segment, last_segment is True if the parsed segment is the last
 
768
            possible one in the data block.
 
769
        """
 
770
        # default is to use all data
 
771
        trim_end = None
 
772
        # accomodate overlap with data before this.
 
773
        if offset < self._parsed_byte_map[index][1]:
 
774
            # overlaps the lower parsed region
 
775
            # skip the parsed data
 
776
            trim_start = self._parsed_byte_map[index][1] - offset
 
777
            # don't trim the start for \n
 
778
            start_adjacent = True
 
779
        elif offset == self._parsed_byte_map[index][1]:
 
780
            # abuts the lower parsed region
 
781
            # use all data
 
782
            trim_start = None
 
783
            # do not trim anything
 
784
            start_adjacent = True
 
785
        else:
 
786
            # does not overlap the lower parsed region
 
787
            # use all data
 
788
            trim_start = None
 
789
            # but trim the leading \n
 
790
            start_adjacent = False
 
791
        if end == self._size:
 
792
            # lines up to the end of all data:
 
793
            # use it all
 
794
            trim_end = None
 
795
            # do not strip to the last \n
 
796
            end_adjacent = True
 
797
            last_segment = True
 
798
        elif index + 1 == len(self._parsed_byte_map):
 
799
            # at the end of the parsed data
 
800
            # use it all
 
801
            trim_end = None
 
802
            # but strip to the last \n
 
803
            end_adjacent = False
 
804
            last_segment = True
 
805
        elif end == self._parsed_byte_map[index + 1][0]:
 
806
            # buts up against the next parsed region
 
807
            # use it all
 
808
            trim_end = None
 
809
            # do not strip to the last \n
 
810
            end_adjacent = True
 
811
            last_segment = True
 
812
        elif end > self._parsed_byte_map[index + 1][0]:
 
813
            # overlaps into the next parsed region
 
814
            # only consider the unparsed data
 
815
            trim_end = self._parsed_byte_map[index + 1][0] - offset
 
816
            # do not strip to the last \n as we know its an entire record
 
817
            end_adjacent = True
 
818
            last_segment = end < self._parsed_byte_map[index + 1][1]
 
819
        else:
 
820
            # does not overlap into the next region
 
821
            # use it all
 
822
            trim_end = None
 
823
            # but strip to the last \n
 
824
            end_adjacent = False
 
825
            last_segment = True
 
826
        # now find bytes to discard if needed
 
827
        if not start_adjacent:
 
828
            # work around python bug in rfind
 
829
            if trim_start is None:
 
830
                trim_start = data.find('\n') + 1
 
831
            else:
 
832
                trim_start = data.find('\n', trim_start) + 1
 
833
            assert trim_start != 0, 'no \n was present'
 
834
            # print 'removing start', offset, trim_start, repr(data[:trim_start])
 
835
        if not end_adjacent:
 
836
            # work around python bug in rfind
 
837
            if trim_end is None:
 
838
                trim_end = data.rfind('\n') + 1
 
839
            else:
 
840
                trim_end = data.rfind('\n', None, trim_end) + 1
 
841
            assert trim_end != 0, 'no \n was present'
 
842
            # print 'removing end', offset, trim_end, repr(data[trim_end:])
 
843
        # adjust offset and data to the parseable data.
 
844
        trimmed_data = data[trim_start:trim_end]
 
845
        assert trimmed_data, 'read unneeded data [%d:%d] from [%d:%d]' % (
 
846
            trim_start, trim_end, offset, offset + len(data))
 
847
        if trim_start:
 
848
            offset += trim_start
 
849
        # print "parsing", repr(trimmed_data)
 
850
        # splitlines mangles the \r delimiters.. don't use it.
 
851
        lines = trimmed_data.split('\n')
 
852
        del lines[-1]
 
853
        pos = offset
 
854
        first_key = None
 
855
        key = None
 
856
        for line in lines:
 
857
            if line == '':
 
858
                # must be at the end
 
859
                assert self._size == pos + 1, "%s %s" % (self._size, pos)
 
860
                continue
 
861
            elements = line.split('\0')
 
862
            if len(elements) != self._expected_elements:
 
863
                raise errors.BadIndexData(self)
 
864
            # keys are tuples
 
865
            key = tuple(elements[:self._key_length])
 
866
            if first_key is None:
 
867
                first_key = key
 
868
            absent, references, value = elements[-3:]
 
869
            ref_lists = []
 
870
            for ref_string in references.split('\t'):
 
871
                ref_lists.append(tuple([
 
872
                    int(ref) for ref in ref_string.split('\r') if ref
 
873
                    ]))
 
874
            ref_lists = tuple(ref_lists)
 
875
            self._keys_by_offset[pos] = (key, absent, ref_lists, value)
 
876
            pos += len(line) + 1 # +1 for the \n
 
877
            if absent:
 
878
                continue
 
879
            if self.node_ref_lists:
 
880
                node_value = (value, ref_lists)
 
881
            else:
 
882
                node_value = value
 
883
            self._bisect_nodes[key] = node_value
 
884
            # print "parsed ", key
 
885
        self._parsed_bytes(offset, first_key, offset + len(trimmed_data), key)
 
886
        return offset + len(trimmed_data), last_segment
 
887
 
 
888
    def _parsed_bytes(self, start, start_key, end, end_key):
 
889
        """Mark the bytes from start to end as parsed.
 
890
 
 
891
        Calling self._parsed_bytes(1,2) will mark one byte (the one at offset
 
892
        1) as parsed.
 
893
 
 
894
        :param start: The start of the parsed region.
 
895
        :param end: The end of the parsed region.
 
896
        """
 
897
        index = self._parsed_byte_index(start)
 
898
        new_value = (start, end)
 
899
        new_key = (start_key, end_key)
 
900
        if index == -1:
 
901
            # first range parsed is always the beginning.
 
902
            self._parsed_byte_map.insert(index, new_value)
 
903
            self._parsed_key_map.insert(index, new_key)
 
904
            return
 
905
        # four cases:
 
906
        # new region
 
907
        # extend lower region
 
908
        # extend higher region
 
909
        # combine two regions
 
910
        if (index + 1 < len(self._parsed_byte_map) and
 
911
            self._parsed_byte_map[index][1] == start and
 
912
            self._parsed_byte_map[index + 1][0] == end):
 
913
            # combine two regions
 
914
            self._parsed_byte_map[index] = (self._parsed_byte_map[index][0],
 
915
                self._parsed_byte_map[index + 1][1])
 
916
            self._parsed_key_map[index] = (self._parsed_key_map[index][0],
 
917
                self._parsed_key_map[index + 1][1])
 
918
            del self._parsed_byte_map[index + 1]
 
919
            del self._parsed_key_map[index + 1]
 
920
        elif self._parsed_byte_map[index][1] == start:
 
921
            # extend the lower entry
 
922
            self._parsed_byte_map[index] = (
 
923
                self._parsed_byte_map[index][0], end)
 
924
            self._parsed_key_map[index] = (
 
925
                self._parsed_key_map[index][0], end_key)
 
926
        elif (index + 1 < len(self._parsed_byte_map) and
 
927
            self._parsed_byte_map[index + 1][0] == end):
 
928
            # extend the higher entry
 
929
            self._parsed_byte_map[index + 1] = (
 
930
                start, self._parsed_byte_map[index + 1][1])
 
931
            self._parsed_key_map[index + 1] = (
 
932
                start_key, self._parsed_key_map[index + 1][1])
 
933
        else:
 
934
            # new entry
 
935
            self._parsed_byte_map.insert(index + 1, new_value)
 
936
            self._parsed_key_map.insert(index + 1, new_key)
 
937
 
 
938
    def _read_and_parse(self, readv_ranges):
 
939
        """Read the the ranges and parse the resulting data.
 
940
 
 
941
        :param readv_ranges: A prepared readv range list.
 
942
        """
 
943
        if readv_ranges:
 
944
            readv_data = self._transport.readv(self._name, readv_ranges, True,
 
945
                self._size)
 
946
            # parse
 
947
            for offset, data in readv_data:
 
948
                if self._bisect_nodes is None:
 
949
                    # this must be the start
 
950
                    assert offset == 0
 
951
                    offset, data = self._parse_header_from_bytes(data)
 
952
                # print readv_ranges, "[%d:%d]" % (offset, offset + len(data))
 
953
                self._parse_region(offset, data)
 
954
 
 
955
    def _signature(self):
 
956
        """The file signature for this index type."""
 
957
        return _SIGNATURE
 
958
 
 
959
    def validate(self):
 
960
        """Validate that everything in the index can be accessed."""
 
961
        # iter_all validates completely at the moment, so just do that.
 
962
        for node in self.iter_all_entries():
 
963
            pass
 
964
 
 
965
 
 
966
class CombinedGraphIndex(object):
 
967
    """A GraphIndex made up from smaller GraphIndices.
 
968
    
 
969
    The backing indices must implement GraphIndex, and are presumed to be
 
970
    static data.
 
971
 
 
972
    Queries against the combined index will be made against the first index,
 
973
    and then the second and so on. The order of index's can thus influence
 
974
    performance significantly. For example, if one index is on local disk and a
 
975
    second on a remote server, the local disk index should be before the other
 
976
    in the index list.
 
977
    """
 
978
 
 
979
    def __init__(self, indices):
 
980
        """Create a CombinedGraphIndex backed by indices.
 
981
 
 
982
        :param indices: An ordered list of indices to query for data.
 
983
        """
 
984
        self._indices = indices
 
985
 
 
986
    def __repr__(self):
 
987
        return "%s(%s)" % (
 
988
                self.__class__.__name__,
 
989
                ', '.join(map(repr, self._indices)))
 
990
 
 
991
    def insert_index(self, pos, index):
 
992
        """Insert a new index in the list of indices to query.
 
993
 
 
994
        :param pos: The position to insert the index.
 
995
        :param index: The index to insert.
 
996
        """
 
997
        self._indices.insert(pos, index)
 
998
 
 
999
    def iter_all_entries(self):
 
1000
        """Iterate over all keys within the index
 
1001
 
 
1002
        Duplicate keys across child indices are presumed to have the same
 
1003
        value and are only reported once.
 
1004
 
 
1005
        :return: An iterable of (index, key, reference_lists, value).
 
1006
            There is no defined order for the result iteration - it will be in
 
1007
            the most efficient order for the index.
 
1008
        """
 
1009
        seen_keys = set()
 
1010
        for index in self._indices:
 
1011
            for node in index.iter_all_entries():
 
1012
                if node[1] not in seen_keys:
 
1013
                    yield node
 
1014
                    seen_keys.add(node[1])
 
1015
 
 
1016
    def iter_entries(self, keys):
 
1017
        """Iterate over keys within the index.
 
1018
 
 
1019
        Duplicate keys across child indices are presumed to have the same
 
1020
        value and are only reported once.
 
1021
 
 
1022
        :param keys: An iterable providing the keys to be retrieved.
 
1023
        :return: An iterable of (index, key, reference_lists, value). There is no
 
1024
            defined order for the result iteration - it will be in the most
 
1025
            efficient order for the index.
 
1026
        """
 
1027
        keys = set(keys)
 
1028
        for index in self._indices:
 
1029
            if not keys:
 
1030
                return
 
1031
            for node in index.iter_entries(keys):
 
1032
                keys.remove(node[1])
 
1033
                yield node
 
1034
 
 
1035
    def iter_entries_prefix(self, keys):
 
1036
        """Iterate over keys within the index using prefix matching.
 
1037
 
 
1038
        Duplicate keys across child indices are presumed to have the same
 
1039
        value and are only reported once.
 
1040
 
 
1041
        Prefix matching is applied within the tuple of a key, not to within
 
1042
        the bytestring of each key element. e.g. if you have the keys ('foo',
 
1043
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
 
1044
        only the former key is returned.
 
1045
 
 
1046
        :param keys: An iterable providing the key prefixes to be retrieved.
 
1047
            Each key prefix takes the form of a tuple the length of a key, but
 
1048
            with the last N elements 'None' rather than a regular bytestring.
 
1049
            The first element cannot be 'None'.
 
1050
        :return: An iterable as per iter_all_entries, but restricted to the
 
1051
            keys with a matching prefix to those supplied. No additional keys
 
1052
            will be returned, and every match that is in the index will be
 
1053
            returned.
 
1054
        """
 
1055
        keys = set(keys)
 
1056
        if not keys:
 
1057
            return
 
1058
        seen_keys = set()
 
1059
        for index in self._indices:
 
1060
            for node in index.iter_entries_prefix(keys):
 
1061
                if node[1] in seen_keys:
 
1062
                    continue
 
1063
                seen_keys.add(node[1])
 
1064
                yield node
 
1065
 
 
1066
    def key_count(self):
 
1067
        """Return an estimate of the number of keys in this index.
 
1068
        
 
1069
        For CombinedGraphIndex this is approximated by the sum of the keys of
 
1070
        the child indices. As child indices may have duplicate keys this can
 
1071
        have a maximum error of the number of child indices * largest number of
 
1072
        keys in any index.
 
1073
        """
 
1074
        return sum((index.key_count() for index in self._indices), 0)
 
1075
 
 
1076
    def validate(self):
 
1077
        """Validate that everything in the index can be accessed."""
 
1078
        for index in self._indices:
 
1079
            index.validate()
 
1080
 
 
1081
 
 
1082
class InMemoryGraphIndex(GraphIndexBuilder):
 
1083
    """A GraphIndex which operates entirely out of memory and is mutable.
 
1084
 
 
1085
    This is designed to allow the accumulation of GraphIndex entries during a
 
1086
    single write operation, where the accumulated entries need to be immediately
 
1087
    available - for example via a CombinedGraphIndex.
 
1088
    """
 
1089
 
 
1090
    def add_nodes(self, nodes):
 
1091
        """Add nodes to the index.
 
1092
 
 
1093
        :param nodes: An iterable of (key, node_refs, value) entries to add.
 
1094
        """
 
1095
        if self.reference_lists:
 
1096
            for (key, value, node_refs) in nodes:
 
1097
                self.add_node(key, value, node_refs)
 
1098
        else:
 
1099
            for (key, value) in nodes:
 
1100
                self.add_node(key, value)
 
1101
 
 
1102
    def iter_all_entries(self):
 
1103
        """Iterate over all keys within the index
 
1104
 
 
1105
        :return: An iterable of (index, key, reference_lists, value). There is no
 
1106
            defined order for the result iteration - it will be in the most
 
1107
            efficient order for the index (in this case dictionary hash order).
 
1108
        """
 
1109
        if 'evil' in debug.debug_flags:
 
1110
            trace.mutter_callsite(3,
 
1111
                "iter_all_entries scales with size of history.")
 
1112
        if self.reference_lists:
 
1113
            for key, (absent, references, value) in self._nodes.iteritems():
 
1114
                if not absent:
 
1115
                    yield self, key, value, references
 
1116
        else:
 
1117
            for key, (absent, references, value) in self._nodes.iteritems():
 
1118
                if not absent:
 
1119
                    yield self, key, value
 
1120
 
 
1121
    def iter_entries(self, keys):
 
1122
        """Iterate over keys within the index.
 
1123
 
 
1124
        :param keys: An iterable providing the keys to be retrieved.
 
1125
        :return: An iterable of (index, key, reference_lists, value). There is no
 
1126
            defined order for the result iteration - it will be in the most
 
1127
            efficient order for the index (keys iteration order in this case).
 
1128
        """
 
1129
        keys = set(keys)
 
1130
        if self.reference_lists:
 
1131
            for key in keys.intersection(self._keys):
 
1132
                node = self._nodes[key]
 
1133
                if not node[0]:
 
1134
                    yield self, key, node[2], node[1]
 
1135
        else:
 
1136
            for key in keys.intersection(self._keys):
 
1137
                node = self._nodes[key]
 
1138
                if not node[0]:
 
1139
                    yield self, key, node[2]
 
1140
 
 
1141
    def iter_entries_prefix(self, keys):
 
1142
        """Iterate over keys within the index using prefix matching.
 
1143
 
 
1144
        Prefix matching is applied within the tuple of a key, not to within
 
1145
        the bytestring of each key element. e.g. if you have the keys ('foo',
 
1146
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
 
1147
        only the former key is returned.
 
1148
 
 
1149
        :param keys: An iterable providing the key prefixes to be retrieved.
 
1150
            Each key prefix takes the form of a tuple the length of a key, but
 
1151
            with the last N elements 'None' rather than a regular bytestring.
 
1152
            The first element cannot be 'None'.
 
1153
        :return: An iterable as per iter_all_entries, but restricted to the
 
1154
            keys with a matching prefix to those supplied. No additional keys
 
1155
            will be returned, and every match that is in the index will be
 
1156
            returned.
 
1157
        """
 
1158
        # XXX: To much duplication with the GraphIndex class; consider finding
 
1159
        # a good place to pull out the actual common logic.
 
1160
        keys = set(keys)
 
1161
        if not keys:
 
1162
            return
 
1163
        if self._key_length == 1:
 
1164
            for key in keys:
 
1165
                # sanity check
 
1166
                if key[0] is None:
 
1167
                    raise errors.BadIndexKey(key)
 
1168
                if len(key) != self._key_length:
 
1169
                    raise errors.BadIndexKey(key)
 
1170
                node = self._nodes[key]
 
1171
                if node[0]:
 
1172
                    continue 
 
1173
                if self.reference_lists:
 
1174
                    yield self, key, node[2], node[1]
 
1175
                else:
 
1176
                    yield self, key, node[2]
 
1177
            return
 
1178
        for key in keys:
 
1179
            # sanity check
 
1180
            if key[0] is None:
 
1181
                raise errors.BadIndexKey(key)
 
1182
            if len(key) != self._key_length:
 
1183
                raise errors.BadIndexKey(key)
 
1184
            # find what it refers to:
 
1185
            key_dict = self._nodes_by_key
 
1186
            elements = list(key)
 
1187
            # find the subdict to return
 
1188
            try:
 
1189
                while len(elements) and elements[0] is not None:
 
1190
                    key_dict = key_dict[elements[0]]
 
1191
                    elements.pop(0)
 
1192
            except KeyError:
 
1193
                # a non-existant lookup.
 
1194
                continue
 
1195
            if len(elements):
 
1196
                dicts = [key_dict]
 
1197
                while dicts:
 
1198
                    key_dict = dicts.pop(-1)
 
1199
                    # can't be empty or would not exist
 
1200
                    item, value = key_dict.iteritems().next()
 
1201
                    if type(value) == dict:
 
1202
                        # push keys 
 
1203
                        dicts.extend(key_dict.itervalues())
 
1204
                    else:
 
1205
                        # yield keys
 
1206
                        for value in key_dict.itervalues():
 
1207
                            yield (self, ) + value
 
1208
            else:
 
1209
                yield (self, ) + key_dict
 
1210
 
 
1211
    def key_count(self):
 
1212
        """Return an estimate of the number of keys in this index.
 
1213
        
 
1214
        For InMemoryGraphIndex the estimate is exact.
 
1215
        """
 
1216
        return len(self._keys)
 
1217
 
 
1218
    def validate(self):
 
1219
        """In memory index's have no known corruption at the moment."""
 
1220
 
 
1221
 
 
1222
class GraphIndexPrefixAdapter(object):
 
1223
    """An adapter between GraphIndex with different key lengths.
 
1224
 
 
1225
    Queries against this will emit queries against the adapted Graph with the
 
1226
    prefix added, queries for all items use iter_entries_prefix. The returned
 
1227
    nodes will have their keys and node references adjusted to remove the 
 
1228
    prefix. Finally, an add_nodes_callback can be supplied - when called the
 
1229
    nodes and references being added will have prefix prepended.
 
1230
    """
 
1231
 
 
1232
    def __init__(self, adapted, prefix, missing_key_length,
 
1233
        add_nodes_callback=None):
 
1234
        """Construct an adapter against adapted with prefix."""
 
1235
        self.adapted = adapted
 
1236
        self.prefix_key = prefix + (None,)*missing_key_length
 
1237
        self.prefix = prefix
 
1238
        self.prefix_len = len(prefix)
 
1239
        self.add_nodes_callback = add_nodes_callback
 
1240
 
 
1241
    def add_nodes(self, nodes):
 
1242
        """Add nodes to the index.
 
1243
 
 
1244
        :param nodes: An iterable of (key, node_refs, value) entries to add.
 
1245
        """
 
1246
        # save nodes in case its an iterator
 
1247
        nodes = tuple(nodes)
 
1248
        translated_nodes = []
 
1249
        try:
 
1250
            # Add prefix_key to each reference node_refs is a tuple of tuples,
 
1251
            # so split it apart, and add prefix_key to the internal reference
 
1252
            for (key, value, node_refs) in nodes:
 
1253
                adjusted_references = (
 
1254
                    tuple(tuple(self.prefix + ref_node for ref_node in ref_list)
 
1255
                        for ref_list in node_refs))
 
1256
                translated_nodes.append((self.prefix + key, value,
 
1257
                    adjusted_references))
 
1258
        except ValueError:
 
1259
            # XXX: TODO add an explicit interface for getting the reference list
 
1260
            # status, to handle this bit of user-friendliness in the API more 
 
1261
            # explicitly.
 
1262
            for (key, value) in nodes:
 
1263
                translated_nodes.append((self.prefix + key, value))
 
1264
        self.add_nodes_callback(translated_nodes)
 
1265
 
 
1266
    def add_node(self, key, value, references=()):
 
1267
        """Add a node to the index.
 
1268
 
 
1269
        :param key: The key. keys are non-empty tuples containing
 
1270
            as many whitespace-free utf8 bytestrings as the key length
 
1271
            defined for this index.
 
1272
        :param references: An iterable of iterables of keys. Each is a
 
1273
            reference to another key.
 
1274
        :param value: The value to associate with the key. It may be any
 
1275
            bytes as long as it does not contain \0 or \n.
 
1276
        """
 
1277
        self.add_nodes(((key, value, references), ))
 
1278
 
 
1279
    def _strip_prefix(self, an_iter):
 
1280
        """Strip prefix data from nodes and return it."""
 
1281
        for node in an_iter:
 
1282
            # cross checks
 
1283
            if node[1][:self.prefix_len] != self.prefix:
 
1284
                raise errors.BadIndexData(self)
 
1285
            for ref_list in node[3]:
 
1286
                for ref_node in ref_list:
 
1287
                    if ref_node[:self.prefix_len] != self.prefix:
 
1288
                        raise errors.BadIndexData(self)
 
1289
            yield node[0], node[1][self.prefix_len:], node[2], (
 
1290
                tuple(tuple(ref_node[self.prefix_len:] for ref_node in ref_list)
 
1291
                for ref_list in node[3]))
 
1292
 
 
1293
    def iter_all_entries(self):
 
1294
        """Iterate over all keys within the index
 
1295
 
 
1296
        iter_all_entries is implemented against the adapted index using
 
1297
        iter_entries_prefix.
 
1298
 
 
1299
        :return: An iterable of (index, key, reference_lists, value). There is no
 
1300
            defined order for the result iteration - it will be in the most
 
1301
            efficient order for the index (in this case dictionary hash order).
 
1302
        """
 
1303
        return self._strip_prefix(self.adapted.iter_entries_prefix([self.prefix_key]))
 
1304
 
 
1305
    def iter_entries(self, keys):
 
1306
        """Iterate over keys within the index.
 
1307
 
 
1308
        :param keys: An iterable providing the keys to be retrieved.
 
1309
        :return: An iterable of (key, reference_lists, value). There is no
 
1310
            defined order for the result iteration - it will be in the most
 
1311
            efficient order for the index (keys iteration order in this case).
 
1312
        """
 
1313
        return self._strip_prefix(self.adapted.iter_entries(
 
1314
            self.prefix + key for key in keys))
 
1315
 
 
1316
    def iter_entries_prefix(self, keys):
 
1317
        """Iterate over keys within the index using prefix matching.
 
1318
 
 
1319
        Prefix matching is applied within the tuple of a key, not to within
 
1320
        the bytestring of each key element. e.g. if you have the keys ('foo',
 
1321
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
 
1322
        only the former key is returned.
 
1323
 
 
1324
        :param keys: An iterable providing the key prefixes to be retrieved.
 
1325
            Each key prefix takes the form of a tuple the length of a key, but
 
1326
            with the last N elements 'None' rather than a regular bytestring.
 
1327
            The first element cannot be 'None'.
 
1328
        :return: An iterable as per iter_all_entries, but restricted to the
 
1329
            keys with a matching prefix to those supplied. No additional keys
 
1330
            will be returned, and every match that is in the index will be
 
1331
            returned.
 
1332
        """
 
1333
        return self._strip_prefix(self.adapted.iter_entries_prefix(
 
1334
            self.prefix + key for key in keys))
 
1335
 
 
1336
    def key_count(self):
 
1337
        """Return an estimate of the number of keys in this index.
 
1338
        
 
1339
        For GraphIndexPrefixAdapter this is relatively expensive - key
 
1340
        iteration with the prefix is done.
 
1341
        """
 
1342
        return len(list(self.iter_all_entries()))
 
1343
 
 
1344
    def validate(self):
 
1345
        """Call the adapted's validate."""
 
1346
        self.adapted.validate()