/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/btree_index.py

Move config to a separate file, support BranchConfig.username().

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
#
17
 
 
18
 
"""B+Tree indices"""
19
 
 
20
 
import array
21
 
import bisect
22
 
from bisect import bisect_right
23
 
from copy import deepcopy
24
 
import math
25
 
import struct
26
 
import tempfile
27
 
import zlib
28
 
 
29
 
from bzrlib import (
30
 
    chunk_writer,
31
 
    debug,
32
 
    errors,
33
 
    index,
34
 
    lru_cache,
35
 
    osutils,
36
 
    trace,
37
 
    )
38
 
from bzrlib.index import _OPTION_NODE_REFS, _OPTION_KEY_ELEMENTS, _OPTION_LEN
39
 
from bzrlib.transport import get_transport
40
 
 
41
 
 
42
 
_BTSIGNATURE = "B+Tree Graph Index 2\n"
43
 
_OPTION_ROW_LENGTHS = "row_lengths="
44
 
_LEAF_FLAG = "type=leaf\n"
45
 
_INTERNAL_FLAG = "type=internal\n"
46
 
_INTERNAL_OFFSET = "offset="
47
 
 
48
 
_RESERVED_HEADER_BYTES = 120
49
 
_PAGE_SIZE = 4096
50
 
 
51
 
# 4K per page: 4MB - 1000 entries
52
 
_NODE_CACHE_SIZE = 1000
53
 
 
54
 
 
55
 
class _BuilderRow(object):
56
 
    """The stored state accumulated while writing out a row in the index.
57
 
 
58
 
    :ivar spool: A temporary file used to accumulate nodes for this row
59
 
        in the tree.
60
 
    :ivar nodes: The count of nodes emitted so far.
61
 
    """
62
 
 
63
 
    def __init__(self):
64
 
        """Create a _BuilderRow."""
65
 
        self.nodes = 0
66
 
        self.spool = tempfile.TemporaryFile()
67
 
        self.writer = None
68
 
 
69
 
    def finish_node(self, pad=True):
70
 
        byte_lines, _, padding = self.writer.finish()
71
 
        if self.nodes == 0:
72
 
            # padded note:
73
 
            self.spool.write("\x00" * _RESERVED_HEADER_BYTES)
74
 
        skipped_bytes = 0
75
 
        if not pad and padding:
76
 
            del byte_lines[-1]
77
 
            skipped_bytes = padding
78
 
        self.spool.writelines(byte_lines)
79
 
        remainder = (self.spool.tell() + skipped_bytes) % _PAGE_SIZE
80
 
        if remainder != 0:
81
 
            raise AssertionError("incorrect node length: %d, %d"
82
 
                                 % (self.spool.tell(), remainder))
83
 
        self.nodes += 1
84
 
        self.writer = None
85
 
 
86
 
 
87
 
class _InternalBuilderRow(_BuilderRow):
88
 
    """The stored state accumulated while writing out internal rows."""
89
 
 
90
 
    def finish_node(self, pad=True):
91
 
        if not pad:
92
 
            raise AssertionError("Must pad internal nodes only.")
93
 
        _BuilderRow.finish_node(self)
94
 
 
95
 
 
96
 
class _LeafBuilderRow(_BuilderRow):
97
 
    """The stored state accumulated while writing out a leaf rows."""
98
 
 
99
 
 
100
 
class BTreeBuilder(index.GraphIndexBuilder):
101
 
    """A Builder for B+Tree based Graph indices.
102
 
 
103
 
    The resulting graph has the structure:
104
 
 
105
 
    _SIGNATURE OPTIONS NODES
106
 
    _SIGNATURE     := 'B+Tree Graph Index 1' NEWLINE
107
 
    OPTIONS        := REF_LISTS KEY_ELEMENTS LENGTH
108
 
    REF_LISTS      := 'node_ref_lists=' DIGITS NEWLINE
109
 
    KEY_ELEMENTS   := 'key_elements=' DIGITS NEWLINE
110
 
    LENGTH         := 'len=' DIGITS NEWLINE
111
 
    ROW_LENGTHS    := 'row_lengths' DIGITS (COMMA DIGITS)*
112
 
    NODES          := NODE_COMPRESSED*
113
 
    NODE_COMPRESSED:= COMPRESSED_BYTES{4096}
114
 
    NODE_RAW       := INTERNAL | LEAF
115
 
    INTERNAL       := INTERNAL_FLAG POINTERS
116
 
    LEAF           := LEAF_FLAG ROWS
117
 
    KEY_ELEMENT    := Not-whitespace-utf8
118
 
    KEY            := KEY_ELEMENT (NULL KEY_ELEMENT)*
119
 
    ROWS           := ROW*
120
 
    ROW            := KEY NULL ABSENT? NULL REFERENCES NULL VALUE NEWLINE
121
 
    ABSENT         := 'a'
122
 
    REFERENCES     := REFERENCE_LIST (TAB REFERENCE_LIST){node_ref_lists - 1}
123
 
    REFERENCE_LIST := (REFERENCE (CR REFERENCE)*)?
124
 
    REFERENCE      := KEY
125
 
    VALUE          := no-newline-no-null-bytes
126
 
    """
127
 
 
128
 
    def __init__(self, reference_lists=0, key_elements=1, spill_at=100000):
129
 
        """See GraphIndexBuilder.__init__.
130
 
 
131
 
        :param spill_at: Optional parameter controlling the maximum number
132
 
            of nodes that BTreeBuilder will hold in memory.
133
 
        """
134
 
        index.GraphIndexBuilder.__init__(self, reference_lists=reference_lists,
135
 
            key_elements=key_elements)
136
 
        self._spill_at = spill_at
137
 
        self._backing_indices = []
138
 
        # A map of {key: (node_refs, value)}
139
 
        self._nodes = {}
140
 
        # Indicate it hasn't been built yet
141
 
        self._nodes_by_key = None
142
 
        self._optimize_for_size = False
143
 
 
144
 
    def add_node(self, key, value, references=()):
145
 
        """Add a node to the index.
146
 
 
147
 
        If adding the node causes the builder to reach its spill_at threshold,
148
 
        disk spilling will be triggered.
149
 
 
150
 
        :param key: The key. keys are non-empty tuples containing
151
 
            as many whitespace-free utf8 bytestrings as the key length
152
 
            defined for this index.
153
 
        :param references: An iterable of iterables of keys. Each is a
154
 
            reference to another key.
155
 
        :param value: The value to associate with the key. It may be any
156
 
            bytes as long as it does not contain \0 or \n.
157
 
        """
158
 
        # we don't care about absent_references
159
 
        node_refs, _ = self._check_key_ref_value(key, references, value)
160
 
        if key in self._nodes:
161
 
            raise errors.BadIndexDuplicateKey(key, self)
162
 
        self._nodes[key] = (node_refs, value)
163
 
        self._keys.add(key)
164
 
        if self._nodes_by_key is not None and self._key_length > 1:
165
 
            self._update_nodes_by_key(key, value, node_refs)
166
 
        if len(self._keys) < self._spill_at:
167
 
            return
168
 
        self._spill_mem_keys_to_disk()
169
 
 
170
 
    def _spill_mem_keys_to_disk(self):
171
 
        """Write the in memory keys down to disk to cap memory consumption.
172
 
 
173
 
        If we already have some keys written to disk, we will combine them so
174
 
        as to preserve the sorted order.  The algorithm for combining uses
175
 
        powers of two.  So on the first spill, write all mem nodes into a
176
 
        single index. On the second spill, combine the mem nodes with the nodes
177
 
        on disk to create a 2x sized disk index and get rid of the first index.
178
 
        On the third spill, create a single new disk index, which will contain
179
 
        the mem nodes, and preserve the existing 2x sized index.  On the fourth,
180
 
        combine mem with the first and second indexes, creating a new one of
181
 
        size 4x. On the fifth create a single new one, etc.
182
 
        """
