/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/store/revision/text.py

  • Committer: Robert Collins
  • Date: 2007-03-01 05:02:47 UTC
  • mto: (2255.11.3 dirstate)
  • mto: This revision was merged to the branch mainline in revision 2322.
  • Revision ID: robertc@robertcollins.net-20070301050247-ufh99m2ze45d7mol
Whitespace fix.

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
"""TextStore based revision store.
 
18
 
 
19
This stores revisions as individual text entries in a TextStore and 
 
20
requires access to a inventory weave to produce object graphs.
 
21
"""
 
22
 
 
23
 
 
24
from cStringIO import StringIO
 
25
 
 
26
 
 
27
from bzrlib import (
 
28
    cache_utf8,
 
29
    errors,
 
30
    osutils,
 
31
    )
 
32
from bzrlib.store.revision import RevisionStore
 
33
from bzrlib.store.text import TextStore
 
34
from bzrlib.store.versioned import VersionedFileStore
 
35
from bzrlib.transport import get_transport
 
36
from bzrlib.tsort import topo_sort
 
37
 
 
38
 
 
39
class TextRevisionStoreTestFactory(object):
 
40
    """Factory to create a TextRevisionStore for testing.
 
41
 
 
42
    This creates a inventory weave and hooks it into the revision store
 
43
    """
 
44
 
 
45
    def create(self, url):
 
46
        """Create a revision store at url."""
 
47
        t = get_transport(url)
 
48
        t.mkdir('revstore')
 
49
        text_store = TextStore(t.clone('revstore'))
 
50
        return TextRevisionStore(text_store)
 
51
 
 
52
    def __str__(self):
 
53
        return "TextRevisionStore"
 
54
 
 
55
 
 
56
class TextRevisionStore(RevisionStore):
 
57
    """A RevisionStore layering on a TextStore and Inventory weave store."""
 
58
 
 
59
    def __init__(self, text_store, serializer=None):
 
60
        """Create a TextRevisionStore object.
 
61
 
 
62
        :param text_store: the text store to put serialised revisions into.
 
63
        """
 
64
        super(TextRevisionStore, self).__init__(serializer)
 
65
        self.text_store = text_store
 
66
        self.text_store.register_suffix('sig')
 
67
 
 
68
    def _add_revision(self, revision, revision_as_file, transaction):
 
69
        """Template method helper to store revision in this store."""
 
70
        # TODO: jam 20070209 text_store.add() still requires a unicode file id
 
71
        unicode_revision_id = cache_utf8.decode(revision.revision_id)
 
72
        self.text_store.add(revision_as_file, unicode_revision_id)
 
73
 
 
74
    def add_revision_signature_text(self, revision_id, signature_text, transaction):
 
75
        """See RevisionStore.add_revision_signature_text()."""
 
76
        # TODO: jam 20070209 text_store.add() still requires a unicode file id
 
77
        revision_id_unicode = cache_utf8.decode(revision_id)
 
78
        self.text_store.add(StringIO(signature_text), revision_id_unicode,
 
79
                            "sig")
 
80
 
 
81
    def all_revision_ids(self, transaction):
 
82
        """See RevisionStore.all_revision_ids()."""
 
83
        # for TextRevisionStores, this is only functional
 
84
        # on listable transports.
 
85
        assert self.text_store.listable()
 
86
        result_graph = {}
 
87
        for rev_id in self.text_store:
 
88
            rev_id = osutils.safe_revision_id(rev_id)
 
89
            rev = self.get_revision(rev_id, transaction)
 
90
            result_graph[rev_id] = rev.parent_ids
 
91
        # remove ghosts
 
92
        for rev_id, parents in result_graph.items():
 
93
            for parent in list(parents):
 
94
                if not parent in result_graph:
 
95
                    del parents[parents.index(parent)]
 
96
        return topo_sort(result_graph.items())
 
97
 
 
98
    def get_revisions(self, revision_ids, transaction):
 
99
        """See RevisionStore.get_revisions()."""
 
100
        revisions = []
 
101
        for revision_id in revision_ids:
 
102
            xml_file = self._get_revision_xml_file(revision_id)
 
103
            try:
 
104
                r = self._serializer.read_revision(xml_file)
 
105
            except SyntaxError, e:
 
106
                raise errors.BzrError('failed to unpack revision_xml',
 
107
                                   [revision_id,
 
108
                                   str(e)])
 
109
            xml_file.close()
 
110
            assert r.revision_id == revision_id
 
111
            revisions.append(r)
 
112
        return revisions
 
113
 
 
114
    def _get_revision_xml_file(self, revision_id):
 
115
        try:
 
116
            return self.text_store.get(revision_id)
 
117
        except (IndexError, KeyError):
 
118
            raise errors.NoSuchRevision(self, revision_id)
 
119
 
 
120
    def _get_signature_text(self, revision_id, transaction):
 
121
        """See RevisionStore._get_signature_text()."""
 
122
        try:
 
123
            return self.text_store.get(revision_id, suffix='sig').read()
 
124
        except (IndexError, KeyError):
 
125
            raise errors.NoSuchRevision(self, revision_id)
 
126
 
 
127
    def has_revision_id(self, revision_id, transaction):
 
128
        """True if the store contains revision_id."""
 
129
        return (revision_id is None
 
130
                or self.text_store.has_id(revision_id))
 
131
 
 
132
    def _has_signature(self, revision_id, transaction):
 
133
        """See RevisionStore._has_signature()."""
 
134
        return self.text_store.has_id(revision_id, suffix='sig')
 
135
 
 
136
    def total_size(self, transaction):
 
137
        """ See RevisionStore.total_size()."""
 
138
        return self.text_store.total_size()