/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
2592.1.4 by Robert Collins
Create a GraphIndexBuilder.
27
from cStringIO import StringIO
2592.1.12 by Robert Collins
Handle basic node adds.
28
import re
2592.1.4 by Robert Collins
Create a GraphIndexBuilder.
29
2592.1.5 by Robert Collins
Trivial index reading.
30
from bzrlib import errors
2592.1.4 by Robert Collins
Create a GraphIndexBuilder.
31
2624.2.8 by Robert Collins
Explicitly mark the number of keys elements in use in GraphIndex files.
32
_OPTION_KEY_ELEMENTS = "key_elements="
2592.1.6 by Robert Collins
Record the number of node reference lists a particular index has.
33
_OPTION_NODE_REFS = "node_ref_lists="
2592.1.4 by Robert Collins
Create a GraphIndexBuilder.
34
_SIGNATURE = "Bazaar Graph Index 1\n"
35
36
2592.1.14 by Robert Collins
Detect bad reference key values.
37
_whitespace_re = re.compile('[\t\n\x0b\x0c\r\x00 ]')
2592.1.12 by Robert Collins
Handle basic node adds.
38
_newline_null_re = re.compile('[\n\0]')
39
40
2592.1.4 by Robert Collins
Create a GraphIndexBuilder.
41
class GraphIndexBuilder(object):
2592.1.18 by Robert Collins
Add space to mark absent nodes.
42
    """A builder that can build a GraphIndex.
43
    
44
    The resulting graph has the structure:
45
    
46
    _SIGNATURE OPTIONS NODES NEWLINE
47
    _SIGNATURE     := 'Bazaar Graph Index 1' NEWLINE
48
    OPTIONS        := 'node_ref_lists=' DIGITS NEWLINE
49
    NODES          := NODE*
50
    NODE           := KEY NULL ABSENT? NULL REFERENCES NULL VALUE NEWLINE
51
    KEY            := Not-whitespace-utf8
52
    ABSENT         := 'a'
2592.1.19 by Robert Collins
Node references are tab separated.
53
    REFERENCES     := REFERENCE_LIST (TAB REFERENCE_LIST){node_ref_lists - 1}
54
    REFERENCE_LIST := (REFERENCE (CR REFERENCE)*)?
55
    REFERENCE      := DIGITS  ; digits is the byte offset in the index of the
56
                              ; referenced key.
2592.1.18 by Robert Collins
Add space to mark absent nodes.
57
    VALUE          := no-newline-no-null-bytes
58
    """
2592.1.4 by Robert Collins
Create a GraphIndexBuilder.
59
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
60
    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.
61
        """Create a GraphIndex builder.
62
63
        :param reference_lists: The number of node references lists for each
64
            entry.
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
65
        :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.
66
        """
67
        self.reference_lists = reference_lists
2592.1.15 by Robert Collins
Detect duplicate key insertion.
68
        self._nodes = {}
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
69
        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.
70
        self._key_length = key_elements
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
71
72
    def _check_key(self, key):
73
        """Raise BadIndexKey if key is not a valid key for this index."""
74
        if type(key) != tuple:
75
            raise errors.BadIndexKey(key)
76
        if self._key_length != len(key):
77
            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.
78
        for element in key:
79
            if not element or _whitespace_re.search(element) is not None:
80
                raise errors.BadIndexKey(element)
2592.1.12 by Robert Collins
Handle basic node adds.
81
2592.1.46 by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method
82
    def add_node(self, key, value, references=()):
2592.1.12 by Robert Collins
Handle basic node adds.
83
        """Add a node to the index.
84
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
85
        :param key: The key. keys are non-empty tuples containing
86
            as many whitespace-free utf8 bytestrings as the key length
87
            defined for this index.
2592.1.12 by Robert Collins
Handle basic node adds.
88
        :param references: An iterable of iterables of keys. Each is a
89
            reference to another key.
90
        :param value: The value to associate with the key. It may be any
91
            bytes as long as it does not contain \0 or \n.
92
        """
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
93
        self._check_key(key)
2592.1.12 by Robert Collins
Handle basic node adds.
94
        if _newline_null_re.search(value) is not None:
95
            raise errors.BadIndexValue(value)
2592.1.13 by Robert Collins
Handle mismatched numbers of reference lists.
96
        if len(references) != self.reference_lists:
97
            raise errors.BadIndexValue(references)
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
98
        node_refs = []
