-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkv.go
49 lines (43 loc) · 969 Bytes
/
kv.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
package jstore
const kvKey = "kv"
type Kv struct {
baseKv
}
// Kv is a simple key (string) to value (string) store in a specific collection
// the default collection uses is called "kv"
func (db *Db) Kv(collection ...string) *Kv {
col := kvKey
if len(collection) > 0 && collection[0] != "" {
col = collection[0]
}
if !db.colExists(col) {
db.content[col] = map[string]interface{}{}
}
k := Kv{
baseKv{
name: col,
db: db,
content: db.content[col].(map[string]interface{}),
},
}
return &k
}
func (kv *Kv) Set(key string, in interface{}) error {
return kv.baseKv.set(key, in)
}
func (kv *Kv) Get(key string, value interface{}) error {
return kv.baseKv.get(key, value)
}
func (kv *Kv) Del(key string) error {
delete(kv.content, key)
if !kv.db.inMemory && !kv.db.ManualFlush {
return kv.db.flushToFile()
}
return nil
}
func (kv *Kv) Exists(key string) bool {
if _, ok := kv.content[key]; !ok {
return false
}
return true
}