27
27
from bzrlib.weavefile import read_weave, write_weave_v5
28
28
from bzrlib.weave import Weave
29
from bzrlib.store import Store
29
from bzrlib.store import TransportStore, hash_prefix
30
30
from bzrlib.atomicfile import AtomicFile
31
from bzrlib.errors import NoSuchFile
31
from bzrlib.errors import NoSuchFile, FileExists
32
32
from bzrlib.trace import mutter
35
class WeaveStore(Store):
35
class WeaveStore(TransportStore):
36
36
"""Collection of several weave files in a directory.
38
38
This has some shortcuts for reading and writing them.
40
40
FILE_SUFFIX = '.weave'
42
def __init__(self, transport):
42
def __init__(self, transport, prefixed=False, precious=False):
43
43
self._transport = transport
44
self._prefixed = prefixed
45
self._precious = precious
45
47
def filename(self, file_id):
46
48
"""Return the path relative to the transport root."""
47
return file_id + WeaveStore.FILE_SUFFIX
50
return hash_prefix(file_id) + file_id + WeaveStore.FILE_SUFFIX
52
return file_id + WeaveStore.FILE_SUFFIX
49
54
def __iter__(self):
50
55
l = len(WeaveStore.FILE_SUFFIX)
51
for f in self._transport.list_dir('.'):
52
if f.endswith(WeaveStore.FILE_SUFFIX):
56
for relpath, st in self._iter_relpaths():
57
if relpath.endswith(WeaveStore.FILE_SUFFIX):
58
yield os.path.basename(relpath[:-l])
56
60
def __contains__(self, fileid):
61
65
return self._transport.get(self.filename(file_id))
63
67
def _put(self, file_id, f):
70
self._transport.mkdir(hash_prefix(file_id))
64
73
return self._transport.put(self.filename(file_id), f)
66
75
def get_weave(self, file_id, transaction):
71
80
w = read_weave(self._get(file_id))
72
81
transaction.map.add_weave(file_id, w)
73
transaction.register_clean(w)
82
transaction.register_clean(w, precious=self._precious)
77
85
def get_lines(self, file_id, rev_id, transaction):
78
86
"""Return text from a particular version of a weave.
89
97
weave = Weave(weave_name=file_id)
90
98
transaction.map.add_weave(file_id, weave)
91
transaction.register_clean(weave)
99
transaction.register_clean(weave, precious=self._precious)
94
102
def put_weave(self, file_id, weave, transaction):
100
108
write_weave_v5(weave, sio)
103
110
self._put(file_id, sio)
106
112
def add_text(self, file_id, rev_id, new_lines, parents, transaction):
107
113
w = self.get_weave_or_empty(file_id, transaction)
108
114
parent_idxs = map(w.lookup, parents)