2592.1.14 by Robert Collins
Detect bad reference key values.
99
        for reference_list in references:
100
            for reference in reference_list:
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
101
                self._check_key(reference)
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
102
                if reference not in self._nodes:
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
103
                    self._nodes[reference] = ('a', (), '')
104
            node_refs.append(tuple(reference_list))
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
105
        if key in self._nodes and self._nodes[key][0] == '':
2592.1.15 by Robert Collins
Detect duplicate key insertion.
106
            raise errors.BadIndexDuplicateKey(key, self)
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
107
        self._nodes[key] = ('', tuple(node_refs), value)
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
108
        if self._key_length > 1:
109
            key_dict = self._nodes_by_key
110
            if self.reference_lists:
111
                key_value = key, value, tuple(node_refs)
112
            else:
113
                key_value = key, value
114
            # possibly should do this on-demand, but it seems likely it is 
115
            # always wanted
2624.2.11 by Robert Collins
Review comments.
116
            # For a key of (foo, bar, baz) create
117
            # _nodes_by_key[foo][bar][baz] = key_value
118
            for subkey in key[:-1]:
119
                key_dict = key_dict.setdefault(subkey, {})
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
120
            key_dict[key[-1]] = key_value
2592.1.6 by Robert Collins
Record the number of node reference lists a particular index has.
121
2592.1.4 by Robert Collins
Create a GraphIndexBuilder.
122
    def finish(self):
2592.1.6 by Robert Collins
Record the number of node reference lists a particular index has.
123
        lines = [_SIGNATURE]
124
        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.
125
        lines.append(_OPTION_KEY_ELEMENTS + str(self._key_length) + '\n')
2624.2.11 by Robert Collins
Review comments.
126
        prefix_length = sum(len(x) for x in lines)
2592.1.22 by Robert Collins
Node references are byte offsets.
127
        # references are byte offsets. To avoid having to do nasty
128
        # polynomial work to resolve offsets (references to later in the 
129
        # file cannot be determined until all the inbetween references have
130
        # been calculated too) we pad the offsets with 0's to make them be
131
        # of consistent length. Using binary offsets would break the trivial
132
        # file parsing.
133
        # to calculate the width of zero's needed we do three passes:
134
        # one to gather all the non-reference data and the number of references.
135
        # one to pad all the data with reference-length and determine entry
136
        # addresses.
137
        # One to serialise.
2592.1.40 by Robert Collins
Reverse index ordering - we do not have date prefixed revids.
138
        
139
        # forward sorted by key. In future we may consider topological sorting,
140
        # at the cost of table scans for direct lookup, or a second index for
141
        # direct lookup
142
        nodes = sorted(self._nodes.items())
2592.1.42 by Robert Collins
Check the index length is as expected, when we have done preprocessing.
143
        # if we do not prepass, we don't know how long it will be up front.
144
        expected_bytes = None
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
145
        # we only need to pre-pass if we have reference lists at all.
146
        if self.reference_lists:
2592.1.41 by Robert Collins
Remove duplication in the index serialisation logic with John's suggestion.
147
            key_offset_info = []
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
148
            non_ref_bytes = prefix_length
149
            total_references = 0
150
            # TODO use simple multiplication for the constants in this loop.
151
            for key, (absent, references, value) in nodes:
2592.1.41 by Robert Collins
Remove duplication in the index serialisation logic with John's suggestion.
152
                # record the offset known *so far* for this key:
153
                # the non reference bytes to date, and the total references to
154
                # date - saves reaccumulating on the second pass
155
                key_offset_info.append((key, non_ref_bytes, total_references))
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
156
                # 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.
157
                # 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.
158
                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.
159
                if self._key_length > 1:
160
                    non_ref_bytes += self._key_length - 1
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
161
                # value is literal bytes, there are 3 null's, 1 NL.
162
                non_ref_bytes += len(value) + 3 + 1
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
163
                # one byte for absent if set.
164
                if absent:
165
                    non_ref_bytes += 1
2592.1.36 by Robert Collins
Bugfix incorrect offset generation when an absent record is before a referenced record.
166
                elif self.reference_lists:
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
167
                    # (ref_lists -1) tabs
168
                    non_ref_bytes += self.reference_lists - 1
169
                    # (ref-1 cr's per ref_list)
170
                    for ref_list in references:
171
                        # how many references across the whole file?
172
                        total_references += len(ref_list)
173
                        # accrue reference separators
174
                        if ref_list:
