From f061a7031181ef53d034c46b696156c143451cce Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Wed, 8 Sep 2021 19:20:07 +0200 Subject: start using chunk cache --- chunk.go | 38 ++------------------------------------ 1 file changed, 2 insertions(+), 36 deletions(-) (limited to 'chunk.go') 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 -} -- cgit v1.2.3