Skip to content

Commit 0c224c7

Browse files
committed
better json format
License: MIT Signed-off-by: Jeromy <[email protected]>
1 parent bc6d256 commit 0c224c7

File tree

4 files changed

+71
-92
lines changed

4 files changed

+71
-92
lines changed

repo/config/datastore.go

+4-20
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
11
package config
22

3-
import (
4-
"encoding/json"
5-
)
6-
73
// DefaultDataStoreDirectory is the directory to store all the local IPFS data.
84
const DefaultDataStoreDirectory = "datastore"
95

106
// Datastore tracks the configuration of the datastore.
117
type Datastore struct {
12-
Type string
13-
Path string
148
StorageMax string // in B, kB, kiB, MB, ...
159
StorageGCWatermark int64 // in percentage to multiply on StorageMax
1610
GCPeriod string // in ns, us, ms, s, m, h
11+
Path string
12+
NoSync bool // deprecated
13+
14+
Spec map[string]interface{}
1715

18-
Params *json.RawMessage
19-
NoSync bool
2016
HashOnRead bool
2117
BloomFilterSize int
2218
}
2319

24-
func (d *Datastore) ParamData() []byte {
25-
if d.Params == nil {
26-
return nil
27-
}
28-
29-
return []byte(*d.Params)
30-
}
31-
3220
type S3Datastore struct {
3321
Region string `json:"region"`
3422
Bucket string `json:"bucket"`
@@ -46,10 +34,6 @@ type LevelDB struct {
4634
Compression string
4735
}
4836

49-
type MeasureDB struct {
50-
Label string
51-
}
52-
5337
// DataStorePath returns the default data store path given a configuration root
5438
// (set an empty string to have the default configuration root)
5539
func DataStorePath(configroot string) (string, error) {

repo/config/init.go

+29-10
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
4040
Gateway: "/ip4/127.0.0.1/tcp/8080",
4141
},
4242

43-
Datastore: datastore,
43+
Datastore: *datastore,
4444
Bootstrap: BootstrapPeerStrings(bootstrapPeers),
4545
Identity: identity,
4646
Discovery: Discovery{MDNS{
@@ -76,19 +76,38 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
7676
return conf, nil
7777
}
7878

79-
func datastoreConfig() (Datastore, error) {
80-
dspath, err := DataStorePath("")
81-
if err != nil {
82-
return Datastore{}, err
83-
}
84-
return Datastore{
85-
Path: dspath,
86-
Type: "leveldb",
79+
func datastoreConfig() (*Datastore, error) {
80+
return &Datastore{
8781
StorageMax: "10GB",
8882
StorageGCWatermark: 90, // 90%
8983
GCPeriod: "1h",
90-
HashOnRead: false,
9184
BloomFilterSize: 0,
85+
Spec: map[string]interface{}{
86+
"type": "mount",
87+
"mounts": []interface{}{
88+
map[string]interface{}{
89+
"mountpoint": "/blocks",
90+
"type": "measure",
91+
"prefix": "flatfs.datastore",
92+
"child": map[string]interface{}{
93+
"type": "flatfs",
94+
"path": "blocks",
95+
"nosync": false,
96+
"prefixLen": 4,
97+
},
98+
},
99+
map[string]interface{}{
100+
"mountpoint": "/",
101+
"type": "measure",
102+
"prefix": "leveldb.datastore",
103+
"child": map[string]interface{}{
104+
"type": "levelds",
105+
"path": "datastore",
106+
"compression": "none",
107+
},
108+
},
109+
},
110+
},
92111
}, nil
93112
}
94113

repo/fsrepo/datastores.go

+32-55
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"path/filepath"
77

88
repo "github.com/ipfs/go-ipfs/repo"
9-
config "github.com/ipfs/go-ipfs/repo/config"
109

1110
ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
1211
mount "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore/syncmount"
@@ -16,70 +15,40 @@ import (
1615
"gx/ipfs/Qmbx2KUs8mUbDUiiESzC1ms7mdmh4pRu8X1V1tffC46M4n/go-ds-flatfs"
1716
)
1817

19-
func (r *FSRepo) constructDatastore(kind string, params []byte) (repo.Datastore, error) {
20-
switch kind {
18+
func (r *FSRepo) constructDatastore(params map[string]interface{}) (repo.Datastore, error) {
19+
switch params["type"] {
2120
case "mount":
22-
var mounts []*mountConfig
23-
if err := json.Unmarshal(params, &mounts); err != nil {
24-
return nil, fmt.Errorf("datastore mount: %v", err)
21+
mounts, ok := params["mounts"].([]interface{})
22+
if !ok {
23+
return nil, fmt.Errorf("mounts field wasnt an array")
2524
}
2625

2726
return r.openMountDatastore(mounts)
2827
case "flatfs":
29-
var flatfsparams config.FlatDS
30-
if err := json.Unmarshal(params, &flatfsparams); err != nil {
31-
return nil, fmt.Errorf("datastore flatfs: %v", err)
32-
}
33-
34-
return r.openFlatfsDatastore(&flatfsparams)
28+
return r.openFlatfsDatastore(params)
3529
case "mem":
3630
return ds.NewMapDatastore(), nil
3731
case "log":
38-
var cfg struct {
39-
Name string
40-
ChildType string
41-
Child *json.RawMessage
42-
}
43-
44-
if err := json.Unmarshal(params, &cfg); err != nil {
45-
return nil, fmt.Errorf("datastore measure: %v", err)
46-
}
47-
48-
child, err := r.constructDatastore(cfg.ChildType, []byte(*cfg.Child))
32+
child, err := r.constructDatastore(params["child"].(map[string]interface{}))
4933
if err != nil {
5034
return nil, err
5135
}
5236

53-
return ds.NewLogDatastore(child, cfg.Name), nil
54-
37+
return ds.NewLogDatastore(child, params["name"].(string)), nil
5538
case "measure":
56-
var measureOpts struct {
57-
Prefix string
58-
ChildType string
59-
Child *json.RawMessage
60-
}
61-
62-
if err := json.Unmarshal(params, &measureOpts); err != nil {
63-
return nil, fmt.Errorf("datastore measure: %v", err)
64-
}
65-
66-
child, err := r.constructDatastore(measureOpts.ChildType, []byte(*measureOpts.Child))
39+
child, err := r.constructDatastore(params["child"].(map[string]interface{}))
6740
if err != nil {
6841
return nil, err
6942
}
7043

71-
return r.openMeasureDB(measureOpts.Prefix, child)
72-
73-
case "levelds":
74-
var c config.LevelDB
75-
if err := json.Unmarshal(params, &c); err != nil {
76-
return nil, fmt.Errorf("datastore levelds: %v", err)
77-
}
44+
prefix := params["prefix"].(string)
7845

79-
return r.openLeveldbDatastore(&c)
46+
return r.openMeasureDB(prefix, child)
8047

48+
case "levelds":
49+
return r.openLeveldbDatastore(params)
8150
default:
82-
return nil, fmt.Errorf("unknown datastore type: %s", kind)
51+
return nil, fmt.Errorf("unknown datastore type: %s", params["type"])
8352
}
8453
}
8554

@@ -89,40 +58,48 @@ type mountConfig struct {
8958
Child *json.RawMessage
9059
}
9160

92-
func (r *FSRepo) openMountDatastore(mountcfg []*mountConfig) (repo.Datastore, error) {
61+
func (r *FSRepo) openMountDatastore(mountcfg []interface{}) (repo.Datastore, error) {
9362
var mounts []mount.Mount
94-
for _, cfg := range mountcfg {
63+
for _, iface := range mountcfg {
64+
cfg := iface.(map[string]interface{})
9565

96-
child, err := r.constructDatastore(cfg.ChildType, []byte(*cfg.Child))
66+
child, err := r.constructDatastore(cfg)
9767
if err != nil {
9868
return nil, err
9969
}
10070

71+
prefix, found := cfg["mountpoint"]
72+
if !found {
73+
return nil, fmt.Errorf("no 'mountpoint' on mount")
74+
}
75+
10176
mounts = append(mounts, mount.Mount{
10277
Datastore: child,
103-
Prefix: ds.NewKey(cfg.Path),
78+
Prefix: ds.NewKey(prefix.(string)),
10479
})
10580
}
10681

10782
return mount.New(mounts), nil
10883
}
10984

110-
func (r *FSRepo) openFlatfsDatastore(params *config.FlatDS) (repo.Datastore, error) {
111-
p := params.Path
85+
func (r *FSRepo) openFlatfsDatastore(params map[string]interface{}) (repo.Datastore, error) {
86+
p := params["path"].(string)
11287
if !filepath.IsAbs(p) {
11388
p = filepath.Join(r.path, p)
11489
}
115-
return flatfs.New(p, params.PrefixLen, params.Sync)
90+
91+
plen := int(params["prefixLen"].(float64))
92+
return flatfs.New(p, plen, params["nosync"].(bool))
11693
}
11794

118-
func (r *FSRepo) openLeveldbDatastore(params *config.LevelDB) (repo.Datastore, error) {
119-
p := params.Path
95+
func (r *FSRepo) openLeveldbDatastore(params map[string]interface{}) (repo.Datastore, error) {
96+
p := params["path"].(string)
12097
if !filepath.IsAbs(p) {
12198
p = filepath.Join(r.path, p)
12299
}
123100

124101
var c ldbopts.Compression
125-
switch params.Compression {
102+
switch params["compression"].(string) {
126103
case "none":
127104
c = ldbopts.NoCompression
128105
case "snappy":

repo/fsrepo/fsrepo.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -353,20 +353,19 @@ func (r *FSRepo) openKeystore() error {
353353

354354
// openDatastore returns an error if the config file is not present.
355355
func (r *FSRepo) openDatastore() error {
356-
switch r.config.Datastore.Type {
357-
case "default", "leveldb", "":
358-
// TODO: This is for legacy configs, remove in the future
359-
d, err := openDefaultDatastore(r)
356+
if r.config.Datastore.Spec != nil {
357+
d, err := r.constructDatastore(r.config.Datastore.Spec)
360358
if err != nil {
361359
return err
362360
}
361+
363362
r.ds = d
364-
default:
365-
d, err := r.constructDatastore(r.config.Datastore.Type, r.config.Datastore.ParamData())
363+
} else {
364+
// TODO: This is for legacy configs, remove in the future
365+
d, err := openDefaultDatastore(r)
366366
if err != nil {
367367
return err
368368
}
369-
370369
r.ds = d
371370
}
372371

0 commit comments

Comments
 (0)