~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store/compressed_text.py

  • Committer: Robert Collins
  • Date: 2005-11-13 20:14:22 UTC
  • mfrom: (1185.16.159)
  • Revision ID: robertc@robertcollins.net-20051113201422-8a34ef413bfc8222
Stores with some compressed texts and some uncompressed texts are now able to
be used. (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Development Ltd
2
 
 
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.
7
 
 
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.
12
 
 
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
16
 
 
17
 
"""
18
 
An implementation the primary storage type CompressedTextStore.
19
 
 
20
 
This store keeps compressed versions of the full text. It does not
21
 
do any sort of delta compression.
22
 
"""
23
 
 
24
 
import gzip
25
 
 
26
 
import bzrlib.store
27
 
from bzrlib.trace import mutter
28
 
from bzrlib.errors import BzrError, FileExists
29
 
 
30
 
from StringIO import StringIO
31
 
 
32
 
class CompressedTextStore(bzrlib.store.TransportStore):
33
 
    """Store that holds files indexed by unique names.
34
 
 
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,
37
 
    such as a UUID.
38
 
 
39
 
    Files are stored gzip compressed, with no delta compression.
40
 
 
41
 
    >>> st = ScratchCompressedTextStore()
42
 
 
43
 
    >>> st.add(StringIO('hello'), 'aa')
44
 
    >>> 'aa' in st
45
 
    True
46
 
    >>> 'foo' in st
47
 
    False
48
 
 
49
 
    You are not allowed to add an id that is already present.
50
 
 
51
 
    Entries can be retrieved as files, which may then be read.
52
 
 
53
 
    >>> st.add(StringIO('goodbye'), '123123')
54
 
    >>> st.get('123123').read()
55
 
    'goodbye'
56
 
    """
57
 
 
58
 
    def _relpath(self, fileid, suffixes=[]):
59
 
        suffixes = suffixes + ['gz']
60
 
        return super(CompressedTextStore, self)._relpath(fileid, suffixes)
61
 
 
62
 
    def _add(self, fn, f):
63
 
        from cStringIO import StringIO
64
 
        from bzrlib.osutils import pumpfile
65
 
        
66
 
        if isinstance(f, basestring):
67
 
            f = StringIO(f)
68
 
            
69
 
        sio = StringIO()
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):
74
 
            gf.write(f)
75
 
        else:
76
 
            pumpfile(f, gf)
77
 
        gf.close()
78
 
        sio.seek(0)
79
 
        self._transport.put(fn, sio)
80
 
 
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, other, pb)
85
 
        if suffix is None or suffix == 'gz':
86
 
            path = self._relpath(fileid)
87
 
        else:
88
 
            path = self._relpath(fileid, [suffix])
89
 
        result = other._transport.copy_to([path], self._transport, pb=pb)
90
 
        assert result == 1      # or what???
91
 
 
92
 
    def __init__(self, transport, prefixed=False):
93
 
        super(CompressedTextStore, self).__init__(transport, prefixed)
94
 
        self.register_suffix('gz')
95
 
 
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)
104
 
        else:
105
 
            from cStringIO import StringIO
106
 
            sio = StringIO(f.read())
107
 
            return gzip.GzipFile(mode='rb', fileobj=sio)
108
 
 
109
 
 
110
 
def ScratchTextStore():
111
 
    return TextStore(ScratchTransport())