-
Notifications
You must be signed in to change notification settings - Fork 15
/
gen_ById.go
211 lines (171 loc) · 4.87 KB
/
gen_ById.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
// Code generated by gotemplate. DO NOT EDIT.
package multi_index
import (
"github.com/eosspark/eos-go/common"
"github.com/eosspark/eos-go/libraries/container"
"github.com/eosspark/eos-go/libraries/multiindex"
)
// template type HashedUniqueIndex(FinalIndex,FinalNode,SuperIndex,SuperNode,Value,Hash,KeyFunc)
type ById struct {
super *ByExpiry // index on the HashedUniqueIndex, IndexBase is the last super index
final *TransactionIdWithExpiryIndex // index under the HashedUniqueIndex, MultiIndex is the final index
inner map[common.TransactionIdType]*ByIdNode // use hashmap to safe HashedUniqueIndex's k/v(HashedUniqueIndexNode)
}
func (i *ById) init(final *TransactionIdWithExpiryIndex) {
i.final = final
i.inner = map[common.TransactionIdType]*ByIdNode{}
i.super = &ByExpiry{}
i.super.init(final)
}
/*generic class*/
/*generic class*/
type ByIdNode struct {
super *ByExpiryNode // index-node on the HashedUniqueIndexNode, IndexBaseNode is the last super node
final *TransactionIdWithExpiryIndexNode // index-node under the HashedUniqueIndexNode, MultiIndexNode is the final index
hash common.TransactionIdType // k of hashmap
}
/*generic class*/
/*generic class*/
func (i *ById) GetSuperIndex() interface{} { return i.super }
func (i *ById) GetFinalIndex() interface{} { return i.final }
func (n *ByIdNode) GetSuperNode() interface{} { return n.super }
func (n *ByIdNode) GetFinalNode() interface{} { return n.final }
func (n *ByIdNode) value() *TransactionIdWithExpiry {
return n.super.value()
}
func (i *ById) Size() int {
return len(i.inner)
}
func (i *ById) Empty() bool {
return len(i.inner) == 0
}
func (i *ById) clear() {
i.inner = map[common.TransactionIdType]*ByIdNode{}
i.super.clear()
}
func (i *ById) Insert(v TransactionIdWithExpiry) (IteratorById, bool) {
fn, res := i.final.insert(v)
if res {
return i.makeIterator(fn), true
}
return i.End(), false
}
func (i *ById) insert(v TransactionIdWithExpiry, fn *TransactionIdWithExpiryIndexNode) (*ByIdNode, bool) {
hash := ByIdFunc(v)
node := ByIdNode{hash: hash}
if _, ok := i.inner[hash]; ok {
container.Logger.Warn("#hash index insert failed")
return nil, false
}
i.inner[hash] = &node
sn, res := i.super.insert(v, fn)
if res {
node.final = fn
node.super = sn
return &node, true
}
delete(i.inner, hash)
return nil, false
}
func (i *ById) Find(k common.TransactionIdType) (IteratorById, bool) {
node, res := i.inner[k]
if res {
return IteratorById{i, node, betweenById}, true
}
return i.End(), false
}
func (i *ById) Each(f func(key common.TransactionIdType, obj TransactionIdWithExpiry)) {
for k, v := range i.inner {
f(k, *v.value())
}
}
func (i *ById) Erase(iter IteratorById) {
i.final.erase(iter.node.final)
}
func (i *ById) erase(n *ByIdNode) {
delete(i.inner, n.hash)
i.super.erase(n.super)
}
func (i *ById) erase_(iter multiindex.IteratorType) {
if itr, ok := iter.(IteratorById); ok {
i.Erase(itr)
} else {
i.super.erase_(iter)
}
}
func (i *ById) Modify(iter IteratorById, mod func(*TransactionIdWithExpiry)) bool {
if _, b := i.final.modify(mod, iter.node.final); b {
return true
}
return false
}
func (i *ById) modify(n *ByIdNode) (*ByIdNode, bool) {
delete(i.inner, n.hash)
hash := ByIdFunc(*n.value())
if _, exist := i.inner[hash]; exist {
container.Logger.Warn("#hash index modify failed")
i.super.erase(n.super)
return nil, false
}
i.inner[hash] = n
if sn, res := i.super.modify(n.super); !res {
delete(i.inner, hash)
return nil, false
} else {
n.super = sn
}
return n, true
}
func (i *ById) modify_(iter multiindex.IteratorType, mod func(*TransactionIdWithExpiry)) bool {
if itr, ok := iter.(IteratorById); ok {
return i.Modify(itr, mod)
} else {
return i.super.modify_(iter, mod)
}
}
func (i *ById) Values() []TransactionIdWithExpiry {
vs := make([]TransactionIdWithExpiry, 0, i.Size())
i.Each(func(key common.TransactionIdType, obj TransactionIdWithExpiry) {
vs = append(vs, obj)
})
return vs
}
type IteratorById struct {
index *ById
node *ByIdNode
position posById
}
type posById byte
const (
//begin = 0
betweenById = 1
endById = 2
)
func (i *ById) makeIterator(fn *TransactionIdWithExpiryIndexNode) IteratorById {
node := fn.GetSuperNode()
for {
if node == nil {
panic("Wrong index node type!")
} else if n, ok := node.(*ByIdNode); ok {
return IteratorById{i, n, betweenById}
} else {
node = node.(multiindex.NodeType).GetSuperNode()
}
}
}
func (i *ById) End() IteratorById {
return IteratorById{i, nil, endById}
}
func (iter IteratorById) Value() (v TransactionIdWithExpiry) {
if iter.position == betweenById {
return *iter.node.value()
}
return
}
func (iter IteratorById) HasNext() bool {
container.Logger.Warn("hashed index iterator is unmoveable")
return false
}
func (iter IteratorById) IsEnd() bool {
return iter.position == endById
}