-
Notifications
You must be signed in to change notification settings - Fork 255
/
memmavl.go
220 lines (197 loc) · 5.16 KB
/
memmavl.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mavl
import (
"fmt"
"sync"
"time"
"github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db"
farm "github.com/dgryski/go-farm"
lru "github.com/hashicorp/golang-lru"
)
// MemTreeOpera memtree操作接口
type MemTreeOpera interface {
Add(key, value interface{})
Get(key interface{}) (value interface{}, ok bool)
Delete(key interface{})
Contains(key interface{}) bool
Len() int
}
// TreeMap map形式memtree
type TreeMap struct {
mpCache map[interface{}]interface{}
lock sync.RWMutex
}
// NewTreeMap new mem tree
func NewTreeMap(size int) *TreeMap {
mp := &TreeMap{}
mp.mpCache = make(map[interface{}]interface{}, size)
return mp
}
// Add 添加元素
func (tm *TreeMap) Add(key, value interface{}) {
tm.lock.Lock()
defer tm.lock.Unlock()
if _, ok := tm.mpCache[key]; ok {
delete(tm.mpCache, key)
return
}
tm.mpCache[key] = value
}
// Get 获取元素
func (tm *TreeMap) Get(key interface{}) (value interface{}, ok bool) {
tm.lock.Lock()
defer tm.lock.Unlock()
if value, ok := tm.mpCache[key]; ok {
return value, ok
}
return nil, false
}
// Delete 删除元素
func (tm *TreeMap) Delete(key interface{}) {
tm.lock.Lock()
defer tm.lock.Unlock()
if _, ok := tm.mpCache[key]; ok {
delete(tm.mpCache, key)
}
}
// Contains 查看是否包含元素
func (tm *TreeMap) Contains(key interface{}) bool {
tm.lock.Lock()
defer tm.lock.Unlock()
if _, ok := tm.mpCache[key]; ok {
return true
}
return false
}
// Len 元素长度
func (tm *TreeMap) Len() int {
tm.lock.Lock()
defer tm.lock.Unlock()
return len(tm.mpCache)
}
// TreeARC lru的mem tree
type TreeARC struct {
arcCache *lru.ARCCache
}
// NewTreeARC new lru mem tree
func NewTreeARC(size int) *TreeARC {
ma := &TreeARC{}
var err error
ma.arcCache, err = lru.NewARC(size)
if err != nil {
panic("New tree lru fail")
}
return ma
}
// Add 添加元素
func (ta *TreeARC) Add(key, value interface{}) {
if ta.arcCache.Contains(key) {
ta.arcCache.Remove(key)
return
}
ta.arcCache.Add(key, value)
}
// Get 获取元素
func (ta *TreeARC) Get(key interface{}) (value interface{}, ok bool) {
return ta.arcCache.Get(key)
}
// Delete 删除元素
func (ta *TreeARC) Delete(key interface{}) {
ta.arcCache.Remove(key)
}
// Contains 查看是否包含元素
func (ta *TreeARC) Contains(key interface{}) bool {
return ta.arcCache.Contains(key)
}
// Len 元素长度
func (ta *TreeARC) Len() int {
return ta.arcCache.Len()
}
// LoadTree2MemDb 从数据库中载入mem tree
func LoadTree2MemDb(db dbm.DB, hash []byte, mp map[uint64]struct{}) {
nDb := newNodeDB(db, true)
node, err := nDb.getLightNode(nil, hash)
if err != nil {
fmt.Println("err", err)
return
}
pri := ""
if len(node.hash) > 32 {
pri = string(node.hash[:16])
}
treelog.Info("hash node", "hash pri", pri, "hash", common.ToHex(node.hash), "height", node.height)
start := time.Now()
leftHash := make([]byte, len(node.leftHash))
copy(leftHash, node.leftHash)
rightHash := make([]byte, len(node.rightHash))
copy(rightHash, node.rightHash)
mp[farm.Hash64(node.hash)] = struct{}{}
node.loadNodeInfo(nDb, mp)
end := time.Now()
treelog.Info("hash node", "cost time", end.Sub(start), "node count", len(mp))
PrintMemStats(1)
}
func (node *Node) loadNodeInfo(db *nodeDB, mp map[uint64]struct{}) {
if node.height == 0 {
//trMem.Add(Hash64(node.hash), &hashNode{leftHash: node.leftHash, rightHash: node.rightHash})
leftHash := make([]byte, len(node.leftHash))
copy(leftHash, node.leftHash)
rightHash := make([]byte, len(node.rightHash))
copy(rightHash, node.rightHash)
mp[farm.Hash64(node.hash)] = struct{}{}
return
}
if node.leftHash != nil {
left, err := db.getLightNode(nil, node.leftHash)
if err != nil {
return
}
//trMem.Add(Hash64(left.hash), &hashNode{leftHash: left.leftHash, rightHash: left.rightHash})
leftHash := make([]byte, len(left.leftHash))
copy(leftHash, left.leftHash)
rightHash := make([]byte, len(left.rightHash))
copy(rightHash, left.rightHash)
mp[farm.Hash64(left.hash)] = struct{}{}
left.loadNodeInfo(db, mp)
}
if node.rightHash != nil {
right, err := db.getLightNode(nil, node.rightHash)
if err != nil {
return
}
//trMem.Add(Hash64(right.hash), &hashNode{leftHash: right.leftHash, rightHash: right.rightHash})
leftHash := make([]byte, len(right.leftHash))
copy(leftHash, right.leftHash)
rightHash := make([]byte, len(right.rightHash))
copy(rightHash, right.rightHash)
mp[farm.Hash64(right.hash)] = struct{}{}
right.loadNodeInfo(db, mp)
}
}
func (ndb *nodeDB) getLightNode(t *Tree, hash []byte) (*Node, error) {
// Doesn't exist, load from db.
var buf []byte
buf, err := ndb.db.Get(hash)
if len(buf) == 0 || err != nil {
return nil, ErrNodeNotExist
}
node, err := MakeNode(buf, t)
if err != nil {
panic(fmt.Sprintf("Error reading IAVLNode. bytes: %X error: %v", buf, err))
}
node.hash = hash
node.key = nil
node.value = nil
return node, nil
}
func copyBytes(b []byte) (copiedBytes []byte) {
if b == nil {
return nil
}
copiedBytes = make([]byte, len(b))
copy(copiedBytes, b)
return copiedBytes
}