2021-06-11 17:53:37 +08:00
|
|
|
package datanode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2021-09-15 10:33:37 +08:00
|
|
|
// 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.
|
2021-06-11 17:53:37 +08:00
|
|
|
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()
|
|
|
|
|
2021-08-11 14:24:09 +08:00
|
|
|
_, ok := c.cacheMap[key]
|
|
|
|
return ok
|
2021-06-11 17:53:37 +08:00
|
|
|
}
|
|
|
|
|
2021-09-15 10:33:37 +08:00
|
|
|
// Cache caches a specific segment ID into the cache
|
2021-06-11 17:53:37 +08:00
|
|
|
func (c *Cache) Cache(segID UniqueID) {
|
|
|
|
c.cacheMu.Lock()
|
|
|
|
defer c.cacheMu.Unlock()
|
|
|
|
|
|
|
|
c.cacheMap[segID] = true
|
|
|
|
}
|
2021-08-11 14:24:09 +08:00
|
|
|
|
2021-09-15 10:33:37 +08:00
|
|
|
// Remove removes a set of segment IDs from the cache
|
2021-08-11 14:24:09 +08:00
|
|
|
func (c *Cache) Remove(segIDs ...UniqueID) {
|
|
|
|
c.cacheMu.Lock()
|
|
|
|
defer c.cacheMu.Unlock()
|
|
|
|
|
|
|
|
for _, id := range segIDs {
|
|
|
|
delete(c.cacheMap, id)
|
|
|
|
}
|
|
|
|
}
|