~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/meta_store.py

  • Committer: John Arbash Meinel
  • Date: 2005-09-16 14:57:59 UTC
  • mto: (1393.2.1)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: john@arbash-meinel.com-20050916145758-ad06c9ae86840f17
Merged up-to-date against mainline, still broken.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
from trace import mutter
18
 
from bzrlib.store import ImmutableStore
19
 
 
20
 
class CachedStore:
 
18
from bzrlib.store import Storage, CompressedTextStore
 
19
from bzrlib.local_transport import LocalTransport
 
20
 
 
21
try:
 
22
    set
 
23
except NameError:
 
24
    from sets import Set as set
 
25
 
 
26
class CachedStore(Storage):
21
27
    """A store that caches data locally, to avoid repeated downloads.
22
28
    The precacache method should be used to avoid server round-trips for
23
29
    every piece of data.
24
30
    """
25
31
    def __init__(self, store, cache_dir):
26
32
        self.source_store = store
27
 
        self.cache_store = ImmutableStore(cache_dir)
 
33
        self.cache_store = CompressedTextStore(LocalTransport(cache_dir))
28
34
 
29
35
    def __getitem__(self, id):
30
36
        mutter("Cache add %s" % id)
32
38
            self.cache_store.add(self.source_store[id], id)
33
39
        return self.cache_store[id]
34
40
 
 
41
    def __contains__(self, fileid):
 
42
        if fileid in self.cache_store:
 
43
            return True
 
44
        if fileid in self.source_store:
 
45
            # We could copy at this time
 
46
            return True
 
47
        return False
 
48
 
 
49
    def get(self, fileids, pb=None):
 
50
        fileids = list(fileids)
 
51
        hasids = self.cache_store.has(fileids)
 
52
        needs = set()
 
53
        for has, fileid in zip(hasids, fileids):
 
54
            if not has:
 
55
                needs.add(fileid)
 
56
        if needs:
 
57
            self.cache_store.copy_multi(self.source_store, needs)
 
58
        return self.cache_store.get(fileids, pb=pb)
 
59
 
35
60
    def prefetch(self, ids):
36
61
        """Copy a series of ids into the cache, before they are used.
37
62
        For remote stores that support pipelining or async downloads, this can
38
63
        increase speed considerably.
 
64
 
 
65
        Failures while prefetching are ignored.
39
66
        """
40
67
        mutter("Prefetch of ids %s" % ",".join(ids))
41
 
        self.cache_store.copy_multi(self.source_store, ids)
 
68
        self.cache_store.copy_multi(self.source_store, ids, 
 
69
                                    permit_failure=True)