~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-06-20 07:55:43 UTC
  • mfrom: (1798 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1799.
  • Revision ID: mbp@sourcefrog.net-20060620075543-b10f6575d4a4fa32
[merge] bzr.dev

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