@cryptotaxi247 / kubo / commits / 6396123b7

feat(fsrepo): expose Datastore in FSRepo interface (+ test)

Brian Tiger Chow committed Jan 14, 2015 at 09:13 UTC 6396123b7fe813c87328f765c8e06765adfb4cb8
2 files changed +53 -2
repo/fsrepo/fsrepo.go
+11 -2
@@ -8,6 +8,7 @@ import (
8 "path"
9 "sync"
10
11 + ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
12 repo "github.com/jbenet/go-ipfs/repo"
13 config "github.com/jbenet/go-ipfs/repo/config"
14 component "github.com/jbenet/go-ipfs/repo/fsrepo/component"
@@ -51,8 +52,7 @@ type FSRepo struct {
52 // configComponent is loaded when FSRepo is opened and kept up to date when
53 // the FSRepo is modified.
54 // TODO test
54 - configComponent component.ConfigComponent
55 - // TODO test
55 + configComponent component.ConfigComponent
56 datastoreComponent component.DatastoreComponent
57 }
58
@@ -240,6 +240,15 @@ func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
240 return r.configComponent.SetConfigKey(key, value)
241 }
242
243 +// Datastore returns a repo-owned datastore. If FSRepo is Closed, return value
244 +// is undefined.
245 +func (r *FSRepo) Datastore() ds.ThreadSafeDatastore {
246 + packageLock.Lock()
247 + d := r.datastoreComponent.Datastore()
248 + packageLock.Unlock()
249 + return d
250 +}
251 +
252 var _ io.Closer = &FSRepo{}
253 var _ repo.Repo = &FSRepo{}
254
repo/fsrepo/fsrepo_test.go
+42
@@ -1,12 +1,15 @@
1 package fsrepo
2
3 import (
4 + "bytes"
5 "io/ioutil"
6 "testing"
7
8 + datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
9 "github.com/jbenet/go-ipfs/repo/config"
10 )
11
12 +// swap arg order
13 func testRepoPath(p string, t *testing.T) string {
14 name, err := ioutil.TempDir("", p)
15 if err != nil {
@@ -76,6 +79,45 @@ func TestCanManageReposIndependently(t *testing.T) {
79 AssertNil(Remove(pathA), t)
80 }
81
82 +func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
83 + path := testRepoPath("test", t)
84 +
85 + Assert(!IsInitialized(path), t, "should NOT be initialized")
86 + AssertNil(Init(path, &config.Config{}), t, "should initialize successfully")
87 + r := At(path)
88 + AssertNil(r.Open(), t, "should open successfully")
89 +
90 + k := "key"
91 + data := []byte(k)
92 + AssertNil(r.Datastore().Put(datastore.NewKey(k), data), t, "Put should be successful")
93 +
94 + AssertNil(r.Close(), t)
95 + _, err := r.Datastore().Get(datastore.NewKey(k))
96 + AssertErr(err, t, "after closer, Get should be fail")
97 +}
98 +
99 +func TestDatastorePersistsFromRepoToRepo(t *testing.T) {
100 + path := testRepoPath("test", t)
101 +
102 + AssertNil(Init(path, &config.Config{}), t)
103 + r1 := At(path)
104 + AssertNil(r1.Open(), t)
105 +
106 + k := "key"
107 + expected := []byte(k)
108 + AssertNil(r1.Datastore().Put(datastore.NewKey(k), expected), t, "using first repo, Put should be successful")
109 + AssertNil(r1.Close(), t)
110 +
111 + r2 := At(path)
112 + AssertNil(r2.Open(), t)
113 + v, err := r2.Datastore().Get(datastore.NewKey(k))
114 + AssertNil(err, t, "using second repo, Get should be successful")
115 + actual, ok := v.([]byte)
116 + Assert(ok, t, "value should be the []byte from r1's Put")
117 + AssertNil(r2.Close(), t)
118 + Assert(bytes.Compare(expected, actual) == 0, t, "data should match")
119 +}
120 +
121 func AssertNil(err error, t *testing.T, msgs ...string) {
122 if err != nil {
123 t.Fatal(msgs, "error:", err)