|
|
|
@ -11,15 +11,14 @@ import ( |
|
|
|
) |
|
|
|
|
|
|
|
var ( |
|
|
|
bytesCache *lru.Cache |
|
|
|
bytesPool *util.BytesPool |
|
|
|
EnableBytesCache = true |
|
|
|
bytesCache *lru.Cache |
|
|
|
bytesPool *util.BytesPool |
|
|
|
) |
|
|
|
|
|
|
|
/* |
|
|
|
There are one level of caching, and one level of pooling. |
|
|
|
|
|
|
|
In pooling, all []byte are fetched and returned to the pool bytesPool. |
|
|
|
|
|
|
|
In caching, the string~[]byte mapping is cached |
|
|
|
*/ |
|
|
|
func init() { |
|
|
|
@ -48,11 +47,13 @@ func (block *Block) increaseReference() { |
|
|
|
func getBytesForFileBlock(r *os.File, offset int64, readSize int) (dataSlice []byte, block *Block, err error) { |
|
|
|
// check cache, return if found
|
|
|
|
cacheKey := fmt.Sprintf("%d:%d:%d", r.Fd(), offset>>3, readSize) |
|
|
|
if obj, found := bytesCache.Get(cacheKey); found { |
|
|
|
block = obj.(*Block) |
|
|
|
block.increaseReference() |
|
|
|
dataSlice = block.Bytes[0:readSize] |
|
|
|
return dataSlice, block, nil |
|
|
|
if EnableBytesCache { |
|
|
|
if obj, found := bytesCache.Get(cacheKey); found { |
|
|
|
block = obj.(*Block) |
|
|
|
block.increaseReference() |
|
|
|
dataSlice = block.Bytes[0:readSize] |
|
|
|
return dataSlice, block, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// get the []byte from pool
|
|
|
|
@ -61,7 +62,9 @@ func getBytesForFileBlock(r *os.File, offset int64, readSize int) (dataSlice []b |
|
|
|
block = &Block{Bytes: b, refCount: 2} |
|
|
|
dataSlice = block.Bytes[0:readSize] |
|
|
|
_, err = r.ReadAt(dataSlice, offset) |
|
|
|
bytesCache.Add(cacheKey, block) |
|
|
|
if EnableBytesCache { |
|
|
|
bytesCache.Add(cacheKey, block) |
|
|
|
} |
|
|
|
return dataSlice, block, err |
|
|
|
} |
|
|
|
|
|
|
|
|