~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2006-11-02 10:20:19 UTC
  • mfrom: (2114 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2119.
  • Revision ID: mbp@sourcefrog.net-20061102102019-9a5a02f485dff6f6
merge bzr.dev and reconcile several changes, also some test fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 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 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
"""VersionedFile based revision store.
 
18
 
 
19
This stores revisions as individual entries in a knit, and signatures in a 
 
20
parallel knit.
 
21
"""
 
22
 
 
23
 
 
24
import bzrlib
 
25
import bzrlib.errors as errors
 
26
from bzrlib.knit import KnitVersionedFile, KnitPlainFactory
 
27
from bzrlib.store.revision import RevisionStore
 
28
from bzrlib.store.versioned import VersionedFileStore
 
29
from bzrlib.transport import get_transport
 
30
 
 
31
 
 
32
class KnitRevisionStoreFactory(object):
 
33
    """Factory to create a KnitRevisionStore for testing."""
 
34
 
 
35
    def create(self, url):
 
36
        """Create a revision store at url."""
 
37
        t = get_transport(url)
 
38
        t.mkdir('revision-store')
 
39
        versioned_file_store = VersionedFileStore(
 
40
            t.clone('revision-store'),
 
41
            precious=True,
 
42
            versionedfile_class=KnitVersionedFile,
 
43
            versionedfile_kwargs={'delta':False, 'factory':KnitPlainFactory()})
 
44
        return KnitRevisionStore(versioned_file_store)
 
45
 
 
46
    def __str__(self):
 
47
        return "KnitRevisionStore"
 
48
 
 
49
 
 
50
class KnitRevisionStore(RevisionStore):
 
51
    """A RevisionStore layering on a VersionedFileStore."""
 
52
 
 
53
    def __init__(self, versioned_file_store):
 
54
        """Create a KnitRevisionStore object.
 
55
 
 
56
        :param versioned_file_store: the text store to use for storing 
 
57
                                     revisions and signatures.
 
58
        """
 
59
        super(KnitRevisionStore, self).__init__()
 
60
        self.versioned_file_store = versioned_file_store
 
61
 
 
62
    def _add_revision(self, revision, revision_as_file, transaction):
 
63
        """Template method helper to store revision in this store."""
 
64
        # FIXME: make this ghost aware at the knit level
 
65
        rf = self.get_revision_file(transaction)
 
66
        self.get_revision_file(transaction).add_lines_with_ghosts(
 
67
            revision.revision_id,
 
68
            revision.parent_ids,
 
69
            bzrlib.osutils.split_lines(revision_as_file.read()))
 
70
 
 
71
    def add_revision_signature_text(self, revision_id, signature_text, transaction):
 
72
        """See RevisionStore.add_revision_signature_text()."""
 
73
        self.get_signature_file(transaction).add_lines(
 
74
            revision_id, [], bzrlib.osutils.split_lines(signature_text))
 
75
 
 
76
    def all_revision_ids(self, transaction):
 
77
        """See RevisionStore.all_revision_ids()."""
 
78
        rev_file = self.get_revision_file(transaction)
 
79
        return rev_file.get_ancestry(rev_file.versions())
 
80
 
 
81
    def get_revisions(self, revision_ids, transaction):
 
82
        """See RevisionStore.get_revisions()."""
 
83
        texts = self._get_serialized_revisions(revision_ids, transaction)
 
84
        revisions = []
 
85
        try:
 
86
            for text, revision_id in zip(texts, revision_ids):
 
87
                r = self._serializer.read_revision_from_string(text)
 
88
                assert r.revision_id == revision_id
 
89
                revisions.append(r)
 
90
        except SyntaxError, e:
 
91
            raise errors.BzrError('failed to unpack revision_xml',
 
92
                                   [revision_id,
 
93
                                   str(e)])
 
94
        return revisions 
 
95
 
 
96
    def _get_serialized_revisions(self, revision_ids, transaction):
 
97
        texts = []
 
98
        vf = self.get_revision_file(transaction)
 
99
        try:
 
100
            return vf.get_texts(revision_ids)
 
101
        except (errors.RevisionNotPresent), e:
 
102
            raise errors.NoSuchRevision(self, e.revision_id)
 
103
 
 
104
    def _get_revision_xml(self, revision_id, transaction):
 
105
        try:
 
106
            return self.get_revision_file(transaction).get_text(revision_id)
 
107
        except (errors.RevisionNotPresent):
 
108
            raise errors.NoSuchRevision(self, revision_id)
 
109
 
 
110
    def get_revision_file(self, transaction):
 
111
        """Get the revision versioned file object."""
 
112
        return self.versioned_file_store.get_weave_or_empty('revisions', transaction)
 
113
 
 
114
    def get_signature_file(self, transaction):
 
115
        """Get the signature text versioned file object."""
 
116
        return self.versioned_file_store.get_weave_or_empty('signatures', transaction)
 
117
 
 
118
    def _get_signature_text(self, revision_id, transaction):
 
119
        """See RevisionStore._get_signature_text()."""
 
120
        try:
 
121
            return self.get_signature_file(transaction).get_text(revision_id)
 
122
        except errors.RevisionNotPresent:
 
123
            raise errors.NoSuchRevision(self, revision_id)
 
124
 
 
125
    def has_revision_id(self, revision_id, transaction):
 
126
        """True if the store contains revision_id."""
 
127
        return (revision_id is None
 
128
                or self.get_revision_file(transaction).has_version(revision_id))
 
129
        
 
130
    def _has_signature(self, revision_id, transaction):
 
131
        """See RevisionStore._has_signature()."""
 
132
        return self.get_signature_file(transaction).has_version(revision_id)
 
133
 
 
134
    def total_size(self, transaction):
 
135
        """ See RevisionStore.total_size()."""
 
136
        return (len(self.all_revision_ids(transaction)),
 
137
            self.versioned_file_store.total_size()[1])