MCPcopy
hub / github.com/restic/restic / Load

Method Load

internal/backend/cache/backend.go:154–197  ·  view source on GitHub ↗

Load loads a file from the cache or the backend.

(ctx context.Context, h backend.Handle, length int, offset int64, consumer func(rd io.Reader) error)

Source from the content-addressed store, hash-verified

152
153// Load loads a file from the cache or the backend.
154func (b *Backend) Load(ctx context.Context, h backend.Handle, length int, offset int64, consumer func(rd io.Reader) error) error {
155 b.inProgressMutex.Lock()
156 waitForFinish, inProgress := b.inProgress[h]
157 b.inProgressMutex.Unlock()
158
159 if inProgress {
160 debug.Log("downloading %v is already in progress, waiting for finish", h)
161 <-waitForFinish
162 debug.Log("downloading %v finished", h)
163 }
164
165 // try loading from cache without checking that the handle is actually cached
166 inCache, err := b.loadFromCache(h, length, offset, consumer)
167 if inCache {
168 if err != nil {
169 debug.Log("error loading %v from cache: %v", h, err)
170 }
171 // the caller must explicitly use cache.Forget() to remove the cache entry
172 return err
173 }
174
175 // if we don't automatically cache this file type, fall back to the backend
176 if !autoCacheTypes(h) {
177 debug.Log("Load(%v, %v, %v): delegating to backend", h, length, offset)
178 return b.Backend.Load(ctx, h, length, offset, consumer)
179 }
180
181 debug.Log("auto-store %v in the cache", h)
182 err = b.cacheFile(ctx, h)
183 if err != nil {
184 return err
185 }
186
187 inCache, err = b.loadFromCache(h, length, offset, consumer)
188 if inCache {
189 if err != nil {
190 debug.Log("error loading %v from cache: %v", h, err)
191 }
192 return err
193 }
194
195 debug.Log("error caching %v: %v, falling back to backend", h, err)
196 return b.Backend.Load(ctx, h, length, offset, consumer)
197}
198
199// Stat tests whether the backend has a file. If it does not exist but still
200// exists in the cache, it is removed from the cache.

Callers

nothing calls this directly

Implementers 11

Backendinternal/backend/azure/azure.go
b2Backendinternal/backend/b2/b2.go
Backendinternal/backend/s3/s3.go
MemoryBackendinternal/backend/mem/mem_backend.go
Backendinternal/backend/dryrun/dry_backend.go
Localinternal/backend/local/local.go
Backendinternal/backend/gs/gs.go
Backendinternal/backend/mock/backend.go
Backendinternal/backend/rest/rest.go
beSwiftinternal/backend/swift/swift.go
SFTPinternal/backend/sftp/sftp.go

Calls 7

loadFromCacheMethod · 0.95
cacheFileMethod · 0.95
LogFunction · 0.92
autoCacheTypesFunction · 0.85
LoadMethod · 0.65
LockMethod · 0.45
UnlockMethod · 0.45

Tested by

no test coverage detected