diff options
author | n-peugnet <n.peugnet@free.fr> | 2021-09-08 19:20:07 +0200 |
---|---|---|
committer | n-peugnet <n.peugnet@free.fr> | 2021-09-08 19:20:07 +0200 |
commit | f061a7031181ef53d034c46b696156c143451cce (patch) | |
tree | a9d70773e06e46cc313f92fa000aca1d7471f817 /repo.go | |
parent | 66cb179e0c751c081fbb9ec769a409a7a8115459 (diff) | |
download | dna-backup-f061a7031181ef53d034c46b696156c143451cce.tar.gz dna-backup-f061a7031181ef53d034c46b696156c143451cce.zip |
start using chunk cache
Diffstat (limited to 'repo.go')
-rw-r--r-- | repo.go | 39 |
1 files changed, 30 insertions, 9 deletions
@@ -28,6 +28,7 @@ import ( "bufio" "bytes" "encoding/gob" + "errors" "fmt" "hash" "io" @@ -225,19 +226,39 @@ func loadFileList(path string) []File { return files } -// GetChunk loads a chunk from the repo. +func (r *Repo) StoreChunkContent(id *ChunkId, reader io.Reader) error { + path := id.Path(r.path) + file, err := os.Create(path) + if err != nil { + return errors.New(fmt.Sprintf("Error creating chunk for '%s'; %s\n", path, err)) + } + n, err := io.Copy(file, reader) + if err != nil { + return errors.New(fmt.Sprintf("Error writing chunk content for '%s', written %d bytes: %s\n", path, n, err)) + } + if err := file.Close(); err != nil { + return errors.New(fmt.Sprintf("Error closing chunk for '%s': %s\n", path, err)) + } + return nil +} + +// LoadChunkContent 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 +func (r *Repo) LoadChunkContent(id *ChunkId) io.ReadSeeker { value, exists := r.chunkCache.Get(id) if !exists { - value, err = io.ReadAll(id.Reader(r)) + path := id.Path(r.path) + f, err := os.Open(path) + if err != nil { + log.Printf("Cannot open chunk '%s': %s\n", path, err) + } + value, err = io.ReadAll(f) if err != nil { - log.Panicf("Could not read from chunk %d: %s", id, err) + log.Panicf("Could not read from chunk '%s': %s\n", path, err) } r.chunkCache.Set(id, value) } - return NewLoadedChunk(id, value) + return bytes.NewReader(value) } func storeChunks(dest string, chunks <-chan []byte) { @@ -349,7 +370,7 @@ func (r *Repo) tryDeltaEncodeChunk(temp BufferedChunk) (Chunk, bool) { id, found := r.findSimilarChunk(temp) if found { var buff bytes.Buffer - if err := r.differ.Diff(id.Reader(r), temp.Reader(), &buff); err != nil { + if err := r.differ.Diff(r.LoadChunkContent(id), temp.Reader(), &buff); err != nil { log.Println("Error trying delta encode chunk:", temp, "with source:", id, ":", err) } else { return &DeltaChunk{ @@ -377,9 +398,9 @@ func (r *Repo) encodeTempChunk(temp BufferedChunk, version int, last *uint64) (c ic := NewLoadedChunk(id, temp.Bytes()) hasher := rabinkarp64.NewFromPol(r.pol) r.hashAndStoreChunk(ic, hasher) - ic.Store(r.path) + r.StoreChunkContent(id, ic.Reader()) log.Println("Add new chunk", id) - return ic, false + return NewStoredChunk(r, id), false } log.Println("Add new partial chunk of size:", chunk.Len()) return |