-
Notifications
You must be signed in to change notification settings - Fork 1
/
linear.go
273 lines (214 loc) · 5.71 KB
/
linear.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
package linear
import (
"errors"
"log"
"sync"
"unsafe"
)
// Linear contains all the private properties
type Linear struct {
items *sync.Map
keys []string
sizeChecker bool
linearSizes int64 // bytes
linearCurrentSize int64 // bytes
mux *sync.RWMutex
}
// New return new linear instance
func New(maxSize int64, sizeChecker bool) *Linear {
// Argument validator
if maxSize <= 0 {
log.Fatalln("linearSizes much higher than 0")
}
currentLinear := Linear{
keys: []string{},
items: &sync.Map{},
sizeChecker: sizeChecker,
linearSizes: maxSize,
linearCurrentSize: 0,
mux: &sync.RWMutex{},
}
return ¤tLinear
}
// Push item to the linear with key
func (l *Linear) Push(key string, value interface{}) error {
// Argument validator
if key == "" && value == nil {
return errors.New("key and value should not be empty")
}
itemSize := int64(unsafe.Sizeof(key)) + int64(unsafe.Sizeof(value))
if itemSize > l.linearSizes {
return errors.New("linear doesn't have enough memory space")
}
// Clean space for new item
if l.sizeChecker {
for l.linearCurrentSize+itemSize > l.linearSizes {
if _, err := l.Take(); err != nil {
return err
}
}
}
l.items.LoadOrStore(key, value)
l.mux.Lock()
l.linearCurrentSize += itemSize
l.keys = append(l.keys, key)
l.mux.Unlock()
return nil
}
// Pop return and remove the last item out of the linear
func (l *Linear) Pop() (interface{}, error) {
// Execution conditions
if l.IsEmpty() {
return nil, errors.New("linear is empty")
}
lastItemIndex := len(l.keys) - 1
item, ok := l.items.Load(l.keys[lastItemIndex])
if !ok {
return nil, nil
}
itemSize := int64(unsafe.Sizeof(item)) + int64(unsafe.Sizeof(l.keys[lastItemIndex]))
l.items.Delete(l.keys[lastItemIndex])
l.mux.Lock()
l.linearCurrentSize -= itemSize
l.keys = removeItemByIndex(l.keys, lastItemIndex) //Update keys slice after remove that key out of the map
l.mux.Unlock()
return item, nil
}
// Take return and remove the first item out of the linear
func (l *Linear) Take() (interface{}, error) {
// Execution conditions
if l.IsEmpty() {
return nil, errors.New("can't take, because linear is empty")
}
item, ok := l.items.Load(l.keys[0])
if !ok {
return nil, nil
}
itemSize := int64(unsafe.Sizeof(item)) + int64(unsafe.Sizeof(l.keys[0]))
l.items.Delete(l.keys[0])
l.mux.Lock()
l.linearCurrentSize -= itemSize
l.keys = removeItemByIndex(l.keys, 0) //Update keys slice after remove that key/value from items map
l.mux.Unlock()
return item, nil
}
// Get method return and remove the item by key out of the linear
func (l *Linear) Get(key string) (interface{}, error) {
// Execution conditions
if l.IsEmpty() {
return nil, errors.New("linear is empty")
}
var (
wg sync.WaitGroup
item interface{}
itemExits bool
itemIndex int
itemIndexExits bool
)
wg.Add(2)
go func() {
item, itemExits = l.items.Load(key)
wg.Done()
}()
go func() {
itemIndex, itemIndexExits = findIndexByItem(key, l.keys)
wg.Done()
}()
wg.Wait()
if !itemExits || !itemIndexExits {
return nil, nil
}
l.items.Delete(key)
l.mux.Lock()
l.linearCurrentSize -= int64(unsafe.Sizeof(item))
l.keys = removeItemByIndex(l.keys, itemIndex) //Update keys slice after remove that key from items map
l.mux.Unlock()
return item, nil
}
// Read method return the item by key from linear without remove it
func (l *Linear) Read(key string) (interface{}, error) {
// Execution conditions
if l.IsEmpty() {
return nil, errors.New("linear is empty")
}
item, ok := l.items.Load(key)
if !ok {
return nil, nil
}
return item, nil
}
// Update reassign value to the key
func (l *Linear) Update(key string, value interface{}) error {
// Argument validator
if key == "" && value == nil {
return errors.New("key and value should not be empty")
}
// Execution conditions
if l.IsEmpty() {
return errors.New("linear is empty")
}
newItemSize := int64(unsafe.Sizeof(key)) + int64(unsafe.Sizeof(value))
if newItemSize > l.linearSizes || l.IsEmpty() {
return errors.New("linear is empty or not enough space")
}
currentSize, exits := l.IsExits(key)
if !exits {
return errors.New("key does not exit")
}
l.items.Store(key, value)
l.mux.Lock()
l.linearCurrentSize -= currentSize
l.linearCurrentSize += newItemSize
l.mux.Unlock()
return nil
}
// Range the LinearClient
func (l *Linear) Range(fn func(key, value interface{}) bool) {
l.items.Range(fn)
}
// IsExits check key exits or not and return size and status
func (l *Linear) IsExits(key string) (int64, bool) {
value, exits := l.items.Load(key)
if !exits {
return 0, false
}
return int64(unsafe.Sizeof(key)) + int64(unsafe.Sizeof(value)), true
}
// IsEmpty check linear size
func (l *Linear) IsEmpty() bool {
return len(l.keys) == 0
}
// GetItems return the map contain items
func (l *Linear) GetItems() *sync.Map {
return l.items
}
// Getkeys return the list of key
func (l *Linear) Getkeys() []string {
return l.keys
}
// GetNumberOfKeys return the number of keys
func (l *Linear) GetNumberOfKeys() int {
return len(l.keys)
}
// GetLinearSizes return the linear size
func (l *Linear) GetLinearSizes() int64 {
return l.linearSizes
}
// SetLinearSizes change the linear size with new value
func (l *Linear) SetLinearSizes(linearSizes int64) error {
// Argument validator
if linearSizes <= 0 {
return errors.New("linearSizes much higher than 0")
}
l.mux.Lock()
l.linearSizes = linearSizes
l.mux.Unlock()
return nil
}
// GetLinearCurrentSize return the current linear size
func (l *Linear) GetLinearCurrentSize() int64 {
l.mux.RLock()
currentSize := l.linearCurrentSize
l.mux.RUnlock()
return currentSize
}