-
Notifications
You must be signed in to change notification settings - Fork 2
/
strict_emap.go
292 lines (236 loc) · 8.24 KB
/
strict_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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright(c) 2016 Ethan Zhuang <[email protected]>.
package emap
import (
"errors"
"reflect"
"sync"
)
// StrictEMap has a read-write locker inside so it is concurrent safe.
// The type of key, value and index is determined by the input keySample, valueSample and indexSample.
// Only the types of the sample inputs matter, the values of the sample inputs are irrelevant.
// All methods of the strict emap must use the same type of the sample inputs otherwise an error will be returned.
type StrictEMap struct {
mtx sync.RWMutex
values map[interface{}]interface{} // key -> value
keys map[interface{}][]interface{} // key -> indices
indices map[interface{}][]interface{} // index -> keys
keyType reflect.Kind
indexType reflect.Kind
valueType reflect.Kind
valueStruct string
}
// NewStrictEMap creates a new strict emap.
// The types of value, key and index are determined by the inputs.
// Try to appoint any unsupported key or index types, such as pointer, will cause an error return.
func NewStrictEMap(keySample interface{}, valueSample interface{}, indexSample interface{}) (*StrictEMap, error) {
keyType := reflect.TypeOf(keySample).Kind()
indexType := reflect.TypeOf(indexSample).Kind()
valueType := reflect.TypeOf(valueSample).Kind()
if !isTypeSupported(keyType) || !isTypeSupported(indexType) {
return nil, errors.New("key or index type not supported")
}
instance := new(StrictEMap)
instance.values = make(map[interface{}]interface{})
instance.keys = make(map[interface{}][]interface{})
instance.indices = make(map[interface{}][]interface{})
instance.keyType = keyType
instance.indexType = indexType
instance.valueType = valueType
if valueType == reflect.Struct {
instance.valueStruct = reflect.ValueOf(valueSample).Type().Name()
}
return instance, nil
}
func isTypeSupported(kind reflect.Kind) bool {
//if kind == reflect.Int ||
//kind == reflect.Int8 ||
//kind == reflect.Int16 ||
//kind == reflect.Int32 ||
//kind == reflect.Int64 ||
//kind == reflect.Uint ||
//kind == reflect.Uint8 ||
//kind == reflect.Uint16 ||
//kind == reflect.Uint32 ||
//kind == reflect.Uint64 ||
//kind == reflect.Float32 ||
//kind == reflect.Float64 ||
//kind == reflect.Complex64 ||
//kind == reflect.Complex128 ||
//kind == reflect.String {
// return true
//}
if kind == reflect.Bool ||
kind == reflect.Uintptr ||
kind == reflect.Array ||
kind == reflect.Chan ||
kind == reflect.Func ||
kind == reflect.Interface ||
kind == reflect.Map ||
kind == reflect.Ptr ||
kind == reflect.Slice ||
kind == reflect.Struct ||
kind == reflect.UnsafePointer {
return false
}
return true
}
// KeyNum returns the total key number in the emap.
func (m *StrictEMap) 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 *StrictEMap) 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 *StrictEMap) 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 *StrictEMap) IndexNumOfKey(key interface{}) int {
m.mtx.RLock()
defer m.mtx.RUnlock()
if m.keyType != reflect.TypeOf(key).Kind() {
return 0
}
if indices, exist := m.keys[key]; exist {
return len(indices)
}
return 0
}
// HasKey returns if the input key exists in the emap.
func (m *StrictEMap) HasKey(key interface{}) bool {
m.mtx.RLock()
defer m.mtx.RUnlock()
if m.keyType != reflect.TypeOf(key).Kind() {
return false
}
if _, exist := m.keys[key]; exist {
return true
}
return false
}
// HasIndex returns if the input index exists in the emap.
func (m *StrictEMap) HasIndex(index interface{}) bool {
m.mtx.RLock()
defer m.mtx.RUnlock()
if m.indexType != reflect.TypeOf(index).Kind() {
return false
}
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 *StrictEMap) Insert(key interface{}, value interface{}, indices ...interface{}) error {
m.mtx.Lock()
defer m.mtx.Unlock()
if m.keyType != reflect.TypeOf(key).Kind() {
return errors.New("key type wrong")
}
for _, index := range indices {
if m.indexType != reflect.TypeOf(index).Kind() {
return errors.New("index type wrong")
}
}
if m.valueType != reflect.TypeOf(value).Kind() {
return errors.New("value type wrong")
}
if m.valueType == reflect.Struct && m.valueStruct != reflect.ValueOf(value).Type().Name() {
return errors.New("struct 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 *StrictEMap) FetchByKey(key interface{}) (interface{}, error) {
m.mtx.RLock()
defer m.mtx.RUnlock()
if m.keyType != reflect.TypeOf(key).Kind() {
return nil, errors.New("key type wrong")
}
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 *StrictEMap) FetchByIndex(index interface{}) ([]interface{}, error) {
m.mtx.RLock()
defer m.mtx.RUnlock()
if m.indexType != reflect.TypeOf(index).Kind() {
return nil, errors.New("index type wrong")
}
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 *StrictEMap) 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 *StrictEMap) DeleteByIndex(index interface{}) error {
m.mtx.Lock()
defer m.mtx.Unlock()
if m.indexType != reflect.TypeOf(index).Kind() {
return errors.New("index type wrong")
}
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 *StrictEMap) AddIndex(key interface{}, index interface{}) error {
m.mtx.Lock()
defer m.mtx.Unlock()
if m.keyType != reflect.TypeOf(key).Kind() {
return errors.New("key type wrong")
}
if m.indexType != reflect.TypeOf(index).Kind() {
return errors.New("index type wrong")
}
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 *StrictEMap) RemoveIndex(key interface{}, index interface{}) error {
m.mtx.Lock()
defer m.mtx.Unlock()
if m.keyType != reflect.TypeOf(key).Kind() {
return errors.New("key type wrong")
}
if m.indexType != reflect.TypeOf(index).Kind() {
return errors.New("index type wrong")
}
return removeIndex(m.keys, m.indices, key, index)
}
// 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 *StrictEMap) 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 *StrictEMap) Foreach(callback func(interface{}, interface{})) {
m.mtx.RLock()
defer m.mtx.RUnlock()
foreach(m.values, callback)
}