-
Notifications
You must be signed in to change notification settings - Fork 2
/
generic_emap.go
240 lines (195 loc) · 6.56 KB
/
generic_emap.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright(c) 2016 Ethan Zhuang <[email protected]>.
package emap
import (
"errors"
"reflect"
"sync"
)
// GenericEMap has a read-write locker inside so it is concurrent safe.
// The value, key and index type is unlimited in the generic emap.
type GenericEMap struct {
mtx sync.RWMutex
interval int
values map[interface{}]interface{} // key -> value
keys map[interface{}][]interface{} // key -> indices
indices map[interface{}][]interface{} // index -> keys
}
// NewGenericEMap creates a new generic emap.
func NewGenericEMap() *GenericEMap {
instance := new(GenericEMap)
instance.values = make(map[interface{}]interface{})
instance.keys = make(map[interface{}][]interface{})
instance.indices = make(map[interface{}][]interface{})
return instance
}
// KeyNum returns the total key number in the emap.
func (m *GenericEMap) KeyNum() int {
m.mtx.RLock()
defer m.mtx.RUnlock()
return len(m.keys)
}
// KeyNumOfIndex returns the total key number of the input index in the emap.
func (m *GenericEMap) KeyNumOfIndex(index interface{}) int {
m.mtx.RLock()
defer m.mtx.RUnlock()
if keys, exist := m.indices[index]; exist {
return len(keys)
}
return 0
}
// IndexNum returns the total index number in the emap.
func (m *GenericEMap) IndexNum() int {
m.mtx.RLock()
defer m.mtx.RUnlock()
return len(m.indices)
}
// IndexNumOfKey returns the total index number of the input key in the emap.
func (m *GenericEMap) IndexNumOfKey(key interface{}) int {
m.mtx.RLock()
defer m.mtx.RUnlock()
if indices, exist := m.keys[key]; exist {
return len(indices)
}
return 0
}
// HasKey returns if the input key exists in the emap.
func (m *GenericEMap) HasKey(key interface{}) bool {
m.mtx.RLock()
defer m.mtx.RUnlock()
if _, exist := m.keys[key]; exist {
return true
}
return false
}
// HasIndex returns if the input index exists in the emap.
func (m *GenericEMap) HasIndex(index interface{}) bool {
m.mtx.RLock()
defer m.mtx.RUnlock()
if _, exist := m.indices[index]; exist {
return true
}
return false
}
// Insert pushes a new value into emap with input key and indices.
// Input key must not be duplicated.
// Input indices are optional.
func (m *GenericEMap) Insert(key interface{}, value interface{}, indices ...interface{}) error {
m.mtx.Lock()
defer m.mtx.Unlock()
if m.interval > 0 {
if _, has := reflect.TypeOf(value).MethodByName("IsExpired"); !has {
return errors.New("value type wrong")
}
}
return insert(m.values, m.keys, m.indices, key, value, indices...)
}
// FetchByKey gets the value in the emap by input key.
// Try to fetch a non-existed key will cause an error return.
func (m *GenericEMap) FetchByKey(key interface{}) (interface{}, error) {
m.mtx.RLock()
defer m.mtx.RUnlock()
return fetchByKey(m.values, key)
}
// FetchByIndex gets the all values in the emap by input index.
// Try to fetch a non-existed index will cause an error return.
func (m *GenericEMap) FetchByIndex(index interface{}) ([]interface{}, error) {
m.mtx.RLock()
defer m.mtx.RUnlock()
return fetchByIndex(m.values, m.indices, index)
}
// DeleteByKey deletes the value in the emap by input key.
// Try to delete a non-existed key will cause an error return.
func (m *GenericEMap) DeleteByKey(key interface{}) error {
m.mtx.Lock()
defer m.mtx.Unlock()
return deleteByKey(m.values, m.keys, m.indices, key)
}
// DeleteByIndex deletes all the values in the emap by input index.
// Try to delete a non-existed index will cause an error return.
func (m *GenericEMap) DeleteByIndex(index interface{}) error {
m.mtx.Lock()
defer m.mtx.Unlock()
return deleteByIndex(m.values, m.keys, m.indices, index)
}
// AddIndex add the input index to the value in the emap of the input key.
// Try to add a duplicate index will cause an error return.
// Try to add an index to a non-existed value will cause an error return.
func (m *GenericEMap) AddIndex(key interface{}, index interface{}) error {
m.mtx.Lock()
defer m.mtx.Unlock()
return addIndex(m.keys, m.indices, key, index)
}
// RemoveIndex remove the input index from the value in the emap of the input key.
// Try to delete a non-existed index will cause an error return.
// Try to delete an index from a non-existed value will cause an error return.
func (m *GenericEMap) RemoveIndex(key interface{}, index interface{}) error {
m.mtx.Lock()
defer m.mtx.Unlock()
return removeIndex(m.keys, m.indices, key, index)
}
// Check checks the internal storage consistency.
// If check fails, an error will be returned to explain the inconsistency.
func (m *GenericEMap) check() error {
m.mtx.Lock()
defer m.mtx.Unlock()
if len(m.keys) != len(m.values) {
return errors.New("total key number not equal to total value number")
}
for key, indices := range m.keys {
if _, existed := m.values[key]; existed {
for _, index := range indices {
if keys, existed := m.indices[index]; existed {
found := false
for _, each := range keys {
if each == key {
found = true
break
}
}
if !found {
return errors.New("key storage is not consistent with index storage")
}
} else {
return errors.New("index not existed in the index storage")
}
}
} else {
return errors.New("key not existed in the value storage")
}
}
for index, keys := range m.indices {
for _, key := range keys {
if indices, existed := m.keys[key]; existed {
found := false
for _, each := range indices {
if each == index {
found = true
break
}
}
if !found {
return errors.New("index storage is not consistent with key storage")
}
} else {
return errors.New("key not existed in the key storage")
}
}
}
return nil
}
// Transform is a higher-order operation which apply the input callback function to each key-value pair in the emap.
// Any error returned by the callback function will interrupt the transforming and the error will be returned.
// If transform successfully, a new golang map is created with each key-value pair returned by the input callback function.
func (m *GenericEMap) Transform(callback func(interface{}, interface{}) (interface{}, error)) (map[interface{}]interface{}, error) {
m.mtx.RLock()
defer m.mtx.RUnlock()
return transform(m.values, callback)
}
// Foreach is a higher-order operation which apply the input callback function to each key-value pair in the emap.
// Since the callback function has no return, the foreach procedure will never be interrupted.
// A typical usage of Foreach is apply a closure.
func (m *GenericEMap) Foreach(callback func(interface{}, interface{})) {
m.mtx.RLock()
defer m.mtx.RUnlock()
foreach(m.values, callback)
}