16
16
# along with this program; if not, write to the Free Software
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
# Author: Martin Pool <mbp@canonical.com>
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.
23
from cStringIO import StringIO
25
27
from bzrlib.weavefile import read_weave, write_weave_v5
26
28
from bzrlib.weave import Weave
29
from bzrlib.store import Store
27
30
from bzrlib.atomicfile import AtomicFile
32
class WeaveStore(object):
31
from bzrlib.errors import NoSuchFile
32
from bzrlib.trace import mutter
37
class WeaveStore(Store):
33
38
"""Collection of several weave files in a directory.
35
40
This has some shortcuts for reading and writing them.
37
def __init__(self, dir, get_file=None):
42
FILE_SUFFIX = '.weave'
44
def __init__(self, transport):
45
self._transport = transport
40
self.enable_cache = False
41
if get_file is not None:
42
self.get_file = get_file
47
self.enable_cache = False
44
def get_file(self, filename):
45
return file(filename, 'rb')
47
50
def filename(self, file_id):
48
return self._dir + os.sep + file_id + '.weave'
51
"""Return the path relative to the transport root."""
52
return file_id + WeaveStore.FILE_SUFFIX
55
l = len(WeaveStore.FILE_SUFFIX)
56
for f in self._transport.list_dir('.'):
57
if f.endswith(WeaveStore.FILE_SUFFIX):
61
def __contains__(self, fileid):
63
return self._transport.has(self.filename(fileid))
65
def _get(self, file_id):
66
return self._transport.get(self.filename(file_id))
68
def _put(self, file_id, f):
69
return self._transport.put(self.filename(file_id), f)
51
72
def get_weave(self, file_id):
52
73
if self.enable_cache:
53
74
if file_id in self._cache:
54
75
return self._cache[file_id]
55
w = read_weave(self.get_file(self.filename(file_id)))
76
w = read_weave(self._get(file_id))
56
77
if self.enable_cache:
57
78
self._cache[file_id] = w
69
90
def get_weave_or_empty(self, file_id):
70
91
"""Return a weave, or an empty one if it doesn't exist."""
72
inf = self.get_file(self.filename(file_id))
74
if e.errno == errno.ENOENT:
75
return Weave(weave_name=file_id)
93
inf = self._get(file_id)
95
return Weave(weave_name=file_id)
79
97
return read_weave(inf)
82
def put_empty_weave(self, file_id):
83
self.put_weave(file_id, Weave())
86
100
def put_weave(self, file_id, weave):
87
101
"""Write back a modified weave"""
88
102
if self.enable_cache:
89
103
self._cache[file_id] = weave
90
weave_fn = self.filename(file_id)
91
af = AtomicFile(weave_fn)
93
write_weave_v5(weave, af)
106
write_weave_v5(weave, sio)
109
self._put(file_id, sio)
99
112
def add_text(self, file_id, rev_id, new_lines, parents):
103
116
self.put_weave(file_id, w)
119
def copy_multi(self, from_store, file_ids):
120
assert isinstance(from_store, WeaveStore)
122
mutter("copy weave {%s} into %s", f, self)
123
self._put(f, from_store._get(f))