aboutsummaryrefslogtreecommitdiff
path: root/chunk.go
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-09-08 19:20:07 +0200
committern-peugnet <n.peugnet@free.fr>2021-09-08 19:20:07 +0200
commitf061a7031181ef53d034c46b696156c143451cce (patch)
treea9d70773e06e46cc313f92fa000aca1d7471f817 /chunk.go
parent66cb179e0c751c081fbb9ec769a409a7a8115459 (diff)
downloaddna-backup-f061a7031181ef53d034c46b696156c143451cce.tar.gz
dna-backup-f061a7031181ef53d034c46b696156c143451cce.zip
start using chunk cache
Diffstat (limited to 'chunk.go')
-rw-r--r--chunk.go38
1 files changed, 2 insertions, 36 deletions
diff --git a/chunk.go b/chunk.go
index ba8334b..196152e 100644
--- a/chunk.go
+++ b/chunk.go
@@ -2,7 +2,6 @@ package main
import (
"bytes"
- "errors"
"fmt"
"io"
"log"
@@ -25,11 +24,6 @@ type BufferedChunk interface {
Bytes() []byte
}
-type StorerChunk interface {
- Chunk
- Store(path string) error
-}
-
type ChunkId struct {
Ver int
Idx uint64
@@ -39,15 +33,6 @@ func (i *ChunkId) Path(repo string) string {
return path.Join(repo, fmt.Sprintf(versionFmt, i.Ver), chunksName, fmt.Sprintf(chunkIdFmt, i.Idx))
}
-func (i *ChunkId) Reader(repo *Repo) io.ReadSeeker {
- path := i.Path(repo.path)
- f, err := os.Open(path)
- if err != nil {
- log.Println("Cannot open chunk: ", path)
- }
- return f
-}
-
func NewLoadedChunk(id *ChunkId, value []byte) *LoadedChunk {
return &LoadedChunk{Id: id, value: value}
}
@@ -74,10 +59,6 @@ func (c *LoadedChunk) Bytes() []byte {
return c.value
}
-func (c *LoadedChunk) Store(path string) error {
- return storeChunk(c.Reader(), c.Id.Path(path))
-}
-
func NewStoredChunk(repo *Repo, id *ChunkId) *StoredChunk {
return &StoredChunk{repo: repo, Id: id}
}
@@ -93,7 +74,7 @@ func (c *StoredChunk) GetId() *ChunkId {
func (c *StoredChunk) Reader() io.ReadSeeker {
// log.Printf("Chunk %d: Reading from file\n", c.id)
- return c.Id.Reader(c.repo)
+ return c.repo.LoadChunkContent(c.Id)
}
func (c *StoredChunk) Len() int {
@@ -142,7 +123,7 @@ type DeltaChunk struct {
func (c *DeltaChunk) Reader() io.ReadSeeker {
var buff bytes.Buffer
- c.repo.Patcher().Patch(c.Source.Reader(c.repo), &buff, bytes.NewReader(c.Patch))
+ c.repo.Patcher().Patch(c.repo.LoadChunkContent(c.Source), &buff, bytes.NewReader(c.Patch))
return bytes.NewReader(buff.Bytes())
}
@@ -150,18 +131,3 @@ func (c *DeltaChunk) Reader() io.ReadSeeker {
func (c *DeltaChunk) Len() int {
return c.Size
}
-
-func storeChunk(r io.Reader, path string) error {
- 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, r)
- 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
-}