milvus/internal/datanode/cache.go
XuanYang-cn 376d39fa02
[skip-ci]Add comment for Cache and allocator (#7924)
Signed-off-by: yangxuan <xuan.yang@zilliz.com>
2021-09-15 10:33:37 +08:00

45 lines
866 B
Go

package datanode
import (
"sync"
)
// Cache stores flusing segments' ids to prevent flushing the same segment again and again.
// Once the segment is flushed, its id will be removed from the cache.
type Cache struct {
cacheMu sync.RWMutex
cacheMap map[UniqueID]bool
}
func newCache() *Cache {
return &Cache{
cacheMap: make(map[UniqueID]bool),
}
}
func (c *Cache) checkIfCached(key UniqueID) bool {
c.cacheMu.Lock()
defer c.cacheMu.Unlock()
_, ok := c.cacheMap[key]
return ok
}
// Cache caches a specific segment ID into the cache
func (c *Cache) Cache(segID UniqueID) {
c.cacheMu.Lock()
defer c.cacheMu.Unlock()
c.cacheMap[segID] = true
}
// Remove removes a set of segment IDs from the cache
func (c *Cache) Remove(segIDs ...UniqueID) {
c.cacheMu.Lock()
defer c.cacheMu.Unlock()
for _, id := range segIDs {
delete(c.cacheMap, id)
}
}