-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodepool.go
154 lines (122 loc) · 2.89 KB
/
nodepool.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
package avl
import (
"sync"
"golang.org/x/xerrors"
)
// NodePool is the container of node in Tree.
type NodePool interface {
// Get returns node by key. The returned error is for the external storage
// error. When node is not found, Get() will return (nil, nil).
Get(key []byte) (Node, error)
// Set inserts node. Like Get, the returned error is for the external
// storage error.
Set(Node) error
// Traverse traverses all the nodes in NodePool. NodeTraverseFunc returns 2
// result, If keep is false or error occurred, traversing will be stopped.
Traverse(NodeTraverseFunc) error
}
// SyncMapNodePool uses sync.Map.
type SyncMapNodePool struct {
m *sync.Map
}
func NewSyncMapNodePool(m *sync.Map) *SyncMapNodePool {
if m == nil {
m = &sync.Map{}
}
return &SyncMapNodePool{m: m}
}
func (mn *SyncMapNodePool) Get(key []byte) (Node, error) {
v, found := mn.m.Load(string(key))
if !found {
return nil, nil
} else if node, ok := v.(Node); !ok {
return nil, InvalidNodeError.Wrapf("not Node type: %T", v)
} else {
return node, nil
}
}
func (mn *SyncMapNodePool) Set(node Node) error {
mn.m.Store(string(node.Key()), node)
return nil
}
func (mn *SyncMapNodePool) Traverse(f NodeTraverseFunc) error {
var err error
mn.m.Range(func(_, value interface{}) bool {
var keep bool
node, ok := value.(Node)
if !ok {
err = xerrors.Errorf("invalid type found in nodepool; value=%T", value)
return false
}
if keep, err = f(node); err != nil {
return false
}
return keep
})
return err
}
// MapNodePool uses builtin map.
type MapNodePool struct {
m map[string]Node
}
func NewMapNodePool(m map[string]Node) *MapNodePool {
if m == nil {
m = map[string]Node{}
}
return &MapNodePool{m: m}
}
func (mn *MapNodePool) Get(key []byte) (Node, error) {
node, found := mn.m[string(key)]
if !found {
return nil, nil
}
return node, nil
}
func (mn *MapNodePool) Set(node Node) error {
mn.m[string(node.Key())] = node
return nil
}
func (mn *MapNodePool) Traverse(f NodeTraverseFunc) error {
for _, node := range mn.m {
if keep, err := f(node); err != nil {
return err
} else if !keep {
break
}
}
return nil
}
type MapMutableNodePool struct {
m map[string]MutableNode
}
func NewMapMutableNodePool(m map[string]MutableNode) *MapMutableNodePool {
if m == nil {
m = map[string]MutableNode{}
}
return &MapMutableNodePool{m: m}
}
func (mn *MapMutableNodePool) Get(key []byte) (Node, error) {
node, found := mn.m[string(key)]
if !found {
return nil, nil
}
return node, nil
}
func (mn *MapMutableNodePool) Set(node Node) error {
n, ok := node.(MutableNode)
if !ok {
return xerrors.Errorf("not MutableNode; %T", node)
}
mn.m[string(node.Key())] = n
return nil
}
func (mn *MapMutableNodePool) Traverse(f NodeTraverseFunc) error {
for _, node := range mn.m {
if keep, err := f(node); err != nil {
return err
} else if !keep {
break
}
}
return nil
}