address p.r. comments
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
Kevin Atkinson committed
Jun 12, 2017 at 21:43 UTC
ee935aba659c4fafacfd7c92cf0df09b3d2168f3
3 files changed
+45
-25
repo/config/init.go
+3
-3
@@ -39,7 +39,7 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
39
Gateway: "/ip4/127.0.0.1/tcp/8080",
40
},
41
42
- Datastore: *datastore,
42
+ Datastore: datastore,
43
Bootstrap: BootstrapPeerStrings(bootstrapPeers),
44
Identity: identity,
45
Discovery: Discovery{MDNS{
@@ -77,8 +77,8 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
77
}
78
79
// DatastoreConfig is an internal function exported to aid in testing.
80
-func DefaultDatastoreConfig() *Datastore {
81
- return &Datastore{
80
+func DefaultDatastoreConfig() Datastore {
81
+ return Datastore{
82
StorageMax: "10GB",
83
StorageGCWatermark: 90, // 90%
84
GCPeriod: "1h",
repo/fsrepo/datastores.go
+37
-17
@@ -1,7 +1,6 @@
1
package fsrepo
2
3
import (
4
- "encoding/json"
4
"fmt"
5
"path/filepath"
6
@@ -20,7 +19,7 @@ func (r *FSRepo) constructDatastore(params map[string]interface{}) (repo.Datasto
19
case "mount":
20
mounts, ok := params["mounts"].([]interface{})
21
if !ok {
23
- return nil, fmt.Errorf("mounts field wasnt an array")
22
+ return nil, fmt.Errorf("'mounts' field is missing or not an array")
23
}
24
25
return r.openMountDatastore(mounts)
@@ -29,19 +28,33 @@ func (r *FSRepo) constructDatastore(params map[string]interface{}) (repo.Datasto
28
case "mem":
29
return ds.NewMapDatastore(), nil
30
case "log":
32
- child, err := r.constructDatastore(params["child"].(map[string]interface{}))
31
+ childField, ok := params["child"].(map[string]interface{})
32
+ if !ok {
33
+ return nil, fmt.Errorf("'child' field is missing or not a map")
34
+ }
35
+ child, err := r.constructDatastore(childField)
36
if err != nil {
37
return nil, err
38
}
36
-
37
- return ds.NewLogDatastore(child, params["name"].(string)), nil
39
+ nameField, ok := params["name"].(string)
40
+ if !ok {
41
+ return nil, fmt.Errorf("'name' field was missing or not a string")
42
+ }
43
+ return ds.NewLogDatastore(child, nameField), nil
44
case "measure":
39
- child, err := r.constructDatastore(params["child"].(map[string]interface{}))
45
+ childField, ok := params["child"].(map[string]interface{})
46
+ if !ok {
47
+ return nil, fmt.Errorf("'child' field was missing or not a map")
48
+ }
49
+ child, err := r.constructDatastore(childField)
50
if err != nil {
51
return nil, err
52
}
53
44
- prefix := params["prefix"].(string)
54
+ prefix, ok := params["prefix"].(string)
55
+ if !ok {
56
+ return nil, fmt.Errorf("'prefix' field was missing or not a string")
57
+ }
58
59
return r.openMeasureDB(prefix, child)
60
@@ -52,12 +65,6 @@ func (r *FSRepo) constructDatastore(params map[string]interface{}) (repo.Datasto
65
}
66
}
67
55
-type mountConfig struct {
56
- Path string
57
- ChildType string
58
- Child *json.RawMessage
59
-}
60
-
68
func (r *FSRepo) openMountDatastore(mountcfg []interface{}) (repo.Datastore, error) {
69
var mounts []mount.Mount
70
for _, iface := range mountcfg {
@@ -83,22 +90,35 @@ func (r *FSRepo) openMountDatastore(mountcfg []interface{}) (repo.Datastore, err
90
}
91
92
func (r *FSRepo) openFlatfsDatastore(params map[string]interface{}) (repo.Datastore, error) {
86
- p := params["path"].(string)
93
+ p, ok := params["path"].(string)
94
+ if !ok {
95
+ return nil, fmt.Errorf("'path' field is missing or not boolean")
96
+ }
97
if !filepath.IsAbs(p) {
98
p = filepath.Join(r.path, p)
99
}
100
91
- sshardFun := params["shardFunc"].(string)
101
+ sshardFun, ok := params["shardFunc"].(string)
102
+ if !ok {
103
+ return nil, fmt.Errorf("'shardFunc' field is missing or not a string")
104
+ }
105
shardFun, err := flatfs.ParseShardFunc(sshardFun)
106
if err != nil {
107
return nil, err
108
}
109
97
- return flatfs.CreateOrOpen(p, shardFun, params["sync"].(bool))
110
+ syncField, ok := params["sync"].(bool)
111
+ if !ok {
112
+ return nil, fmt.Errorf("'sync' field is missing or not boolean")
113
+ }
114
+ return flatfs.CreateOrOpen(p, shardFun, syncField)
115
}
116
117
func (r *FSRepo) openLeveldbDatastore(params map[string]interface{}) (repo.Datastore, error) {
101
- p := params["path"].(string)
118
+ p, ok := params["path"].(string)
119
+ if !ok {
120
+ return nil, fmt.Errorf("'path' field is missing or not string")
121
+ }
122
if !filepath.IsAbs(p) {
123
p = filepath.Join(r.path, p)
124
}
repo/fsrepo/fsrepo_test.go
+5
-5
@@ -40,8 +40,8 @@ func TestCanManageReposIndependently(t *testing.T) {
40
pathB := testRepoPath("b", t)
41
42
t.Log("initialize two repos")
43
- assert.Nil(Init(pathA, &config.Config{Datastore: *config.DefaultDatastoreConfig()}), t, "a", "should initialize successfully")
44
- assert.Nil(Init(pathB, &config.Config{Datastore: *config.DefaultDatastoreConfig()}), t, "b", "should initialize successfully")
43
+ assert.Nil(Init(pathA, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t, "a", "should initialize successfully")
44
+ assert.Nil(Init(pathB, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t, "b", "should initialize successfully")
45
46
t.Log("ensure repos initialized")
47
assert.True(IsInitialized(pathA), t, "a should be initialized")
@@ -67,7 +67,7 @@ func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
67
path := testRepoPath("test", t)
68
69
assert.True(!IsInitialized(path), t, "should NOT be initialized")
70
- assert.Nil(Init(path, &config.Config{Datastore: *config.DefaultDatastoreConfig()}), t, "should initialize successfully")
70
+ assert.Nil(Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t, "should initialize successfully")
71
r, err := Open(path)
72
assert.Nil(err, t, "should open successfully")
73
@@ -84,7 +84,7 @@ func TestDatastorePersistsFromRepoToRepo(t *testing.T) {
84
t.Parallel()
85
path := testRepoPath("test", t)
86
87
- assert.Nil(Init(path, &config.Config{Datastore: *config.DefaultDatastoreConfig()}), t)
87
+ assert.Nil(Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t)
88
r1, err := Open(path)
89
assert.Nil(err, t)
90
@@ -106,7 +106,7 @@ func TestDatastorePersistsFromRepoToRepo(t *testing.T) {
106
func TestOpenMoreThanOnceInSameProcess(t *testing.T) {
107
t.Parallel()
108
path := testRepoPath("", t)
109
- assert.Nil(Init(path, &config.Config{Datastore: *config.DefaultDatastoreConfig()}), t)
109
+ assert.Nil(Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t)
110
111
r1, err := Open(path)
112
assert.Nil(err, t, "first repo should open successfully")