~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store/__init__.py

  • Committer: Robert Collins
  • Date: 2005-10-16 14:19:24 UTC
  • mto: This revision was merged to the branch mainline in revision 1459.
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051016141924-8dcbb20f1c03756e
Many transport related tweaks:

- Remove get_partial as it was not used at all.
- Move store._iter_relpaths into transport as iter_files_recursive.
- Nuke ImmutableMemoryStore, replaced with MemoryTransport.
- Remove all direct use of the stat module from store logic.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
"""
26
26
 
27
27
from cStringIO import StringIO
28
 
from stat import ST_MODE, S_ISDIR, ST_SIZE
29
28
from zlib import adler32
30
29
 
 
30
import bzrlib
31
31
import bzrlib.errors as errors
32
32
from bzrlib.errors import BzrError, UnlistableStore, TransportNotPossible
33
33
from bzrlib.trace import mutter
34
 
import bzrlib.transport
 
34
import bzrlib.transport as transport
35
35
from bzrlib.transport.local import LocalTransport
36
36
 
37
37
######################################################################
238
238
        relpaths = (self._relpath(fid) for fid in fileids)
239
239
        return self._transport.has_multi(relpaths, pb=pb)
240
240
 
241
 
    def __init__(self, transport, prefixed=False):
242
 
        assert isinstance(transport, bzrlib.transport.Transport)
 
241
    def __init__(self, a_transport, prefixed=False):
 
242
        assert isinstance(a_transport, transport.Transport)
243
243
        super(TransportStore, self).__init__()
244
 
        self._transport = transport
 
244
        self._transport = a_transport
245
245
        self._prefixed = prefixed
246
246
        self._suffixes = set()
247
247
 
269
269
 
270
270
    __str__ = __repr__
271
271
 
272
 
    def _iter_relpaths(self):
273
 
        """Iter the relative paths of files in the transports sub-tree."""
274
 
        transport = self._transport
275
 
        queue = list(transport.list_dir('.'))
276
 
        while queue:
277
 
            relpath = queue.pop(0)
278
 
            st = transport.stat(relpath)
279
 
            if S_ISDIR(st[ST_MODE]):
280
 
                for i, basename in enumerate(transport.list_dir(relpath)):
281
 
                    queue.insert(i, relpath+'/'+basename)
282
 
            else:
283
 
                yield relpath, st
284
 
 
285
272
    def listable(self):
286
273
        """Return True if this store is able to be listed."""
287
274
        return self._transport.listable()
298
285
        the content."""
299
286
        total = 0
300
287
        count = 0
301
 
        for relpath, st in self._iter_relpaths():
 
288
        for relpath in self._transport.iter_files_recursive():
302
289
            count += 1
303
 
            total += st[ST_SIZE]
 
290
            total += self._transport.stat(relpath).st_size
304
291
                
305
292
        return count, total
306
293
 
307
294
 
308
 
class ImmutableMemoryStore(Store):
309
 
    """A memory only store."""
310
 
 
311
 
    def __contains__(self, fileid):
312
 
        return self._contents.has_key(fileid)
313
 
 
314
 
    def __init__(self):
315
 
        super(ImmutableMemoryStore, self).__init__()
316
 
        self._contents = {}
317
 
 
318
 
    def add(self, stream, fileid, compressed=True):
319
 
        if self._contents.has_key(fileid):
320
 
            raise StoreError("fileid %s already in the store" % fileid)
321
 
        self._contents[fileid] = stream.read()
322
 
 
323
 
    def get(self, fileid):
324
 
        """Returns a file reading from a particular entry."""
325
 
        if not self._contents.has_key(fileid):
326
 
            raise IndexError
327
 
        return StringIO(self._contents[fileid])
328
 
 
329
 
    def _item_size(self, fileid):
330
 
        return len(self._contents[fileid])
331
 
 
332
 
    def __iter__(self):
333
 
        return iter(self._contents.keys())
334
 
 
335
 
    def total_size(self):
336
 
        result = 0
337
 
        count = 0
338
 
        for fileid in self:
339
 
            count += 1
340
 
            result += self._item_size(fileid)
341
 
        return count, result
 
295
def ImmutableMemoryStore():
 
296
    return bzrlib.store.text.TextStore(transport.memory.MemoryTransport())
342
297
        
343
298
 
344
299
class CachedStore(Store):