175
                            non_ref_bytes += len(ref_list) - 1
176
            # how many digits are needed to represent the total byte count?
177
            digits = 1
2592.1.22 by Robert Collins
Node references are byte offsets.
178
            possible_total_bytes = non_ref_bytes + total_references*digits
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
179
            while 10 ** digits < possible_total_bytes:
180
                digits += 1
181
                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.
182
            expected_bytes = possible_total_bytes + 1 # terminating newline
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
183
            # resolve key addresses.
184
            key_addresses = {}
2592.1.41 by Robert Collins
Remove duplication in the index serialisation logic with John's suggestion.
185
            for key, non_ref_bytes, total_references in key_offset_info:
186
                key_addresses[key] = non_ref_bytes + total_references*digits
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
187
            # serialise
188
            format_string = '%%0%sd' % digits
189
        for key, (absent, references, value) in nodes:
2592.1.19 by Robert Collins
Node references are tab separated.
190
            flattened_references = []
191
            for ref_list in references:
2592.1.22 by Robert Collins
Node references are byte offsets.
192
                ref_addresses = []
193
                for reference in ref_list:
194
                    ref_addresses.append(format_string % key_addresses[reference])
195
                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.
196
            string_key = '\x00'.join(key)
2624.2.11 by Robert Collins
Review comments.
197
            lines.append("%s\x00%s\x00%s\x00%s\n" % (string_key, absent,
2592.1.19 by Robert Collins
Node references are tab separated.
198
                '\t'.join(flattened_references), value))
2592.1.6 by Robert Collins
Record the number of node reference lists a particular index has.
199
        lines.append('\n')
2592.1.42 by Robert Collins
Check the index length is as expected, when we have done preprocessing.
200
        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.
201
        if expected_bytes and len(result.getvalue()) != expected_bytes:
202
            raise errors.BzrError('Failed index creation. Internal error:'
203
                ' mismatched output length and expected length: %d %d' %
204
                (len(result.getvalue()), expected_bytes))
2592.1.6 by Robert Collins
Record the number of node reference lists a particular index has.
205
        return StringIO(''.join(lines))
2592.1.5 by Robert Collins
Trivial index reading.
206
207
208
class GraphIndex(object):
209
    """An index for data with embedded graphs.
2592.1.10 by Robert Collins
Make validate detect node reference parsing errors.
210
 
211
    The index maps keys to a list of key reference lists, and a value.
212
    Each node has the same number of key reference lists. Each key reference
213
    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.
214
    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.
215
    hidden in the interface: keys and key references are always tuples of
216
    bytestrings, never the internal representation (e.g. dictionary offsets).
2592.1.30 by Robert Collins
Absent entries are not yeilded.
217
218
    It is presumed that the index will not be mutated - it is static data.
2592.1.34 by Robert Collins
Cleanup docs.
219
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.
220
    Successive iter_all_entries calls will read the entire index each time.
221
    Additionally, iter_entries calls will read the index linearly until the
222
    desired keys are found. XXX: This must be fixed before the index is
2592.1.34 by Robert Collins
Cleanup docs.
223
    suitable for production use. :XXX
2592.1.5 by Robert Collins
Trivial index reading.
224
    """
225
226
    def __init__(self, transport, name):
227
        """Open an index called name on transport.
228
229
        :param transport: A bzrlib.transport.Transport.
230
        :param name: A path to provide to transport API calls.
231
        """
232
        self._transport = transport
233
        self._name = name
2624.2.2 by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram.
234
        self._nodes = None
235
        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.
236
        self._nodes_by_key = None
2624.2.2 by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram.
237
238
    def _buffer_all(self):
239
        """Buffer all the index data.
240
241
        Mutates self._nodes and self.keys_by_offset.
2592.1.5 by Robert Collins
Trivial index reading.
242
        """
2592.1.27 by Robert Collins
Test missing end lines with non-empty indices.
243
        stream = self._transport.get(self._name)
244
        self._read_prefix(stream)
2624.2.11 by Robert Collins
Review comments.
245
        expected_elements = 3 + self._key_length
2592.1.27 by Robert Collins
Test missing end lines with non-empty indices.
246
        line_count = 0
2624.2.2 by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram.
247
        # raw data keyed by offset
248
        self._keys_by_offset = {}
249
        # ready-to-return key:value or key:value, node_ref_lists
250
        self._nodes = {}
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
251
        self._nodes_by_key = {}
