/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/reconcile.py

  • Committer: Robert Collins
  • Date: 2006-02-24 23:13:20 UTC
  • mto: (1587.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 1588.
  • Revision ID: robertc@robertcollins.net-20060224231320-dbaf879d3070bfd7
Replace the slow topo_sort routine with a much faster one for non trivial datasets.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# (C) 2005, 2006 Canonical Limited.
 
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
"""Reconcilers are able to fix some potential data errors in a branch."""
 
18
 
 
19
 
 
20
import bzrlib.branch
 
21
import bzrlib.errors as errors
 
22
import bzrlib.progress
 
23
from bzrlib.trace import mutter
 
24
from bzrlib.tsort import TopoSorter
 
25
import bzrlib.ui as ui
 
26
 
 
27
 
 
28
def reconcile(dir):
 
29
    """Reconcile the data in dir.
 
30
 
 
31
    Currently this is limited to a inventory 'reweave'.
 
32
 
 
33
    This is a convenience method, and the public api, for using a 
 
34
    Reconciler object.
 
35
    """
 
36
    reconciler = Reconciler(dir)
 
37
    reconciler.reconcile()
 
38
 
 
39
 
 
40
class Reconciler(object):
 
41
    """Reconcilers are used to reconcile existing data.
 
42
 
 
43
    Currently this is limited to a single repository, and consists
 
44
    of an inventory reweave with revision cross-checks.
 
45
    """
 
46
 
 
47
    def __init__(self, dir):
 
48
        self.bzrdir = dir
 
49
 
 
50
    def reconcile(self):
 
51
        """Actually perform the reconciliation."""
 
52
        self.pb = ui.ui_factory.progress_bar()
 
53
        self.repo = self.bzrdir.open_repository()
 
54
        self.repo.lock_write()
 
55
        try:
 
56
            self.pb.note('Reconciling repository %s',
 
57
                         self.repo.bzrdir.root_transport.base)
 
58
            self._reweave_inventory()
 
59
        finally:
 
60
            self.repo.unlock()
 
61
        self.pb.note('Reconciliation complete.')
 
62
 
 
63
    def _reweave_inventory(self):
 
64
        """Regenerate the inventory weave for the repository from scratch."""
 
65
        self.pb.update('Reading inventory data.')
 
66
        self.inventory = self.repo.get_inventory_weave()
 
67
        self.repo.control_weaves.put_weave('inventory.backup',
 
68
                                           self.inventory,
 
69
                                           self.repo.get_transaction())
 
70
        self.pb.note('Backup Inventory created.')
 
71
        # asking for '' should never return a non-empty weave
 
72
        new_inventory = self.repo.control_weaves.get_weave_or_empty('',
 
73
            self.repo.get_transaction())
 
74
 
 
75
        # the total set of revisions to process
 
76
        self.pending = set([file_id for file_id in self.repo.revision_store])
 
77
 
 
78
        # total steps = 1 read per revision + one insert into the inventory
 
79
        self.total = len(self.pending) * 2
 
80
        self.count = 0
 
81
 
 
82
        # mapping from revision_id to parents
 
83
        self._rev_graph = {}
 
84
        # we need the revision id of each revision and its available parents list
 
85
        for rev_id in self.pending:
 
86
            # put a revision into the graph.
 
87
            self._graph_revision(rev_id)
 
88
 
 
89
        # we have topological order of revisions and non ghost parents ready.
 
90
        for rev_id in TopoSorter(self._rev_graph.items()).iter_topo_order():
 
91
            parents = self._rev_graph[rev_id]
 
92
            # double check this really is in topological order.
 
93
            unavailable = [p for p in parents if p not in new_inventory]
 
94
            assert len(unavailable) == 0
 
95
            # this entry has all the non ghost parents in the inventory
 
96
            # file already.
 
97
            self._reweave_step('adding inventories')
 
98
            new_inventory.add(rev_id, parents, self.inventory.get(rev_id))
 
99
 
 
100
        # if this worked, the set of new_inventory.names should equal
 
101
        # self.pending
 
102
        assert set(new_inventory.names()) == self.pending
 
103
        self.pb.update('Writing weave')
 
104
        self.repo.control_weaves.put_weave('inventory',
 
105
                                           new_inventory,
 
106
                                           self.repo.get_transaction())
 
107
        self.inventory = None
 
108
        self.pb.note('Inventory regenerated.')
 
109
 
 
110
    def _graph_revision(self, rev_id):
 
111
        """Load a revision into the revision graph."""
 
112
        # pick a random revision
 
113
        # analyse revision id rev_id and put it in the stack.
 
114
        self._reweave_step('loading revisions')
 
115
        rev = self.repo.get_revision(rev_id)
 
116
        assert rev.revision_id == rev_id
 
117
        parents = []
 
118
        for parent in rev.parent_ids:
 
119
            if parent in self.inventory:
 
120
                parents.append(parent)
 
121
            else:
 
122
                mutter('found ghost %s', parent)
 
123
        self._rev_graph[rev_id] = parents   
 
124
 
 
125
    def _reweave_step(self, message):
 
126
        """Mark a single step of regeneration complete."""
 
127
        self.pb.update(message, self.count, self.total)
 
128
        self.count += 1