forked from ipfs/kubo
/
datastores.go
247 lines (211 loc) · 6.37 KB
/
datastores.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
package fsrepo
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"github.com/ipfs/go-ipfs/repo"
ds "gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
"gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore/mount"
"gx/ipfs/QmafCXHpwUKGUrt6eQF97U1ornFxTicsw7iyYSAhu9aXgc/go-ds-measure"
)
// ConfigFromMap creates a new datastore config from a map
type ConfigFromMap func(map[string]interface{}) (DatastoreConfig, error)
// DatastoreConfig is an abstraction of a datastore config. A "spec"
// is first converted to a DatastoreConfig and then Create() is called
// to instantiate a new datastore
type DatastoreConfig interface {
// DiskSpec returns a minimal configuration of the datastore
// represting what is stored on disk. Run time values are
// excluded.
DiskSpec() DiskSpec
// Create instantiate a new datastore from this config
Create(path string) (repo.Datastore, error)
}
// DiskSpec is a minimal representation of the characteristic values of the
// datastore. If two diskspecs are the same, the loader assumes that they refer
// to exactly the same datastore. If they differ at all, it is assumed they are
// completely different datastores and a migration will be performed. Runtime
// values such as cache options or concurrency options should not be added
// here.
type DiskSpec map[string]interface{}
// Bytes returns a minimal JSON encoding of the DiskSpec
func (spec DiskSpec) Bytes() []byte {
b, err := json.Marshal(spec)
if err != nil {
// should not happen
panic(err)
}
return bytes.TrimSpace(b)
}
// String returns a minimal JSON encoding of the DiskSpec
func (spec DiskSpec) String() string {
return string(spec.Bytes())
}
var datastores map[string]ConfigFromMap
func init() {
datastores = map[string]ConfigFromMap{
"mount": MountDatastoreConfig,
"mem": MemDatastoreConfig,
"log": LogDatastoreConfig,
"measure": MeasureDatastoreConfig,
}
}
func AddDatastoreConfigHandler(name string, dsc ConfigFromMap) error {
_, ok := datastores[name]
if ok {
return fmt.Errorf("already have a datastore named %q", name)
}
datastores[name] = dsc
return nil
}
// AnyDatastoreConfig returns a DatastoreConfig from a spec based on
// the "type" parameter
func AnyDatastoreConfig(params map[string]interface{}) (DatastoreConfig, error) {
which, ok := params["type"].(string)
if !ok {
return nil, fmt.Errorf("'type' field missing or not a string")
}
fun, ok := datastores[which]
if !ok {
return nil, fmt.Errorf("unknown datastore type: %s", which)
}
return fun(params)
}
type mountDatastoreConfig struct {
mounts []premount
}
type premount struct {
ds DatastoreConfig
prefix ds.Key
}
// MountDatastoreConfig returns a mount DatastoreConfig from a spec
func MountDatastoreConfig(params map[string]interface{}) (DatastoreConfig, error) {
var res mountDatastoreConfig
mounts, ok := params["mounts"].([]interface{})
if !ok {
return nil, fmt.Errorf("'mounts' field is missing or not an array")
}
for _, iface := range mounts {
cfg, ok := iface.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("expected map for mountpoint")
}
child, err := AnyDatastoreConfig(cfg)
if err != nil {
return nil, err
}
prefix, found := cfg["mountpoint"]
if !found {
return nil, fmt.Errorf("no 'mountpoint' on mount")
}
res.mounts = append(res.mounts, premount{
ds: child,
prefix: ds.NewKey(prefix.(string)),
})
}
sort.Slice(res.mounts,
func(i, j int) bool {
return res.mounts[i].prefix.String() > res.mounts[j].prefix.String()
})
return &res, nil
}
func (c *mountDatastoreConfig) DiskSpec() DiskSpec {
cfg := map[string]interface{}{"type": "mount"}
mounts := make([]interface{}, len(c.mounts))
for i, m := range c.mounts {
c := m.ds.DiskSpec()
if c == nil {
c = make(map[string]interface{})
}
c["mountpoint"] = m.prefix.String()
mounts[i] = c
}
cfg["mounts"] = mounts
return cfg
}
func (c *mountDatastoreConfig) Create(path string) (repo.Datastore, error) {
mounts := make([]mount.Mount, len(c.mounts))
for i, m := range c.mounts {
ds, err := m.ds.Create(path)
if err != nil {
return nil, err
}
mounts[i].Datastore = ds
mounts[i].Prefix = m.prefix
}
return mount.New(mounts), nil
}
type memDatastoreConfig struct {
cfg map[string]interface{}
}
// MemDatastoreConfig returns a memory DatastoreConfig from a spec
func MemDatastoreConfig(params map[string]interface{}) (DatastoreConfig, error) {
return &memDatastoreConfig{params}, nil
}
func (c *memDatastoreConfig) DiskSpec() DiskSpec {
return nil
}
func (c *memDatastoreConfig) Create(string) (repo.Datastore, error) {
return ds.NewMapDatastore(), nil
}
type logDatastoreConfig struct {
child DatastoreConfig
name string
}
// LogDatastoreConfig returns a log DatastoreConfig from a spec
func LogDatastoreConfig(params map[string]interface{}) (DatastoreConfig, error) {
childField, ok := params["child"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("'child' field is missing or not a map")
}
child, err := AnyDatastoreConfig(childField)
if err != nil {
return nil, err
}
name, ok := params["name"].(string)
if !ok {
return nil, fmt.Errorf("'name' field was missing or not a string")
}
return &logDatastoreConfig{child, name}, nil
}
func (c *logDatastoreConfig) Create(path string) (repo.Datastore, error) {
child, err := c.child.Create(path)
if err != nil {
return nil, err
}
return ds.NewLogDatastore(child, c.name), nil
}
func (c *logDatastoreConfig) DiskSpec() DiskSpec {
return c.child.DiskSpec()
}
type measureDatastoreConfig struct {
child DatastoreConfig
prefix string
}
// MeasureDatastoreConfig returns a measure DatastoreConfig from a spec
func MeasureDatastoreConfig(params map[string]interface{}) (DatastoreConfig, error) {
childField, ok := params["child"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("'child' field is missing or not a map")
}
child, err := AnyDatastoreConfig(childField)
if err != nil {
return nil, err
}
prefix, ok := params["prefix"].(string)
if !ok {
return nil, fmt.Errorf("'prefix' field was missing or not a string")
}
return &measureDatastoreConfig{child, prefix}, nil
}
func (c *measureDatastoreConfig) DiskSpec() DiskSpec {
return c.child.DiskSpec()
}
func (c measureDatastoreConfig) Create(path string) (repo.Datastore, error) {
child, err := c.child.Create(path)
if err != nil {
return nil, err
}
return measure.New(c.prefix, child), nil
}