2592.1.27 by Robert Collins
Test missing end lines with non-empty indices.
252
        trailers = 0
253
        pos = stream.tell()
254
        for line in stream.readlines():
255
            if line == '\n':
256
                trailers += 1
257
                continue
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
258
            elements = line.split('\0')
2624.2.11 by Robert Collins
Review comments.
259
            if len(elements) != expected_elements:
260
                raise errors.BadIndexData(self)
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
261
            # keys are tuples
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
262
            key = tuple(elements[:self._key_length])
263
            absent, references, value = elements[-3:]
2592.1.43 by Robert Collins
Various index tweaks and test clarity from John's review.
264
            value = value[:-1] # remove the newline
2592.1.28 by Robert Collins
Basic two pass iter_all_entries.
265
            ref_lists = []
266
            for ref_string in references.split('\t'):
267
                ref_lists.append(tuple([
268
                    int(ref) for ref in ref_string.split('\r') if ref
269
                    ]))
270
            ref_lists = tuple(ref_lists)
2624.2.2 by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram.
271
            self._keys_by_offset[pos] = (key, absent, ref_lists, value)
2592.1.28 by Robert Collins
Basic two pass iter_all_entries.
272
            pos += len(line)
2624.2.2 by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram.
273
        for key, absent, references, value in self._keys_by_offset.itervalues():
2592.1.30 by Robert Collins
Absent entries are not yeilded.
274
            if absent:
275
                continue
2592.1.28 by Robert Collins
Basic two pass iter_all_entries.
276
            # resolve references:
277
            if self.node_ref_lists:
278
                node_refs = []
279
                for ref_list in references:
2624.2.2 by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram.
280
                    node_refs.append(tuple([self._keys_by_offset[ref][0] for ref in ref_list]))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
281
                node_value = (value, tuple(node_refs))
2592.1.28 by Robert Collins
Basic two pass iter_all_entries.
282
            else:
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
283
                node_value = value
284
            self._nodes[key] = node_value
285
            if self._key_length > 1:
286
                subkey = list(reversed(key[:-1]))
287
                key_dict = self._nodes_by_key
288
                if self.node_ref_lists:
289
                    key_value = key, node_value[0], node_value[1]
290
                else:
291
                    key_value = key, node_value
292
                # possibly should do this on-demand, but it seems likely it is 
293
                # always wanted
2624.2.11 by Robert Collins
Review comments.
294
                # For a key of (foo, bar, baz) create
295
                # _nodes_by_key[foo][bar][baz] = key_value
296
                for subkey in key[:-1]:
297
                    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.
298
                key_dict[key[-1]] = key_value
2624.2.6 by Robert Collins
Remove performance overhead of set intersection against dicts in index iteraction.
299
        self._keys = set(self._nodes)
2592.1.27 by Robert Collins
Test missing end lines with non-empty indices.
300
        if trailers != 1:
301
            # there must be one line - the empty trailer line.
302
            raise errors.BadIndexData(self)
303
2624.2.2 by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram.
304
    def iter_all_entries(self):
305
        """Iterate over all keys within the index.
306
307
        :return: An iterable of (key, value) or (key, value, reference_lists).
308
            The former tuple is used when there are no reference lists in the
309
            index, making the API compatible with simple key:value index types.
310
            There is no defined order for the result iteration - it will be in
311
            the most efficient order for the index.
312
        """
313
        if self._nodes is None:
314
            self._buffer_all()
315
        if self.node_ref_lists:
316
            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.
317
                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.
318
        else:
319
            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.
320
                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.
321
2592.1.27 by Robert Collins
Test missing end lines with non-empty indices.
322
    def _read_prefix(self, stream):
323
        signature = stream.read(len(self._signature()))
324
        if not signature == self._signature():
325
            raise errors.BadIndexFormatSignature(self._name, GraphIndex)
326
        options_line = stream.readline()
327
        if not options_line.startswith(_OPTION_NODE_REFS):
328
            raise errors.BadIndexOptions(self)
329
        try:
330
            self.node_ref_lists = int(options_line[len(_OPTION_NODE_REFS):-1])
331
        except ValueError:
332
            raise errors.BadIndexOptions(self)
2624.2.8 by Robert Collins
Explicitly mark the number of keys elements in use in GraphIndex files.
333
        options_line = stream.readline()
334
        if not options_line.startswith(_OPTION_KEY_ELEMENTS):
335
            raise errors.BadIndexOptions(self)
336
        try:
