-
Notifications
You must be signed in to change notification settings - Fork 19
/
dbs.go
303 lines (242 loc) · 8.24 KB
/
dbs.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Package dbs is a wrapper for Package db
// Package provides functions for working with map[string]*db.BoltAPI
package dbs
import (
"errors"
"net/http"
"github.com/ShoshinNikita/log"
"github.com/ShoshinNikita/boltBrowser/internal/db"
)
// allDB keeps all opened databases. string – the path to the db
var allDB map[string]*db.BoltAPI
// DBInfo consist main info about db
type DBInfo struct {
Name string `json:"name"`
DBPath string `json:"dbPath"`
Size int64 `json:"size"`
ReadOnly bool `json:"readOnly"`
}
// Init – initializing allDB
func Init() {
allDB = make(map[string]*db.BoltAPI)
}
// OpenDB is a wrapper for *BoltAPI.Open()
func OpenDB(dbPath string, readOnly bool) (dbName string, code int, err error) {
// Check if db was opened
if _, ok := allDB[dbPath]; ok {
return "", http.StatusBadRequest, errors.New("This DB was already opened")
}
newDB, err := db.Open(dbPath, db.Options{ReadOnly: readOnly})
if err != nil {
return "", http.StatusInternalServerError, err
}
allDB[dbPath] = newDB
log.Infof("DB \"%s\" (%s) was opened\n", newDB.Name, dbPath)
return newDB.Name, http.StatusOK, nil
}
// CreateDB is a wrapper for *BoltAPI.Create()
func CreateDB(path string) (dbName, dbPath string, code int, err error) {
newDB, err := db.Create(path)
if err != nil {
return "", "", http.StatusInternalServerError, err
}
allDB[newDB.DBPath] = newDB
log.Infof("DB \"%s\" (%s) was created\n", dbName, dbPath)
return newDB.Name, newDB.DBPath, http.StatusCreated, nil
}
// CloseDB is a wrapper for *BoltAPI.Close()
func CloseDB(dbPath string) (code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
dbName := allDB[dbPath].Name
allDB[dbPath].Close()
delete(allDB, dbPath)
log.Infof("DB \"%s\" (%s) was closed\n", dbName, dbPath)
return http.StatusOK, nil
}
// NextBucket is a wrapper for *BoltAPI.Next()
func NextBucket(dbPath, bucket string) (data db.Data, code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return data, http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
data, err = allDB[dbPath].Next(bucket)
if err != nil {
return data, http.StatusInternalServerError, err
}
return data, http.StatusOK, nil
}
// PrevBucket is a wrapper for *BoltAPI.Back()
func PrevBucket(dbPath string) (data db.Data, code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return data, http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
data, err = allDB[dbPath].Back()
if err != nil {
return data, http.StatusInternalServerError, err
}
return data, http.StatusOK, nil
}
// GetRoot is a wrapper for *BoltAPI.GetRoot()
func GetRoot(dbPath string) (data db.Data, code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return data, http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
data, err = allDB[dbPath].GetRoot()
if err != nil {
return data, http.StatusInternalServerError, err
}
return data, http.StatusOK, nil
}
// GetDBsList returns all opened BoltAPI
func GetDBsList() (list []DBInfo) {
for _, v := range allDB {
info := DBInfo{Name: v.Name, DBPath: v.DBPath, Size: v.Size}
list = append(list, info)
}
return list
}
// GetCurrent is a wrapper for *BoltAPI.GetCurrent()
func GetCurrent(dbPath string) (info DBInfo, data db.Data, code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return info, data, http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
info.DBPath = dbPath
info.Name = allDB[dbPath].Name
info.Size = allDB[dbPath].Size
info.ReadOnly = allDB[dbPath].ReadOnly
data, err = allDB[dbPath].GetCurrent()
if err != nil {
return info, data, http.StatusInternalServerError, err
}
return info, data, http.StatusOK, nil
}
// GetNextRecords is a wrapper for *BoltAPI.NextRecords()
func GetNextRecords(dbPath string) (data db.Data, code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return data, http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
data, err = allDB[dbPath].NextRecords()
if err != nil {
return data, http.StatusInternalServerError, err
}
return data, http.StatusOK, nil
}
// GetPrevRecrods is a wrapper for *BoltAPI.PrevRecords()
func GetPrevRecrods(dbPath string) (data db.Data, code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return data, http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
data, err = allDB[dbPath].PrevRecords()
if err != nil {
return data, http.StatusInternalServerError, err
}
return data, http.StatusOK, nil
}
// Search is a wrapper for *BoltAPI.SearchRegexp() and *BoltAPI.Search()
func Search(dbPath, mode, text string) (records []db.Record, path string, recordsAmount int, code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return records, "", 0, http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
if mode == "regex" {
records, path, recordsAmount, err = allDB[dbPath].SearchRegexp(text)
} else {
records, path, recordsAmount, err = allDB[dbPath].Search(text)
}
if err != nil {
return records, "", 0, http.StatusInternalServerError, err
}
return records, path, recordsAmount, http.StatusOK, nil
}
// AddBucket is a wrapper for *BoltAPI.AddBucket()
func AddBucket(dbPath, bucketName string) (code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
err = allDB[dbPath].AddBucket(bucketName)
if err != nil {
if err == db.ErrNeedWriteMode {
return http.StatusForbidden, err
}
return http.StatusInternalServerError, err
}
return http.StatusCreated, nil
}
// EditBucketName is a wrapper for *BoltAPI.EditBucketName()
func EditBucketName(dbPath, oldName, newName string) (code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
err = allDB[dbPath].EditBucketName(oldName, newName)
if err != nil {
if err == db.ErrNeedWriteMode {
return http.StatusForbidden, err
}
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
// DeleteBucket is a wrapper for *BoltAPI.DeleteBucket()
func DeleteBucket(dbPath, bucketName string) (code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
err = allDB[dbPath].DeleteBucket(bucketName)
if err != nil {
if err == db.ErrNeedWriteMode {
return http.StatusForbidden, err
}
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
// AddRecord is a wrapper for *BoltAPI.AddRecord()
func AddRecord(dbPath, key, value string) (code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
err = allDB[dbPath].AddRecord(key, value)
if err != nil {
if err == db.ErrNeedWriteMode {
return http.StatusForbidden, err
}
return http.StatusInternalServerError, err
}
return http.StatusCreated, nil
}
// EditRecord is a wrapper for *BoltAPI.EditRecord()
func EditRecord(dbPath, oldKey, newKey, newValue string) (code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
err = allDB[dbPath].EditRecord(oldKey, newKey, newValue)
if err != nil {
if err == db.ErrNeedWriteMode {
return http.StatusForbidden, err
}
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
// DeleteRecord is a wrapper for *BoltAPI.DeleteRecord()
func DeleteRecord(dbPath, key string) (code int, err error) {
if _, ok := allDB[dbPath]; !ok {
return http.StatusBadRequest, errors.New("There's no any db with such path (" + dbPath + ")")
}
err = allDB[dbPath].DeleteRecord(key)
if err != nil {
if err == db.ErrNeedWriteMode {
return http.StatusForbidden, err
}
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
// CloseDBs closes all databases
func CloseDBs() {
for k := range allDB {
allDB[k].Close()
delete(allDB, k)
}
log.Infoln("All databases were closed")
}