183
 
        iterators_to_combine = [self._iter_mem_nodes()]
184
 
        pos = -1
185
 
        for pos, backing in enumerate(self._backing_indices):
186
 
            if backing is None:
187
 
                pos -= 1
188
 
                break
189
 
            iterators_to_combine.append(backing.iter_all_entries())
190
 
        backing_pos = pos + 1
191
 
        new_backing_file, size = \
192
 
            self._write_nodes(self._iter_smallest(iterators_to_combine),
193
 
                              allow_optimize=False)
194
 
        dir_path, base_name = osutils.split(new_backing_file.name)
195
 
        # Note: The transport here isn't strictly needed, because we will use
196
 
        #       direct access to the new_backing._file object
197
 
        new_backing = BTreeGraphIndex(get_transport(dir_path),
198
 
                                      base_name, size)
199
 
        # GC will clean up the file
200
 
        new_backing._file = new_backing_file
201
 
        if len(self._backing_indices) == backing_pos:
202
 
            self._backing_indices.append(None)
203
 
        self._backing_indices[backing_pos] = new_backing
204
 
        for pos in range(backing_pos):
205
 
            self._backing_indices[pos] = None
206
 
        self._keys = set()
207
 
        self._nodes = {}
208
 
        self._nodes_by_key = None
209
 
 
210
 
    def add_nodes(self, nodes):
211
 
        """Add nodes to the index.
212
 
 
213
 
        :param nodes: An iterable of (key, node_refs, value) entries to add.
214
 
        """
215
 
        if self.reference_lists:
216
 
            for (key, value, node_refs) in nodes:
217
 
                self.add_node(key, value, node_refs)
218
 
        else:
219
 
            for (key, value) in nodes:
220
 
                self.add_node(key, value)
221
 
 
222
 
    def _iter_mem_nodes(self):
223
 
        """Iterate over the nodes held in memory."""
224
 
        nodes = self._nodes
225
 
        if self.reference_lists:
226
 
            for key in sorted(nodes):
227
 
                references, value = nodes[key]
228
 
                yield self, key, value, references
229
 
        else:
230
 
            for key in sorted(nodes):
231
 
                references, value = nodes[key]
232
 
                yield self, key, value
233
 
 
234
 
    def _iter_smallest(self, iterators_to_combine):
235
 
        if len(iterators_to_combine) == 1:
236
 
            for value in iterators_to_combine[0]:
237
 
                yield value
238
 
            return
239
 
        current_values = []
240
 
        for iterator in iterators_to_combine:
241
 
            try:
242
 
                current_values.append(iterator.next())
243
 
            except StopIteration:
244
 
                current_values.append(None)
245
 
        last = None
246
 
        while True:
247
 
            # Decorate candidates with the value to allow 2.4's min to be used.
248
 
            candidates = [(item[1][1], item) for item
249
 
                in enumerate(current_values) if item[1] is not None]
250
 
            if not len(candidates):
251
 
                return
252
 
            selected = min(candidates)
253
 
            # undecorate back to (pos, node)
254
 
            selected = selected[1]
255
 
            if last == selected[1][1]:
256
 
                raise errors.BadIndexDuplicateKey(last, self)
257
 
            last = selected[1][1]
258
 
            # Yield, with self as the index
259
 
            yield (self,) + selected[1][1:]
260
 
            pos = selected[0]
261
 
            try:
262
 
                current_values[pos] = iterators_to_combine[pos].next()
263
 
            except StopIteration:
264
 
                current_values[pos] = None
265
 
 
266
 
    def _add_key(self, string_key, line, rows, allow_optimize=True):
267
 
        """Add a key to the current chunk.
268
 
 
269
 
        :param string_key: The key to add.
270
 
        :param line: The fully serialised key and value.
271
 
        :param allow_optimize: If set to False, prevent setting the optimize
272
 
            flag when writing out. This is used by the _spill_mem_keys_to_disk
273
 
            functionality.
274
 
        """
275
 
        if rows[-1].writer is None:
276
 
            # opening a new leaf chunk;
277
 
            for pos, internal_row in enumerate(rows[:-1]):
278
 
                # flesh out any internal nodes that are needed to
279
 
                # preserve the height of the tree
280
 
                if internal_row.writer is None:
281
 
                    length = _PAGE_SIZE
282
 
                    if internal_row.nodes == 0:
283
 
                        length -= _RESERVED_HEADER_BYTES # padded
284
 
                    if allow_optimize:
285
 
                        optimize_for_size = self._optimize_for_size
286
 
                    else:
287
 
                        optimize_for_size = False
288
 
                    internal_row.writer = chunk_writer.ChunkWriter(length, 0,
289
 
                        optimize_for_size=optimize_for_size)
290
 
                    internal_row.writer.write(_INTERNAL_FLAG)
291
 
                    internal_row.writer.write(_INTERNAL_OFFSET +
292
 
                        str(rows[pos + 1].nodes) + "\n")
293
 
            # add a new leaf
294
 
            length = _PAGE_SIZE
295
 
            if rows[-1].nodes == 0:
296
 
                length -= _RESERVED_HEADER_BYTES # padded
297
 
            rows[-1].writer = chunk_writer.ChunkWriter(length,
298
 
                optimize_for_size=self._optimize_for_size)
299
 
            rows[-1].writer.write(_LEAF_FLAG)
300
 
        if rows[-1].writer.write(line):
301
 
            # this key did not fit in the node:
302
 
            rows[-1].finish_node()
303
 
            key_line = string_key + "\n"
304
 
            new_row = True
305
 
            for row in reversed(rows[:-1]):
306
 
                # Mark the start of the next node in the node above. If it
307
 
                # doesn't fit then propogate upwards until we find one that
308
 
                # it does fit into.
309
 
                if row.writer.write(key_line):
310
 
                    row.finish_node()
311
 
                else:
312
 
                    # We've found a node that can handle the pointer.
313
 
                    new_row = False
314
 
                    break
315
 
            # If we reached the current root without being able to mark the
316
 
            # division point, then we need a new root:
317
 
            if new_row:
318
 
                # We need a new row
319
 
                if 'index' in debug.debug_flags:
320
 
                    trace.mutter('Inserting new global row.')
321
 
                new_row = _InternalBuilderRow()
322
 
                reserved_bytes = 0
323
 
                rows.insert(0, new_row)
324
 
                # This will be padded, hence the -100
325
 
                new_row.writer = chunk_writer.ChunkWriter(
326
 
                    _PAGE_SIZE - _RESERVED_HEADER_BYTES,
327
 
                    reserved_bytes,
328
 
                    optimize_for_size=self._optimize_for_size)
329
 
                new_row.writer.write(_INTERNAL_FLAG)
330
 
                new_row.writer.write(_INTERNAL_OFFSET +
331
 
                    str(rows[1].nodes - 1) + "\n")
332
 
                new_row.writer.write(key_line)
333
 
            self._add_key(string_key, line, rows, allow_optimize=allow_optimize)
334
 
 
335
 
    def _write_nodes(self, node_iterator, allow_optimize=True):
336
 
        """Write node_iterator out as a B+Tree.
337
 
 
338
 
        :param node_iterator: An iterator of sorted nodes. Each node should
339
 
            match the output given by iter_all_entries.
340
 
        :param allow_optimize: If set to False, prevent setting the optimize
341
 
            flag when writing out. This is used by the _spill_mem_keys_to_disk
342
 
            functionality.
343
 
        :return: A file handle for a temporary file containing a B+Tree for
344
 
            the nodes.
345
 
        """
346
 
        # The index rows - rows[0] is the root, rows[1] is the layer under it
