-
Notifications
You must be signed in to change notification settings - Fork 0
/
badgerds.go
127 lines (104 loc) · 2.65 KB
/
badgerds.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
package badgerds
import (
"fmt"
"os"
"path/filepath"
"github.com/ipfs/go-ipfs/plugin"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-ipfs/repo/fsrepo"
humanize "gx/ipfs/QmQMxG9D52TirZd9eLA37nxiNspnMRkKbyPWrVAa1gvtSy/go-humanize"
badgerds "gx/ipfs/QmTNJogwkhnbHeRmAXWtzvN2KgVko2oNmHHQN1ggHVhF91/go-ds-badger"
)
// Plugins is exported list of plugins that will be loaded
var Plugins = []plugin.Plugin{
&badgerdsPlugin{},
}
type badgerdsPlugin struct{}
var _ plugin.PluginDatastore = (*badgerdsPlugin)(nil)
func (*badgerdsPlugin) Name() string {
return "ds-badgerds"
}
func (*badgerdsPlugin) Version() string {
return "0.1.0"
}
func (*badgerdsPlugin) Init() error {
return nil
}
func (*badgerdsPlugin) DatastoreTypeName() string {
return "badgerds"
}
type datastoreConfig struct {
path string
syncWrites bool
truncate bool
vlogFileSize int64
}
// BadgerdsDatastoreConfig returns a configuration stub for a badger datastore
// from the given parameters
func (*badgerdsPlugin) DatastoreConfigParser() fsrepo.ConfigFromMap {
return func(params map[string]interface{}) (fsrepo.DatastoreConfig, error) {
var c datastoreConfig
var ok bool
c.path, ok = params["path"].(string)
if !ok {
return nil, fmt.Errorf("'path' field is missing or not string")
}
sw, ok := params["syncWrites"]
if !ok {
c.syncWrites = true
} else {
if swb, ok := sw.(bool); ok {
c.syncWrites = swb
} else {
return nil, fmt.Errorf("'syncWrites' field was not a boolean")
}
}
truncate, ok := params["truncate"]
if !ok {
c.truncate = true
} else {
if truncate, ok := truncate.(bool); ok {
c.truncate = truncate
} else {
return nil, fmt.Errorf("'truncate' field was not a boolean")
}
}
vls, ok := params["vlogFileSize"]
if !ok {
// default to 1GiB
c.vlogFileSize = badgerds.DefaultOptions.ValueLogFileSize
} else {
if vlogSize, ok := vls.(string); ok {
s, err := humanize.ParseBytes(vlogSize)
if err != nil {
return nil, err
}
c.vlogFileSize = int64(s)
} else {
return nil, fmt.Errorf("'vlogFileSize' field was not a string")
}
}
return &c, nil
}
}
func (c *datastoreConfig) DiskSpec() fsrepo.DiskSpec {
return map[string]interface{}{
"type": "badgerds",
"path": c.path,
}
}
func (c *datastoreConfig) Create(path string) (repo.Datastore, error) {
p := c.path
if !filepath.IsAbs(p) {
p = filepath.Join(path, p)
}
err := os.MkdirAll(p, 0755)
if err != nil {
return nil, err
}
defopts := badgerds.DefaultOptions
defopts.SyncWrites = c.syncWrites
defopts.Truncate = c.truncate
defopts.ValueLogFileSize = c.vlogFileSize
return badgerds.NewDatastore(p, &defopts)
}