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.
27
from bzrlib.trace import mutter
28
from bzrlib.errors import BzrError, FileExists
30
from StringIO import StringIO
32
class CompressedTextStore(bzrlib.store.TransportStore):
33
"""Store that holds files indexed by unique names.
35
Files can be added, but not modified once they are in. Typically
36
the hash is used as the name, or something else known to be unique,
39
Files are stored gzip compressed, with no delta compression.
41
>>> st = ScratchCompressedTextStore()
43
>>> st.add(StringIO('hello'), 'aa')
49
You are not allowed to add an id that is already present.
51
Entries can be retrieved as files, which may then be read.
53
>>> st.add(StringIO('goodbye'), '123123')
54
>>> st.get('123123').read()
58
def _relpath(self, fileid, suffixes=[]):
59
suffixes = suffixes + ['gz']
60
return super(CompressedTextStore, self)._relpath(fileid, suffixes)
62
def _add(self, fn, f):
63
from cStringIO import StringIO
64
from bzrlib.osutils import pumpfile
66
if isinstance(f, basestring):
70
gf = gzip.GzipFile(mode='wb', fileobj=sio)
71
# if pumpfile handles files that don't fit in ram,
72
# so will this function
73
if isinstance(f, basestring):
79
self._transport.put(fn, sio)
81
def _copy_one(self, fileid, suffix, other, pb):
82
if not (isinstance(other, CompressedTextStore)
83
and other._prefixed == self._prefixed):
84
return super(CompressedTextStore, self)._copy_one(fileid, suffix,
86
if suffix is None or suffix == 'gz':
87
path = self._relpath(fileid)
89
path = self._relpath(fileid, [suffix])
90
assert other._transport.copy_to([path], self._transport, pb=pb) == 1
92
def __init__(self, transport, prefixed=False):
93
super(CompressedTextStore, self).__init__(transport, prefixed)
94
self.register_suffix('gz')
96
def _get(self, filename):
97
"""Returns a file reading from a particular entry."""
98
f = self._transport.get(filename)
99
# gzip.GzipFile.read() requires a tell() function
100
# but some transports return objects that cannot seek
101
# so buffer them in a StringIO instead
102
if hasattr(f, 'tell'):
103
return gzip.GzipFile(mode='rb', fileobj=f)
105
from cStringIO import StringIO
106
sio = StringIO(f.read())
107
return gzip.GzipFile(mode='rb', fileobj=sio)
110
def ScratchTextStore():
111
return TextStore(ScratchTransport())