347
 
        # etc.
348
 
        rows = []
349
 
        # forward sorted by key. In future we may consider topological sorting,
350
 
        # at the cost of table scans for direct lookup, or a second index for
351
 
        # direct lookup
352
 
        key_count = 0
353
 
        # A stack with the number of nodes of each size. 0 is the root node
354
 
        # and must always be 1 (if there are any nodes in the tree).
355
 
        self.row_lengths = []
356
 
        # Loop over all nodes adding them to the bottom row
357
 
        # (rows[-1]). When we finish a chunk in a row,
358
 
        # propogate the key that didn't fit (comes after the chunk) to the
359
 
        # row above, transitively.
360
 
        for node in node_iterator:
361
 
            if key_count == 0:
362
 
                # First key triggers the first row
363
 
                rows.append(_LeafBuilderRow())
364
 
            key_count += 1
365
 
            string_key, line = _btree_serializer._flatten_node(node,
366
 
                                    self.reference_lists)
367
 
            self._add_key(string_key, line, rows, allow_optimize=allow_optimize)
368
 
        for row in reversed(rows):
369
 
            pad = (type(row) != _LeafBuilderRow)
370
 
            row.finish_node(pad=pad)
371
 
        result = tempfile.NamedTemporaryFile(prefix='bzr-index-')
372
 
        lines = [_BTSIGNATURE]
373
 
        lines.append(_OPTION_NODE_REFS + str(self.reference_lists) + '\n')
374
 
        lines.append(_OPTION_KEY_ELEMENTS + str(self._key_length) + '\n')
375
 
        lines.append(_OPTION_LEN + str(key_count) + '\n')
376
 
        row_lengths = [row.nodes for row in rows]
377
 
        lines.append(_OPTION_ROW_LENGTHS + ','.join(map(str, row_lengths)) + '\n')
378
 
        result.writelines(lines)
379
 
        position = sum(map(len, lines))
380
 
        root_row = True
381
 
        if position > _RESERVED_HEADER_BYTES:
382
 
            raise AssertionError("Could not fit the header in the"
383
 
                                 " reserved space: %d > %d"
384
 
                                 % (position, _RESERVED_HEADER_BYTES))
385
 
        # write the rows out:
386
 
        for row in rows:
387
 
            reserved = _RESERVED_HEADER_BYTES # reserved space for first node
388
 
            row.spool.flush()
389
 
            row.spool.seek(0)
390
 
            # copy nodes to the finalised file.
391
 
            # Special case the first node as it may be prefixed
392
 
            node = row.spool.read(_PAGE_SIZE)
393
 
            result.write(node[reserved:])
394
 
            result.write("\x00" * (reserved - position))
395
 
            position = 0 # Only the root row actually has an offset
396
 
            copied_len = osutils.pumpfile(row.spool, result)
397
 
            if copied_len != (row.nodes - 1) * _PAGE_SIZE:
398
 
                if type(row) != _LeafBuilderRow:
399
 
                    raise AssertionError("Incorrect amount of data copied"
400
 
                        " expected: %d, got: %d"
401
 
                        % ((row.nodes - 1) * _PAGE_SIZE,
402
 
                           copied_len))
403
 
        result.flush()
404
 
        size = result.tell()
405
 
        result.seek(0)
406
 
        return result, size
407
 
 
408
 
    def finish(self):
409
 
        """Finalise the index.
410
 
 
411
 
        :return: A file handle for a temporary file containing the nodes added
412
 
            to the index.
413
 
        """
414
 
        return self._write_nodes(self.iter_all_entries())[0]
415
 
 
416
 
    def iter_all_entries(self):
417
 
        """Iterate over all keys within the index
418
 
 
419
 
        :return: An iterable of (index, key, reference_lists, value). There is no
420
 
            defined order for the result iteration - it will be in the most
421
 
            efficient order for the index (in this case dictionary hash order).
422
 
        """
423
 
        if 'evil' in debug.debug_flags:
424
 
            trace.mutter_callsite(3,
425
 
                "iter_all_entries scales with size of history.")
426
 
        # Doing serial rather than ordered would be faster; but this shouldn't
427
 
        # be getting called routinely anyway.
428
 
        iterators = [self._iter_mem_nodes()]
429
 
        for backing in self._backing_indices:
430
 
            if backing is not None:
431
 
                iterators.append(backing.iter_all_entries())
432
 
        if len(iterators) == 1:
433
 
            return iterators[0]
434
 
        return self._iter_smallest(iterators)
435
 
 
436
 
    def iter_entries(self, keys):
437
 
        """Iterate over keys within the index.
438
 
 
439
 
        :param keys: An iterable providing the keys to be retrieved.
440
 
        :return: An iterable of (index, key, value, reference_lists). There is no
441
 
            defined order for the result iteration - it will be in the most
442
 
            efficient order for the index (keys iteration order in this case).
443
 
        """
444
 
        keys = set(keys)
445
 
        local_keys = keys.intersection(self._keys)
446
 
        if self.reference_lists:
447
 
            for key in local_keys:
448
 
                node = self._nodes[key]
449
 
                yield self, key, node[1], node[0]
450
 
        else:
451
 
            for key in local_keys:
452
 
                node = self._nodes[key]
453
 
                yield self, key, node[1]
454
 
        # Find things that are in backing indices that have not been handled
455
 
        # yet.
456
 
        if not self._backing_indices:
457
 
            return # We won't find anything there either
458
 
        # Remove all of the keys that we found locally
459
 
        keys.difference_update(local_keys)
460
 
        for backing in self._backing_indices:
461
 
            if backing is None:
462
 
                continue
463
 
            if not keys:
464
 
                return
465
 
            for node in backing.iter_entries(keys):
466
 
                keys.remove(node[1])
467
 
                yield (self,) + node[1:]
468
 
 
469
 
    def iter_entries_prefix(self, keys):
470
 
        """Iterate over keys within the index using prefix matching.
471
 
 
472
 
        Prefix matching is applied within the tuple of a key, not to within
473
 
        the bytestring of each key element. e.g. if you have the keys ('foo',
474
 
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
475
 
        only the former key is returned.
476
 
 
477
 
        :param keys: An iterable providing the key prefixes to be retrieved.
478
 
            Each key prefix takes the form of a tuple the length of a key, but
479
 
            with the last N elements 'None' rather than a regular bytestring.
480
 
            The first element cannot be 'None'.
481
 
        :return: An iterable as per iter_all_entries, but restricted to the
482
 
            keys with a matching prefix to those supplied. No additional keys
483
 
            will be returned, and every match that is in the index will be
484
 
            returned.
485
 
        """
486
 
        # XXX: To much duplication with the GraphIndex class; consider finding
487
 
        # a good place to pull out the actual common logic.
488
 
        keys = set(keys)
489
 
        if not keys:
490
 
            return
491
 
        for backing in self._backing_indices:
492
 
            if backing is None:
493
 
                continue
494
 
            for node in backing.iter_entries_prefix(keys):
495
 
                yield (self,) + node[1:]
496
 
        if self._key_length == 1:
497
 
            for key in keys:
498
 
                # sanity check
499
 
                if key[0] is None:
500
 
                    raise errors.BadIndexKey(key)
501
 
                if len(key) != self._key_length:
502
 
                    raise errors.BadIndexKey(key)
503
 
                try:
504
 
                    node = self._nodes[key]
505
 
                except KeyError:
506
 
                    continue
507
 
                if self.reference_lists:
508
 
                    yield self, key, node[1], node[0]
509
 
                else:
510
 
                    yield self, key, node[1]
511
 
            return
512
 
        for key in keys:
513
 
            # sanity check
514
 
            if key[0] is None:
515
 
                raise errors.BadIndexKey(key)
516
 
            if len(key) != self._key_length:
