-
Notifications
You must be signed in to change notification settings - Fork 340
/
format_blob_cache.go
179 lines (140 loc) · 4.53 KB
/
format_blob_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package format
import (
"bytes"
"context"
"os"
"path/filepath"
"sync"
"time"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/atomicfile"
"github.com/kopia/kopia/internal/cache"
"github.com/kopia/kopia/internal/cachedir"
"github.com/kopia/kopia/internal/clock"
"github.com/kopia/kopia/repo/blob"
)
// DefaultRepositoryBlobCacheDuration is the duration for which we treat cached kopia.repository
// as valid.
const DefaultRepositoryBlobCacheDuration = 15 * time.Minute
// blobCache encapsulates cache for format blobs.
// Note that the cache only stores very small number of blobs at the root of the repository,
// usually 1 or 2.
type blobCache interface {
Get(ctx context.Context, blobID blob.ID) ([]byte, time.Time, bool)
Put(ctx context.Context, blobID blob.ID, data []byte) (time.Time, error)
Remove(ctx context.Context, ids []blob.ID)
}
type nullCache struct{}
//nolint:revive
func (nullCache) Get(ctx context.Context, blobID blob.ID) ([]byte, time.Time, bool) {
return nil, time.Time{}, false
}
//nolint:revive
func (nullCache) Put(ctx context.Context, blobID blob.ID, data []byte) (time.Time, error) {
return clock.Now(), nil
}
//nolint:revive
func (nullCache) Remove(ctx context.Context, ids []blob.ID) {
}
type inMemoryCache struct {
timeNow func() time.Time // +checklocksignore
mu sync.Mutex
// +checklocks:mu
data map[blob.ID][]byte
// +checklocks:mu
times map[blob.ID]time.Time
}
func (c *inMemoryCache) Get(ctx context.Context, blobID blob.ID) ([]byte, time.Time, bool) {
c.mu.Lock()
defer c.mu.Unlock()
data, ok := c.data[blobID]
if ok {
return data, c.times[blobID], true
}
return nil, time.Time{}, false
}
func (c *inMemoryCache) Put(ctx context.Context, blobID blob.ID, data []byte) (time.Time, error) {
c.mu.Lock()
defer c.mu.Unlock()
c.data[blobID] = data
c.times[blobID] = c.timeNow()
return c.times[blobID], nil
}
func (c *inMemoryCache) Remove(ctx context.Context, ids []blob.ID) {
c.mu.Lock()
defer c.mu.Unlock()
for _, blobID := range ids {
delete(c.data, blobID)
delete(c.times, blobID)
}
}
type onDiskCache struct {
cacheDirectory string
}
func (c *onDiskCache) Get(ctx context.Context, blobID blob.ID) ([]byte, time.Time, bool) {
cachedFile := filepath.Join(c.cacheDirectory, string(blobID))
cst, err := os.Stat(cachedFile)
if err != nil {
return nil, time.Time{}, false
}
cacheMTime := cst.ModTime()
//nolint:gosec
data, err := os.ReadFile(cachedFile)
return data, cacheMTime, err == nil
}
func (c *onDiskCache) Put(ctx context.Context, blobID blob.ID, data []byte) (time.Time, error) {
cachedFile := filepath.Join(c.cacheDirectory, string(blobID))
// optimistically assume cache directory exist, create it if not
if err := atomicfile.Write(cachedFile, bytes.NewReader(data)); err != nil {
if err := os.MkdirAll(c.cacheDirectory, cache.DirMode); err != nil && !os.IsExist(err) {
return time.Time{}, errors.Wrap(err, "unable to create cache directory")
}
if err := cachedir.WriteCacheMarker(c.cacheDirectory); err != nil {
return time.Time{}, errors.Wrap(err, "unable to write cache directory marker")
}
if err := atomicfile.Write(cachedFile, bytes.NewReader(data)); err != nil {
return time.Time{}, errors.Wrapf(err, "unable to write to cache: %v", string(blobID))
}
}
cst, err := os.Stat(cachedFile)
if err != nil {
return time.Time{}, errors.Wrap(err, "unable to open cache file")
}
return cst.ModTime(), nil
}
func (c *onDiskCache) Remove(ctx context.Context, ids []blob.ID) {
for _, blobID := range ids {
fname := filepath.Join(c.cacheDirectory, string(blobID))
log(ctx).Infof("deleting %v", fname)
if err := os.Remove(fname); err != nil && !os.IsNotExist(err) {
log(ctx).Debugf("unable to remove cached repository format blob: %v", err)
}
}
}
// NewDiskCache returns on-disk blob cache.
func NewDiskCache(cacheDir string) blobCache {
return &onDiskCache{cacheDir}
}
// NewMemoryBlobCache returns in-memory blob cache.
func NewMemoryBlobCache(timeNow func() time.Time) blobCache {
return &inMemoryCache{
timeNow: timeNow,
data: map[blob.ID][]byte{},
times: map[blob.ID]time.Time{},
}
}
// NewFormatBlobCache creates an implementationof blobCache for particular cache settings.
func NewFormatBlobCache(cacheDir string, validDuration time.Duration, timeNow func() time.Time) blobCache {
if cacheDir != "" {
return NewDiskCache(cacheDir)
}
if validDuration > 0 {
return NewMemoryBlobCache(timeNow)
}
return &nullCache{}
}
var (
_ blobCache = (*nullCache)(nil)
_ blobCache = (*inMemoryCache)(nil)
_ blobCache = (*onDiskCache)(nil)
)