diff options
| author | chrislu <chris.lu@gmail.com> | 2022-02-25 21:55:04 -0800 |
|---|---|---|
| committer | chrislu <chris.lu@gmail.com> | 2022-02-25 21:55:04 -0800 |
| commit | 3ad5fa6f6f513df152ff155b1fdd93a3c411b6ca (patch) | |
| tree | f8547d0111603a2765c0179a0f00c1817a0ea9f9 /weed/util/chunk_cache/chunk_cache.go | |
| parent | fc7a4957eae6a3f58fb52c88210019e4af89a290 (diff) | |
| download | seaweedfs-3ad5fa6f6f513df152ff155b1fdd93a3c411b6ca.tar.xz seaweedfs-3ad5fa6f6f513df152ff155b1fdd93a3c411b6ca.zip | |
chunk cache adds function ReadChunkAt
Diffstat (limited to 'weed/util/chunk_cache/chunk_cache.go')
| -rw-r--r-- | weed/util/chunk_cache/chunk_cache.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/weed/util/chunk_cache/chunk_cache.go b/weed/util/chunk_cache/chunk_cache.go index 40d24b322..5c4be17a1 100644 --- a/weed/util/chunk_cache/chunk_cache.go +++ b/weed/util/chunk_cache/chunk_cache.go @@ -13,6 +13,7 @@ var ErrorOutOfBounds = errors.New("attempt to read out of bounds") type ChunkCache interface { GetChunk(fileId string, minSize uint64) (data []byte) GetChunkSlice(fileId string, offset, length uint64) []byte + ReadChunkAt(data []byte, fileId string, offset uint64) (n int, err error) SetChunk(fileId string, data []byte) } @@ -145,6 +146,54 @@ func (c *TieredChunkCache) doGetChunkSlice(fileId string, offset, length uint64) return nil } +func (c *TieredChunkCache) ReadChunkAt(data []byte, fileId string, offset uint64) (n int, err error) { + if c == nil { + return 0, nil + } + + c.RLock() + defer c.RUnlock() + + minSize := offset + uint64(len(data)) + if minSize <= c.onDiskCacheSizeLimit0 { + n, err = c.memCache.readChunkAt(data, fileId, offset) + if err != nil { + glog.Errorf("failed to read from memcache: %s", err) + } + if n >= int(minSize) { + return n, nil + } + } + + fid, err := needle.ParseFileIdFromString(fileId) + if err != nil { + glog.Errorf("failed to parse file id %s", fileId) + return n, nil + } + + if minSize <= c.onDiskCacheSizeLimit0 { + n, err = c.diskCaches[0].readChunkAt(data, fid.Key, offset) + if n >= int(minSize) { + return + } + } + if minSize <= c.onDiskCacheSizeLimit1 { + n, err = c.diskCaches[1].readChunkAt(data, fid.Key, offset) + if n >= int(minSize) { + return + } + } + { + n, err = c.diskCaches[2].readChunkAt(data, fid.Key, offset) + if n >= int(minSize) { + return + } + } + + return 0, nil + +} + func (c *TieredChunkCache) SetChunk(fileId string, data []byte) { if c == nil { return |