517
 
                raise errors.BadIndexKey(key)
518
 
            # find what it refers to:
519
 
            key_dict = self._get_nodes_by_key()
520
 
            elements = list(key)
521
 
            # find the subdict to return
522
 
            try:
523
 
                while len(elements) and elements[0] is not None:
524
 
                    key_dict = key_dict[elements[0]]
525
 
                    elements.pop(0)
526
 
            except KeyError:
527
 
                # a non-existant lookup.
528
 
                continue
529
 
            if len(elements):
530
 
                dicts = [key_dict]
531
 
                while dicts:
532
 
                    key_dict = dicts.pop(-1)
533
 
                    # can't be empty or would not exist
534
 
                    item, value = key_dict.iteritems().next()
535
 
                    if type(value) == dict:
536
 
                        # push keys
537
 
                        dicts.extend(key_dict.itervalues())
538
 
                    else:
539
 
                        # yield keys
540
 
                        for value in key_dict.itervalues():
541
 
                            yield (self, ) + value
542
 
            else:
543
 
                yield (self, ) + key_dict
544
 
 
545
 
    def _get_nodes_by_key(self):
546
 
        if self._nodes_by_key is None:
547
 
            nodes_by_key = {}
548
 
            if self.reference_lists:
549
 
                for key, (references, value) in self._nodes.iteritems():
550
 
                    key_dict = nodes_by_key
551
 
                    for subkey in key[:-1]:
552
 
                        key_dict = key_dict.setdefault(subkey, {})
553
 
                    key_dict[key[-1]] = key, value, references
554
 
            else:
555
 
                for key, (references, value) in self._nodes.iteritems():
556
 
                    key_dict = nodes_by_key
557
 
                    for subkey in key[:-1]:
558
 
                        key_dict = key_dict.setdefault(subkey, {})
559
 
                    key_dict[key[-1]] = key, value
560
 
            self._nodes_by_key = nodes_by_key
561
 
        return self._nodes_by_key
562
 
 
563
 
    def key_count(self):
564
 
        """Return an estimate of the number of keys in this index.
565
 
 
566
 
        For InMemoryGraphIndex the estimate is exact.
567
 
        """
568
 
        return len(self._keys) + sum(backing.key_count() for backing in
569
 
            self._backing_indices if backing is not None)
570
 
 
571
 
    def validate(self):
572
 
        """In memory index's have no known corruption at the moment."""
573
 
 
574
 
 
575
 
class _LeafNode(object):
576
 
    """A leaf node for a serialised B+Tree index."""
577
 
 
578
 
    def __init__(self, bytes, key_length, ref_list_length):
579
 
        """Parse bytes to create a leaf node object."""
580
 
        # splitlines mangles the \r delimiters.. don't use it.
581
 
        self.keys = dict(_btree_serializer._parse_leaf_lines(bytes,
582
 
            key_length, ref_list_length))
583
 
 
584
 
 
585
 
class _InternalNode(object):
586
 
    """An internal node for a serialised B+Tree index."""
587
 
 
588
 
    def __init__(self, bytes):
589
 
        """Parse bytes to create an internal node object."""
590
 
        # splitlines mangles the \r delimiters.. don't use it.
591
 
        self.keys = self._parse_lines(bytes.split('\n'))
592
 
 
593
 
    def _parse_lines(self, lines):
594
 
        nodes = []
595
 
        self.offset = int(lines[1][7:])
596
 
        for line in lines[2:]:
597
 
            if line == '':
598
 
                break
599
 
            nodes.append(tuple(line.split('\0')))
600
 
        return nodes
601
 
 
602
 
 
603
 
class BTreeGraphIndex(object):
604
 
    """Access to nodes via the standard GraphIndex interface for B+Tree's.
605
 
 
606
 
    Individual nodes are held in a LRU cache. This holds the root node in
607
 
    memory except when very large walks are done.
608
 
    """
609
 
 
610
 
    def __init__(self, transport, name, size):
611
 
        """Create a B+Tree index object on the index name.
612
 
 
613
 
        :param transport: The transport to read data for the index from.
614
 
        :param name: The file name of the index on transport.
615
 
        :param size: Optional size of the index in bytes. This allows
616
 
            compatibility with the GraphIndex API, as well as ensuring that
617
 
            the initial read (to read the root node header) can be done
618
 
            without over-reading even on empty indices, and on small indices
619
 
            allows single-IO to read the entire index.
620
 
        """
621
 
        self._transport = transport
622
 
        self._name = name
623
 
        self._size = size
624
 
        self._file = None
625
 
        self._recommended_pages = self._compute_recommended_pages()
626
 
        self._root_node = None
627
 
        # Default max size is 100,000 leave values
628
 
        self._leaf_value_cache = None # lru_cache.LRUCache(100*1000)
629
 
        self._leaf_node_cache = lru_cache.LRUCache(_NODE_CACHE_SIZE)
630
 
        self._internal_node_cache = lru_cache.LRUCache()
631
 
        self._key_count = None
632
 
        self._row_lengths = None
633
 
        self._row_offsets = None # Start of each row, [-1] is the end
634
 
 
635
 
    def __eq__(self, other):
636
 
        """Equal when self and other were created with the same parameters."""
637
 
        return (
638
 
            type(self) == type(other) and
639
 
            self._transport == other._transport and
640
 
            self._name == other._name and
641
 
            self._size == other._size)
642
 
 
643
 
    def __ne__(self, other):
644
 
        return not self.__eq__(other)
645
 
 
646
 
    def _get_and_cache_nodes(self, nodes):
647
 
        """Read nodes and cache them in the lru.
648
 
 
649
 
        The nodes list supplied is sorted and then read from disk, each node
650
 
        being inserted it into the _node_cache.
651
 
 
652
 
        Note: Asking for more nodes than the _node_cache can contain will
653
 
        result in some of the results being immediately discarded, to prevent
654
 
        this an assertion is raised if more nodes are asked for than are
655
 
        cachable.
656
 
 
657
 
        :return: A dict of {node_pos: node}
658
 
        """
659
 
        found = {}
660
 
        start_of_leaves = None
661
 
        for node_pos, node in self._read_nodes(sorted(nodes)):
662
 
            if node_pos == 0: # Special case
663
 
                self._root_node = node
664
 
            else:
665
 
                if start_of_leaves is None:
666
 
                    start_of_leaves = self._row_offsets[-2]
667
 
                if node_pos < start_of_leaves:
668
 
                    self._internal_node_cache.add(node_pos, node)
669
 
                else:
670
 
                    self._leaf_node_cache.add(node_pos, node)
671
 
            found[node_pos] = node
672
 
        return found
673
 
 
674
 
    def _compute_recommended_pages(self):
675
 
        """Convert transport's recommended_page_size into btree pages.
676
 
 
677
 
        recommended_page_size is in bytes, we want to know how many _PAGE_SIZE
678
 
        pages fit in that length.
679
 
        """
680
 
        recommended_read = self._transport.recommended_page_size()
681
 
        recommended_pages = int(math.ceil(recommended_read /
682
 
                                          float(_PAGE_SIZE)))
683
 
        return recommended_pages
684
 
 
685
 
    def _compute_total_pages_in_index(self):
686
 
        """How many pages are in the index.
687
 
 
688
 
        If we have read the header we will use the value stored there.
689
 
        Otherwise it will be computed based on the length of the index.
690
 
        """
691
 
        if self._size is None:
692
 
            raise AssertionError('_compute_total_pages_in_index should not be'
693
 
                                 ' called when self._size is None')
694
 
        if self._root_node is not None:
695
 
            # This is the number of pages as defined by the header
696
 
            return self._row_offsets[-1]
697
 
        # This is the number of pages as defined by the size of the index. They
698
 
        # should be indentical.
