~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-29 20:34:25 UTC
  • mfrom: (1185.11.24)
  • mto: (1393.1.12)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: john@arbash-meinel.com-20050929203425-7fc2ea87f449dfe8
Merged in split-storage-2 branch. Need to cleanup a little bit more still.

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 bzrlib.trace import mutter
18
 
from bzrlib.store import ImmutableStore
19
 
 
20
 
 
21
 
class CachedStore:
 
18
from bzrlib.store import Store
 
19
from bzrlib.store.compressed_text import CompressedTextStore
 
20
from bzrlib.transport.local import LocalTransport
 
21
 
 
22
class CachedStore(Store):
22
23
    """A store that caches data locally, to avoid repeated downloads.
23
24
    The precacache method should be used to avoid server round-trips for
24
25
    every piece of data.
25
26
    """
26
27
    def __init__(self, store, cache_dir):
27
28
        self.source_store = store
28
 
        self.cache_store = ImmutableStore(cache_dir)
 
29
        self.cache_store = CompressedTextStore(LocalTransport(cache_dir))
29
30
 
30
31
    def __getitem__(self, id):
31
32
        mutter("Cache add %s" % id)
33
34
            self.cache_store.add(self.source_store[id], id)
34
35
        return self.cache_store[id]
35
36
 
 
37
    def __contains__(self, fileid):
 
38
        if fileid in self.cache_store:
 
39
            return True
 
40
        if fileid in self.source_store:
 
41
            # We could copy at this time
 
42
            return True
 
43
        return False
 
44
 
 
45
    def get(self, fileids, permit_failure=False, pb=None):
 
46
        fileids = list(fileids)
 
47
        hasids = self.cache_store.has(fileids)
 
48
        needs = set()
 
49
        for has, fileid in zip(hasids, fileids):
 
50
            if not has:
 
51
                needs.add(fileid)
 
52
        if needs:
 
53
            self.cache_store.copy_multi(self.source_store, needs,
 
54
                    permit_failure=permit_failure)
 
55
        return self.cache_store.get(fileids,
 
56
                permit_failure=permit_failure, pb=pb)
 
57
 
36
58
    def prefetch(self, ids):
37
59
        """Copy a series of ids into the cache, before they are used.
38
60
        For remote stores that support pipelining or async downloads, this can
43
65
        mutter("Prefetch of ids %s" % ",".join(ids))
44
66
        self.cache_store.copy_multi(self.source_store, ids, 
45
67
                                    permit_failure=True)
 
68