1
# Copyright (C) 2005 by Canonical Development Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
An implementation the primary storage type CompressedTextStore.
20
This store keeps compressed versions of the full text. It does not
21
do any sort of delta compression.
24
import os, tempfile, gzip
27
from bzrlib.store import hash_prefix
28
from bzrlib.trace import mutter
29
from bzrlib.errors import BzrError, FileExists
31
from StringIO import StringIO
32
from stat import ST_SIZE
34
class CompressedTextStore(bzrlib.store.TransportStore):
35
"""Store that holds files indexed by unique names.
37
Files can be added, but not modified once they are in. Typically
38
the hash is used as the name, or something else known to be unique,
41
Files are stored gzip compressed, with no delta compression.
43
>>> st = ScratchCompressedTextStore()
45
>>> st.add(StringIO('hello'), 'aa')
51
You are not allowed to add an id that is already present.
53
Entries can be retrieved as files, which may then be read.
55
>>> st.add(StringIO('goodbye'), '123123')
56
>>> st['123123'].read()
60
def __init__(self, transport, prefixed=False):
61
super(CompressedTextStore, self).__init__(transport)
62
self._prefixed = prefixed
64
def _check_fileid(self, fileid):
65
if '\\' in fileid or '/' in fileid:
66
raise ValueError("invalid store id %r" % fileid)
68
def _relpath(self, fileid):
69
self._check_fileid(fileid)
71
return hash_prefix(fileid) + fileid + ".gz"
75
def add(self, f, fileid):
76
"""Add contents of a file into the store.
78
f -- An open file, or file-like object."""
79
# TODO: implement an add_multi which can do some of it's
80
# own piplelining, and possible take advantage of
81
# transport.put_multi(). The problem is that
82
# entries potentially need to be compressed as they
83
# are received, which implies translation, which
84
# means it isn't as straightforward as we would like.
85
from cStringIO import StringIO
86
from bzrlib.osutils import pumpfile
88
mutter("add store entry %r" % (fileid))
89
if isinstance(f, basestring):
92
fn = self._relpath(fileid)
93
if self._transport.has(fn):
94
raise BzrError("store %r already contains id %r" % (self._transport.base, fileid))
98
self._transport.mkdir(hash_prefix(fileid))
103
gf = gzip.GzipFile(mode='wb', fileobj=sio)
104
# if pumpfile handles files that don't fit in ram,
105
# so will this function
106
if isinstance(f, basestring):
112
self._transport.put(fn, sio)
114
def _do_copy(self, other, to_copy, pb, permit_failure=False):
115
if isinstance(other, CompressedTextStore):
116
return self._copy_multi_text(other, to_copy, pb,
117
permit_failure=permit_failure)
118
return super(CompressedTextStore, self)._do_copy(other, to_copy,
119
pb, permit_failure=permit_failure)
121
def _copy_multi_text(self, other, to_copy, pb,
122
permit_failure=False):
123
# Because of _transport, we can no longer assume
124
# that they are on the same filesystem, we can, however
125
# assume that we only need to copy the exact bytes,
126
# we don't need to process the files.
131
for fileid, has in zip(to_copy, other.has(to_copy)):
133
new_to_copy.add(fileid)
136
to_copy = new_to_copy
137
#mutter('_copy_multi_text copying %s, failed %s' % (to_copy, failed))
139
paths = [self._relpath(fileid) for fileid in to_copy]
140
count = other._transport.copy_to(paths, self._transport, pb=pb)
141
assert count == len(to_copy)
144
def __contains__(self, fileid):
146
fn = self._relpath(fileid)
147
return self._transport.has(fn)
149
def has(self, fileids, pb=None):
150
"""Return True/False for each entry in fileids.
152
:param fileids: A List or generator yielding file ids.
153
:return: A generator or list returning True/False for each entry.
155
relpaths = (self._relpath(fid) for fid in fileids)
156
return self._transport.has_multi(relpaths, pb=pb)
158
def get(self, fileids, permit_failure=False, pb=None):
159
"""Return a set of files, one for each requested entry.
161
TODO: Write some tests to make sure that permit_failure is
164
TODO: What should the exception be for a missing file?
165
KeyError, or NoSuchFile?
168
# This next code gets a bit hairy because it can allow
169
# to not request a file which doesn't seem to exist.
170
# Also, the same fileid may be requested twice, so we
171
# can't just build up a map.
172
rel_paths = [self._relpath(fid) for fid in fileids]
175
#mutter('CompressedTextStore.get(permit_failure=%s)' % permit_failure)
178
for path, has in zip(rel_paths,
179
self._transport.has_multi(rel_paths)):
181
existing_paths.append(path)
182
is_requested.append(True)
184
is_requested.append(False)
185
#mutter('Retrieving %s out of %s' % (existing_paths, rel_paths))
187
#mutter('Retrieving all %s' % (rel_paths, ))
188
existing_paths = rel_paths
189
is_requested = [True for x in rel_paths]
192
for f in self._transport.get_multi(existing_paths, pb=pb):
193
assert count < len(is_requested)
194
while not is_requested[count]:
197
if hasattr(f, 'tell'):
198
yield gzip.GzipFile(mode='rb', fileobj=f)
200
from cStringIO import StringIO
201
sio = StringIO(f.read())
202
yield gzip.GzipFile(mode='rb', fileobj=sio)
205
while count < len(is_requested):
210
for relpath, st in self._iter_relpaths():
211
if relpath.endswith(".gz"):
212
yield os.path.basename(relpath)[:-3]
214
yield os.path.basename(relpath)
217
return len(list(self._iter_relpath()))
219
def __getitem__(self, fileid):
220
"""Returns a file reading from a particular entry."""
221
f = super(CompressedTextStore, self).__getitem__(fileid)
222
# gzip.GzipFile.read() requires a tell() function
223
# but some transports return objects that cannot seek
224
# so buffer them in a StringIO instead
225
if hasattr(f, 'tell'):
226
return gzip.GzipFile(mode='rb', fileobj=f)
228
from cStringIO import StringIO
229
sio = StringIO(f.read())
230
return gzip.GzipFile(mode='rb', fileobj=sio)
232
def total_size(self):
233
"""Return (count, bytes)
235
This is the (compressed) size stored on disk, not the size of
239
for relpath, st in self._iter_relpaths():
246
class ScratchCompressedTextStore(CompressedTextStore):
247
"""Self-destructing test subclass of CompressedTextStore.
249
The Store only exists for the lifetime of the Python object.
250
Obviously you should not put anything precious in it.
253
from transport import transport
254
t = transport(tempfile.mkdtemp())
255
super(ScratchCompressedTextStore, self).__init__(t)
258
self._transport.delete_multi(self._transport.list_dir('.'))
259
os.rmdir(self._transport.base)
260
mutter("%r destroyed" % self)