/
persistent.go
126 lines (105 loc) · 3.27 KB
/
persistent.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
// Package persistent provides a wrapper around a key-value database for use as
// general node-wide storage.
package persistent
import (
"bytes"
"errors"
"fmt"
"path/filepath"
"github.com/dgraph-io/badger/v3"
"github.com/dgraph-io/badger/v3/options"
cmnBadger "github.com/oasisprotocol/oasis-core/go/common/badger"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/logging"
)
const dbName = "persistent-store.badger.db"
// ErrNotFound is returned when the requested key could not be found in the database.
var ErrNotFound = errors.New("persistent: key not found in database")
// GetPersistentStoreDBDir returns the database directory path for the node with
// the given data directory.
func GetPersistentStoreDBDir(dataDir string) string {
return filepath.Join(dataDir, dbName)
}
// CommonStore is the interface to the common storage for the node.
type CommonStore struct {
db *badger.DB
gc *cmnBadger.GCWorker
}
// Close closes the database handle.
func (cs *CommonStore) Close() {
cs.gc.Close()
cs.db.Close()
}
// GetServiceStore returns a handle to a per-service bucket for the given service.
func (cs *CommonStore) GetServiceStore(name string) (*ServiceStore, error) {
ss := &ServiceStore{
store: cs,
name: []byte(name),
}
return ss, nil
}
// NewCommonStore opens the default common node storage and returns a handle.
func NewCommonStore(dataDir string) (*CommonStore, error) {
logger := logging.GetLogger("common/persistent")
opts := badger.DefaultOptions(GetPersistentStoreDBDir(dataDir))
opts = opts.WithLogger(cmnBadger.NewLogAdapter(logger))
opts = opts.WithSyncWrites(true)
opts = opts.WithCompression(options.None)
db, err := cmnBadger.Open(opts)
if err != nil {
return nil, fmt.Errorf("failed to open persistence database: %w", err)
}
cs := &CommonStore{
db: db,
gc: cmnBadger.NewGCWorker(logger, db),
}
return cs, nil
}
// ServiceStore is a storage wrapper that automatically calls view callbacks with appropriate buckets.
type ServiceStore struct {
store *CommonStore
name []byte
}
// Close invalidates the per-service database handle.
func (ss *ServiceStore) Close() {
ss.store = nil
}
// GetCBOR is a helper for retrieving CBOR-serialized values.
func (ss *ServiceStore) GetCBOR(key []byte, value interface{}) error {
return ss.store.db.View(func(tx *badger.Txn) error {
item, txErr := tx.Get(ss.dbKey(key))
switch txErr {
case nil:
case badger.ErrKeyNotFound:
return ErrNotFound
default:
return txErr
}
return item.Value(func(val []byte) error {
if val == nil {
return ErrNotFound
}
return cbor.Unmarshal(val, value)
})
})
}
// PutCBOR is a helper for storing CBOR-serialized values.
func (ss *ServiceStore) PutCBOR(key []byte, value interface{}) error {
return ss.store.db.Update(func(tx *badger.Txn) error {
return tx.Set(ss.dbKey(key), cbor.Marshal(value))
})
}
// Delete removes the specified key from the service store.
func (ss *ServiceStore) Delete(key []byte) error {
return ss.store.db.Update(func(tx *badger.Txn) error {
switch err := tx.Delete(ss.dbKey(key)); err {
case badger.ErrKeyNotFound:
return ErrNotFound
default:
return err
}
})
}
func (ss *ServiceStore) dbKey(key []byte) []byte {
return bytes.Join([][]byte{ss.name, key}, []byte{'.'})
}