diff options
| author | Chris Lu <chris.lu@gmail.com> | 2017-06-03 11:44:24 -0700 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2017-06-03 11:44:24 -0700 |
| commit | 5047bdb4a20f756f5d025be0788403dbb2db9523 (patch) | |
| tree | 7a7843d226d1aa68715b0a7d5812715dc715613c /weed/storage/needle_byte_cache.go | |
| parent | f5bed84340b8b96c8b134b849f302120d85708c6 (diff) | |
| download | seaweedfs-5047bdb4a20f756f5d025be0788403dbb2db9523.tar.xz seaweedfs-5047bdb4a20f756f5d025be0788403dbb2db9523.zip | |
skip bytes cache
Diffstat (limited to 'weed/storage/needle_byte_cache.go')
| -rw-r--r-- | weed/storage/needle_byte_cache.go | 75 |
1 files changed, 3 insertions, 72 deletions
diff --git a/weed/storage/needle_byte_cache.go b/weed/storage/needle_byte_cache.go index dfc32bcbf..78c1ea862 100644 --- a/weed/storage/needle_byte_cache.go +++ b/weed/storage/needle_byte_cache.go @@ -1,80 +1,11 @@ package storage import ( - "fmt" "os" - "sync/atomic" - - "github.com/hashicorp/golang-lru" - - "github.com/chrislusf/seaweedfs/weed/util" ) -var ( - 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() { - bytesPool = util.NewBytesPool() - bytesCache, _ = lru.NewWithEvict(512, func(key interface{}, value interface{}) { - value.(*Block).decreaseReference() - }) -} - -type Block struct { - Bytes []byte - refCount int32 -} - -func (block *Block) decreaseReference() { - if atomic.AddInt32(&block.refCount, -1) == 0 { - bytesPool.Put(block.Bytes) - } -} -func (block *Block) increaseReference() { - atomic.AddInt32(&block.refCount, 1) -} - -// get bytes from the LRU cache of []byte first, then from the bytes pool -// when []byte in LRU cache is evicted, it will be put back to the bytes pool -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 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 - b := bytesPool.Get(readSize) - // refCount = 2, one by the bytesCache, one by the actual needle object - block = &Block{Bytes: b, refCount: 2} - dataSlice = block.Bytes[0:readSize] +func getBytesForFileBlock(r *os.File, offset int64, readSize int) (dataSlice []byte, err error) { + dataSlice = make([]byte, readSize) _, err = r.ReadAt(dataSlice, offset) - if EnableBytesCache { - bytesCache.Add(cacheKey, block) - } - return dataSlice, block, err -} - -func (n *Needle) ReleaseMemory() { - if n.rawBlock != nil { - n.rawBlock.decreaseReference() - } -} -func ReleaseBytes(b []byte) { - bytesPool.Put(b) + return dataSlice, err } |
