/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/index.py

  • Committer: Robert Collins
  • Date: 2007-10-05 10:45:11 UTC
  • mto: (2592.3.168 repository)
  • mto: This revision was merged to the branch mainline in revision 2908.
  • Revision ID: robertc@robertcollins.net-20071005104511-e1uy11glm79wrjtb
* New module ``bzrlib.bisect_multi`` with generic multiple-bisection-at-once
  logic, currently only available for byte-based lookup
  (``bisect_multi_bytes``). (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Indexing facilities."""
 
18
 
 
19
__all__ = [
 
20
    'CombinedGraphIndex',
 
21
    'GraphIndex',
 
22
    'GraphIndexBuilder',
 
23
    'GraphIndexPrefixAdapter',
 
24
    'InMemoryGraphIndex',
 
25
    ]
 
26
 
 
27
from cStringIO import StringIO
 
28
import re
 
29
 
 
30
from bzrlib.lazy_import import lazy_import
 
31
lazy_import(globals(), """
 
32
from bzrlib import trace
 
33
from bzrlib.trace import mutter
 
34
""")
 
35
from bzrlib import debug, errors
 
36
 
 
37
_OPTION_KEY_ELEMENTS = "key_elements="
 
38
_OPTION_LEN = "len="
 
39
_OPTION_NODE_REFS = "node_ref_lists="
 
40
_SIGNATURE = "Bazaar Graph Index 1\n"
 
41
 
 
42
 
 
43
_whitespace_re = re.compile('[\t\n\x0b\x0c\r\x00 ]')
 
44
_newline_null_re = re.compile('[\n\0]')
 
45
 
 
46
 
 
47
class GraphIndexBuilder(object):
 
48
    """A builder that can build a GraphIndex.
 
49
    
 
50
    The resulting graph has the structure:
 
51
    
 
52
    _SIGNATURE OPTIONS NODES NEWLINE
 
53
    _SIGNATURE     := 'Bazaar Graph Index 1' NEWLINE
 
54
    OPTIONS        := 'node_ref_lists=' DIGITS NEWLINE
 
55
    NODES          := NODE*
 
56
    NODE           := KEY NULL ABSENT? NULL REFERENCES NULL VALUE NEWLINE
 
57
    KEY            := Not-whitespace-utf8
 
58
    ABSENT         := 'a'
 
59
    REFERENCES     := REFERENCE_LIST (TAB REFERENCE_LIST){node_ref_lists - 1}
 
60
    REFERENCE_LIST := (REFERENCE (CR REFERENCE)*)?
 
61
    REFERENCE      := DIGITS  ; digits is the byte offset in the index of the
 
62
                              ; referenced key.
 
63
    VALUE          := no-newline-no-null-bytes
 
64
    """
 
65
 
 
66
    def __init__(self, reference_lists=0, key_elements=1):
 
67
        """Create a GraphIndex builder.
 
68
 
 
69
        :param reference_lists: The number of node references lists for each
 
70
            entry.
 
71
        :param key_elements: The number of bytestrings in each key.
 
72
        """
 
73
        self.reference_lists = reference_lists
 
74
        self._keys = set()
 
75
        self._nodes = {}
 
76
        self._nodes_by_key = {}
 
77
        self._key_length = key_elements
 
78
 
 
79
    def _check_key(self, key):
 
80
        """Raise BadIndexKey if key is not a valid key for this index."""
 
81
        if type(key) != tuple:
 
82
            raise errors.BadIndexKey(key)
 
83
        if self._key_length != len(key):
 
84
            raise errors.BadIndexKey(key)
 
85
        for element in key:
 
86
            if not element or _whitespace_re.search(element) is not None:
 
87
                raise errors.BadIndexKey(element)
 
88
 
 
89
    def add_node(self, key, value, references=()):
 
90
        """Add a node to the index.
 
91
 
 
92
        :param key: The key. keys are non-empty tuples containing
 
93
            as many whitespace-free utf8 bytestrings as the key length
 
94
            defined for this index.
 
95
        :param references: An iterable of iterables of keys. Each is a
 
96
            reference to another key.
 
97
        :param value: The value to associate with the key. It may be any
 
98
            bytes as long as it does not contain \0 or \n.
 
99
        """
 
100
        self._check_key(key)
 
101
        if _newline_null_re.search(value) is not None:
 
102
            raise errors.BadIndexValue(value)
 
103
        if len(references) != self.reference_lists:
 
104
            raise errors.BadIndexValue(references)
 
105
        node_refs = []
 
106
        for reference_list in references:
 
107
            for reference in reference_list:
 
108
                self._check_key(reference)
 
109
                if reference not in self._nodes:
 
110
                    self._nodes[reference] = ('a', (), '')
 
111
            node_refs.append(tuple(reference_list))
 
112
        if key in self._nodes and self._nodes[key][0] == '':
 
113
            raise errors.BadIndexDuplicateKey(key, self)
 
114
        self._nodes[key] = ('', tuple(node_refs), value)
 
115
        self._keys.add(key)
 
116
        if self._key_length > 1:
 
117
            key_dict = self._nodes_by_key
 
118
            if self.reference_lists:
 
119
                key_value = key, value, tuple(node_refs)
 
120
            else:
 
121
                key_value = key, value
 
122
            # possibly should do this on-demand, but it seems likely it is 
 
123
            # always wanted
 
124
            # For a key of (foo, bar, baz) create
 
125
            # _nodes_by_key[foo][bar][baz] = key_value
 
126
            for subkey in key[:-1]:
 
127
                key_dict = key_dict.setdefault(subkey, {})
 
128
            key_dict[key[-1]] = key_value
 
129
 
 
130
    def finish(self):
 
131
        lines = [_SIGNATURE]
 
132
        lines.append(_OPTION_NODE_REFS + str(self.reference_lists) + '\n')
 
133
        lines.append(_OPTION_KEY_ELEMENTS + str(self._key_length) + '\n')
 
134
        lines.append(_OPTION_LEN + str(len(self._keys)) + '\n')
 
135
        prefix_length = sum(len(x) for x in lines)
 
136
        # references are byte offsets. To avoid having to do nasty
 
137
        # polynomial work to resolve offsets (references to later in the 
 
138
        # file cannot be determined until all the inbetween references have
 
139
        # been calculated too) we pad the offsets with 0's to make them be
 
140
        # of consistent length. Using binary offsets would break the trivial
 
141
        # file parsing.
 
142
        # to calculate the width of zero's needed we do three passes:
 
143
        # one to gather all the non-reference data and the number of references.
 
144
        # one to pad all the data with reference-length and determine entry
 
145
        # addresses.
 
146
        # One to serialise.
 
147
        
 
148
        # forward sorted by key. In future we may consider topological sorting,
 
149
        # at the cost of table scans for direct lookup, or a second index for
 
150
        # direct lookup
 
151
        nodes = sorted(self._nodes.items())
 
152
        # if we do not prepass, we don't know how long it will be up front.
 
153
        expected_bytes = None
 
154
        # we only need to pre-pass if we have reference lists at all.
 
155
        if self.reference_lists:
 
156
            key_offset_info = []
 
157
            non_ref_bytes = prefix_length
 
158
            total_references = 0
 
159
            # TODO use simple multiplication for the constants in this loop.
 
160
            for key, (absent, references, value) in nodes:
 
161
                # record the offset known *so far* for this key:
 
162
                # the non reference bytes to date, and the total references to
 
163
                # date - saves reaccumulating on the second pass
 
164
                key_offset_info.append((key, non_ref_bytes, total_references))
 
165
                # key is literal, value is literal, there are 3 null's, 1 NL
 
166
                # key is variable length tuple, \x00 between elements
 
167
                non_ref_bytes += sum(len(element) for element in key)
 
168
                if self._key_length > 1:
 
169
                    non_ref_bytes += self._key_length - 1
 
170
                # value is literal bytes, there are 3 null's, 1 NL.
 
171
                non_ref_bytes += len(value) + 3 + 1
 
172
                # one byte for absent if set.
 
173
                if absent:
 
174
                    non_ref_bytes += 1
 
175
                elif self.reference_lists:
 
176
                    # (ref_lists -1) tabs
 
177
                    non_ref_bytes += self.reference_lists - 1
 
178
                    # (ref-1 cr's per ref_list)
 
179
                    for ref_list in references:
 
180
                        # how many references across the whole file?
 
181
                        total_references += len(ref_list)
 
182
                        # accrue reference separators
 
183
                        if ref_list:
 
184
                            non_ref_bytes += len(ref_list) - 1
 
185
            # how many digits are needed to represent the total byte count?
 
186
            digits = 1
 
187
            possible_total_bytes = non_ref_bytes + total_references*digits
 
188
            while 10 ** digits < possible_total_bytes:
 
189
                digits += 1
 
190
                possible_total_bytes = non_ref_bytes + total_references*digits
 
191
            expected_bytes = possible_total_bytes + 1 # terminating newline
 
192
            # resolve key addresses.
 
193
            key_addresses = {}
 
194
            for key, non_ref_bytes, total_references in key_offset_info:
 
195
                key_addresses[key] = non_ref_bytes + total_references*digits
 
196
            # serialise
 
197
            format_string = '%%0%sd' % digits
 
198
        for key, (absent, references, value) in nodes:
 
199
            flattened_references = []
 
200
            for ref_list in references:
 
201
                ref_addresses = []
 
202
                for reference in ref_list:
 
203
                    ref_addresses.append(format_string % key_addresses[reference])
 
204
                flattened_references.append('\r'.join(ref_addresses))
 
205
            string_key = '\x00'.join(key)
 
206
            lines.append("%s\x00%s\x00%s\x00%s\n" % (string_key, absent,
 
207
                '\t'.join(flattened_references), value))
 
208
        lines.append('\n')
 
209
        result = StringIO(''.join(lines))
 
210
        if expected_bytes and len(result.getvalue()) != expected_bytes:
 
211
            raise errors.BzrError('Failed index creation. Internal error:'
 
212
                ' mismatched output length and expected length: %d %d' %
 
213
                (len(result.getvalue()), expected_bytes))
 
214
        return StringIO(''.join(lines))
 
215
 
 
216
 
 
217
class GraphIndex(object):
 
218
    """An index for data with embedded graphs.
 
219
 
 
220
    The index maps keys to a list of key reference lists, and a value.
 
221
    Each node has the same number of key reference lists. Each key reference
 
222
    list can be empty or an arbitrary length. The value is an opaque NULL
 
223
    terminated string without any newlines. The storage of the index is 
 
224
    hidden in the interface: keys and key references are always tuples of
 
225
    bytestrings, never the internal representation (e.g. dictionary offsets).
 
226
 
 
227
    It is presumed that the index will not be mutated - it is static data.
 
228
 
 
229
    Successive iter_all_entries calls will read the entire index each time.
 
230
    Additionally, iter_entries calls will read the index linearly until the
 
231
    desired keys are found. XXX: This must be fixed before the index is
 
232
    suitable for production use. :XXX
 
233
    """
 
234
 
 
235
    def __init__(self, transport, name, size):
 
236
        """Open an index called name on transport.
 
237
 
 
238
        :param transport: A bzrlib.transport.Transport.
 
239
        :param name: A path to provide to transport API calls.
 
240
        :param size: The size of the index in bytes. This is used for bisection
 
241
            logic to perform partial index reads. While the size could be
 
242
            obtained by statting the file this introduced an additional round
 
243
            trip that is avoided by having it supplied.
 
244
        """
 
245
        self._transport = transport
 
246
        self._name = name
 
247
        self._nodes = None
 
248
        self._parsed_byte_map = []
 
249
        self._key_count = None
 
250
        self._keys_by_offset = None
 
251
        self._nodes_by_key = None
 
252
        self._size = size
 
253
 
 
254
    def _buffer_all(self):
 
255
        """Buffer all the index data.
 
256
 
 
257
        Mutates self._nodes and self.keys_by_offset.
 
258
        """
 
259
        if 'index' in debug.debug_flags:
 
260
            mutter('Reading entire index %s', self._transport.abspath(self._name))
 
261
        stream = self._transport.get(self._name)
 
262
        self._read_prefix(stream)
 
263
        expected_elements = 3 + self._key_length
 
264
        line_count = 0
 
265
        # raw data keyed by offset
 
266
        self._keys_by_offset = {}
 
267
        # ready-to-return key:value or key:value, node_ref_lists
 
268
        self._nodes = {}
 
269
        self._nodes_by_key = {}
 
270
        trailers = 0
 
271
        pos = stream.tell()
 
272
        for line in stream.readlines():
 
273
            if line == '\n':
 
274
                trailers += 1
 
275
                continue
 
276
            elements = line.split('\0')
 
277
            if len(elements) != expected_elements:
 
278
                raise errors.BadIndexData(self)
 
279
            # keys are tuples
 
280
            key = tuple(elements[:self._key_length])
 
281
            absent, references, value = elements[-3:]
 
282
            value = value[:-1] # remove the newline
 
283
            ref_lists = []
 
284
            for ref_string in references.split('\t'):
 
285
                ref_lists.append(tuple([
 
286
                    int(ref) for ref in ref_string.split('\r') if ref
 
287
                    ]))
 
288
            ref_lists = tuple(ref_lists)
 
289
            self._keys_by_offset[pos] = (key, absent, ref_lists, value)
 
290
            pos += len(line)
 
291
        for key, absent, references, value in self._keys_by_offset.itervalues():
 
292
            if absent:
 
293
                continue
 
294
            # resolve references:
 
295
            if self.node_ref_lists:
 
296
                node_refs = []
 
297
                for ref_list in references:
 
298
                    node_refs.append(tuple([self._keys_by_offset[ref][0] for ref in ref_list]))
 
299
                node_value = (value, tuple(node_refs))
 
300
            else:
 
301
                node_value = value
 
302
            self._nodes[key] = node_value
 
303
            if self._key_length > 1:
 
304
                subkey = list(reversed(key[:-1]))
 
305
                key_dict = self._nodes_by_key
 
306
                if self.node_ref_lists:
 
307
                    key_value = key, node_value[0], node_value[1]
 
308
                else:
 
309
                    key_value = key, node_value
 
310
                # possibly should do this on-demand, but it seems likely it is 
 
311
                # always wanted
 
312
                # For a key of (foo, bar, baz) create
 
313
                # _nodes_by_key[foo][bar][baz] = key_value
 
314
                for subkey in key[:-1]:
 
315
                    key_dict = key_dict.setdefault(subkey, {})
 
316
                key_dict[key[-1]] = key_value
 
317
        # cache the keys for quick set intersections
 
318
        self._keys = set(self._nodes)
 
319
        if trailers != 1:
 
320
            # there must be one line - the empty trailer line.
 
321
            raise errors.BadIndexData(self)
 
322
 
 
323
    def iter_all_entries(self):
 
324
        """Iterate over all keys within the index.
 
325
 
 
326
        :return: An iterable of (index, key, value) or (index, key, value, reference_lists).
 
327
            The former tuple is used when there are no reference lists in the
 
328
            index, making the API compatible with simple key:value index types.
 
329
            There is no defined order for the result iteration - it will be in
 
330
            the most efficient order for the index.
 
331
        """
 
332
        if 'evil' in debug.debug_flags:
 
333
            trace.mutter_callsite(3,
 
334
                "iter_all_entries scales with size of history.")
 
335
        if self._nodes is None:
 
336
            self._buffer_all()
 
337
        if self.node_ref_lists:
 
338
            for key, (value, node_ref_lists) in self._nodes.iteritems():
 
339
                yield self, key, value, node_ref_lists
 
340
        else:
 
341
            for key, value in self._nodes.iteritems():
 
342
                yield self, key, value
 
343
 
 
344
    def _read_prefix(self, stream):
 
345
        signature = stream.read(len(self._signature()))
 
346
        if not signature == self._signature():
 
347
            raise errors.BadIndexFormatSignature(self._name, GraphIndex)
 
348
        options_line = stream.readline()
 
349
        if not options_line.startswith(_OPTION_NODE_REFS):
 
350
            raise errors.BadIndexOptions(self)
 
351
        try:
 
352
            self.node_ref_lists = int(options_line[len(_OPTION_NODE_REFS):-1])
 
353
        except ValueError:
 
354
            raise errors.BadIndexOptions(self)
 
355
        options_line = stream.readline()
 
356
        if not options_line.startswith(_OPTION_KEY_ELEMENTS):
 
357
            raise errors.BadIndexOptions(self)
 
358
        try:
 
359
            self._key_length = int(options_line[len(_OPTION_KEY_ELEMENTS):-1])
 
360
        except ValueError:
 
361
            raise errors.BadIndexOptions(self)
 
362
        options_line = stream.readline()
 
363
        if not options_line.startswith(_OPTION_LEN):
 
364
            raise errors.BadIndexOptions(self)
 
365
        try:
 
366
            self._key_count = int(options_line[len(_OPTION_LEN):-1])
 
367
        except ValueError:
 
368
            raise errors.BadIndexOptions(self)
 
369
 
 
370
    def iter_entries(self, keys):
 
371
        """Iterate over keys within the index.
 
372
 
 
373
        :param keys: An iterable providing the keys to be retrieved.
 
374
        :return: An iterable as per iter_all_entries, but restricted to the
 
375
            keys supplied. No additional keys will be returned, and every
 
376
            key supplied that is in the index will be returned.
 
377
        """
 
378
        keys = set(keys)
 
379
        if not keys:
 
380
            return
 
381
        if self._nodes is None:
 
382
            self._buffer_all()
 
383
        keys = keys.intersection(self._keys)
 
384
        if self.node_ref_lists:
 
385
            for key in keys:
 
386
                value, node_refs = self._nodes[key]
 
387
                yield self, key, value, node_refs
 
388
        else:
 
389
            for key in keys:
 
390
                yield self, key, self._nodes[key]
 
391
 
 
392
    def iter_entries_prefix(self, keys):
 
393
        """Iterate over keys within the index using prefix matching.
 
394
 
 
395
        Prefix matching is applied within the tuple of a key, not to within
 
396
        the bytestring of each key element. e.g. if you have the keys ('foo',
 
397
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
 
398
        only the former key is returned.
 
399
 
 
400
        :param keys: An iterable providing the key prefixes to be retrieved.
 
401
            Each key prefix takes the form of a tuple the length of a key, but
 
402
            with the last N elements 'None' rather than a regular bytestring.
 
403
            The first element cannot be 'None'.
 
404
        :return: An iterable as per iter_all_entries, but restricted to the
 
405
            keys with a matching prefix to those supplied. No additional keys
 
406
            will be returned, and every match that is in the index will be
 
407
            returned.
 
408
        """
 
409
        keys = set(keys)
 
410
        if not keys:
 
411
            return
 
412
        # load data - also finds key lengths
 
413
        if self._nodes is None:
 
414
            self._buffer_all()
 
415
        if self._key_length == 1:
 
416
            for key in keys:
 
417
                # sanity check
 
418
                if key[0] is None:
 
419
                    raise errors.BadIndexKey(key)
 
420
                if len(key) != self._key_length:
 
421
                    raise errors.BadIndexKey(key)
 
422
                if self.node_ref_lists:
 
423
                    value, node_refs = self._nodes[key]
 
424
                    yield self, key, value, node_refs
 
425
                else:
 
426
                    yield self, key, self._nodes[key]
 
427
            return
 
428
        for key in keys:
 
429
            # sanity check
 
430
            if key[0] is None:
 
431
                raise errors.BadIndexKey(key)
 
432
            if len(key) != self._key_length:
 
433
                raise errors.BadIndexKey(key)
 
434
            # find what it refers to:
 
435
            key_dict = self._nodes_by_key
 
436
            elements = list(key)
 
437
            # find the subdict whose contents should be returned.
 
438
            try:
 
439
                while len(elements) and elements[0] is not None:
 
440
                    key_dict = key_dict[elements[0]]
 
441
                    elements.pop(0)
 
442
            except KeyError:
 
443
                # a non-existant lookup.
 
444
                continue
 
445
            if len(elements):
 
446
                dicts = [key_dict]
 
447
                while dicts:
 
448
                    key_dict = dicts.pop(-1)
 
449
                    # can't be empty or would not exist
 
450
                    item, value = key_dict.iteritems().next()
 
451
                    if type(value) == dict:
 
452
                        # push keys 
 
453
                        dicts.extend(key_dict.itervalues())
 
454
                    else:
 
455
                        # yield keys
 
456
                        for value in key_dict.itervalues():
 
457
                            # each value is the key:value:node refs tuple
 
458
                            # ready to yield.
 
459
                            yield (self, ) + value
 
460
            else:
 
461
                # the last thing looked up was a terminal element
 
462
                yield (self, ) + key_dict
 
463
 
 
464
    def key_count(self):
 
465
        """Return an estimate of the number of keys in this index.
 
466
        
 
467
        For GraphIndex the estimate is exact.
 
468
        """
 
469
        if self._key_count is None:
 
470
            # really this should just read the prefix
 
471
            self._buffer_all()
 
472
        return self._key_count
 
473
 
 
474
    def _signature(self):
 
475
        """The file signature for this index type."""
 
476
        return _SIGNATURE
 
477
 
 
478
    def validate(self):
 
479
        """Validate that everything in the index can be accessed."""
 
480
        # iter_all validates completely at the moment, so just do that.
 
481
        for node in self.iter_all_entries():
 
482
            pass
 
483
 
 
484
 
 
485
class CombinedGraphIndex(object):
 
486
    """A GraphIndex made up from smaller GraphIndices.
 
487
    
 
488
    The backing indices must implement GraphIndex, and are presumed to be
 
489
    static data.
 
490
 
 
491
    Queries against the combined index will be made against the first index,
 
492
    and then the second and so on. The order of index's can thus influence
 
493
    performance significantly. For example, if one index is on local disk and a
 
494
    second on a remote server, the local disk index should be before the other
 
495
    in the index list.
 
496
    """
 
497
 
 
498
    def __init__(self, indices):
 
499
        """Create a CombinedGraphIndex backed by indices.
 
500
 
 
501
        :param indices: An ordered list of indices to query for data.
 
502
        """
 
503
        self._indices = indices
 
504
 
 
505
    def __repr__(self):
 
506
        return "%s(%s)" % (
 
507
                self.__class__.__name__,
 
508
                ', '.join(map(repr, self._indices)))
 
509
 
 
510
    def insert_index(self, pos, index):
 
511
        """Insert a new index in the list of indices to query.
 
512
 
 
513
        :param pos: The position to insert the index.
 
514
        :param index: The index to insert.
 
515
        """
 
516
        self._indices.insert(pos, index)
 
517
 
 
518
    def iter_all_entries(self):
 
519
        """Iterate over all keys within the index
 
520
 
 
521
        Duplicate keys across child indices are presumed to have the same
 
522
        value and are only reported once.
 
523
 
 
524
        :return: An iterable of (index, key, reference_lists, value).
 
525
            There is no defined order for the result iteration - it will be in
 
526
            the most efficient order for the index.
 
527
        """
 
528
        seen_keys = set()
 
529
        for index in self._indices:
 
530
            for node in index.iter_all_entries():
 
531
                if node[1] not in seen_keys:
 
532
                    yield node
 
533
                    seen_keys.add(node[1])
 
534
 
 
535
    def iter_entries(self, keys):
 
536
        """Iterate over keys within the index.
 
537
 
 
538
        Duplicate keys across child indices are presumed to have the same
 
539
        value and are only reported once.
 
540
 
 
541
        :param keys: An iterable providing the keys to be retrieved.
 
542
        :return: An iterable of (index, key, reference_lists, value). There is no
 
543
            defined order for the result iteration - it will be in the most
 
544
            efficient order for the index.
 
545
        """
 
546
        keys = set(keys)
 
547
        for index in self._indices:
 
548
            if not keys:
 
549
                return
 
550
            for node in index.iter_entries(keys):
 
551
                keys.remove(node[1])
 
552
                yield node
 
553
 
 
554
    def iter_entries_prefix(self, keys):
 
555
        """Iterate over keys within the index using prefix matching.
 
556
 
 
557
        Duplicate keys across child indices are presumed to have the same
 
558
        value and are only reported once.
 
559
 
 
560
        Prefix matching is applied within the tuple of a key, not to within
 
561
        the bytestring of each key element. e.g. if you have the keys ('foo',
 
562
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
 
563
        only the former key is returned.
 
564
 
 
565
        :param keys: An iterable providing the key prefixes to be retrieved.
 
566
            Each key prefix takes the form of a tuple the length of a key, but
 
567
            with the last N elements 'None' rather than a regular bytestring.
 
568
            The first element cannot be 'None'.
 
569
        :return: An iterable as per iter_all_entries, but restricted to the
 
570
            keys with a matching prefix to those supplied. No additional keys
 
571
            will be returned, and every match that is in the index will be
 
572
            returned.
 
573
        """
 
574
        keys = set(keys)
 
575
        if not keys:
 
576
            return
 
577
        seen_keys = set()
 
578
        for index in self._indices:
 
579
            for node in index.iter_entries_prefix(keys):
 
580
                if node[1] in seen_keys:
 
581
                    continue
 
582
                seen_keys.add(node[1])
 
583
                yield node
 
584
 
 
585
    def key_count(self):
 
586
        """Return an estimate of the number of keys in this index.
 
587
        
 
588
        For CombinedGraphIndex this is approximated by the sum of the keys of
 
589
        the child indices. As child indices may have duplicate keys this can
 
590
        have a maximum error of the number of child indices * largest number of
 
591
        keys in any index.
 
592
        """
 
593
        return sum((index.key_count() for index in self._indices), 0)
 
594
 
 
595
    def validate(self):
 
596
        """Validate that everything in the index can be accessed."""
 
597
        for index in self._indices:
 
598
            index.validate()
 
599
 
 
600
 
 
601
class InMemoryGraphIndex(GraphIndexBuilder):
 
602
    """A GraphIndex which operates entirely out of memory and is mutable.
 
603
 
 
604
    This is designed to allow the accumulation of GraphIndex entries during a
 
605
    single write operation, where the accumulated entries need to be immediately
 
606
    available - for example via a CombinedGraphIndex.
 
607
    """
 
608
 
 
609
    def add_nodes(self, nodes):
 
610
        """Add nodes to the index.
 
611
 
 
612
        :param nodes: An iterable of (key, node_refs, value) entries to add.
 
613
        """
 
614
        if self.reference_lists:
 
615
            for (key, value, node_refs) in nodes:
 
616
                self.add_node(key, value, node_refs)
 
617
        else:
 
618
            for (key, value) in nodes:
 
619
                self.add_node(key, value)
 
620
 
 
621
    def iter_all_entries(self):
 
622
        """Iterate over all keys within the index
 
623
 
 
624
        :return: An iterable of (index, key, reference_lists, value). There is no
 
625
            defined order for the result iteration - it will be in the most
 
626
            efficient order for the index (in this case dictionary hash order).
 
627
        """
 
628
        if 'evil' in debug.debug_flags:
 
629
            trace.mutter_callsite(3,
 
630
                "iter_all_entries scales with size of history.")
 
631
        if self.reference_lists:
 
632
            for key, (absent, references, value) in self._nodes.iteritems():
 
633
                if not absent:
 
634
                    yield self, key, value, references
 
635
        else:
 
636
            for key, (absent, references, value) in self._nodes.iteritems():
 
637
                if not absent:
 
638
                    yield self, key, value
 
639
 
 
640
    def iter_entries(self, keys):
 
641
        """Iterate over keys within the index.
 
642
 
 
643
        :param keys: An iterable providing the keys to be retrieved.
 
644
        :return: An iterable of (index, key, reference_lists, value). There is no
 
645
            defined order for the result iteration - it will be in the most
 
646
            efficient order for the index (keys iteration order in this case).
 
647
        """
 
648
        keys = set(keys)
 
649
        if self.reference_lists:
 
650
            for key in keys.intersection(self._keys):
 
651
                node = self._nodes[key]
 
652
                if not node[0]:
 
653
                    yield self, key, node[2], node[1]
 
654
        else:
 
655
            for key in keys.intersection(self._keys):
 
656
                node = self._nodes[key]
 
657
                if not node[0]:
 
658
                    yield self, key, node[2]
 
659
 
 
660
    def iter_entries_prefix(self, keys):
 
661
        """Iterate over keys within the index using prefix matching.
 
662
 
 
663
        Prefix matching is applied within the tuple of a key, not to within
 
664
        the bytestring of each key element. e.g. if you have the keys ('foo',
 
665
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
 
666
        only the former key is returned.
 
667
 
 
668
        :param keys: An iterable providing the key prefixes to be retrieved.
 
669
            Each key prefix takes the form of a tuple the length of a key, but
 
670
            with the last N elements 'None' rather than a regular bytestring.
 
671
            The first element cannot be 'None'.
 
672
        :return: An iterable as per iter_all_entries, but restricted to the
 
673
            keys with a matching prefix to those supplied. No additional keys
 
674
            will be returned, and every match that is in the index will be
 
675
            returned.
 
676
        """
 
677
        # XXX: To much duplication with the GraphIndex class; consider finding
 
678
        # a good place to pull out the actual common logic.
 
679
        keys = set(keys)
 
680
        if not keys:
 
681
            return
 
682
        if self._key_length == 1:
 
683
            for key in keys:
 
684
                # sanity check
 
685
                if key[0] is None:
 
686
                    raise errors.BadIndexKey(key)
 
687
                if len(key) != self._key_length:
 
688
                    raise errors.BadIndexKey(key)
 
689
                node = self._nodes[key]
 
690
                if node[0]:
 
691
                    continue 
 
692
                if self.reference_lists:
 
693
                    yield self, key, node[2], node[1]
 
694
                else:
 
695
                    yield self, key, node[2]
 
696
            return
 
697
        for key in keys:
 
698
            # sanity check
 
699
            if key[0] is None:
 
700
                raise errors.BadIndexKey(key)
 
701
            if len(key) != self._key_length:
 
702
                raise errors.BadIndexKey(key)
 
703
            # find what it refers to:
 
704
            key_dict = self._nodes_by_key
 
705
            elements = list(key)
 
706
            # find the subdict to return
 
707
            try:
 
708
                while len(elements) and elements[0] is not None:
 
709
                    key_dict = key_dict[elements[0]]
 
710
                    elements.pop(0)
 
711
            except KeyError:
 
712
                # a non-existant lookup.
 
713
                continue
 
714
            if len(elements):
 
715
                dicts = [key_dict]
 
716
                while dicts:
 
717
                    key_dict = dicts.pop(-1)
 
718
                    # can't be empty or would not exist
 
719
                    item, value = key_dict.iteritems().next()
 
720
                    if type(value) == dict:
 
721
                        # push keys 
 
722
                        dicts.extend(key_dict.itervalues())
 
723
                    else:
 
724
                        # yield keys
 
725
                        for value in key_dict.itervalues():
 
726
                            yield (self, ) + value
 
727
            else:
 
728
                yield (self, ) + key_dict
 
729
 
 
730
    def key_count(self):
 
731
        """Return an estimate of the number of keys in this index.
 
732
        
 
733
        For InMemoryGraphIndex the estimate is exact.
 
734
        """
 
735
        return len(self._keys)
 
736
 
 
737
    def validate(self):
 
738
        """In memory index's have no known corruption at the moment."""
 
739
 
 
740
 
 
741
class GraphIndexPrefixAdapter(object):
 
742
    """An adapter between GraphIndex with different key lengths.
 
743
 
 
744
    Queries against this will emit queries against the adapted Graph with the
 
745
    prefix added, queries for all items use iter_entries_prefix. The returned
 
746
    nodes will have their keys and node references adjusted to remove the 
 
747
    prefix. Finally, an add_nodes_callback can be supplied - when called the
 
748
    nodes and references being added will have prefix prepended.
 
749
    """
 
750
 
 
751
    def __init__(self, adapted, prefix, missing_key_length,
 
752
        add_nodes_callback=None):
 
753
        """Construct an adapter against adapted with prefix."""
 
754
        self.adapted = adapted
 
755
        self.prefix_key = prefix + (None,)*missing_key_length
 
756
        self.prefix = prefix
 
757
        self.prefix_len = len(prefix)
 
758
        self.add_nodes_callback = add_nodes_callback
 
759
 
 
760
    def add_nodes(self, nodes):
 
761
        """Add nodes to the index.
 
762
 
 
763
        :param nodes: An iterable of (key, node_refs, value) entries to add.
 
764
        """
 
765
        # save nodes in case its an iterator
 
766
        nodes = tuple(nodes)
 
767
        translated_nodes = []
 
768
        try:
 
769
            # Add prefix_key to each reference node_refs is a tuple of tuples,
 
770
            # so split it apart, and add prefix_key to the internal reference
 
771
            for (key, value, node_refs) in nodes:
 
772
                adjusted_references = (
 
773
                    tuple(tuple(self.prefix + ref_node for ref_node in ref_list)
 
774
                        for ref_list in node_refs))
 
775
                translated_nodes.append((self.prefix + key, value,
 
776
                    adjusted_references))
 
777
        except ValueError:
 
778
            # XXX: TODO add an explicit interface for getting the reference list
 
779
            # status, to handle this bit of user-friendliness in the API more 
 
780
            # explicitly.
 
781
            for (key, value) in nodes:
 
782
                translated_nodes.append((self.prefix + key, value))
 
783
        self.add_nodes_callback(translated_nodes)
 
784
 
 
785
    def add_node(self, key, value, references=()):
 
786
        """Add a node to the index.
 
787
 
 
788
        :param key: The key. keys are non-empty tuples containing
 
789
            as many whitespace-free utf8 bytestrings as the key length
 
790
            defined for this index.
 
791
        :param references: An iterable of iterables of keys. Each is a
 
792
            reference to another key.
 
793
        :param value: The value to associate with the key. It may be any
 
794
            bytes as long as it does not contain \0 or \n.
 
795
        """
 
796
        self.add_nodes(((key, value, references), ))
 
797
 
 
798
    def _strip_prefix(self, an_iter):
 
799
        """Strip prefix data from nodes and return it."""
 
800
        for node in an_iter:
 
801
            # cross checks
 
802
            if node[1][:self.prefix_len] != self.prefix:
 
803
                raise errors.BadIndexData(self)
 
804
            for ref_list in node[3]:
 
805
                for ref_node in ref_list:
 
806
                    if ref_node[:self.prefix_len] != self.prefix:
 
807
                        raise errors.BadIndexData(self)
 
808
            yield node[0], node[1][self.prefix_len:], node[2], (
 
809
                tuple(tuple(ref_node[self.prefix_len:] for ref_node in ref_list)
 
810
                for ref_list in node[3]))
 
811
 
 
812
    def iter_all_entries(self):
 
813
        """Iterate over all keys within the index
 
814
 
 
815
        iter_all_entries is implemented against the adapted index using
 
816
        iter_entries_prefix.
 
817
 
 
818
        :return: An iterable of (index, key, reference_lists, value). There is no
 
819
            defined order for the result iteration - it will be in the most
 
820
            efficient order for the index (in this case dictionary hash order).
 
821
        """
 
822
        return self._strip_prefix(self.adapted.iter_entries_prefix([self.prefix_key]))
 
823
 
 
824
    def iter_entries(self, keys):
 
825
        """Iterate over keys within the index.
 
826
 
 
827
        :param keys: An iterable providing the keys to be retrieved.
 
828
        :return: An iterable of (key, reference_lists, value). There is no
 
829
            defined order for the result iteration - it will be in the most
 
830
            efficient order for the index (keys iteration order in this case).
 
831
        """
 
832
        return self._strip_prefix(self.adapted.iter_entries(
 
833
            self.prefix + key for key in keys))
 
834
 
 
835
    def iter_entries_prefix(self, keys):
 
836
        """Iterate over keys within the index using prefix matching.
 
837
 
 
838
        Prefix matching is applied within the tuple of a key, not to within
 
839
        the bytestring of each key element. e.g. if you have the keys ('foo',
 
840
        'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
 
841
        only the former key is returned.
 
842
 
 
843
        :param keys: An iterable providing the key prefixes to be retrieved.
 
844
            Each key prefix takes the form of a tuple the length of a key, but
 
845
            with the last N elements 'None' rather than a regular bytestring.
 
846
            The first element cannot be 'None'.
 
847
        :return: An iterable as per iter_all_entries, but restricted to the
 
848
            keys with a matching prefix to those supplied. No additional keys
 
849
            will be returned, and every match that is in the index will be
 
850
            returned.
 
851
        """
 
852
        return self._strip_prefix(self.adapted.iter_entries_prefix(
 
853
            self.prefix + key for key in keys))
 
854
 
 
855
    def key_count(self):
 
856
        """Return an estimate of the number of keys in this index.
 
857
        
 
858
        For GraphIndexPrefixAdapter this is relatively expensive - key
 
859
        iteration with the prefix is done.
 
860
        """
 
861
        return len(list(self.iter_all_entries()))
 
862
 
 
863
    def validate(self):
 
864
        """Call the adapted's validate."""
 
865
        self.adapted.validate()