699
 
        total_pages = int(math.ceil(self._size / float(_PAGE_SIZE)))
700
 
        return total_pages
701
 
 
702
 
    def _expand_offsets(self, offsets):
703
 
        """Find extra pages to download.
704
 
 
705
 
        The idea is that we always want to make big-enough requests (like 64kB
706
 
        for http), so that we don't waste round trips. So given the entries
707
 
        that we already have cached and the new pages being downloaded figure
708
 
        out what other pages we might want to read.
709
 
 
710
 
        See also doc/developers/btree_index_prefetch.txt for more details.
711
 
 
712
 
        :param offsets: The offsets to be read
713
 
        :return: A list of offsets to download
714
 
        """
715
 
        if 'index' in debug.debug_flags:
716
 
            trace.mutter('expanding: %s\toffsets: %s', self._name, offsets)
717
 
 
718
 
        if len(offsets) >= self._recommended_pages:
719
 
            # Don't add more, we are already requesting more than enough
720
 
            if 'index' in debug.debug_flags:
721
 
                trace.mutter('  not expanding large request (%s >= %s)',
722
 
                             len(offsets), self._recommended_pages)
723
 
            return offsets
724
 
        if self._size is None:
725
 
            # Don't try anything, because we don't know where the file ends
726
 
            if 'index' in debug.debug_flags:
727
 
                trace.mutter('  not expanding without knowing index size')
728
 
            return offsets
729
 
        total_pages = self._compute_total_pages_in_index()
730
 
        cached_offsets = self._get_offsets_to_cached_pages()
731
 
        # If reading recommended_pages would read the rest of the index, just
732
 
        # do so.
733
 
        if total_pages - len(cached_offsets) <= self._recommended_pages:
734
 
            # Read whatever is left
735
 
            if cached_offsets:
736
 
                expanded = [x for x in xrange(total_pages)
737
 
                               if x not in cached_offsets]
738
 
            else:
739
 
                expanded = range(total_pages)
740
 
            if 'index' in debug.debug_flags:
741
 
                trace.mutter('  reading all unread pages: %s', expanded)
742
 
            return expanded
743
 
 
744
 
        if self._root_node is None:
745
 
            # ATM on the first read of the root node of a large index, we don't
746
 
            # bother pre-reading any other pages. This is because the
747
 
            # likelyhood of actually reading interesting pages is very low.
748
 
            # See doc/developers/btree_index_prefetch.txt for a discussion, and
749
 
            # a possible implementation when we are guessing that the second
750
 
            # layer index is small
751
 
            final_offsets = offsets
752
 
        else:
753
 
            tree_depth = len(self._row_lengths)
754
 
            if len(cached_offsets) < tree_depth and len(offsets) == 1:
755
 
                # We haven't read enough to justify expansion
756
 
                # If we are only going to read the root node, and 1 leaf node,
757
 
                # then it isn't worth expanding our request. Once we've read at
758
 
                # least 2 nodes, then we are probably doing a search, and we
759
 
                # start expanding our requests.
760
 
                if 'index' in debug.debug_flags:
761
 
                    trace.mutter('  not expanding on first reads')
762
 
                return offsets
763
 
            final_offsets = self._expand_to_neighbors(offsets, cached_offsets,
764
 
                                                      total_pages)
765
 
 
766
 
        final_offsets = sorted(final_offsets)
767
 
        if 'index' in debug.debug_flags:
768
 
            trace.mutter('expanded:  %s', final_offsets)
769
 
        return final_offsets
770
 
 
771
 
    def _expand_to_neighbors(self, offsets, cached_offsets, total_pages):
772
 
        """Expand requests to neighbors until we have enough pages.
773
 
 
774
 
        This is called from _expand_offsets after policy has determined that we
775
 
        want to expand.
776
 
        We only want to expand requests within a given layer. We cheat a little
777
 
        bit and assume all requests will be in the same layer. This is true
778
 
        given the current design, but if it changes this algorithm may perform
779
 
        oddly.
780
 
 
781
 
        :param offsets: requested offsets
782
 
        :param cached_offsets: offsets for pages we currently have cached
783
 
        :return: A set() of offsets after expansion
784
 
        """
785
 
        final_offsets = set(offsets)
786
 
        first = end = None
787
 
        new_tips = set(final_offsets)
788
 
        while len(final_offsets) < self._recommended_pages and new_tips:
789
 
            next_tips = set()
790
 
            for pos in new_tips:
791
 
                if first is None:
792
 
                    first, end = self._find_layer_first_and_end(pos)
793
 
                previous = pos - 1
794
 
                if (previous > 0
795
 
                    and previous not in cached_offsets
796
 
                    and previous not in final_offsets
797
 
                    and previous >= first):
798
 
                    next_tips.add(previous)
799
 
                after = pos + 1
800
 
                if (after < total_pages
801
 
                    and after not in cached_offsets
802
 
                    and after not in final_offsets
803
 
                    and after < end):
804
 
                    next_tips.add(after)
805
 
                # This would keep us from going bigger than
806
 
                # recommended_pages by only expanding the first offsets.
807
 
                # However, if we are making a 'wide' request, it is
808
 
                # reasonable to expand all points equally.
809
 
                # if len(final_offsets) > recommended_pages:
810
 
                #     break
811
 
            final_offsets.update(next_tips)
812
 
            new_tips = next_tips
813
 
        return final_offsets
814
 
 
815
 
    def external_references(self, ref_list_num):
816
 
        if self._root_node is None:
817
 
            self._get_root_node()
818
 
        if ref_list_num + 1 > self.node_ref_lists:
819
 
            raise ValueError('No ref list %d, index has %d ref lists'
820
 
                % (ref_list_num, self.node_ref_lists))
821
 
        keys = set()
822
 
        refs = set()
823
 
        for node in self.iter_all_entries():
824
 
            keys.add(node[1])
825
 
            refs.update(node[3][ref_list_num])
826
 
        return refs - keys
827
 
 
828
 
    def _find_layer_first_and_end(self, offset):
829
 
        """Find the start/stop nodes for the layer corresponding to offset.
830
 
 
831
 
        :return: (first, end)
832
 
            first is the first node in this layer
833
 
            end is the first node of the next layer
834
 
        """
835
 
        first = end = 0
836
 
        for roffset in self._row_offsets:
837
 
            first = end
838
 
            end = roffset
839
 
            if offset < roffset:
840
 
                break
841
 
        return first, end
842
 
 
843
 
    def _get_offsets_to_cached_pages(self):
844
 
        """Determine what nodes we already have cached."""
845
 
        cached_offsets = set(self._internal_node_cache.keys())
846
 
        cached_offsets.update(self._leaf_node_cache.keys())
847
 
        if self._root_node is not None:
848
 
            cached_offsets.add(0)
849
 
        return cached_offsets
850
 
 
851
 
    def _get_root_node(self):
852
 
        if self._root_node is None:
853
 
            # We may not have a root node yet
854
 
            self._get_internal_nodes([0])
855
 
        return self._root_node
856
 
 
857
 
    def _get_nodes(self, cache, node_indexes):
858
 
        found = {}
859
 
        needed = []
860
 
        for idx in node_indexes:
861
 
            if idx == 0 and self._root_node is not None:
862
 
                found[0] = self._root_node
863
 
                continue
864
 
            try:
865
 
                found[idx] = cache[idx]
866
 
            except KeyError:
867
 
                needed.append(idx)
868
 
        if not needed:
869
 
            return found
870
 
        needed = self._expand_offsets(needed)
871
 
        found.update(self._get_and_cache_nodes(needed))
872
 
        return found
873
 
 
874
 
    def _get_internal_nodes(self, node_indexes):
875
 
        """Get a node, from cache or disk.
876
 
 
877
 
        After getting it, the node will be cached.
878
 
        """
879
 
        return self._get_nodes(self._internal_node_cache, node_indexes)
880
 
 
881
 
    def _cache_leaf_values(self, nodes):
882
 
        """Cache directly from key => value, skipping the btree."""
883
 
        if self._leaf_value_cache is not None:
884
 
            for node in nodes.itervalues():
885
 
                for key, value in node.keys.iteritems():
886
 
                    if key in self._leaf_value_cache:
887
 
                        # Don't add the rest of the keys, we've seen this node
888
 
                        # before.
889
 
                        break
890
 
                    self._leaf_value_cache[key] = value
891
 
 
892
 
    def _get_leaf_nodes(self, node_indexes):
893
 
        """Get a bunch of nodes, from cache or disk."""
894
 
        found = self._get_nodes(self._leaf_node_cache, node_indexes)
895
 
        self._cache_leaf_values(found)
896
 
        return found
897
 
 
898
 
    def iter_all_entries(self):
899
 
        """Iterate over all keys within the index.
900
 
 
901
 
        :return: An iterable of (index, key, value) or (index, key, value, reference_lists).
902
 
            The former tuple is used when there are no reference lists in the
903
 
            index, making the API compatible with simple key:value index types.
904
 
            There is no defined order for the result iteration - it will be in
905
 
            the most efficient order for the index.
906
 
        """
907
 
        if 'evil' in debug.debug_flags:
908
 
            trace.mutter_callsite(3,
909
 
                "iter_all_entries scales with size of history.")
910
 
        if not self.key_count():
911
 
            return
912
 
        if self._row_offsets[-1] == 1:
913
 
            # There is only the root node, and we read that via key_count()
914
 
            if self.node_ref_lists:
915
 
                for key, (value, refs) in sorted(self._root_node.keys.items()):
916
 
                    yield (self, key, value, refs)
917
 
            else:
918
 
                for key, (value, refs) in sorted(self._root_node.keys.items()):
919
 
                    yield (self, key, value)
920
 
            return
921
 
        start_of_leaves = self._row_offsets[-2]
922
 
        end_of_leaves = self._row_offsets[-1]
923
 
        needed_offsets = range(start_of_leaves, end_of_leaves)
924
 
        if needed_offsets == [0]:
925
 
            # Special case when we only have a root node, as we have already
926
 
            # read everything
927
 
            nodes = [(0, self._root_node)]
928
 
        else:
929
 
            nodes = self._read_nodes(needed_offsets)
930
 
        # We iterate strictly in-order so that we can use this function
931
 
        # for spilling index builds to disk.
932
 
        if self.node_ref_lists:
933
 
            for _, node in nodes:
934
 
                for key, (value, refs) in sorted(node.keys.items()):
935
 
                    yield (self, key, value, refs)
936
 
        else:
937
 
            for _, node in nodes:
938
 
                for key, (value, refs) in sorted(node.keys.items()):
939
 
                    yield (self, key, value)
940
 
 
941
 
    @staticmethod
942
 
    def _multi_bisect_right(in_keys, fixed_keys):
943
 
        """Find the positions where each 'in_key' would fit in fixed_keys.
944
 
 
945
 
        This is equivalent to doing "bisect_right" on each in_key into
946
 
        fixed_keys
947
 
 
948
 
        :param in_keys: A sorted list of keys to match with fixed_keys
949
 
        :param fixed_keys: A sorted list of keys to match against
950
 
        :return: A list of (integer position, [key list]) tuples.
951
 
        """
952
 
        if not in_keys:
953
 
            return []
954
 
        if not fixed_keys:
955
 
            # no pointers in the fixed_keys list, which means everything must
956
 
            # fall to the left.
957
 
            return [(0, in_keys)]
958
 
 
959
 
        # TODO: Iterating both lists will generally take M + N steps
960
 
        #       Bisecting each key will generally take M * log2 N steps.
961
 
        #       If we had an efficient way to compare, we could pick the method
962
 
        #       based on which has the fewer number of steps.
963
 
        #       There is also the argument that bisect_right is a compiled
964
 
        #       function, so there is even more to be gained.
965
 
        # iter_steps = len(in_keys) + len(fixed_keys)
966
 
        # bisect_steps = len(in_keys) * math.log(len(fixed_keys), 2)
967
 
        if len(in_keys) == 1: # Bisect will always be faster for M = 1
968
 
            return [(bisect_right(fixed_keys, in_keys[0]), in_keys)]
969
 
        # elif bisect_steps < iter_steps:
970
 
        #     offsets = {}
971
 
        #     for key in in_keys:
972
 
        #         offsets.setdefault(bisect_right(fixed_keys, key),
973
 
        #                            []).append(key)
974
 
        #     return [(o, offsets[o]) for o in sorted(offsets)]
975
 
        in_keys_iter = iter(in_keys)
976
 
        fixed_keys_iter = enumerate(fixed_keys)
977
 
        cur_in_key = in_keys_iter.next()
978
 
        cur_fixed_offset, cur_fixed_key = fixed_keys_iter.next()
979
 
 
980
 
        class InputDone(Exception): pass
981
 
        class FixedDone(Exception): pass
982
 
 
983
 
        output = []
984
 
        cur_out = []
985
 
 
986
 
        # TODO: Another possibility is that rather than iterating on each side,
987
 
        #       we could use a combination of bisecting and iterating. For
988
 
        #       example, while cur_in_key < fixed_key, bisect to find its
989
 
        #       point, then iterate all matching keys, then bisect (restricted
990
 
        #       to only the remainder) for the next one, etc.
991
 
        try:
992
 
            while True:
993
 
                if cur_in_key < cur_fixed_key:
994
 
                    cur_keys = []
995
 
                    cur_out = (cur_fixed_offset, cur_keys)
996
 
                    output.append(cur_out)
997
 
                    while cur_in_key < cur_fixed_key:
998
 
                        cur_keys.append(cur_in_key)
999
 
                        try:
1000
 
                            cur_in_key = in_keys_iter.next()
1001
 
                        except StopIteration:
1002
 
                            raise InputDone
1003
 
                    # At this point cur_in_key must be >= cur_fixed_key
1004
 
                # step the cur_fixed_key until we pass the cur key, or walk off
1005
 
                # the end
1006
 
                while cur_in_key >= cur_fixed_key:
1007
 
                    try:
1008
 
                        cur_fixed_offset, cur_fixed_key = fixed_keys_iter.next()
1009
 
                    except StopIteration:
1010
 
                        raise FixedDone
1011
 
        except InputDone:
1012
 
            # We consumed all of the input, nothing more to do
1013
 
            pass
1014
 
        except FixedDone:
1015
 
            # There was some input left, but we consumed all of fixed, so we
1016
 
            # have to add one more for the tail
1017
 
            cur_keys = [cur_in_key]
1018
 
            cur_keys.extend(in_keys_iter)
1019
 
            cur_out = (len(fixed_keys), cur_keys)
1020
 
            output.append(cur_out)
1021
 
        return output
1022
 
 
1023
 
    def iter_entries(self, keys):
1024
 
        """Iterate over keys within the index.
1025
 
 
1026
 
        :param keys: An iterable providing the keys to be retrieved.
1027
 
        :return: An iterable as per iter_all_entries, but restricted to the
1028
 
            keys supplied. No additional keys will be returned, and every
1029
 
            key supplied that is in the index will be returned.
1030
 
        """
1031
 
        # 6 seconds spent in miss_torture using the sorted() line.
1032
 
        # Even with out of order disk IO it seems faster not to sort it when
1033
 
        # large queries are being made.
1034
 
        # However, now that we are doing multi-way bisecting, we need the keys
1035
 
        # in sorted order anyway. We could change the multi-way code to not
1036
 
        # require sorted order. (For example, it bisects for the first node,
1037
 
        # does an in-order search until a key comes before the current point,
1038
 
        # which it then bisects for, etc.)
1039
 
        keys = frozenset(keys)
1040
 
        if not keys:
1041
 
            return
1042
 
 
1043
 
        if not self.key_count():
1044
 
            return
1045
 
 
1046
 
        needed_keys = []
1047
 
        if self._leaf_value_cache is None:
1048
 
            needed_keys = keys
1049
 
        else:
1050
 
            for key in keys:
1051
 
                value = self._leaf_value_cache.get(key, None)
1052
 
                if value is not None:
1053
 
                    # This key is known not to be here, skip it
1054
 
                    value, refs = value
1055
 
                    if self.node_ref_lists:
1056
 
                        yield (self, key, value, refs)
1057
 
                    else:
1058
 
                        yield (self, key, value)
1059
 
                else:
1060
 
                    needed_keys.append(key)
1061
 
 
1062
 
        last_key = None
1063
 
        needed_keys = keys
1064
 
        if not needed_keys:
1065
 
            return
1066
 
        # 6 seconds spent in miss_torture using the sorted() line.
1067
 
        # Even with out of order disk IO it seems faster not to sort it when
1068
 
        # large queries are being made.
1069
 
        needed_keys = sorted(needed_keys)
1070
 
 
1071
 
        nodes_and_keys = [(0, needed_keys)]
1072
 
 
1073
 
        for row_pos, next_row_start in enumerate(self._row_offsets[1:-1]):
1074
 
            node_indexes = [idx for idx, s_keys in nodes_and_keys]
1075
 
            nodes = self._get_internal_nodes(node_indexes)
1076
 
 
1077
 
            next_nodes_and_keys = []
1078
 
            for node_index, sub_keys in nodes_and_keys:
1079
 
                node = nodes[node_index]
1080
 
                positions = self._multi_bisect_right(sub_keys, node.keys)
1081
 
                node_offset = next_row_start + node.offset
1082
 
                next_nodes_and_keys.extend([(node_offset + pos, s_keys)
1083
 
                                           for pos, s_keys in positions])
1084
 
            nodes_and_keys = next_nodes_and_keys
1085
 
        # We should now be at the _LeafNodes
1086
 
        node_indexes = [idx for idx, s_keys in nodes_and_keys]
1087
 
 
1088
 
        # TODO: We may *not* want to always read all the nodes in one
1089
 
        #       big go. Consider setting a max size on this.
1090
 
 
1091
 
        nodes = self._get_leaf_nodes(node_indexes)
1092
 
        for node_index, sub_keys in nodes_and_keys:
1093
 
            if not sub_keys:
1094
 
                continue
1095
 
            node = nodes[node_index]
1096
 
            for next_sub_key in sub_keys:
1097
 
                if next_sub_key in node.keys:
1098
 
                    value, refs = node.keys[next_sub_key]
1099
 
                    if self.node_ref_lists:
1100
 
                        yield (self, next_sub_key, value, refs)
1101
 
                    else:
1102
 
                        yield (self, next_sub_key, value)
1103
 
 
1104
 
    def iter_entries_prefix(self, keys):
1105
 
        """Iterate over keys within the index using prefix matching.
1106
 
 
1107
 
        Prefix matching is applied within the tuple of a key, not to within
1108
 
        the bytestring of each key element. e.g. if you have the keys ('foo',
1109
 
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
1110
 
        only the former key is returned.
1111
 
 
1112
 
        WARNING: Note that this method currently causes a full index parse
1113
 
        unconditionally (which is reasonably appropriate as it is a means for
1114
 
        thunking many small indices into one larger one and still supplies
1115
 
        iter_all_entries at the thunk layer).
1116
 
 
1117
 
        :param keys: An iterable providing the key prefixes to be retrieved.
1118
 
            Each key prefix takes the form of a tuple the length of a key, but
1119
 
            with the last N elements 'None' rather than a regular bytestring.
1120
 
            The first element cannot be 'None'.
1121
 
        :return: An iterable as per iter_all_entries, but restricted to the
1122
 
            keys with a matching prefix to those supplied. No additional keys
1123
 
            will be returned, and every match that is in the index will be
1124
 
            returned.
1125
 
        """
1126
 
        keys = sorted(set(keys))
1127
 
        if not keys:
1128
 
            return
1129
 
        # Load if needed to check key lengths
1130
 
        if self._key_count is None:
1131
 
            self._get_root_node()
1132
 
        # TODO: only access nodes that can satisfy the prefixes we are looking
1133
 
        # for. For now, to meet API usage (as this function is not used by
1134
 
        # current bzrlib) just suck the entire index and iterate in memory.
1135
 
        nodes = {}
1136
 
        if self.node_ref_lists:
1137
 
            if self._key_length == 1:
1138
 
                for _1, key, value, refs in self.iter_all_entries():
1139
 
                    nodes[key] = value, refs
1140
 
            else:
1141
 
                nodes_by_key = {}
1142
 
                for _1, key, value, refs in self.iter_all_entries():
1143
 
                    key_value = key, value, refs
1144
 
                    # For a key of (foo, bar, baz) create
1145
 
                    # _nodes_by_key[foo][bar][baz] = key_value
1146
 
                    key_dict = nodes_by_key
1147
 
                    for subkey in key[:-1]:
1148
 
                        key_dict = key_dict.setdefault(subkey, {})
1149
 
                    key_dict[key[-1]] = key_value
1150
 
        else:
1151
 
            if self._key_length == 1:
1152
 
                for _1, key, value in self.iter_all_entries():
1153
 
                    nodes[key] = value
1154
 
            else:
1155
 
                nodes_by_key = {}
1156
 
                for _1, key, value in self.iter_all_entries():
1157
 
                    key_value = key, value
1158
 
                    # For a key of (foo, bar, baz) create
1159
 
                    # _nodes_by_key[foo][bar][baz] = key_value
1160
 
                    key_dict = nodes_by_key
1161
 
                    for subkey in key[:-1]:
1162
 
                        key_dict = key_dict.setdefault(subkey, {})
1163
 
                    key_dict[key[-1]] = key_value
1164
 
        if self._key_length == 1:
1165
 
            for key in keys:
1166
 
                # sanity check
1167
 
                if key[0] is None:
1168
 
                    raise errors.BadIndexKey(key)
1169
 
                if len(key) != self._key_length:
1170
 
                    raise errors.BadIndexKey(key)
1171
 
                try:
1172
 
                    if self.node_ref_lists:
1173
 
                        value, node_refs = nodes[key]
1174
 
                        yield self, key, value, node_refs
1175
 
                    else:
1176
 
                        yield self, key, nodes[key]
1177
 
                except KeyError:
1178
 
                    pass
1179
 
            return
1180
 
        for key in keys:
1181
 
            # sanity check
1182
 
            if key[0] is None:
1183
 
                raise errors.BadIndexKey(key)
1184
 
            if len(key) != self._key_length:
1185
 
                raise errors.BadIndexKey(key)
1186
 
            # find what it refers to:
1187
 
            key_dict = nodes_by_key
1188
 
            elements = list(key)
1189
 
            # find the subdict whose contents should be returned.
1190
 
            try:
1191
 
                while len(elements) and elements[0] is not None:
1192
 
                    key_dict = key_dict[elements[0]]
1193
 
                    elements.pop(0)
1194
 
            except KeyError:
1195
 
                # a non-existant lookup.
1196
 
                continue
1197
 
            if len(elements):
1198
 
                dicts = [key_dict]
1199
 
                while dicts:
1200
 
                    key_dict = dicts.pop(-1)
1201
 
                    # can't be empty or would not exist
1202
 
                    item, value = key_dict.iteritems().next()
1203
 
                    if type(value) == dict:
1204
 
                        # push keys
