-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathapi.go
537 lines (495 loc) · 10.7 KB
/
api.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
package pudge
import (
"bytes"
"encoding/gob"
"os"
)
// DefaultConfig is default config
var DefaultConfig = &Config{
FileMode: 0644,
DirMode: 0755,
SyncInterval: 0,
StoreMode: 0}
// Open return db object if it opened.
// Create new db if not exist.
// Read db to obj if exist.
// Or error if any.
// Default Config (if nil): &Config{FileMode: 0644, DirMode: 0755, SyncInterval: 0}
func Open(f string, cfg *Config) (*Db, error) {
if cfg == nil {
cfg = DefaultConfig
}
dbs.RLock()
db, ok := dbs.dbs[f]
if ok {
dbs.RUnlock()
return db, nil
}
dbs.RUnlock()
dbs.Lock()
db, err := newDb(f, cfg)
//log.Println("n", db.name, db.config.StoreMode)
if err == nil {
dbs.dbs[f] = db
}
dbs.Unlock()
return db, err
}
// Set store any key value to db
func (db *Db) Set(key, value interface{}) error {
db.Lock()
defer db.Unlock()
k, err := KeyToBinary(key)
if err != nil {
return err
}
v, err := ValToBinary(value)
if err != nil {
return err
}
//log.Println("Set:", k, v)
oldCmd, exists := db.vals[string(k)]
//fmt.Println("StoreMode", db.config.StoreMode)
if db.storemode == 2 {
cmd := &Cmd{}
cmd.Size = uint32(len(v))
cmd.Val = make([]byte, len(v))
copy(cmd.Val, v)
db.vals[string(k)] = cmd
} else {
cmd, err := writeKeyVal(db.fk, db.fv, k, v, exists, oldCmd)
if err != nil {
return err
}
db.vals[string(k)] = cmd
}
if !exists {
db.appendKey(k)
}
return err
}
// Get return value by key
// Return error if any.
func (db *Db) Get(key, value interface{}) error {
db.RLock()
defer db.RUnlock()
k, err := KeyToBinary(key)
if err != nil {
return err
}
if val, ok := db.vals[string(k)]; ok {
switch value.(type) {
case *[]byte:
b := make([]byte, val.Size)
if db.storemode == 2 {
copy(b, val.Val)
} else {
_, err := db.fv.ReadAt(b, int64(val.Seek))
if err != nil {
return err
}
}
*value.(*[]byte) = b
return nil
default:
buf := new(bytes.Buffer)
b := make([]byte, val.Size)
if db.storemode == 2 {
//fmt.Println(val)
copy(b, val.Val)
} else {
_, err := db.fv.ReadAt(b, int64(val.Seek))
if err != nil {
return err
}
}
buf.Write(b)
err = gob.NewDecoder(buf).Decode(value)
return err
}
}
return ErrKeyNotFound
}
// Close - sync & close files.
// Return error if any.
func (db *Db) Close() error {
if db.cancelSyncer != nil {
db.cancelSyncer()
}
db.Lock()
defer db.Unlock()
if db.storemode == 2 && db.name != "" {
db.sort()
keys := make([][]byte, len(db.keys))
copy(keys, db.keys)
db.storemode = 0
for _, k := range keys {
if val, ok := db.vals[string(k)]; ok {
writeKeyVal(db.fk, db.fv, k, val.Val, false, nil)
}
}
}
if db.fk != nil {
err := db.fk.Sync()
if err != nil {
return err
}
err = db.fk.Close()
if err != nil {
return err
}
}
if db.fv != nil {
err := db.fv.Sync()
if err != nil {
return err
}
err = db.fv.Close()
if err != nil {
return err
}
}
dbs.Lock()
delete(dbs.dbs, db.name)
dbs.Unlock()
return nil
}
// CloseAll - close all opened Db
func CloseAll() (err error) {
dbs.Lock()
stores := dbs.dbs
dbs.Unlock()
for _, db := range stores {
err = db.Close()
if err != nil {
break
}
}
return err
}
// DeleteFile close and delete file
func (db *Db) DeleteFile() error {
return DeleteFile(db.name)
}
// DeleteFile close db and delete file
func DeleteFile(file string) error {
if file == "" {
return nil
}
dbs.Lock()
db, ok := dbs.dbs[file]
if ok {
dbs.Unlock()
err := db.Close()
if err != nil {
return err
}
} else {
dbs.Unlock()
}
err := os.Remove(file)
if err != nil {
return err
}
err = os.Remove(file + ".idx")
return err
}
// Has return true if key exists.
// Return error if any.
func (db *Db) Has(key interface{}) (bool, error) {
db.RLock()
defer db.RUnlock()
k, err := KeyToBinary(key)
if err != nil {
return false, err
}
_, has := db.vals[string(k)]
return has, nil
}
// FileSize returns the total size of the disk storage used by the DB.
func (db *Db) FileSize() (int64, error) {
db.RLock()
defer db.RUnlock()
var err error
is, err := db.fk.Stat()
if err != nil {
return -1, err
}
ds, err := db.fv.Stat()
if err != nil {
return -1, err
}
return is.Size() + ds.Size(), nil
}
// Count returns the number of items in the Db.
func (db *Db) Count() (int, error) {
db.RLock()
defer db.RUnlock()
return len(db.keys), nil
}
// Delete remove key
// Returns error if key not found
func (db *Db) Delete(key interface{}) error {
db.Lock()
defer db.Unlock()
k, err := KeyToBinary(key)
if err != nil {
return err
}
if _, ok := db.vals[string(k)]; ok {
delete(db.vals, string(k))
db.deleteFromKeys(k)
writeKey(db.fk, 1, 0, 0, k, -1)
return nil
}
return ErrKeyNotFound
}
// KeysByPrefix return keys with prefix
// in ascending or descending order (false - descending,true - ascending)
// if limit == 0 return all keys
// if offset > 0 - skip offset records
// If from not nil - return keys after from (from not included)
func (db *Db) KeysByPrefix(prefix []byte, limit, offset int, asc bool) ([][]byte, error) {
//log.Println("KeysByPrefix")
db.RLock()
defer db.RUnlock()
// resulting array
arr := make([][]byte, 0, 0)
found := db.foundPref(prefix, asc)
if found < 0 || found >= len(db.keys) || !startFrom(db.keys[found], prefix) {
//not found
return arr, ErrKeyNotFound
}
start, end := checkInterval(found, limit, offset, 0, len(db.keys), asc)
if start < 0 || start >= len(db.keys) {
return arr, nil
}
if asc {
for i := start; i <= end; i++ {
if !startFrom(db.keys[i], prefix) {
break
}
arr = append(arr, db.keys[i])
}
} else {
for i := start; i >= end; i-- {
if !startFrom(db.keys[i], prefix) {
break
}
arr = append(arr, db.keys[i])
}
}
return arr, nil
}
// Keys return keys in ascending or descending order (false - descending,true - ascending)
// if limit == 0 return all keys
// if offset > 0 - skip offset records
// If from not nil - return keys after from (from not included)
func (db *Db) Keys(from interface{}, limit, offset int, asc bool) ([][]byte, error) {
// resulting array
//log.Println("pudge", from, from == nil)
arr := make([][]byte, 0, 0)
excludeFrom := 0
if from != nil {
excludeFrom = 1
k, err := KeyToBinary(from)
//log.Println(bytes.Equal(k[len(k)-1:], []byte("*")))
if err != nil {
return arr, err
}
if len(k) > 1 && bytes.Equal(k[len(k)-1:], []byte("*")) {
byteOrStr := false
switch from.(type) {
case []byte:
byteOrStr = true
case string:
byteOrStr = true
}
if byteOrStr {
prefix := make([]byte, len(k)-1)
copy(prefix, k)
return db.KeysByPrefix(prefix, limit, offset, asc)
}
}
}
db.RLock()
defer db.RUnlock()
find, err := db.findKey(from, asc)
if from != nil && err != nil {
return nil, err
}
start, end := checkInterval(find, limit, offset, excludeFrom, len(db.keys), asc)
if start < 0 || start >= len(db.keys) {
return arr, nil
}
if asc {
for i := start; i <= end; i++ {
arr = append(arr, db.keys[i])
}
} else {
for i := start; i >= end; i-- {
arr = append(arr, db.keys[i])
}
}
return arr, nil
}
// Counter return int64 incremented on incr
func (db *Db) Counter(key interface{}, incr int) (int64, error) {
mutex.Lock()
var counter int64
err := db.Get(key, &counter)
if err != nil && err != ErrKeyNotFound {
return -1, err
}
//mutex.Lock()
counter = counter + int64(incr)
//mutex.Unlock()
err = db.Set(key, counter)
mutex.Unlock()
return counter, err
}
// Set store any key value to db with opening if needed
func Set(f string, key, value interface{}) error {
db, err := Open(f, nil)
if err != nil {
return err
}
return db.Set(key, value)
}
// Sets store vals and keys
// Use it for mass insertion
// every pair must contain key and value
func Sets(file string, pairs []interface{}) (err error) {
db, err := Open(file, nil)
if err != nil {
return err
}
for i := range pairs {
if i%2 != 0 {
// on odd - append val and store key
if pairs[i] == nil || pairs[i-1] == nil {
break
}
err = db.Set(pairs[i-1], pairs[i])
if err != nil {
break
}
}
}
return err
}
// Get return value by key with opening if needed
// Return error if any.
func Get(f string, key, value interface{}) error {
db, err := Open(f, nil)
if err != nil {
return err
}
return db.Get(key, value)
}
// Gets return key/value pairs in random order
// result contains key and value
// Gets not return error if key not found
// If no keys found return empty result
func Gets(file string, keys []interface{}) (result [][]byte) {
db, err := Open(file, nil)
if err != nil {
return nil
}
for _, key := range keys {
var v []byte
err := db.Get(key, &v)
if err == nil {
k, err := KeyToBinary(key)
if err == nil {
val, err := ValToBinary(v)
if err == nil {
result = append(result, k)
result = append(result, val)
}
}
}
}
return result
}
// Counter return int64 incremented on incr with lazy open
func Counter(f string, key interface{}, incr int) (int64, error) {
db, err := Open(f, nil)
if err != nil {
return 0, err
}
return db.Counter(key, incr)
}
// Delete remove key
// Returns error if key not found
func Delete(f string, key interface{}) error {
db, err := Open(f, nil)
if err != nil {
return err
}
return db.Delete(key)
}
// Keys return keys in ascending or descending order (false - descending,true - ascending)
// if limit == 0 return all keys
// if offset > 0 - skip offset records
// If from not nil - return keys after from (from not included)
func Keys(f string, from interface{}, limit, offset int, asc bool) ([][]byte, error) {
db, err := Open(f, nil)
if err != nil {
return nil, err
}
return db.Keys(from, limit, offset, asc)
}
// Has return true if key exists.
// Return error if any.
func Has(f string, key interface{}) (bool, error) {
db, err := Open(f, nil)
if err != nil {
return false, err
}
return db.Has(key)
}
// Count returns the number of items in the Db.
func Count(f string) (int, error) {
db, err := Open(f, nil)
if err != nil {
return -1, err
}
return db.Count()
}
// Close - sync & close files.
// Return error if any.
func Close(f string) error {
db, err := Open(f, nil)
if err != nil {
return err
}
return db.Close()
}
// BackupAll - backup all opened Db
// if dir not set it will be backup
// delete old backup file before run
// ignore all errors
func BackupAll(dir string) (err error) {
if dir == "" {
dir = "backup"
}
dbs.Lock()
stores := dbs.dbs
dbs.Unlock()
//tmp := make(map[string]string)
for _, db := range stores {
backup := dir + "/" + db.name
DeleteFile(backup)
keys, err := db.Keys(nil, 0, 0, true)
if err == nil {
for _, k := range keys {
var b []byte
db.Get(k, &b)
Set(backup, k, b)
}
}
Close(backup)
}
return err
}