-
Notifications
You must be signed in to change notification settings - Fork 85
/
storage.go
77 lines (64 loc) · 2.22 KB
/
storage.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
package basedb
import (
"context"
"time"
)
// Options for creating all db type
type Options struct {
Ctx context.Context
Path string `yaml:"Path" env:"DB_PATH" env-default:"./data/db" env-description:"Path for storage"`
Reporting bool `yaml:"Reporting" env:"DB_REPORTING" env-default:"false" env-description:"Flag to run on-off db size reporting"`
GCInterval time.Duration `yaml:"GCInterval" env:"DB_GC_INTERVAL" env-default:"6m" env-description:"Interval between garbage collection cycles. Set to 0 to disable."`
}
// Reader is a read-only accessor to the database.
type Reader interface {
Get(prefix []byte, key []byte) (Obj, bool, error)
GetMany(prefix []byte, keys [][]byte, iterator func(Obj) error) error
GetAll(prefix []byte, handler func(int, Obj) error) error
}
// ReadWrite is a read-write accessor to the database.
type ReadWriter interface {
Reader
Set(prefix []byte, key []byte, value []byte) error
SetMany(prefix []byte, n int, next func(int) (Obj, error)) error
Delete(prefix []byte, key []byte) error
}
// Txn is a read-write transaction.
type Txn interface {
ReadWriter
// TODO: add iterator
Commit() error
Discard()
}
type ReadTxn interface {
Reader
Discard()
}
// Database interface for Badger DB
type Database interface {
ReadWriter
Begin() Txn
BeginRead() ReadTxn
Using(rw ReadWriter) ReadWriter
UsingReader(r Reader) Reader
// TODO: consider moving these functions into Reader and ReadWriter interfaces?
CountPrefix(prefix []byte) (int64, error)
DeletePrefix(prefix []byte) (int, error)
DropPrefix(prefix []byte) error
Update(fn func(Txn) error) error
Close() error
}
// GarbageCollector is an interface implemented by storage engines which demand garbage collection.
type GarbageCollector interface {
// QuickGC runs a short garbage collection cycle to reclaim some unused disk space.
// Designed to be called periodically while the database is being used.
QuickGC(context.Context) error
// FullGC runs a long garbage collection cycle to reclaim (ideally) all unused disk space.
// Designed to be called when the database is not being used.
FullGC(context.Context) error
}
// Obj struct for getting key/value from storage
type Obj struct {
Key []byte
Value []byte
}