-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
147 lines (129 loc) · 2.97 KB
/
map.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
package utils
import (
"sync"
"sync/atomic"
)
// AdvanceMap wrap around sync map in go
type AdvanceMap struct {
count int64 // length of the map
items *sync.Map // store all data
mutex sync.RWMutex
}
// NewAdvanceMap linter
func NewAdvanceMap() *AdvanceMap {
return &AdvanceMap{
count: 0,
items: &sync.Map{},
}
}
func (a *AdvanceMap) getCount() *int64 {
a.mutex.RLock()
defer a.mutex.RUnlock()
return &a.count
}
func (a *AdvanceMap) decreCount() {
atomic.AddInt64(a.getCount(), -int64(1))
}
func (a *AdvanceMap) increCount() {
atomic.AddInt64(a.getCount(), 1)
}
func (a *AdvanceMap) getItems() *sync.Map {
a.mutex.Lock()
defer a.mutex.Unlock()
return a.items
}
// Len return len of sync map
func (a *AdvanceMap) Len() int64 {
a.mutex.RLock()
defer a.mutex.RUnlock()
return atomic.LoadInt64(&a.count)
}
// Set set string key with interface value
func (a *AdvanceMap) Set(key string, value interface{}) {
if items := a.getItems(); items != nil {
items.Store(key, value)
a.increCount()
}
}
// Get get item with key string
func (a *AdvanceMap) Get(key string) (interface{}, bool) {
if items := a.getItems(); items != nil {
return items.Load(key)
}
return nil, false
}
// Delete map with key string
func (a *AdvanceMap) Delete(key interface{}) {
if items := a.getItems(); items != nil {
items.Delete(key)
a.decreCount()
}
}
// GetKeys get all keys of map
func (a *AdvanceMap) GetKeys() []string {
temp := make([]string, 0)
a.Iter(func(key, value interface{}) bool {
item, ok := key.(string)
if ok {
temp = append(temp, item)
}
return true
})
return temp
}
// Iter go though a map
func (a *AdvanceMap) Iter(callBack func(key, value interface{}) bool) {
if items := a.getItems(); items != nil {
items.Range(callBack)
}
}
// Capture a current map
// Warning: Current data will be delete after using it
func (a *AdvanceMap) Capture() map[string]interface{} {
tempMap := make(map[string]interface{})
tempFunc := func(key, value interface{}) bool {
keyStr, ok := key.(string)
if ok {
tempMap[keyStr] = value
}
return true
}
if items := a.getItems(); items != nil {
items.Range(func(key, value interface{}) bool {
// add key value
tempFunc(key, value)
// delete value
a.Delete(key)
return true
})
}
return tempMap
}
// ToMap returning map to this value
func (a *AdvanceMap) ToMap() map[string]interface{} {
tempMap := make(map[string]interface{})
if items := a.getItems(); items != nil {
items.Range(func(key, value interface{}) bool {
keyStr, ok := key.(string)
if ok {
tempMap[keyStr] = value
}
return true
})
}
return tempMap
}
// Geti get item with key interface
func (a *AdvanceMap) Geti(key interface{}) (interface{}, bool) {
if items := a.getItems(); items != nil {
return items.Load(key)
}
return nil, false
}
// Seti set interface key with interface value
func (a *AdvanceMap) Seti(key, value interface{}) {
if items := a.getItems(); items != nil {
items.Store(key, value)
a.increCount()
}
}