-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafekv.go
104 lines (88 loc) · 2.57 KB
/
safekv.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
package rkv
import (
"io"
"sync"
)
// SafeRkv wraps Rkv to provide goroutine safe access to KV store.
type SafeRkv struct {
Rkv
// mutex lock, only one goroutine can access KV datastore at one time
mu sync.Mutex
}
// Make sure SafeRkv implements our common Interface.
var _ Interface = (*SafeRkv)(nil)
// NewSafe opens or creates new Rkv.
func NewSafe(filename string) (*SafeRkv, error) {
kv, err := New(filename)
return &SafeRkv{Rkv: *kv}, err
}
// Compact same as Rkv function but goroutine friendly.
func (kv *SafeRkv) Compact() error {
kv.mu.Lock()
defer kv.mu.Unlock()
return kv.Rkv.Compact()
}
// Put same as Rkv function but goroutine friendly.
func (kv *SafeRkv) Put(key string, value interface{}) error {
kv.mu.Lock()
defer kv.mu.Unlock()
return kv.Rkv.Put(key, value)
}
// PutForDays same as Rkv function but goroutine friendly.
func (kv *SafeRkv) PutForDays(key string, value interface{}, days int32) error {
kv.mu.Lock()
defer kv.mu.Unlock()
return kv.Rkv.PutForDays(key, value, days)
}
// Exist same as Rkv function but goroutine friendly.
func (kv *SafeRkv) Exist(key string) bool {
kv.mu.Lock()
defer kv.mu.Unlock()
return kv.Rkv.Exist(key)
}
// Get same as Rkv function but goroutine friendly.
func (kv *SafeRkv) Get(key string, value interface{}) error {
kv.mu.Lock()
defer kv.mu.Unlock()
return kv.Rkv.Get(key, value)
}
// GetBytes same as Rkv function but goroutine friendly.
func (kv *SafeRkv) GetBytes(key string) ([]byte, error) {
kv.mu.Lock()
defer kv.mu.Unlock()
return kv.Rkv.GetBytes(key)
}
// Delete same as Rkv function but goroutine friendly.
func (kv *SafeRkv) Delete(key string) error {
kv.mu.Lock()
defer kv.mu.Unlock()
return kv.Rkv.Delete(key)
}
// DeleteAllKeys same as Rkv function but goroutine friendly.
func (kv *SafeRkv) DeleteAllKeys(with string) error {
kv.mu.Lock()
defer kv.mu.Unlock()
return kv.Rkv.DeleteAllKeys(with)
}
// GetKeys same as Rkv function but goroutine friendly.
func (kv *SafeRkv) GetKeys(with string, limit int) []string {
kv.mu.Lock()
defer kv.mu.Unlock()
return kv.Rkv.GetKeys(with, limit)
}
// ExportJSON same as Rkv function but goroutine friendly.
func (kv *SafeRkv) ExportJSON(w io.Writer) error {
kv.mu.Lock()
defer kv.mu.Unlock()
return kv.Rkv.ExportJSON(w)
}
// exportKeys same as Rkv function but goroutine friendly.
func (kv *SafeRkv) exportKeys(w io.Writer, arr []string) error {
kv.mu.Lock()
defer kv.mu.Unlock()
return kv.Rkv.exportKeys(w, arr)
}
// Iterator is unsupported in goroutines.
func (kv *SafeRkv) Iterator(with string) <-chan string {
panic("rkv: unsupported function on SafeRkv")
}