This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
node_versions.go
198 lines (172 loc) · 4.7 KB
/
node_versions.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
package iavl
import (
"fmt"
"sync"
)
// not Thread-Safe
type NodeVersions struct {
nums []int
mtx sync.Mutex
changes map[int64]int // version -> num, changes will be merged to nums when commit
maxVersions int
maxNodes int
firstVersion int64
nextVersion int64
nextVersionIdx int
totalNodes int
}
func NewNodeVersions(maxVersions int, maxNodes int, lastVersion int64) *NodeVersions {
return &NodeVersions{
nums: make([]int, maxVersions, maxVersions),
changes: make(map[int64]int, 32),
maxVersions: maxVersions,
maxNodes: maxNodes,
firstVersion: lastVersion - 1,
nextVersion: lastVersion,
nextVersionIdx: 0,
totalNodes: 0,
}
}
func (nv *NodeVersions) Inc1(version int64) {
nv.changes[version]++
}
func (nv *NodeVersions) Inc1WithLock(version int64) {
nv.mtx.Lock()
nv.changes[version]++
nv.mtx.Unlock()
}
func (nv *NodeVersions) Inc(version int64, times int) {
nv.changes[version] += times
}
func (nv *NodeVersions) Dec1(version int64) {
nv.changes[version]--
}
func (nv *NodeVersions) Dec(version int64, times int) {
nv.changes[version] -= times
}
func (nv *NodeVersions) Update(fromVersion, toVersion int64) {
if fromVersion != toVersion {
nv.changes[fromVersion]--
nv.changes[toVersion]++
}
}
func (nv *NodeVersions) Reset(tree *ImmutableTree) {
nv.nums = make([]int, nv.maxVersions, nv.maxVersions)
nv.changes = make(map[int64]int, 32)
nv.nextVersionIdx = 0
nv.totalNodes = 0
if tree == nil {
nv.firstVersion = 0
nv.nextVersion = 1
return
}
nv.firstVersion = tree.version - 1
nv.nextVersion = tree.version
var iter func(root *Node)
iter = func(root *Node) {
if root == nil {
return
}
// root's version is the biggest in its branch.
iter(root.leftNode)
nv.Inc1(root.loadVersion)
iter(root.rightNode)
}
iter(tree.root)
for version, num := range nv.changes {
idx := nv.getIndex(version)
if idx < 0 {
continue
}
nv.nums[idx] = num
if version < nv.firstVersion {
nv.firstVersion = version
}
nv.totalNodes += num
}
nv.changes = make(map[int64]int, len(nv.changes))
}
// we never prune nodes that have the same version with root node version.
func (nv *NodeVersions) Commit(newVersion int64) (maxPruneVersion int64, pruneNum int, err error) {
if newVersion != nv.nextVersion {
return 0, 0, fmt.Errorf("expect version %d, got %d", nv.nextVersion, newVersion)
}
olderVersionNums := 0
for version, num := range nv.changes {
if version > nv.nextVersion {
// should not happen
return 0, 0, fmt.Errorf("some changes happen on a future version %d, latest version is %d", version, newVersion)
} else if version == nv.nextVersion {
continue
// skip it first, will handle this version later to avoid losing the original num of version (nv.nextVersion - nv.maxVersions)
}
if num == 0 {
continue
}
versionIdx := nv.getIndex(version)
if versionIdx < 0 {
olderVersionNums += num
continue
}
nv.nums[versionIdx] += num
nv.totalNodes += num
if version < nv.firstVersion {
// some old version may be loaded in this round.
nv.firstVersion = version
}
}
maxPruneVersion, pruneNum = nv.prune()
nv.nums[nv.nextVersionIdx] = nv.changes[nv.nextVersion]
nv.totalNodes += nv.changes[nv.nextVersion] - pruneNum
pruneNum += olderVersionNums
nv.changes = make(map[int64]int, len(nv.changes))
nv.nextVersion++
nv.nextVersionIdx = nv.getNextIdx(nv.nextVersionIdx)
nv.firstVersion = maxPruneVersion + 1
return maxPruneVersion, pruneNum, nil
}
func (nv *NodeVersions) Rollback() {
nv.changes = make(map[int64]int, len(nv.changes))
}
func (nv *NodeVersions) prune() (maxPruneVersion int64, prunedNum int) {
if nv.totalNodes <= nv.maxNodes {
return nv.nextVersion - int64(nv.maxVersions), nv.nums[nv.nextVersionIdx]
}
toPruneNum := nv.totalNodes - nv.maxNodes
i := nv.getIndex(nv.firstVersion) // start from the idx of firstVersion to skip some zero nums.
for {
if nv.nums[i] > 0 {
prunedNum += nv.nums[i]
nv.nums[i] = 0
if prunedNum >= toPruneNum { // the actual prune num would be equal or more than toPruneNum
break
}
}
i = nv.getNextIdx(i)
}
maxPruneVersion = nv.getVersion(i)
return maxPruneVersion, prunedNum
}
func (nv *NodeVersions) getNextIdx(idx int) int {
if idx < nv.maxVersions-1 {
return idx + 1
} else {
return 0
}
}
func (nv *NodeVersions) getIndex(version int64) int {
if version < nv.nextVersion-int64(nv.maxVersions) {
return -1
}
idx := nv.nextVersionIdx - int(nv.nextVersion-version)
if idx < 0 {
idx += nv.maxVersions
}
return idx
}
func (nv *NodeVersions) getVersion(idx int) int64 {
if idx >= nv.nextVersionIdx {
return nv.nextVersion - int64(nv.maxVersions) + int64(idx-nv.nextVersionIdx)
}
return nv.nextVersion - int64(nv.nextVersionIdx-idx)
}