forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lru.go
204 lines (172 loc) · 3.95 KB
/
lru.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
Copyright 2013 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Modified by Dgraph Labs, Inc.
// Package lru implements an LRU cache.
package posting
import (
"container/list"
"context"
"sync"
"gopkg.in/adibiarsotp/dgraph.v83/x"
)
// listCache is an LRU cache.
type listCache struct {
sync.Mutex
ctx context.Context
// MaxSize is the maximum size of cache before an item is evicted.
MaxSize uint64
curSize uint64
evicts uint64
ll *list.List
cache map[string]*list.Element
}
type CacheStats struct {
Length int
Size uint64
NumEvicts uint64
}
type entry struct {
key string
pl *List
size uint64
}
// New creates a new Cache.
func newListCache(maxSize uint64) *listCache {
return &listCache{
ctx: context.Background(),
MaxSize: maxSize,
ll: list.New(),
cache: make(map[string]*list.Element),
}
}
func (c *listCache) UpdateMaxSize() {
c.Lock()
defer c.Unlock()
if c.curSize < (50 << 20) {
c.MaxSize = 50 << 20
x.Println("LRU cache max size is being set to 50 MB")
x.LcacheCapacity.Set(50 << 20)
return
}
x.LcacheCapacity.Set(int64(c.curSize))
c.MaxSize = c.curSize
}
// TODO: fingerprint can collide
// Add adds a value to the cache.
func (c *listCache) PutIfMissing(key string, pl *List) (res *List) {
c.Lock()
defer c.Unlock()
if ee, ok := c.cache[key]; ok {
c.ll.MoveToFront(ee)
res = ee.Value.(*entry).pl
return res
}
e := &entry{
key: key,
pl: pl,
size: uint64(pl.EstimatedSize()),
}
if e.size < 100 {
e.size = 100
}
c.curSize += e.size
ele := c.ll.PushFront(e)
c.cache[key] = ele
c.removeOldest()
return e.pl
}
func (c *listCache) removeOldest() {
for c.curSize > c.MaxSize {
ele := c.ll.Back()
if ele == nil {
c.curSize = 0
break
}
c.ll.Remove(ele)
c.evicts++
e := ele.Value.(*entry)
c.curSize -= e.size
e.pl.SetForDeletion()
// If length of mutation layer is zero, then we won't call pstore.SetAsync and the
// key wont be deleted from cache. So lets delete it now if SyncIfDirty returns false.
if committed, _ := e.pl.SyncIfDirty(true); !committed {
delete(c.cache, e.key)
}
}
}
// Get looks up a key's value from the cache.
func (c *listCache) Get(key string) (pl *List) {
c.Lock()
defer c.Unlock()
if ele, hit := c.cache[key]; hit {
c.ll.MoveToFront(ele)
e := ele.Value.(*entry)
est := uint64(e.pl.EstimatedSize())
c.curSize += est - e.size
e.size = est
return e.pl
}
return nil
}
// Len returns the number of items in the cache.
func (c *listCache) Stats() CacheStats {
c.Lock()
defer c.Unlock()
return CacheStats{
Length: c.ll.Len(),
Size: c.curSize,
NumEvicts: c.evicts,
}
}
func (c *listCache) Each(f func(key []byte, val *List)) {
c.Lock()
defer c.Unlock()
ele := c.ll.Front()
for ele != nil {
e := ele.Value.(*entry)
f(e.pl.key, e.pl)
ele = ele.Next()
}
}
func (c *listCache) Reset() {
c.Lock()
defer c.Unlock()
c.ll = list.New()
c.cache = make(map[string]*list.Element)
c.curSize = 0
}
func (c *listCache) clear(remove func(key []byte) bool) error {
c.Lock()
defer c.Unlock()
for k, e := range c.cache {
kv := e.Value.(*entry)
if !remove(kv.pl.key) {
continue
}
c.ll.Remove(e)
kv.pl.SetForDeletion()
if committed, _ := kv.pl.SyncIfDirty(true); !committed {
delete(c.cache, k)
}
}
return nil
}
// delete removes a key from cache
func (c *listCache) delete(key []byte) {
c.Lock()
defer c.Unlock()
if ele, ok := c.cache[string(key)]; ok {
c.ll.Remove(ele)
delete(c.cache, string(key))
}
}