337
            self._key_length = int(options_line[len(_OPTION_KEY_ELEMENTS):-1])
338
        except ValueError:
339
            raise errors.BadIndexOptions(self)
2592.1.5 by Robert Collins
Trivial index reading.
340
341
    def iter_entries(self, keys):
342
        """Iterate over keys within the index.
343
344
        :param keys: An iterable providing the keys to be retrieved.
2592.1.46 by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method
345
        :return: An iterable as per iter_all_entries, but restricted to the
346
            keys supplied. No additional keys will be returned, and every
347
            key supplied that is in the index will be returned.
2592.1.5 by Robert Collins
Trivial index reading.
348
        """
2592.1.29 by Robert Collins
Basic iter_entries working.
349
        keys = set(keys)
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.
350
        if not keys:
351
            return
2624.2.3 by Robert Collins
Make GraphIndex.iter_entries do hash lookups rather than table scans.
352
        if self._nodes is None:
353
            self._buffer_all()
2624.2.6 by Robert Collins
Remove performance overhead of set intersection against dicts in index iteraction.
354
        keys = keys.intersection(self._keys)
2624.2.3 by Robert Collins
Make GraphIndex.iter_entries do hash lookups rather than table scans.
355
        if self.node_ref_lists:
356
            for key in keys:
357
                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.
358
                yield self, key, value, node_refs
2624.2.3 by Robert Collins
Make GraphIndex.iter_entries do hash lookups rather than table scans.
359
        else:
360
            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.
361
                yield self, key, self._nodes[key]
2592.1.7 by Robert Collins
A validate that goes boom.
362
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
363
    def iter_entries_prefix(self, keys):
364
        """Iterate over keys within the index using prefix matching.
365
366
        Prefix matching is applied within the tuple of a key, not to within
367
        the bytestring of each key element. e.g. if you have the keys ('foo',
368
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
369
        only the former key is returned.
370
371
        :param keys: An iterable providing the key prefixes to be retrieved.
372
            Each key prefix takes the form of a tuple the length of a key, but
373
            with the last N elements 'None' rather than a regular bytestring.
374
            The first element cannot be 'None'.
375
        :return: An iterable as per iter_all_entries, but restricted to the
376
            keys with a matching prefix to those supplied. No additional keys
377
            will be returned, and every match that is in the index will be
378
            returned.
379
        """
380
        keys = set(keys)
381
        if not keys:
382
            return
383
        # load data - also finds key lengths
384
        if self._nodes is None:
385
            self._buffer_all()
386
        if self._key_length == 1:
387
            for key in keys:
388
                # sanity check
389
                if key[0] is None:
390
                    raise errors.BadIndexKey(key)
391
                if len(key) != self._key_length:
392
                    raise errors.BadIndexKey(key)
393
                if self.node_ref_lists:
394
                    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.
395
                    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.
396
                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.
397
                    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.
398
            return
399
        for key in keys:
400
            # sanity check
401
            if key[0] is None:
402
                raise errors.BadIndexKey(key)
403
            if len(key) != self._key_length:
404
                raise errors.BadIndexKey(key)
405
            # find what it refers to:
406
            key_dict = self._nodes_by_key
407
            elements = list(key)
2624.2.11 by Robert Collins
Review comments.
408
            # 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.
409
            try:
410
                while len(elements) and elements[0] is not None:
411
                    key_dict = key_dict[elements[0]]
412
                    elements.pop(0)
413
            except KeyError:
414
                # a non-existant lookup.
415
                continue
416
            if len(elements):
417
                dicts = [key_dict]
418
                while dicts:
419
                    key_dict = dicts.pop(-1)
420
                    # can't be empty or would not exist
421
                    item, value = key_dict.iteritems().next()
422
                    if type(value) == dict:
423
                        # push keys 
424
                        dicts.extend(key_dict.itervalues())
425
                    else:
426
                        # yield keys
427
                        for value in key_dict.itervalues():
2624.2.11 by Robert Collins
Review comments.
428
                            # each value is the key:value:node refs tuple
429
                            # 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.
430
                            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.
431
            else:
2624.2.11 by Robert Collins
Review comments.
432
                # 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.
433
                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.
434
2592.1.8 by Robert Collins
Empty files should validate ok.
435
    def _signature(self):
436
        """The file signature for this index type."""
437
        return _SIGNATURE
438
2592.1.7 by Robert Collins
A validate that goes boom.
439
    def validate(self):
440
        """Validate that everything in the index can be accessed."""
