From 09e126bae50dac2fe2dee2b074789c6c6e06bf8c Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 17 Aug 2020 20:20:08 -0700 Subject: [PATCH] refactoring: use interface --- weed/filer2/reader_at.go | 6 +++--- weed/util/chunk_cache/chunk_cache.go | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/weed/filer2/reader_at.go b/weed/filer2/reader_at.go index 4d8e48cb5..b036e64fe 100644 --- a/weed/filer2/reader_at.go +++ b/weed/filer2/reader_at.go @@ -19,7 +19,7 @@ type ChunkReadAt struct { readerLock sync.Mutex fileSize int64 - chunkCache *chunk_cache.TieredChunkCache + chunkCache chunk_cache.ChunkCache } // var _ = io.ReaderAt(&ChunkReadAt{}) @@ -53,7 +53,7 @@ func LookupFn(filerClient filer_pb.FilerClient) LookupFileIdFunctionType { } } -func NewChunkReaderAtFromClient(filerClient filer_pb.FilerClient, chunkViews []*ChunkView, chunkCache *chunk_cache.TieredChunkCache, fileSize int64) *ChunkReadAt { +func NewChunkReaderAtFromClient(filerClient filer_pb.FilerClient, chunkViews []*ChunkView, chunkCache chunk_cache.ChunkCache, fileSize int64) *ChunkReadAt { return &ChunkReadAt{ chunkViews: chunkViews, @@ -103,7 +103,7 @@ func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, err error) { bufferOffset := chunkStart - chunk.LogicOffset + chunk.Offset copied := copy(p[chunkStart-startOffset:chunkStop-startOffset], buffer[bufferOffset:bufferOffset+chunkStop-chunkStart]) n += copied - startOffset, remaining = startOffset + int64(copied), remaining-int64(copied) + startOffset, remaining = startOffset+int64(copied), remaining-int64(copied) } glog.V(4).Infof("doReadAt [%d,%d), n:%v, err:%v", offset, offset+int64(len(p)), n, err) diff --git a/weed/util/chunk_cache/chunk_cache.go b/weed/util/chunk_cache/chunk_cache.go index d01e2163b..a1a054215 100644 --- a/weed/util/chunk_cache/chunk_cache.go +++ b/weed/util/chunk_cache/chunk_cache.go @@ -13,6 +13,11 @@ const ( onDiskCacheSizeLimit1 = 4 * memCacheSizeLimit ) +type ChunkCache interface { + GetChunk(fileId string, minSize uint64) (data []byte) + SetChunk(fileId string, data []byte) +} + // a global cache for recently accessed file chunks type TieredChunkCache struct { memCache *ChunkCacheInMemory