3
# Copyright (C) 2005 Canonical Ltd
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
# XXX: Some consideration of the problems that might occur if there are
20
# files whose id differs only in case. That should probably be forbidden.
25
from cStringIO import StringIO
28
from bzrlib.weavefile import read_weave, write_weave_v5
29
from bzrlib.weave import Weave
30
from bzrlib.store import TransportStore, hash_prefix
31
from bzrlib.atomicfile import AtomicFile
32
from bzrlib.errors import NoSuchFile, FileExists
33
from bzrlib.trace import mutter
36
class WeaveStore(TransportStore):
37
"""Collection of several weave files in a directory.
39
This has some shortcuts for reading and writing them.
41
FILE_SUFFIX = '.weave'
43
def __init__(self, transport, prefixed=False, precious=False,
44
dir_mode=None, file_mode=None):
45
super(WeaveStore, self).__init__(transport,
46
dir_mode=dir_mode, file_mode=file_mode,
47
prefixed=prefixed, compressed=False)
48
self._precious = precious
50
def filename(self, file_id):
51
"""Return the path relative to the transport root."""
53
return hash_prefix(file_id) + urllib.quote(file_id) + WeaveStore.FILE_SUFFIX
55
return urllib.quote(file_id) + WeaveStore.FILE_SUFFIX
58
l = len(WeaveStore.FILE_SUFFIX)
59
for relpath in self._iter_files_recursive():
60
if relpath.endswith(WeaveStore.FILE_SUFFIX):
61
yield os.path.basename(relpath[:-l])
63
def has_id(self, fileid):
64
return self._transport.has(self.filename(fileid))
66
def _get(self, file_id):
67
return self._transport.get(self.filename(file_id))
69
def _put(self, file_id, f):
72
self._transport.mkdir(hash_prefix(file_id), mode=self._dir_mode)
75
return self._transport.put(self.filename(file_id), f, mode=self._file_mode)
77
def get_weave(self, file_id, transaction):
78
weave = transaction.map.find_weave(file_id)
80
mutter("cache hit in %s for %s", self, file_id)
82
w = read_weave(self._get(file_id))
83
transaction.map.add_weave(file_id, w)
84
transaction.register_clean(w, precious=self._precious)
87
def get_lines(self, file_id, rev_id, transaction):
88
"""Return text from a particular version of a weave.
90
Returned as a list of lines."""
91
w = self.get_weave(file_id, transaction)
92
return w.get(w.lookup(rev_id))
94
def get_weave_prelude_or_empty(self, file_id, transaction):
95
"""cheap version that reads the prelude but not the lines
97
weave = transaction.map.find_weave(file_id)
99
mutter("cache hit in %s for %s", self, file_id)
102
return read_weave(self._get(file_id),prelude=True)
104
return Weave(weave_name=file_id)
106
def get_weave_or_empty(self, file_id, transaction):
107
"""Return a weave, or an empty one if it doesn't exist."""
109
return self.get_weave(file_id, transaction)
111
weave = Weave(weave_name=file_id)
112
transaction.map.add_weave(file_id, weave)
113
transaction.register_clean(weave, precious=self._precious)
116
def put_weave(self, file_id, weave, transaction):
117
"""Write back a modified weave"""
118
transaction.register_dirty(weave)
119
# TODO FOR WRITE TRANSACTIONS: this should be done in a callback
120
# from the transaction, when it decides to save.
122
write_weave_v5(weave, sio)
124
self._put(file_id, sio)
126
def add_text(self, file_id, rev_id, new_lines, parents, transaction):
127
w = self.get_weave_or_empty(file_id, transaction)
128
parent_idxs = map(w.lookup, parents)
129
w.add(rev_id, parent_idxs, new_lines)
130
self.put_weave(file_id, w, transaction)
132
def add_identical_text(self, file_id, old_rev_id, new_rev_id, parents,
134
w = self.get_weave_or_empty(file_id, transaction)
135
parent_idxs = map(w.lookup, parents)
136
w.add_identical(old_rev_id, new_rev_id, parent_idxs)
137
self.put_weave(file_id, w, transaction)
139
def copy_multi(self, from_store, file_ids):
140
assert isinstance(from_store, WeaveStore)
142
mutter("copy weave {%s} into %s", f, self)
143
self._put(f, from_store._get(f))