-
Notifications
You must be signed in to change notification settings - Fork 1
/
replacer.go
53 lines (43 loc) · 983 Bytes
/
replacer.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
package buff
import (
lru "github.com/hashicorp/golang-lru"
)
type Replacer interface {
// Remove the victim frame as defined by replace ment policy
// return evicted frameID if found
Victim() (int, bool)
// frameID should not be victimized until unpin
Pin(frameID int)
// allow frame to be victimizedable
Unpin(frameID int)
// items that can be victimized
Size() int
}
type LRUReplacer struct {
internal *lru.Cache
}
type ClockReplacer struct{}
// TODO
func NewLRUReplacer(numPage int) *LRUReplacer {
c, err := lru.New(numPage)
if err != nil {
panic(err)
}
return &LRUReplacer{
internal: c,
}
}
func (r *LRUReplacer) Pin(frameID int) {
r.internal.Remove(frameID)
}
func (r *LRUReplacer) Victim() (int, bool) {
key, _, ok := r.internal.RemoveOldest()
if !ok {
return 0, false
}
return key.(int), ok
}
func (r *LRUReplacer) Unpin(frameID int) {
r.internal.ContainsOrAdd(frameID, 1)
}
func (r *LRUReplacer) Size() int { return r.internal.Len() }