~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Robert Collins
  • Date: 2007-09-05 05:51:34 UTC
  • mto: (2592.3.126 repository)
  • mto: This revision was merged to the branch mainline in revision 2879.
  • Revision ID: robertc@robertcollins.net-20070905055134-pwbueao0qq6krf9u
nuke _read_tree_state and snapshot from inventory, moving responsibility into the commit builder.

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