Debian Codegraph @

cache.go

raw
1 package desync
2
3 import (
4 "fmt"
5
6 "github.com/pkg/errors"
7 )
8
9 // Cache is used to connect a (typically remote) store with a local store which
10 // functions as disk cache. Any request to the cache for a chunk will first be
11 // routed to the local store, and if that fails to the slower remote store.
12 // Any chunks retrieved from the remote store will be stored in the local one.
13 type Cache struct {
14 s Store
15 l WriteStore
16 }
17
18 // NewCache returns a cache router that uses a local store as cache before
19 // accessing a (supposedly slower) remote one.
20 func NewCache(s Store, l WriteStore) Cache {
21 return Cache{s: s, l: l}
22 }
23
24 // GetChunk first asks the local store for the chunk and then the remote one.
25 // If we get a chunk from the remote, it's stored locally too.
26 func (c Cache) GetChunk(id ChunkID) (*Chunk, error) {
27 chunk, err := c.l.GetChunk(id)
28 switch err.(type) {
29 case nil:
30 return chunk, nil
31 case ChunkMissing:
32 default:
33 return chunk, err
34 }
35 // At this point we failed to find chunk in the local cache. Ask the remote
36 chunk, err = c.s.GetChunk(id)
37 if err != nil {
38 return chunk, err
39 }
40 // Got the chunk. Store it in the local cache for next time
41 if err = c.l.StoreChunk(chunk); err != nil {
42 return chunk, errors.Wrap(err, "failed to store in local cache")
43 }
44 return chunk, nil
45 }
46
47 // HasChunk first checks the cache for the chunk, then the store.
48 func (c Cache) HasChunk(id ChunkID) (bool, error) {
49 if hasChunk, err := c.l.HasChunk(id); err != nil || hasChunk {
50 return hasChunk, err
51 }
52 return c.s.HasChunk(id)
53 }
54
55 func (c Cache) String() string {
56 return fmt.Sprintf("store:%s with cache %s", c.s, c.l)
57 }
58
59 // Close the underlying writable chunk store
60 func (c Cache) Close() error {
61 c.l.Close()
62 return c.s.Close()
63 }
64
65 // RepairableCache is a cache whose GetChunk() function will return ChunkMissing error instead of ChunkInvalid
66 // so caller can redownload invalid chunk from store
67 type RepairableCache struct {
68 l WriteStore
69 }
70
71 // Create new RepairableCache that wraps WriteStore and modify its GetChunk() so ChunkInvalid error
72 // will be replaced by ChunkMissing error
73 func NewRepairableCache(l WriteStore) RepairableCache {
74 return RepairableCache{l: l}
75 }
76
77 func (r RepairableCache) GetChunk(id ChunkID) (*Chunk, error) {
78 chunk, err := r.l.GetChunk(id)
79 var chunkInvalidErr ChunkInvalid
80 if err != nil && errors.As(err, &chunkInvalidErr) {
81 return chunk, ChunkMissing{ID: chunkInvalidErr.ID}
82 }
83 return chunk, err
84 }
85
86 func (r RepairableCache) HasChunk(id ChunkID) (bool, error) {
87 return r.l.HasChunk(id)
88 }
89
90 func (r RepairableCache) Close() error {
91 return r.l.Close()
92 }
93
94 func (r RepairableCache) String() string {
95 return r.l.String()
96 }
97
98 func (r RepairableCache) StoreChunk(c *Chunk) error {
99 return r.l.StoreChunk(c)
100 }