2592.1.27 by Robert Collins
Test missing end lines with non-empty indices.
441
        # iter_all validates completely at the moment, so just do that.
442
        for node in self.iter_all_entries():
443
            pass
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
444
445
446
class CombinedGraphIndex(object):
447
    """A GraphIndex made up from smaller GraphIndices.
448
    
449
    The backing indices must implement GraphIndex, and are presumed to be
450
    static data.
2592.1.45 by Robert Collins
Tweak documentation as per Aaron's review.
451
452
    Queries against the combined index will be made against the first index,
453
    and then the second and so on. The order of index's can thus influence
454
    performance significantly. For example, if one index is on local disk and a
455
    second on a remote server, the local disk index should be before the other
456
    in the index list.
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
457
    """
458
459
    def __init__(self, indices):
460
        """Create a CombinedGraphIndex backed by indices.
461
2592.1.45 by Robert Collins
Tweak documentation as per Aaron's review.
462
        :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.
463
        """
464
        self._indices = indices
2592.1.37 by Robert Collins
Add CombinedGraphIndex.insert_index.
465
466
    def insert_index(self, pos, index):
467
        """Insert a new index in the list of indices to query.
468
469
        :param pos: The position to insert the index.
470
        :param index: The index to insert.
471
        """
472
        self._indices.insert(pos, index)
473
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
474
    def iter_all_entries(self):
475
        """Iterate over all keys within the index
476
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.
477
        Duplicate keys across child indices are presumed to have the same
478
        value and are only reported once.
479
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
480
        :return: An iterable of (key, reference_lists, value). There is no
481
            defined order for the result iteration - it will be in the most
482
            efficient order for the index.
483
        """
484
        seen_keys = set()
485
        for index in self._indices:
486
            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.
487
                if node[1] not in seen_keys:
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
488
                    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.
489
                    seen_keys.add(node[1])
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
490
491
    def iter_entries(self, keys):
492
        """Iterate over keys within the index.
493
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.
494
        Duplicate keys across child indices are presumed to have the same
495
        value and are only reported once.
496
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
497
        :param keys: An iterable providing the keys to be retrieved.
498
        :return: An iterable of (key, reference_lists, value). There is no
499
            defined order for the result iteration - it will be in the most
500
            efficient order for the index.
501
        """
502
        keys = set(keys)
2592.1.39 by Robert Collins
CombinedGraphIndex.iter_entries does not need to see all entries.
503
        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.
504
            if not keys:
505
                return
2592.1.39 by Robert Collins
CombinedGraphIndex.iter_entries does not need to see all entries.
506
            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.
507
                keys.remove(node[1])
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
508
                yield node
509
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
510
    def iter_entries_prefix(self, keys):
511
        """Iterate over keys within the index using prefix matching.
512
513
        Duplicate keys across child indices are presumed to have the same
514
        value and are only reported once.
515
516
        Prefix matching is applied within the tuple of a key, not to within
517
        the bytestring of each key element. e.g. if you have the keys ('foo',
518
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
519
        only the former key is returned.
520
521
        :param keys: An iterable providing the key prefixes to be retrieved.
522
            Each key prefix takes the form of a tuple the length of a key, but
523
            with the last N elements 'None' rather than a regular bytestring.
524
            The first element cannot be 'None'.
525
        :return: An iterable as per iter_all_entries, but restricted to the
526
            keys with a matching prefix to those supplied. No additional keys
527
            will be returned, and every match that is in the index will be
528
            returned.
529
        """
530
        keys = set(keys)
531
        if not keys:
532
            return
533
        seen_keys = set()
534
        for index in self._indices:
535
            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.
536
                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.
537
                    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.
538
                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.
539
                yield node
540
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
541
    def validate(self):
542
        """Validate that everything in the index can be accessed."""
543
        for index in self._indices:
544
            index.validate()
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
545
546
547
class InMemoryGraphIndex(GraphIndexBuilder):
548
    """A GraphIndex which operates entirely out of memory and is mutable.
549
550
    This is designed to allow the accumulation of GraphIndex entries during a
551
    single write operation, where the accumulated entries need to be immediately
552
    available - for example via a CombinedGraphIndex.
553
    """
554
555
    def add_nodes(self, nodes):
556
        """Add nodes to the index.
557
558
        :param nodes: An iterable of (key, node_refs, value) entries to add.
559
        """
