25
25
from bzrlib.weavefile import read_weave, write_weave_v5
26
26
from bzrlib.weave import Weave
27
from bzrlib.store import Store
27
28
from bzrlib.atomicfile import AtomicFile
32
class WeaveStore(object):
29
from bzrlib.errors import NoSuchFile
31
from cStringIO import StringIO
35
class WeaveStore(Store):
33
36
"""Collection of several weave files in a directory.
35
38
This has some shortcuts for reading and writing them.
37
def __init__(self, dir, get_file=None):
40
def __init__(self, transport):
41
self._transport = transport
40
self.enable_cache = False
41
if get_file is not None:
42
self.get_file = get_file
43
self.enable_cache = False
44
def get_file(self, filename):
45
return file(filename, 'rb')
47
46
def filename(self, file_id):
48
return self._dir + os.sep + file_id + '.weave'
47
"""Return the path relative to the transport root."""
48
return file_id + '.weave'
50
def _get(self, file_id):
51
return self._transport.get(self.filename(file_id))
53
def _put(self, file_id, f):
54
return self._transport.put(self.filename(file_id), f)
51
57
def get_weave(self, file_id):
52
58
if self.enable_cache:
53
59
if file_id in self._cache:
54
60
return self._cache[file_id]
55
w = read_weave(self.get_file(self.filename(file_id)))
61
w = read_weave(self._get(file_id))
56
62
if self.enable_cache:
57
63
self._cache[file_id] = w
69
75
def get_weave_or_empty(self, file_id):
70
76
"""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)
78
inf = self._get(file_id)
80
return Weave(weave_name=file_id)
79
82
return read_weave(inf)
82
def put_empty_weave(self, file_id):
83
self.put_weave(file_id, Weave())
86
85
def put_weave(self, file_id, weave):
87
86
"""Write back a modified weave"""
88
87
if self.enable_cache:
89
88
self._cache[file_id] = weave
90
weave_fn = self.filename(file_id)
91
af = AtomicFile(weave_fn)
93
write_weave_v5(weave, af)
91
write_weave_v5(weave, sio)
94
self._put(file_id, sio)
99
97
def add_text(self, file_id, rev_id, new_lines, parents):