~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store/revision/knit.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-03-08 00:37:41 UTC
  • mfrom: (1594.2.4 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060308003741-08afccbf89005e87
Merge in :
 * Knit repositories use knits
 * Nested progress bar support.
 * Ghost aware graph api.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 by Canonical Ltd
 
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 version 2 as published by
 
5
# the Free Software Foundation.
 
6
#
 
7
# This program is distributed in the hope that it will be useful,
 
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
# GNU General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License
 
13
# along with this program; if not, write to the Free Software
 
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
 
 
16
"""VersionedFile based revision store.
 
17
 
 
18
This stores revisions as individual entries in a knit, and signatures in a 
 
19
parallel knit.
 
20
"""
 
21
 
 
22
 
 
23
import bzrlib
 
24
import bzrlib.errors as errors
 
25
from bzrlib.knit import KnitVersionedFile
 
26
from bzrlib.store.revision import RevisionStore
 
27
from bzrlib.store.versioned import VersionedFileStore
 
28
from bzrlib.transport import get_transport
 
29
 
 
30
 
 
31
class KnitRevisionStoreFactory(object):
 
32
    """Factory to create a KnitRevisionStore for testing."""
 
33
 
 
34
    def create(self, url):
 
35
        """Create a revision store at url."""
 
36
        t = get_transport(url)
 
37
        t.mkdir('revision-store')
 
38
        versioned_file_store = VersionedFileStore(
 
39
            t.clone('revision-store'),
 
40
            precious=True,
 
41
            versionedfile_class=KnitVersionedFile)
 
42
        return KnitRevisionStore(versioned_file_store)
 
43
 
 
44
    def __str__(self):
 
45
        return "KnitRevisionStore"
 
46
 
 
47
 
 
48
class KnitRevisionStore(RevisionStore):
 
49
    """A RevisionStore layering on a VersionedFileStore."""
 
50
 
 
51
    def __init__(self, versioned_file_store):
 
52
        """Create a KnitRevisionStore object.
 
53
 
 
54
        :param versioned_file_store: the text store to use for storing 
 
55
                                     revisions and signatures.
 
56
        """
 
57
        super(KnitRevisionStore, self).__init__()
 
58
        self.versioned_file_store = versioned_file_store
 
59
 
 
60
    def _add_revision(self, revision, revision_as_file, transaction):
 
61
        """Template method helper to store revision in this store."""
 
62
        # FIXME: make this ghost aware at the knit level
 
63
        rf = self.get_revision_file(transaction)
 
64
        parents = []
 
65
        for parent_id in revision.parent_ids:
 
66
            if rf.has_version(parent_id):
 
67
                parents.append(parent_id)
 
68
        self.get_revision_file(transaction).add_lines(
 
69
            revision.revision_id,
 
70
            parents,
 
71
            revision_as_file.readlines())
 
72
 
 
73
    def add_revision_signature_text(self, revision_id, signature_text, transaction):
 
74
        """See RevisionStore.add_revision_signature_text()."""
 
75
        self.get_signature_file(transaction).add_lines(
 
76
            revision_id, [], bzrlib.osutils.split_lines(signature_text))
 
77
 
 
78
    def all_revision_ids(self, transaction):
 
79
        """See RevisionStore.all_revision_ids()."""
 
80
        rev_file = self.get_revision_file(transaction)
 
81
        return rev_file.get_ancestry(rev_file.versions())
 
82
 
 
83
    def get_revision(self, revision_id, transaction):
 
84
        """See RevisionStore.get_revision()."""
 
85
        xml = self._get_revision_xml(revision_id, transaction)
 
86
        try:
 
87
            r = self._serializer.read_revision_from_string(xml)
 
88
        except SyntaxError, e:
 
89
            raise errors.BzrError('failed to unpack revision_xml',
 
90
                                   [revision_id,
 
91
                                   str(e)])
 
92
        assert r.revision_id == revision_id
 
93
        return r
 
94
 
 
95
    def _get_revision_xml(self, revision_id, transaction):
 
96
        try:
 
97
            return self.get_revision_file(transaction).get_text(revision_id)
 
98
        except (errors.RevisionNotPresent):
 
99
            raise errors.NoSuchRevision(self, revision_id)
 
100
 
 
101
    def get_revision_file(self, transaction):
 
102
        """Get the revision versioned file object."""
 
103
        return self.versioned_file_store.get_weave_or_empty('revisions', transaction)
 
104
 
 
105
    def get_signature_file(self, transaction):
 
106
        """Get the signature text versioned file object."""
 
107
        return self.versioned_file_store.get_weave_or_empty('signatures', transaction)
 
108
 
 
109
    def _get_signature_text(self, revision_id, transaction):
 
110
        """See RevisionStore._get_signature_text()."""
 
111
        try:
 
112
            return self.get_signature_file(transaction).get_text(revision_id)
 
113
        except errors.RevisionNotPresent:
 
114
            raise errors.NoSuchRevision(self, revision_id)
 
115
 
 
116
    def has_revision_id(self, revision_id, transaction):
 
117
        """True if the store contains revision_id."""
 
118
        return (revision_id is None
 
119
                or self.get_revision_file(transaction).has_version(revision_id))
 
120
        
 
121
    def _has_signature(self, revision_id, transaction):
 
122
        """See RevisionStore._has_signature()."""
 
123
        return self.get_signature_file(transaction).has_version(revision_id)
 
124
 
 
125
    def total_size(self, transaction):
 
126
        """ See RevisionStore.total_size()."""
 
127
        return self.versioned_file_store.total_size()