~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/index.py

  • Committer: Martin Packman
  • Date: 2011-10-06 16:41:45 UTC
  • mfrom: (6015.33.10 2.4)
  • mto: This revision was merged to the branch mainline in revision 6202.
  • Revision ID: martin.packman@canonical.com-20111006164145-o98oqn32440extgt
Merge 2.4 into dev

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:
462
470
        trailers = 0
463
471
        pos = stream.tell()
464
472
        lines = stream.read().split('\n')
 
473
        # GZ 2009-09-20: Should really use a try/finally block to ensure close
465
474
        stream.close()
466
475
        del lines[-1]
467
476
        _, _, _, trailers = self._parse_lines(lines, pos)
670
679
        if self._nodes is not None:
671
680
            return self._iter_entries_from_total_buffer(keys)
672
681
        else:
673
 
            return (result[1] for result in bisect_multi_bytes(
 
682
            return (result[1] for result in bisect_multi.bisect_multi_bytes(
674
683
                self._lookup_keys_via_location, self._size, keys))
675
684
 
676
685
    def iter_entries_prefix(self, keys):
1287
1296
    def get_parent_map(self, keys):
1288
1297
        """See graph.StackedParentsProvider.get_parent_map"""
1289
1298
        search_keys = set(keys)
1290
 
        if NULL_REVISION in search_keys:
1291
 
            search_keys.discard(NULL_REVISION)
1292
 
            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:[]}
1293
1302
        else:
1294
1303
            found_parents = {}
1295
1304
        for index, key, value, refs in self.iter_entries(search_keys):
1296
1305
            parents = refs[0]
1297
1306
            if not parents:
1298
 
                parents = (NULL_REVISION,)
 
1307
                parents = (_mod_revision.NULL_REVISION,)
1299
1308
            found_parents[key] = parents
1300
1309
        return found_parents
1301
1310
 
1433
1442
        """
1434
1443
        indices_info = zip(self._index_names, self._indices)
1435
1444
        if 'index' in debug.debug_flags:
1436
 
            mutter('CombinedGraphIndex reordering: currently %r, promoting %r',
1437
 
                   indices_info, hit_indices)
 
1445
            trace.mutter('CombinedGraphIndex reordering: currently %r, '
 
1446
                         'promoting %r', indices_info, hit_indices)
1438
1447
        hit_names = []
1439
1448
        unhit_names = []
1440
1449
        new_hit_indices = []
1457
1466
        self._indices = new_hit_indices + unhit_indices
1458
1467
        self._index_names = hit_names + unhit_names
1459
1468
        if 'index' in debug.debug_flags:
1460
 
            mutter('CombinedGraphIndex reordered: %r', self._indices)
 
1469
            trace.mutter('CombinedGraphIndex reordered: %r', self._indices)
1461
1470
        return hit_names
1462
1471
 
1463
1472
    def _move_to_front_by_name(self, hit_names):