-
Notifications
You must be signed in to change notification settings - Fork 60
/
init.go
56 lines (45 loc) · 1.37 KB
/
init.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package badger
import (
"os"
"path/filepath"
view2 "github.com/hyperledger-labs/fabric-smart-client/platform/view"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/flogging"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/ttxdb"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/ttxdb/driver"
"github.com/pkg/errors"
)
var logger = flogging.MustGetLogger("token-sdk.ttxdb.badger")
const (
// OptsKey is the key for the opts in the config
OptsKey = "token.ttxdb.persistence.opts"
)
type Opts struct {
Path string
}
type Driver struct {
}
func (d Driver) Open(sp view2.ServiceProvider, name string) (driver.TokenTransactionDB, error) {
opts := &Opts{}
err := view2.GetConfigService(sp).UnmarshalKey(OptsKey, opts)
if err != nil {
return nil, errors.Wrapf(err, "failed getting opts for vault")
}
opts.Path = filepath.Join(opts.Path, name)
logger.Debugf("init kvs with badger at [%s]", opts.Path)
err = os.MkdirAll(opts.Path, 0755)
if err != nil {
return nil, errors.Wrapf(err, "failed creating folders for vault [%s]", opts.Path)
}
persistence, err := OpenDB(opts.Path)
if err != nil {
return nil, errors.Wrapf(err, "failed opening vault [%s]", opts.Path)
}
return persistence, nil
}
func init() {
ttxdb.Register("badger", &Driver{})
}