-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnegative_cache.go
More file actions
53 lines (44 loc) · 1.12 KB
/
negative_cache.go
File metadata and controls
53 lines (44 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package blazedb
import "sync"
const negativeCacheShardCount = 16
const negativeCacheMaxShardEntries = 4096
type negativeCache struct {
shards [negativeCacheShardCount]negativeCacheShard
}
type negativeCacheShard struct {
mu sync.RWMutex
keys map[chunkKey]struct{}
}
func newNegativeCache() *negativeCache {
c := &negativeCache{}
for i := range c.shards {
c.shards[i].keys = make(map[chunkKey]struct{}, 128)
}
return c
}
func (c *negativeCache) shard(key chunkKey) *negativeCacheShard {
h := uint32(key.x)*31 + uint32(key.z) + uint32(key.dimID)*131
return &c.shards[h&(negativeCacheShardCount-1)]
}
func (c *negativeCache) has(key chunkKey) bool {
shard := c.shard(key)
shard.mu.RLock()
_, ok := shard.keys[key]
shard.mu.RUnlock()
return ok
}
func (c *negativeCache) put(key chunkKey) {
shard := c.shard(key)
shard.mu.Lock()
if len(shard.keys) >= negativeCacheMaxShardEntries {
shard.keys = make(map[chunkKey]struct{}, 128)
}
shard.keys[key] = struct{}{}
shard.mu.Unlock()
}
func (c *negativeCache) delete(key chunkKey) {
shard := c.shard(key)
shard.mu.Lock()
delete(shard.keys, key)
shard.mu.Unlock()
}