~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/index.py

Merge bzr.dev, update to use new hooks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
31
31
 
32
32
from bzrlib.lazy_import import lazy_import
33
33
lazy_import(globals(), """
34
 
from bzrlib import trace
35
 
from bzrlib.bisect_multi import bisect_multi_bytes
36
 
from bzrlib.revision import NULL_REVISION
37
 
from bzrlib.trace import mutter
 
34
from bzrlib import (
 
35
    bisect_multi,
 
36
    revision as _mod_revision,
 
37
    trace,
 
38
    )
38
39
""")
39
40
from bzrlib import (
40
41
    debug,
69
70
class GraphIndexBuilder(object):
70
71
    """A builder that can build a GraphIndex.
71
72
 
72
 
    The resulting graph has the structure:
 
73
    The resulting graph has the structure::
73
74
 
74
 
    _SIGNATURE OPTIONS NODES NEWLINE
75
 
    _SIGNATURE     := 'Bazaar Graph Index 1' NEWLINE
76
 
    OPTIONS        := 'node_ref_lists=' DIGITS NEWLINE
77
 
    NODES          := NODE*
78
 
    NODE           := KEY NULL ABSENT? NULL REFERENCES NULL VALUE NEWLINE
79
 
    KEY            := Not-whitespace-utf8
80
 
    ABSENT         := 'a'
81
 
    REFERENCES     := REFERENCE_LIST (TAB REFERENCE_LIST){node_ref_lists - 1}
82
 
    REFERENCE_LIST := (REFERENCE (CR REFERENCE)*)?
83
 
    REFERENCE      := DIGITS  ; digits is the byte offset in the index of the
84
 
                              ; referenced key.
85
 
    VALUE          := no-newline-no-null-bytes
 
75
      _SIGNATURE OPTIONS NODES NEWLINE
 
76
      _SIGNATURE     := 'Bazaar Graph Index 1' NEWLINE
 
77
      OPTIONS        := 'node_ref_lists=' DIGITS NEWLINE
 
78
      NODES          := NODE*
 
79
      NODE           := KEY NULL ABSENT? NULL REFERENCES NULL VALUE NEWLINE
 
80
      KEY            := Not-whitespace-utf8
 
81
      ABSENT         := 'a'
 
82
      REFERENCES     := REFERENCE_LIST (TAB REFERENCE_LIST){node_ref_lists - 1}
 
83
      REFERENCE_LIST := (REFERENCE (CR REFERENCE)*)?
 
84
      REFERENCE      := DIGITS  ; digits is the byte offset in the index of the
 
85
                                ; referenced key.
 
86
      VALUE          := no-newline-no-null-bytes
86
87
    """
87
88
 
88
89
    def __init__(self, reference_lists=0, key_elements=1):
184
185
        :param value: The value associate with this key. Must not contain
185
186
            newlines or null characters.
186
187
        :return: (node_refs, absent_references)
187
 
            node_refs   basically a packed form of 'references' where all
188
 
                        iterables are tuples
189
 
            absent_references   reference keys that are not in self._nodes.
190
 
                                This may contain duplicates if the same key is
191
 
                                referenced in multiple lists.
 
188
        
 
189
            * node_refs: basically a packed form of 'references' where all
 
190
              iterables are tuples
 
191
            * absent_references: reference keys that are not in self._nodes.
 
192
              This may contain duplicates if the same key is referenced in
 
193
              multiple lists.
192
194
        """
193
195
        as_st = StaticTuple.from_sequence
194
196
        self._check_key(key)
219
221
        :param references: An iterable of iterables of keys. Each is a
220
222
            reference to another key.
221
223
        :param value: The value to associate with the key. It may be any
222
 
            bytes as long as it does not contain \0 or \n.
 
224
            bytes as long as it does not contain \\0 or \\n.
223
225
        """
224
226
        (node_refs,
225
227
         absent_references) = self._check_key_ref_value(key, references, value)
243
245
        """
244
246
        
245
247
    def finish(self):
 
248
        """Finish the index.
 
249
 
 
250
        :returns: cStringIO holding the full context of the index as it 
 
251
        should be written to disk.
 
252
        """
246
253
        lines = [_SIGNATURE]
247
254
        lines.append(_OPTION_NODE_REFS + str(self.reference_lists) + '\n')
248
255
        lines.append(_OPTION_KEY_ELEMENTS + str(self._key_length) + '\n')
444
451
            # We already did this
445
452
            return
446
453
        if 'index' in debug.debug_flags:
447
 
            mutter('Reading entire index %s', self._transport.abspath(self._name))
 
454
            trace.mutter('Reading entire index %s',
 
455
                          self._transport.abspath(self._name))
448
456
        if stream is None:
449
457
            stream = self._transport.get(self._name)
450
458
            if self._base_offset != 0:
671
679
        if self._nodes is not None:
672
680
            return self._iter_entries_from_total_buffer(keys)
673
681
        else:
674
 
            return (result[1] for result in bisect_multi_bytes(
 
682
            return (result[1] for result in bisect_multi.bisect_multi_bytes(
675
683
                self._lookup_keys_via_location, self._size, keys))
676
684
 
677
685
    def iter_entries_prefix(self, keys):
1288
1296
    def get_parent_map(self, keys):
1289
1297
        """See graph.StackedParentsProvider.get_parent_map"""
1290
1298
        search_keys = set(keys)
1291
 
        if NULL_REVISION in search_keys:
1292
 
            search_keys.discard(NULL_REVISION)
1293
 
            found_parents = {NULL_REVISION:[]}
 
1299
        if _mod_revision.NULL_REVISION in search_keys:
 
1300
            search_keys.discard(_mod_revision.NULL_REVISION)
 
1301
            found_parents = {_mod_revision.NULL_REVISION:[]}
1294
1302
        else:
1295
1303
            found_parents = {}
1296
1304
        for index, key, value, refs in self.iter_entries(search_keys):
1297
1305
            parents = refs[0]
1298
1306
            if not parents:
1299
 
                parents = (NULL_REVISION,)
 
1307
                parents = (_mod_revision.NULL_REVISION,)
1300
1308
            found_parents[key] = parents
1301
1309
        return found_parents
1302
1310
 
1434
1442
        """
1435
1443
        indices_info = zip(self._index_names, self._indices)
1436
1444
        if 'index' in debug.debug_flags:
1437
 
            mutter('CombinedGraphIndex reordering: currently %r, promoting %r',
1438
 
                   indices_info, hit_indices)
 
1445
            trace.mutter('CombinedGraphIndex reordering: currently %r, '
 
1446
                         'promoting %r', indices_info, hit_indices)
1439
1447
        hit_names = []
1440
1448
        unhit_names = []
1441
1449
        new_hit_indices = []
1458
1466
        self._indices = new_hit_indices + unhit_indices
1459
1467
        self._index_names = hit_names + unhit_names
1460
1468
        if 'index' in debug.debug_flags:
1461
 
            mutter('CombinedGraphIndex reordered: %r', self._indices)
 
1469
            trace.mutter('CombinedGraphIndex reordered: %r', self._indices)
1462
1470
        return hit_names
1463
1471
 
1464
1472
    def _move_to_front_by_name(self, hit_names):