1
# Copyright (C) 2006 by Canonical Ltd
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.
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.
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
16
"""TextStore based revision store.
18
This stores revisions as individual text entries in a TextStore and
19
requires access to a inventory weave to produce object graphs.
23
from cStringIO import StringIO
27
import bzrlib.errors as errors
28
from bzrlib.store.revision import RevisionStore
29
from bzrlib.store.text import TextStore
30
from bzrlib.store.versioned import VersionedFileStore
31
from bzrlib.transport import get_transport
32
from bzrlib.tsort import topo_sort
35
class TextRevisionStoreTestFactory(object):
36
"""Factory to create a TextRevisionStore for testing.
38
This creates a inventory weave and hooks it into the revision store
41
def create(self, url):
42
"""Create a revision store at url."""
43
t = get_transport(url)
45
text_store = TextStore(t.clone('revstore'))
46
return TextRevisionStore(text_store)
49
return "TextRevisionStore"
52
class TextRevisionStore(RevisionStore):
53
"""A RevisionStore layering on a TextStore and Inventory weave store."""
55
def __init__(self, text_store, serializer=None):
56
"""Create a TextRevisionStore object.
58
:param text_store: the text store to put serialised revisions into.
60
super(TextRevisionStore, self).__init__(serializer)
61
self.text_store = text_store
62
self.text_store.register_suffix('sig')
64
def _add_revision(self, revision, revision_as_file, transaction):
65
"""Template method helper to store revision in this store."""
66
self.text_store.add(revision_as_file, revision.revision_id)
68
def add_revision_signature_text(self, revision_id, signature_text, transaction):
69
"""See RevisionStore.add_revision_signature_text()."""
70
self.text_store.add(StringIO(signature_text), revision_id, "sig")
72
def all_revision_ids(self, transaction):
73
"""See RevisionStore.all_revision_ids()."""
74
# for TextRevisionStores, this is only functional
75
# on listable transports.
76
assert self.text_store.listable()
78
for rev_id in self.text_store:
79
rev = self.get_revision(rev_id, transaction)
80
result_graph[rev_id] = rev.parent_ids
82
for rev_id, parents in result_graph.items():
83
for parent in list(parents):
84
if not parent in result_graph:
85
del parents[parents.index(parent)]
86
return topo_sort(result_graph.items())
88
def get_revision(self, revision_id, transaction):
89
"""See RevisionStore.get_revision()."""
90
xml_file = self._get_revision_xml_file(revision_id)
92
r = self._serializer.read_revision(xml_file)
93
except SyntaxError, e:
94
raise errors.BzrError('failed to unpack revision_xml',
98
assert r.revision_id == revision_id
101
def _get_revision_xml_file(self, revision_id):
103
return self.text_store.get(revision_id)
104
except (IndexError, KeyError):
105
raise bzrlib.errors.NoSuchRevision(self, revision_id)
107
def _get_signature_text(self, revision_id, transaction):
108
"""See RevisionStore._get_signature_text()."""
110
return self.text_store.get(revision_id, suffix='sig').read()
111
except (IndexError, KeyError):
112
raise errors.NoSuchRevision(self, revision_id)
114
def has_revision_id(self, revision_id, transaction):
115
"""True if the store contains revision_id."""
116
return (revision_id is None
117
or self.text_store.has_id(revision_id))
119
def _has_signature(self, revision_id, transaction):
120
"""See RevisionStore._has_signature()."""
121
return self.text_store.has_id(revision_id, suffix='sig')
123
def total_size(self, transaction):
124
""" See RevisionStore.total_size()."""
125
return self.text_store.total_size()