forked from btcsuite/btcd
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcache.go
More file actions
113 lines (94 loc) · 2.41 KB
/
cache.go
File metadata and controls
113 lines (94 loc) · 2.41 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
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
package node
import (
"container/list"
"sync"
"sync/atomic"
"github.com/lbryio/lbcd/claimtrie/change"
)
type cacheLeaf struct {
node *Node
element *list.Element
changes []change.Change
height int32
}
type Cache struct {
nodes map[string]*cacheLeaf
order *list.List
mtx sync.Mutex
limit int
}
func (nc *Cache) insert(name []byte, n *Node, height int32) {
key := string(name)
nc.mtx.Lock()
defer nc.mtx.Unlock()
atomic.AddInt32(&n.refcnt, 1)
existing := nc.nodes[key]
if existing != nil {
existing.node.Close()
existing.node = n
existing.height = height
existing.changes = nil
nc.order.MoveToFront(existing.element)
return
}
for nc.order.Len() >= nc.limit {
// TODO: maybe ensure that we don't remove nodes that have a lot of changes?
exp := nc.order.Back().Value.(string)
expired := nc.nodes[exp]
delete(nc.nodes, exp)
nc.order.Remove(nc.order.Back())
expired.node.Close()
}
element := nc.order.PushFront(key)
nc.nodes[key] = &cacheLeaf{node: n, element: element, height: height}
}
func (nc *Cache) fetch(name []byte, height int32) (*Node, []change.Change, int32) {
key := string(name)
nc.mtx.Lock()
defer nc.mtx.Unlock()
existing := nc.nodes[key]
if existing != nil && existing.height <= height {
nc.order.MoveToFront(existing.element)
atomic.AddInt32(&existing.node.refcnt, 1)
return existing.node, existing.changes, existing.height
}
return nil, nil, -1
}
func (nc *Cache) addChanges(changes []change.Change, height int32) {
nc.mtx.Lock()
defer nc.mtx.Unlock()
for _, c := range changes {
key := string(c.Name)
existing := nc.nodes[key]
if existing != nil && existing.height <= height {
existing.changes = append(existing.changes, c)
}
}
}
func (nc *Cache) drop(names [][]byte) {
nc.mtx.Lock()
defer nc.mtx.Unlock()
for _, name := range names {
key := string(name)
existing := nc.nodes[key]
if existing != nil {
// we can't roll it backwards because we don't know its previous height value; just toast it
delete(nc.nodes, key)
nc.order.Remove(existing.element)
existing.node.Close()
}
}
}
func (nc *Cache) clear() {
nc.mtx.Lock()
defer nc.mtx.Unlock()
for _, existing := range nc.nodes {
existing.node.Close()
}
nc.nodes = map[string]*cacheLeaf{}
nc.order = list.New()
// we'll let the GC sort out the remains...
}
func NewCache(limit int) *Cache {
return &Cache{limit: limit, nodes: map[string]*cacheLeaf{}, order: list.New()}
}