aboutsummaryrefslogtreecommitdiff
path: root/repo.go
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-09-08 18:54:11 +0200
committern-peugnet <n.peugnet@free.fr>2021-09-08 18:54:11 +0200
commit66cb179e0c751c081fbb9ec769a409a7a8115459 (patch)
tree2c3dbdace7f8a36f22b66de5ba929d7f2b5d9da4 /repo.go
parent31432f931012a6cb3ee2c153127e6669a4dbb959 (diff)
downloaddna-backup-66cb179e0c751c081fbb9ec769a409a7a8115459.tar.gz
dna-backup-66cb179e0c751c081fbb9ec769a409a7a8115459.zip
add cache and start using it in repo
Diffstat (limited to 'repo.go')
-rw-r--r--repo.go18
1 files changed, 18 insertions, 0 deletions
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 {