chunk cache adds function ReadChunkAt

This commit is contained in:
chrislu
2022-02-25 21:55:04 -08:00
parent fc7a4957ea
commit 3ad5fa6f6f
5 changed files with 125 additions and 9 deletions

View File

@@ -1,9 +1,8 @@
package chunk_cache
import (
"time"
"github.com/karlseguin/ccache/v2"
"time"
)
// a global cache for recently accessed file chunks
@@ -45,6 +44,21 @@ func (c *ChunkCacheInMemory) getChunkSlice(fileId string, offset, length uint64)
return data[offset : int(offset)+wanted], nil
}
func (c *ChunkCacheInMemory) readChunkAt(buffer []byte, fileId string, offset uint64) (int, error) {
item := c.cache.Get(fileId)
if item == nil {
return 0, nil
}
data := item.Value().([]byte)
item.Extend(time.Hour)
wanted := min(len(buffer), len(data)-int(offset))
if wanted < 0 {
return 0, ErrorOutOfBounds
}
n := copy(buffer, data[offset:int(offset)+wanted])
return n, nil
}
func (c *ChunkCacheInMemory) SetChunk(fileId string, data []byte) {
localCopy := make([]byte, len(data))
copy(localCopy, data)