2624.2.1 by Robert Collins
InMemoryGraphIndex.add_nodes was inconsistent with other metods for non-node-reference indices.
560
        if self.reference_lists:
561
            for (key, value, node_refs) in nodes:
562
                self.add_node(key, value, node_refs)
563
        else:
564
            for (key, value) in nodes:
565
                self.add_node(key, value)
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
566
567
    def iter_all_entries(self):
568
        """Iterate over all keys within the index
569
570
        :return: An iterable of (key, reference_lists, value). There is no
571
            defined order for the result iteration - it will be in the most
572
            efficient order for the index (in this case dictionary hash order).
573
        """
2592.1.46 by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method
574
        if self.reference_lists:
575
            for key, (absent, references, value) in self._nodes.iteritems():
576
                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.
577
                    yield self, key, value, references
2592.1.46 by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method
578
        else:
579
            for key, (absent, references, value) in self._nodes.iteritems():
580
                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.
581
                    yield self, key, value
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
582
583
    def iter_entries(self, keys):
584
        """Iterate over keys within the index.
585
586
        :param keys: An iterable providing the keys to be retrieved.
587
        :return: An iterable of (key, reference_lists, value). There is no
588
            defined order for the result iteration - it will be in the most
589
            efficient order for the index (keys iteration order in this case).
590
        """
591
        keys = set(keys)
2592.1.46 by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method
592
        if self.reference_lists:
593
            for key in keys.intersection(self._nodes):
594
                node = self._nodes[key]
595
                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.
596
                    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
597
        else:
598
            for key in keys.intersection(self._nodes):
599
                node = self._nodes[key]
600
                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.
601
                    yield self, key, node[2]
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
602
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
603
    def iter_entries_prefix(self, keys):
604
        """Iterate over keys within the index using prefix matching.
605
606
        Prefix matching is applied within the tuple of a key, not to within
607
        the bytestring of each key element. e.g. if you have the keys ('foo',
608
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
609
        only the former key is returned.
610
611
        :param keys: An iterable providing the key prefixes to be retrieved.
612
            Each key prefix takes the form of a tuple the length of a key, but
613
            with the last N elements 'None' rather than a regular bytestring.
614
            The first element cannot be 'None'.
615
        :return: An iterable as per iter_all_entries, but restricted to the
616
            keys with a matching prefix to those supplied. No additional keys
617
            will be returned, and every match that is in the index will be
618
            returned.
619
        """
620
        # XXX: To much duplication with the GraphIndex class; consider finding
621
        # a good place to pull out the actual common logic.
622
        keys = set(keys)
623
        if not keys:
624
            return
625
        if self._key_length == 1:
626
            for key in keys:
627
                # sanity check
628
                if key[0] is None:
629
                    raise errors.BadIndexKey(key)
630
                if len(key) != self._key_length:
631
                    raise errors.BadIndexKey(key)
632
                node = self._nodes[key]
633
                if node[0]:
634
                    continue 
635
                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.
636
                    yield self, key, node[2], node[1]
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
637
                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.
638
                    yield self ,key, node[2]
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
639
            return
640
        for key in keys:
641
            # sanity check
642
            if key[0] is None:
643
                raise errors.BadIndexKey(key)
644
            if len(key) != self._key_length:
645
                raise errors.BadIndexKey(key)
646
            # find what it refers to:
647
            key_dict = self._nodes_by_key
648
            elements = list(key)
649
            # find the subdict to return
650
            try:
651
                while len(elements) and elements[0] is not None:
652
                    key_dict = key_dict[elements[0]]
653
                    elements.pop(0)
654
            except KeyError:
655
                # a non-existant lookup.
656
                continue
657
            if len(elements):
658
                dicts = [key_dict]
659
                while dicts:
660
                    key_dict = dicts.pop(-1)
661
                    # can't be empty or would not exist
662
                    item, value = key_dict.iteritems().next()
663
                    if type(value) == dict:
664
                        # push keys 
665
                        dicts.extend(key_dict.itervalues())
666
                    else:
667
                        # yield keys
668
                        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.
669
                            yield (self, ) + value
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
670
            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.
671
                yield (self, ) + key_dict
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
672
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
673
    def validate(self):
674
        """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.
675
676
677
class GraphIndexPrefixAdapter(object):
678
    """An adapter between GraphIndex with different key lengths.
679
680
    Queries against this will emit queries against the adapted Graph with the
681
    prefix added, queries for all items use iter_entries_prefix. The returned
