-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_structure_mock.go
43 lines (37 loc) · 899 Bytes
/
data_structure_mock.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
package kece
import (
"errors"
"time"
)
type dataStructureMock struct {
db map[string]*Schema
}
// newHashMap init new hashmap data store
func newStructureMock() DataStructure {
ds := new(dataStructureMock)
ds.db = make(map[string]*Schema)
return ds
}
// Insert new data to storage with new key and value
func (h *dataStructureMock) Insert(key, value []byte) *Schema {
newData := &Schema{Key: key, Value: value, Timestamp: time.Now()}
h.db[string(key)] = newData
return newData
}
// Search data based on key
func (h *dataStructureMock) Search(key []byte) (*Schema, error) {
value, ok := h.db[string(key)]
if !ok {
return nil, errors.New(ErrorEmptyValue)
}
return value, nil
}
// Delete data based on key
func (h *dataStructureMock) Delete(key []byte) error {
_, ok := h.db[string(key)]
if !ok {
return errors.New(ErrorEmptyValue)
}
delete(h.db, string(key))
return nil
}