~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/index.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-07-16 18:36:24 UTC
  • mfrom: (3514.2.19 weave_merge)
  • Revision ID: pqm@pqm.ubuntu.com-20080716183624-7yu6n1raw6sa4x80
(jam) Restore the real Weave merge for --format=weave.

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