Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-unixfs
Commits
6396123b
Commit
6396123b
authored
Jan 14, 2015
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(fsrepo): expose Datastore in FSRepo interface (+ test)
parent
b685f92c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
2 deletions
+53
-2
repo/fsrepo/fsrepo.go
repo/fsrepo/fsrepo.go
+11
-2
repo/fsrepo/fsrepo_test.go
repo/fsrepo/fsrepo_test.go
+42
-0
No files found.
repo/fsrepo/fsrepo.go
View file @
6396123b
...
...
@@ -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
{}
...
...
repo/fsrepo/fsrepo_test.go
View file @
6396123b
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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment