~bzr-pqm/bzr/bzr.dev

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