-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjsonstore.go
148 lines (134 loc) · 2.89 KB
/
jsonstore.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
package jsonstore
import (
"compress/gzip"
"encoding/json"
"io"
"os"
"regexp"
"strings"
"sync"
)
// NoSuchKeyError is thrown when calling Get with invalid key
type NoSuchKeyError struct {
key string
}
func (err NoSuchKeyError) Error() string {
return "jsonstore: no such key \"" + err.key + "\""
}
// JSONStore is the basic store object.
type JSONStore struct {
Data map[string]json.RawMessage
sync.RWMutex
}
// Open will load a jsonstore from a file.
func Open(filename string) (*JSONStore, error) {
var err error
f, err := os.Open(filename)
defer f.Close()
if err != nil {
return nil, err
}
var w io.Reader
if strings.HasSuffix(filename, ".gz") {
w, err = gzip.NewReader(f)
if err != nil {
return nil, err
}
} else {
w = f
}
toOpen := make(map[string]string)
err = json.NewDecoder(w).Decode(&toOpen)
if err != nil {
return nil, err
}
ks := new(JSONStore)
ks.Data = make(map[string]json.RawMessage)
for key := range toOpen {
ks.Data[key] = json.RawMessage(toOpen[key])
}
return ks, nil
}
// Save writes the jsonstore to disk
func Save(ks *JSONStore, filename string) (err error) {
ks.RLock()
defer ks.RUnlock()
toSave := make(map[string]string)
for key := range ks.Data {
toSave[key] = string(ks.Data[key])
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
if strings.HasSuffix(filename, ".gz") {
w := gzip.NewWriter(f)
defer w.Close()
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(toSave)
}
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
return enc.Encode(toSave)
}
// Set saves a value at the given key.
func (s *JSONStore) Set(key string, value interface{}) error {
s.Lock()
defer s.Unlock()
if s.Data == nil {
s.Data = make(map[string]json.RawMessage)
}
b, err := json.Marshal(value)
if err != nil {
return err
}
s.Data[key] = json.RawMessage(b)
return nil
}
// Get will return the value associated with a key.
func (s *JSONStore) Get(key string, v interface{}) error {
s.RLock()
defer s.RUnlock()
b, ok := s.Data[key]
if !ok {
return NoSuchKeyError{key}
}
return json.Unmarshal(b, &v)
}
// GetAll is like a filter with a regexp. If the regexp is nil, then it returns everything.
func (s *JSONStore) GetAll(re *regexp.Regexp, limit ...int) map[string]json.RawMessage {
s.RLock()
defer s.RUnlock()
results := make(map[string]json.RawMessage)
for k, v := range s.Data {
if re == nil || re.MatchString(k) {
results[k] = v
if len(limit) > 0 {
if len(results) >= limit[0] {
return results
}
}
}
}
return results
}
// Keys returns all the keys currently in map
func (s *JSONStore) Keys() []string {
s.RLock()
defer s.RUnlock()
keys := make([]string, len(s.Data))
i := 0
for k := range s.Data {
keys[i] = k
i++
}
return keys
}
// Delete removes a key from the store.
func (s *JSONStore) Delete(key string) {
s.Lock()
defer s.Unlock()
delete(s.Data, key)
}