682
    nodes will have their keys and node references adjusted to remove the 
683
    prefix. Finally, an add_nodes_callback can be supplied - when called the
684
    nodes and references being added will have prefix prepended.
685
    """
686
687
    def __init__(self, adapted, prefix, missing_key_length, add_nodes_callback=None):
688
        """Construct an adapter against adapted with prefix."""
689
        self.adapted = adapted
690
        self.prefix = prefix + (None,)*missing_key_length
691
        self.prefix_key = prefix
692
        self.prefix_len = len(prefix)
693
        self.add_nodes_callback = add_nodes_callback
694
2624.2.13 by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter.
695
    def add_nodes(self, nodes):
696
        """Add nodes to the index.
697
698
        :param nodes: An iterable of (key, node_refs, value) entries to add.
699
        """
700
        # save nodes in case its an iterator
701
        nodes = tuple(nodes)
702
        translated_nodes = []
703
        try:
704
            for (key, value, node_refs) in nodes:
705
                adjusted_references = (
706
                    tuple(tuple(self.prefix_key + ref_node for ref_node in ref_list)
707
                        for ref_list in node_refs))
708
                translated_nodes.append((self.prefix_key + key, value,
709
                    adjusted_references))
710
        except ValueError:
711
            # XXX: TODO add an explicit interface for getting the reference list
712
            # status, to handle this bit of user-friendliness in the API more 
713
            # explicitly.
714
            for (key, value) in nodes:
715
                translated_nodes.append((self.prefix_key + key, value))
716
        self.add_nodes_callback(translated_nodes)
717
718
    def add_node(self, key, value, references=()):
719
        """Add a node to the index.
720
721
        :param key: The key. keys are non-empty tuples containing
722
            as many whitespace-free utf8 bytestrings as the key length
723
            defined for this index.
724
        :param references: An iterable of iterables of keys. Each is a
725
            reference to another key.
726
        :param value: The value to associate with the key. It may be any
727
            bytes as long as it does not contain \0 or \n.
728
        """
729
        self.add_nodes(((key, value, references), ))
730
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
731
    def _strip_prefix(self, an_iter):
732
        """Strip prefix data from nodes and return it."""
733
        for node in an_iter:
734
            # cross checks
2624.2.14 by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data.
735
            if node[1][:self.prefix_len] != self.prefix_key:
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
736
                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.
737
            for ref_list in node[3]:
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
738
                for ref_node in ref_list:
739
                    if ref_node[:self.prefix_len] != self.prefix_key:
740
                        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.
741
            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.
742
                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.
743
                for ref_list in node[3]))
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
744
745
    def iter_all_entries(self):
746
        """Iterate over all keys within the index
747
748
        iter_all_entries is implemented against the adapted index using
749
        iter_entries_prefix.
750
751
        :return: An iterable of (key, reference_lists, value). There is no
752
            defined order for the result iteration - it will be in the most
753
            efficient order for the index (in this case dictionary hash order).
754
        """
755
        return self._strip_prefix(self.adapted.iter_entries_prefix([self.prefix]))
756
757
    def iter_entries(self, keys):
758
        """Iterate over keys within the index.
759
760
        :param keys: An iterable providing the keys to be retrieved.
761
        :return: An iterable of (key, reference_lists, value). There is no
762
            defined order for the result iteration - it will be in the most
763
            efficient order for the index (keys iteration order in this case).
764
        """
765
        return self._strip_prefix(self.adapted.iter_entries(
766
            self.prefix_key + key for key in keys))
767
768
    def iter_entries_prefix(self, keys):
769
        """Iterate over keys within the index using prefix matching.
770
771
        Prefix matching is applied within the tuple of a key, not to within
772
        the bytestring of each key element. e.g. if you have the keys ('foo',
773
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
774
        only the former key is returned.
775
776
        :param keys: An iterable providing the key prefixes to be retrieved.
777
            Each key prefix takes the form of a tuple the length of a key, but
778
            with the last N elements 'None' rather than a regular bytestring.
779
            The first element cannot be 'None'.
780
        :return: An iterable as per iter_all_entries, but restricted to the
781
            keys with a matching prefix to those supplied. No additional keys
782
            will be returned, and every match that is in the index will be
783
            returned.
784
        """
785
        return self._strip_prefix(self.adapted.iter_entries_prefix(
786
            self.prefix_key + key for key in keys))
787
788
    def validate(self):
789
        """Call the adapted's validate."""
790
        self.adapted.validate()