~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/index.py

  • Committer: Robert Collins
  • Date: 2010-05-11 08:36:16 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100511083616-b8fjb19zomwupid0
Make all lock methods return Result objects, rather than lock_read returning self, as per John's review.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
 
1
# Copyright (C) 2007-2010 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 (
35
 
    bisect_multi,
36
 
    revision as _mod_revision,
37
 
    trace,
38
 
    )
 
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
39
38
""")
40
39
from bzrlib import (
41
40
    debug,
70
69
class GraphIndexBuilder(object):
71
70
    """A builder that can build a GraphIndex.
72
71
 
73
 
    The resulting graph has the structure::
 
72
    The resulting graph has the structure:
74
73
 
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
 
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
87
86
    """
88
87
 
89
88
    def __init__(self, reference_lists=0, key_elements=1):
185
184
        :param value: The value associate with this key. Must not contain
186
185
            newlines or null characters.
187
186
        :return: (node_refs, absent_references)
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.
 
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.
194
192
        """
195
193
        as_st = StaticTuple.from_sequence
196
194
        self._check_key(key)
221
219
        :param references: An iterable of iterables of keys. Each is a
222
220
            reference to another key.
223
221
        :param value: The value to associate with the key. It may be any
224
 
            bytes as long as it does not contain \\0 or \\n.
 
222
            bytes as long as it does not contain \0 or \n.
225
223
        """
226
224
        (node_refs,
227
225
         absent_references) = self._check_key_ref_value(key, references, value)
446
444
            # We already did this
447
445
            return
448
446
        if 'index' in debug.debug_flags:
449
 
            trace.mutter('Reading entire index %s',
450
 
                          self._transport.abspath(self._name))
 
447
            mutter('Reading entire index %s', self._transport.abspath(self._name))
451
448
        if stream is None:
452
449
            stream = self._transport.get(self._name)
453
450
            if self._base_offset != 0:
465
462
        trailers = 0
466
463
        pos = stream.tell()
467
464
        lines = stream.read().split('\n')
468
 
        # GZ 2009-09-20: Should really use a try/finally block to ensure close
469
465
        stream.close()
470
466
        del lines[-1]
471
467
        _, _, _, trailers = self._parse_lines(lines, pos)
674
670
        if self._nodes is not None:
675
671
            return self._iter_entries_from_total_buffer(keys)
676
672
        else:
677
 
            return (result[1] for result in bisect_multi.bisect_multi_bytes(
 
673
            return (result[1] for result in bisect_multi_bytes(
678
674
                self._lookup_keys_via_location, self._size, keys))
679
675
 
680
676
    def iter_entries_prefix(self, keys):
1291
1287
    def get_parent_map(self, keys):
1292
1288
        """See graph.StackedParentsProvider.get_parent_map"""
1293
1289
        search_keys = set(keys)
1294
 
        if _mod_revision.NULL_REVISION in search_keys:
1295
 
            search_keys.discard(_mod_revision.NULL_REVISION)
1296
 
            found_parents = {_mod_revision.NULL_REVISION:[]}
 
1290
        if NULL_REVISION in search_keys:
 
1291
            search_keys.discard(NULL_REVISION)
 
1292
            found_parents = {NULL_REVISION:[]}
1297
1293
        else:
1298
1294
            found_parents = {}
1299
1295
        for index, key, value, refs in self.iter_entries(search_keys):
1300
1296
            parents = refs[0]
1301
1297
            if not parents:
1302
 
                parents = (_mod_revision.NULL_REVISION,)
 
1298
                parents = (NULL_REVISION,)
1303
1299
            found_parents[key] = parents
1304
1300
        return found_parents
1305
1301
 
1437
1433
        """
1438
1434
        indices_info = zip(self._index_names, self._indices)
1439
1435
        if 'index' in debug.debug_flags:
1440
 
            trace.mutter('CombinedGraphIndex reordering: currently %r, '
1441
 
                         'promoting %r', indices_info, hit_indices)
 
1436
            mutter('CombinedGraphIndex reordering: currently %r, promoting %r',
 
1437
                   indices_info, hit_indices)
1442
1438
        hit_names = []
1443
1439
        unhit_names = []
1444
1440
        new_hit_indices = []
1461
1457
        self._indices = new_hit_indices + unhit_indices
1462
1458
        self._index_names = hit_names + unhit_names
1463
1459
        if 'index' in debug.debug_flags:
1464
 
            trace.mutter('CombinedGraphIndex reordered: %r', self._indices)
 
1460
            mutter('CombinedGraphIndex reordered: %r', self._indices)
1465
1461
        return hit_names
1466
1462
 
1467
1463
    def _move_to_front_by_name(self, hit_names):