~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/index.py

  • Committer: Vincent Ladeuil
  • Date: 2017-01-17 13:48:10 UTC
  • mfrom: (6615.3.6 merges)
  • mto: This revision was merged to the branch mainline in revision 6620.
  • Revision ID: v.ladeuil+lp@free.fr-20170117134810-j9p3lidfy6pfyfsc
Merge 2.7, resolving conflicts

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