~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/meta_store.py

fixme note for bzr status

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 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):
 
18
from bzrlib.store import ImmutableStore
 
19
 
 
20
class CachedStore:
27
21
    """A store that caches data locally, to avoid repeated downloads.
28
22
    The precacache method should be used to avoid server round-trips for
29
23
    every piece of data.
30
24
    """
31
25
    def __init__(self, store, cache_dir):
32
26
        self.source_store = store
33
 
        self.cache_store = CompressedTextStore(LocalTransport(cache_dir))
 
27
        self.cache_store = ImmutableStore(cache_dir)
34
28
 
35
29
    def __getitem__(self, id):
36
30
        mutter("Cache add %s" % id)
38
32
            self.cache_store.add(self.source_store[id], id)
39
33
        return self.cache_store[id]
40
34
 
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
 
 
60
35
    def prefetch(self, ids):
61
36
        """Copy a series of ids into the cache, before they are used.
62
37
        For remote stores that support pipelining or async downloads, this can
63
38
        increase speed considerably.
64
 
 
65
 
        Failures while prefetching are ignored.
66
39
        """
67
40
        mutter("Prefetch of ids %s" % ",".join(ids))
68
 
        self.cache_store.copy_multi(self.source_store, ids, 
69
 
                                    permit_failure=True)
 
41
        self.cache_store.copy_multi(self.source_store, ids)