1205
 
                        dicts.extend(key_dict.itervalues())
1206
 
                    else:
1207
 
                        # yield keys
1208
 
                        for value in key_dict.itervalues():
1209
 
                            # each value is the key:value:node refs tuple
1210
 
                            # ready to yield.
1211
 
                            yield (self, ) + value
1212
 
            else:
1213
 
                # the last thing looked up was a terminal element
1214
 
                yield (self, ) + key_dict
1215
 
 
1216
 
    def key_count(self):
1217
 
        """Return an estimate of the number of keys in this index.
1218
 
 
1219
 
        For BTreeGraphIndex the estimate is exact as it is contained in the
1220
 
        header.
1221
 
        """
1222
 
        if self._key_count is None:
1223
 
            self._get_root_node()
1224
 
        return self._key_count
1225
 
 
1226
 
    def _compute_row_offsets(self):
1227
 
        """Fill out the _row_offsets attribute based on _row_lengths."""
1228
 
        offsets = []
1229
 
        row_offset = 0
1230
 
        for row in self._row_lengths:
1231
 
            offsets.append(row_offset)
1232
 
            row_offset += row
1233
 
        offsets.append(row_offset)
1234
 
        self._row_offsets = offsets
1235
 
 
1236
 
    def _parse_header_from_bytes(self, bytes):
1237
 
        """Parse the header from a region of bytes.
1238
 
 
1239
 
        :param bytes: The data to parse.
1240
 
        :return: An offset, data tuple such as readv yields, for the unparsed
1241
 
            data. (which may be of length 0).
1242
 
        """
1243
 
        signature = bytes[0:len(self._signature())]
1244
 
        if not signature == self._signature():
1245
 
            raise errors.BadIndexFormatSignature(self._name, BTreeGraphIndex)
1246
 
        lines = bytes[len(self._signature()):].splitlines()
1247
 
        options_line = lines[0]
1248
 
        if not options_line.startswith(_OPTION_NODE_REFS):
1249
 
            raise errors.BadIndexOptions(self)
1250
 
        try:
1251
 
            self.node_ref_lists = int(options_line[len(_OPTION_NODE_REFS):])
1252
 
        except ValueError:
1253
 
            raise errors.BadIndexOptions(self)
1254
 
        options_line = lines[1]
1255
 
        if not options_line.startswith(_OPTION_KEY_ELEMENTS):
1256
 
            raise errors.BadIndexOptions(self)
1257
 
        try:
1258
 
            self._key_length = int(options_line[len(_OPTION_KEY_ELEMENTS):])
1259
 
        except ValueError:
1260
 
            raise errors.BadIndexOptions(self)
1261
 
        options_line = lines[2]
1262
 
        if not options_line.startswith(_OPTION_LEN):
1263
 
            raise errors.BadIndexOptions(self)
1264
 
        try:
1265
 
            self._key_count = int(options_line[len(_OPTION_LEN):])
1266
 
        except ValueError:
1267
 
            raise errors.BadIndexOptions(self)
1268
 
        options_line = lines[3]
1269
 
        if not options_line.startswith(_OPTION_ROW_LENGTHS):
1270
 
            raise errors.BadIndexOptions(self)
1271
 
        try:
1272
 
            self._row_lengths = map(int, [length for length in
1273
 
                options_line[len(_OPTION_ROW_LENGTHS):].split(',')
1274
 
                if len(length)])
1275
 
        except ValueError:
1276
 
            raise errors.BadIndexOptions(self)
1277
 
        self._compute_row_offsets()
1278
 
 
1279
 
        # calculate the bytes we have processed
1280
 
        header_end = (len(signature) + sum(map(len, lines[0:4])) + 4)
1281
 
        return header_end, bytes[header_end:]
1282
 
 
1283
 
    def _read_nodes(self, nodes):
1284
 
        """Read some nodes from disk into the LRU cache.
1285
 
 
1286
 
        This performs a readv to get the node data into memory, and parses each
1287
 
        node, then yields it to the caller. The nodes are requested in the
1288
 
        supplied order. If possible doing sort() on the list before requesting
1289
 
        a read may improve performance.
1290
 
 
1291
 
        :param nodes: The nodes to read. 0 - first node, 1 - second node etc.
1292
 
        :return: None
1293
 
        """
1294
 
        # may be the byte string of the whole file
1295
 
        bytes = None
1296
 
        # list of (offset, length) regions of the file that should, evenually
1297
 
        # be read in to data_ranges, either from 'bytes' or from the transport
1298
 
        ranges = []
1299
 
        for index in nodes:
1300
 
            offset = index * _PAGE_SIZE
1301
 
            size = _PAGE_SIZE
1302
 
            if index == 0:
1303
 
                # Root node - special case
1304
 
                if self._size:
1305
 
                    size = min(_PAGE_SIZE, self._size)
1306
 
                else:
1307
 
                    # The only case where we don't know the size, is for very
1308
 
                    # small indexes. So we read the whole thing
1309
 
                    bytes = self._transport.get_bytes(self._name)
1310
 
                    self._size = len(bytes)
1311
 
                    # the whole thing should be parsed out of 'bytes'
1312
 
                    ranges.append((0, len(bytes)))
1313
 
                    break
1314
 
            else:
1315
 
                if offset > self._size:
1316
 
                    raise AssertionError('tried to read past the end'
1317
 
                                         ' of the file %s > %s'
1318
 
                                         % (offset, self._size))
1319
 
                size = min(size, self._size - offset)
1320
 
            ranges.append((offset, size))
1321
 
        if not ranges:
1322
 
            return
1323
 
        elif bytes is not None:
1324
 
            # already have the whole file
1325
 
            data_ranges = [(start, bytes[start:start+_PAGE_SIZE])
1326
 
                           for start in xrange(0, len(bytes), _PAGE_SIZE)]
1327
 
        elif self._file is None:
1328
 
            data_ranges = self._transport.readv(self._name, ranges)
1329
 
        else:
1330
 
            data_ranges = []
1331
 
            for offset, size in ranges:
1332
 
                self._file.seek(offset)
1333
 
                data_ranges.append((offset, self._file.read(size)))
1334
 
        for offset, data in data_ranges:
1335
 
            if offset == 0:
1336
 
                # extract the header
1337
 
                offset, data = self._parse_header_from_bytes(data)
1338
 
                if len(data) == 0:
1339
 
                    continue
1340
 
            bytes = zlib.decompress(data)
1341
 
            if bytes.startswith(_LEAF_FLAG):
1342
 
                node = _LeafNode(bytes, self._key_length, self.node_ref_lists)
1343
 
            elif bytes.startswith(_INTERNAL_FLAG):
1344
 
                node = _InternalNode(bytes)
1345
 
            else:
1346
 
                raise AssertionError("Unknown node type for %r" % bytes)
1347
 
            yield offset / _PAGE_SIZE, node
1348
 
 
1349
 
    def _signature(self):
1350
 
        """The file signature for this index type."""
1351
 
        return _BTSIGNATURE
1352
 
 
1353
 
    def validate(self):
1354
 
        """Validate that everything in the index can be accessed."""
1355
 
        # just read and parse every node.
1356
 
        self._get_root_node()
1357
 
        if len(self._row_lengths) > 1:
1358
 
            start_node = self._row_offsets[1]
1359
 
        else:
1360
 
            # We shouldn't be reading anything anyway
1361
 
            start_node = 1
1362
 
        node_end = self._row_offsets[-1]
1363
 
        for node in self._read_nodes(range(start_node, node_end)):
1364
 
            pass
1365
 
 
1366
 
 
1367
 
try:
1368
 
    from bzrlib import _btree_serializer_c as _btree_serializer
1369
 
except ImportError:
1370
 
    from bzrlib import _btree_serializer_py as _btree_serializer