-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore-query.go
134 lines (107 loc) · 3.71 KB
/
store-query.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
package minidb
import (
"errors"
"strings"
)
// Update updates the key's value. It returns nil if updated.
func (db *MiniStore) Update(key string, v interface{}) error {
d := db.getOrCreateMutex("store_update_" + key)
d.Lock()
defer d.Unlock()
if _, ok := db.content[key]; !ok {
return errors.New("unknown key")
}
db.content[key] = v
db.writeToDB()
return nil
}
// Remove attemps to remove the key from the db if it exists.
// It returns nil if it is removed
func (db *MiniStore) Remove(key string) error {
d := db.getOrCreateMutex("store_remove_" + key)
d.Lock()
defer d.Unlock()
if _, ok := db.content[key]; !ok {
return errors.New("key does not exists")
}
// remove
delete(db.content, key)
db.writeToDB()
return nil
}
// GetBool finds the key with bool value and returns if exits.
// It panics if there is an error in type assertion.
func (db *MiniStore) GetBool(key string) bool {
return db.getValue(key).(bool)
}
// GetString finds the key with the string value and returns if exists.
// It panics if there is an error in type assertion.
func (db *MiniStore) GetString(key string) string {
return db.getValue(key).(string)
}
// GetInt finds the key with the int value and returns if exists.
// It panics if there is an error in type assertion.
func (db *MiniStore) GetInt(key string) int {
return db.getValue(key).(int)
}
// GetFloat32 finds the key with the float32 value and returns if exists.
// It panics if there is an error in type assertion.
func (db *MiniStore) GetFloat32(key string) float32 {
return db.getValue(key).(float32)
}
// GetFloat64 finds the key with the float64 value and returns if exists.
// It panics if there is an error in type assertion.
func (db *MiniStore) GetFloat64(key string) float64 {
return db.getValue(key).(float64)
}
// GetBoolSlice finds the key with the []bool value and returns if exits.
// It panics if there is an error in type assertion.
func (db *MiniStore) GetBoolSlice(key string) []bool {
return db.getValue(key).([]bool)
}
// GetStringSlice finds the key with the []string value and returns if exists.
// It panics if there is an error in type assertion.
func (db *MiniStore) GetStringSlice(key string) []string {
return db.getValue(key).([]string)
}
// GetIntSlice finds the key with the []int value and returns if exists.
// It panics if there is an error in type assertion.
func (db *MiniStore) GetIntSlice(key string) []int {
return db.getValue(key).([]int)
}
// GetFloat32Slice finds the key with the []float32 value and returns if exists.
// It panics if there is an error in type assertion.
func (db *MiniStore) GetFloat32Slice(key string) []float32 {
return db.getValue(key).([]float32)
}
// GetFloat64Slice finds the key with the []float64 value and returns if exists.
// It panics if there is an error in type assertion.
func (db *MiniStore) GetFloat64Slice(key string) []float64 {
return db.getValue(key).([]float64)
}
// Get finds the key and returns an interface value.
// It also returns false if the key does not exist and true, otherwise.
func (db *MiniStore) Get(key string) (interface{}, bool) {
return db.getValueOK(key)
}
// IsExists asserts if the key exists. You should use Get() if you want to get
// the Raw value and if it exists.
func (db *MiniStore) IsExists(key string) bool {
_, ok := db.getValueOK(key)
return ok
}
// List returns the content of db.store
func (db *MiniStore) List() map[string]interface{} {
return db.content
}
// FindKeys finds all the keys that contains `key`. It will return only all of the matched keys.
// This is useful for searching keys.
func (db *MiniStore) FindKey(key string) []string {
result := []string{}
for i := range db.content {
if strings.Contains(i, key) {
result = append(result, i)
}
}
return result
}