/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: Aaron Bentley
  • Date: 2008-10-28 10:43:48 UTC
  • mfrom: (0.15.43 unshelve)
  • mto: (0.16.95 shelf-ui)
  • mto: This revision was merged to the branch mainline in revision 3820.
  • Revision ID: aaron@aaronbentley.com-20081028104348-buezeiob2p7xj6i1
merge unshelve into shelf-manager

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