From 66cb179e0c751c081fbb9ec769a409a7a8115459 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Wed, 8 Sep 2021 18:54:11 +0200 Subject: add cache and start using it in repo --- repo.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'repo.go') diff --git a/repo.go b/repo.go index eee0f9f..49ff088 100644 --- a/repo.go +++ b/repo.go @@ -39,6 +39,7 @@ import ( "reflect" "github.com/chmduquesne/rollinghash/rabinkarp64" + "github.com/n-peugnet/dna-backup/cache" ) type FingerprintMap map[uint64]*ChunkId @@ -55,6 +56,7 @@ type Repo struct { patcher Patcher fingerprints FingerprintMap sketches SketchMap + chunkCache cache.Cacher } type File struct { @@ -83,6 +85,7 @@ func NewRepo(path string) *Repo { patcher: &Bsdiff{}, fingerprints: make(FingerprintMap), sketches: make(SketchMap), + chunkCache: cache.NewFifoCache(1000), } } @@ -222,6 +225,21 @@ func loadFileList(path string) []File { return files } +// GetChunk loads a chunk from the repo. +// If the chunk is in cache, get it from cache, else read it from drive. +func (r *Repo) GetChunk(id *ChunkId) *LoadedChunk { + var err error + value, exists := r.chunkCache.Get(id) + if !exists { + value, err = io.ReadAll(id.Reader(r)) + if err != nil { + log.Panicf("Could not read from chunk %d: %s", id, err) + } + r.chunkCache.Set(id, value) + } + return NewLoadedChunk(id, value) +} + func storeChunks(dest string, chunks <-chan []byte) { i := 0 for c := range chunks { -- cgit v1.2.3