~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-10-10 23:18:27 UTC
  • mfrom: (1437)
  • mto: This revision was merged to the branch mainline in revision 1438.
  • Revision ID: robertc@robertcollins.net-20051010231827-f9e2dda2e92bf565
mergeĀ fromĀ upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import os, tempfile, gzip
25
25
 
26
26
import bzrlib.store
 
27
from bzrlib.store import hash_prefix
27
28
from bzrlib.trace import mutter
28
 
from bzrlib.errors import BzrError
 
29
from bzrlib.errors import BzrError, FileExists
29
30
 
30
31
from StringIO import StringIO
31
32
from stat import ST_SIZE
56
57
    'goodbye'
57
58
    """
58
59
 
59
 
    def __init__(self, transport):
 
60
    def __init__(self, transport, prefixed=False):
60
61
        super(CompressedTextStore, self).__init__(transport)
 
62
        self._prefixed = prefixed
61
63
 
62
64
    def _check_fileid(self, fileid):
63
65
        if '\\' in fileid or '/' in fileid:
65
67
 
66
68
    def _relpath(self, fileid):
67
69
        self._check_fileid(fileid)
68
 
        return fileid + '.gz'
 
70
        if self._prefixed:
 
71
            return hash_prefix(fileid) + fileid + ".gz"
 
72
        else:
 
73
            return fileid + ".gz"
69
74
 
70
75
    def add(self, f, fileid):
71
76
        """Add contents of a file into the store.
88
93
        if self._transport.has(fn):
89
94
            raise BzrError("store %r already contains id %r" % (self._transport.base, fileid))
90
95
 
 
96
        if self._prefixed:
 
97
            try:
 
98
                self._transport.mkdir(hash_prefix(fileid))
 
99
            except FileExists:
 
100
                pass
91
101
 
92
102
        sio = StringIO()
93
103
        gf = gzip.GzipFile(mode='wb', fileobj=sio)
197
207
            count += 1
198
208
 
199
209
    def __iter__(self):
200
 
        # TODO: case-insensitive?
201
 
        for f in self._transport.list_dir('.'):
202
 
            if f[-3:] == '.gz':
203
 
                yield f[:-3]
 
210
        for relpath, st in self._iter_relpaths():
 
211
            if relpath.endswith(".gz"):
 
212
                yield os.path.basename(relpath)[:-3]
204
213
            else:
205
 
                yield f
 
214
                yield os.path.basename(relpath)
206
215
 
207
216
    def __len__(self):
208
 
        return len([f for f in self._transport.list_dir('.')])
209
 
 
 
217
        return len(list(self._iter_relpath()))
210
218
 
211
219
    def __getitem__(self, fileid):
212
220
        """Returns a file reading from a particular entry."""
213
 
        fn = self._relpath(fileid)
214
 
        # This will throw if the file doesn't exist.
215
 
        try:
216
 
            f = self._transport.get(fn)
217
 
        except:
218
 
            raise KeyError('This store (%s) does not contain %s' % (self, fileid))
219
 
 
 
221
        f = super(CompressedTextStore, self).__getitem__(fileid)
220
222
        # gzip.GzipFile.read() requires a tell() function
221
223
        # but some transports return objects that cannot seek
222
224
        # so buffer them in a StringIO instead
226
228
            from cStringIO import StringIO
227
229
            sio = StringIO(f.read())
228
230
            return gzip.GzipFile(mode='rb', fileobj=sio)
229
 
            
230
231
 
231
232
    def total_size(self):
232
233
        """Return (count, bytes)
235
236
        the content."""
236
237
        total = 0
237
238
        count = 0
238
 
        relpaths = [self._relpath(fid) for fid in self]
239
 
        for st in self._transport.stat_multi(relpaths):
 
239
        for relpath, st in self._iter_relpaths():
240
240
            count += 1
241
241
            total += st[ST_SIZE]
242
242