44
34
class StoreError(Exception):
48
class ImmutableStore(object):
49
"""Store that holds files indexed by unique names.
51
Files can be added, but not modified once they are in. Typically
52
the hash is used as the name, or something else known to be unique,
55
>>> st = ImmutableScratchStore()
57
>>> st.add(StringIO('hello'), 'aa')
63
You are not allowed to add an id that is already present.
65
Entries can be retrieved as files, which may then be read.
67
>>> st.add(StringIO('goodbye'), '123123')
68
>>> st['123123'].read()
71
TODO: Atomic add by writing to a temporary file and renaming.
73
In bzr 0.0.5 and earlier, files within the store were marked
74
readonly on disk. This is no longer done but existing stores need
38
"""This class represents the abstract storage layout for saving information.
78
def __init__(self, basedir):
79
self._basedir = basedir
81
def _path(self, entry_id):
82
if not isinstance(entry_id, basestring):
83
raise TypeError(type(entry_id))
84
if '\\' in entry_id or '/' in entry_id:
85
raise ValueError("invalid store id %r" % entry_id)
86
return os.path.join(self._basedir, entry_id)
41
_max_buffered_requests = 10
43
def __init__(self, transport):
44
assert isinstance(transport, bzrlib.transport.Transport)
45
self._transport = transport
88
47
def __repr__(self):
89
return "%s(%r)" % (self.__class__.__name__, self._basedir)
91
def add(self, f, fileid, compressed=True):
92
"""Add contents of a file into the store.
94
f -- An open file, or file-like object."""
95
# FIXME: Only works on files that will fit in memory
97
from bzrlib.atomicfile import AtomicFile
99
mutter("add store entry %r" % (fileid))
100
if isinstance(f, types.StringTypes):
48
if self._transport is None:
49
return "%s(None)" % (self.__class__.__name__)
105
p = self._path(fileid)
106
if os.access(p, os.F_OK) or os.access(p + '.gz', os.F_OK):
107
raise BzrError("store %r already contains id %r" % (self._basedir, fileid))
113
af = AtomicFile(fn, 'wb')
116
gf = gzip.GzipFile(mode='wb', fileobj=af)
51
return "%s(%r)" % (self.__class__.__name__, self._transport.base)
56
raise NotImplementedError('Children should define their length')
58
def __getitem__(self, fileid):
59
"""Returns a file reading from a particular entry."""
60
raise NotImplementedError
62
def __contains__(self, fileid):
64
raise NotImplementedError
67
raise NotImplementedError
70
"""Delete an entry from the store."""
71
raise NotImplementedError('Children need to define delete rights.')
73
def add(self, f, fileid):
74
"""Add a file object f to the store accessible from the given fileid"""
75
raise NotImplementedError('Children of Store must define their method of adding entries.')
77
def add_multi(self, entries):
78
"""Add a series of file-like or string objects to the store with the given
81
:param entries: A list of tuples of file,id pairs [(file1, id1), (file2, id2), ...]
82
This could also be a generator yielding (file,id) pairs.
84
for f, fileid in entries:
87
def has(self, fileids):
88
"""Return True/False for each entry in fileids.
90
:param fileids: A List or generator yielding file ids.
91
:return: A generator or list returning True/False for each entry.
93
for fileid in fileids:
126
def copy_multi(self, other, ids, permit_failure=False):
99
def get(self, fileids, permit_failure=False, pb=None):
100
"""Return a set of files, one for each requested entry.
102
:param permit_failure: If true, return None for entries which do not
104
:return: A list or generator of file-like objects, one for each id.
106
for fileid in fileids:
115
def copy_multi(self, other, ids, pb=None, permit_failure=False):
127
116
"""Copy texts for ids from other into self.
129
If an id is present in self, it is skipped.
118
If an id is present in self, it is skipped. A count of copied
119
ids is returned, which may be less than len(ids).
131
Returns (count_copied, failed), where failed is a collection of ids
132
that could not be copied.
121
:param other: Another Store object
122
:param ids: A list of entry ids to be copied
123
:param pb: A ProgressBar object, if none is given, the default will be created.
124
:param permit_failure: Allow missing entries to be ignored
125
:return: (n_copied, [failed]) The number of entries copied successfully,
126
followed by a list of entries which could not be copied (because they
134
pb = bzrlib.ui.ui_factory.progress_bar()
130
pb = bzrlib.ui.ui_factory.progress_bar()
132
ids = list(ids) # Make sure we don't have a generator, since we iterate 2 times
136
133
pb.update('preparing to copy')
137
to_copy = [id for id in ids if id not in self]
138
if isinstance(other, ImmutableStore):
139
return self.copy_multi_immutable(other, to_copy, pb,
140
permit_failure=permit_failure)
135
for file_id, has in zip(ids, self.has(ids)):
137
to_copy.append(file_id)
138
return self._do_copy(other, to_copy, pb, permit_failure=permit_failure)
140
def _do_copy(self, other, to_copy, pb, permit_failure=False):
141
"""This is the standard copying mechanism, just get them one at
142
a time from remote, and store them locally.
144
:param other: Another Store object
145
:param to_copy: A list of entry ids to copy
146
:param pb: A ProgressBar object to display completion status.
147
:param permit_failure: Allow missing entries to be ignored
148
:return: (n_copied, [failed])
149
The number of entries copied, and a list of failed entries.
151
# This should be updated to use add_multi() rather than
152
# the current methods of buffering requests.
153
# One question, is it faster to queue up 1-10 and then copy 1-10
154
# then queue up 11-20, copy 11-20
155
# or to queue up 1-10, copy 1, queue 11, copy 2, etc?
156
# sort of pipeline versus batch.
158
# We can't use self._transport.copy_to because we don't know
159
# whether the local tree is in the same format as other
145
pb.update('copy', count, len(to_copy))
146
if not permit_failure:
147
self.add(other[id], id)
161
def buffer_requests():
163
buffered_requests = []
164
for fileid in to_copy:
156
if not permit_failure:
174
buffered_requests.append((f, fileid))
175
if len(buffered_requests) > self._max_buffered_requests:
176
yield buffered_requests.pop(0)
178
pb.update('copy', count, len(to_copy))
180
for req in buffered_requests:
183
pb.update('copy', count, len(to_copy))
157
185
assert count == len(to_copy)
161
def copy_multi_immutable(self, other, to_copy, pb, permit_failure=False):
166
other_p = other._path(id)
168
osutils.link_or_copy(other_p, p)
169
except (IOError, OSError), e:
170
if e.errno == errno.ENOENT:
171
if not permit_failure:
172
osutils.link_or_copy(other_p+".gz", p+".gz")
175
osutils.link_or_copy(other_p+".gz", p+".gz")
177
if e.errno == errno.ENOENT:
185
pb.update('copy', count, len(to_copy))
186
assert count == len(to_copy)
191
def __contains__(self, fileid):
193
p = self._path(fileid)
194
return (os.access(p, os.R_OK)
195
or os.access(p + '.gz', os.R_OK))
197
# TODO: Guard against the same thing being stored twice,
198
# compressed and uncompressed
201
for f in os.listdir(self._basedir):
203
# TODO: case-insensitive?
209
return len(os.listdir(self._basedir))
212
def __getitem__(self, fileid):
213
"""Returns a file reading from a particular entry."""
214
p = self._path(fileid)
216
return gzip.GzipFile(p + '.gz', 'rb')
218
if e.errno != errno.ENOENT:
224
if e.errno != errno.ENOENT:
227
raise KeyError(fileid)
230
def total_size(self):
231
"""Return (count, bytes)
233
This is the (compressed) size stored on disk, not the size of
241
total += os.stat(p)[ST_SIZE]
243
total += os.stat(p + '.gz')[ST_SIZE]
250
class ImmutableScratchStore(ImmutableStore):
251
"""Self-destructing test subclass of ImmutableStore.
253
The Store only exists for the lifetime of the Python object.
254
Obviously you should not put anything precious in it.
257
ImmutableStore.__init__(self, tempfile.mkdtemp())
260
for f in os.listdir(self._basedir):
261
fpath = os.path.join(self._basedir, f)
262
# needed on windows, and maybe some other filesystems
263
os.chmod(fpath, 0600)
265
os.rmdir(self._basedir)
266
mutter("%r destroyed" % self)
187
self.add_multi(buffer_requests())
190
return len(to_copy), failed
268
192
def copy_all(store_from, store_to):
269
193
"""Copy all ids from one store to another."""
270
194
if not hasattr(store_from, "__iter__"):
271
195
raise UnlistableStore(store_from)
272
ids = [f for f in store_from]
197
ids = [f for f in store_from]
198
except NotImplementedError:
199
raise UnlistableStore(store_from)
273
200
store_to.copy_multi(store_from, ids)