Commit 6396123b authored by Brian Tiger Chow's avatar Brian Tiger Chow

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

parent b685f92c
......@@ -8,6 +8,7 @@ import (
"path"
"sync"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
repo "github.com/jbenet/go-ipfs/repo"
config "github.com/jbenet/go-ipfs/repo/config"
component "github.com/jbenet/go-ipfs/repo/fsrepo/component"
......@@ -51,8 +52,7 @@ type FSRepo struct {
// configComponent is loaded when FSRepo is opened and kept up to date when
// the FSRepo is modified.
// TODO test
configComponent component.ConfigComponent
// TODO test
configComponent component.ConfigComponent
datastoreComponent component.DatastoreComponent
}
......@@ -240,6 +240,15 @@ func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
return r.configComponent.SetConfigKey(key, value)
}
// Datastore returns a repo-owned datastore. If FSRepo is Closed, return value
// is undefined.
func (r *FSRepo) Datastore() ds.ThreadSafeDatastore {
packageLock.Lock()
d := r.datastoreComponent.Datastore()
packageLock.Unlock()
return d
}
var _ io.Closer = &FSRepo{}
var _ repo.Repo = &FSRepo{}
......
package fsrepo
import (
"bytes"
"io/ioutil"
"testing"
datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
"github.com/jbenet/go-ipfs/repo/config"
)
// swap arg order
func testRepoPath(p string, t *testing.T) string {
name, err := ioutil.TempDir("", p)
if err != nil {
......@@ -76,6 +79,45 @@ func TestCanManageReposIndependently(t *testing.T) {
AssertNil(Remove(pathA), t)
}
func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
path := testRepoPath("test", t)
Assert(!IsInitialized(path), t, "should NOT be initialized")
AssertNil(Init(path, &config.Config{}), t, "should initialize successfully")
r := At(path)
AssertNil(r.Open(), t, "should open successfully")
k := "key"
data := []byte(k)
AssertNil(r.Datastore().Put(datastore.NewKey(k), data), t, "Put should be successful")
AssertNil(r.Close(), t)
_, err := r.Datastore().Get(datastore.NewKey(k))
AssertErr(err, t, "after closer, Get should be fail")
}
func TestDatastorePersistsFromRepoToRepo(t *testing.T) {
path := testRepoPath("test", t)
AssertNil(Init(path, &config.Config{}), t)
r1 := At(path)
AssertNil(r1.Open(), t)
k := "key"
expected := []byte(k)
AssertNil(r1.Datastore().Put(datastore.NewKey(k), expected), t, "using first repo, Put should be successful")
AssertNil(r1.Close(), t)
r2 := At(path)
AssertNil(r2.Open(), t)
v, err := r2.Datastore().Get(datastore.NewKey(k))
AssertNil(err, t, "using second repo, Get should be successful")
actual, ok := v.([]byte)
Assert(ok, t, "value should be the []byte from r1's Put")
AssertNil(r2.Close(), t)
Assert(bytes.Compare(expected, actual) == 0, t, "data should match")
}
func AssertNil(err error, t *testing.T, msgs ...string) {
if err != nil {
t.Fatal(msgs, "error:", err)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment