-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:jbenet/go-ipfs
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package blockstore | ||
|
||
import ( | ||
"errors" | ||
|
||
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go" | ||
|
||
blocks "github.com/jbenet/go-ipfs/blocks" | ||
u "github.com/jbenet/go-ipfs/util" | ||
) | ||
|
||
var ValueTypeMismatch = errors.New("The retrieved value is not a Block") | ||
|
||
type Blockstore interface { | ||
Get(u.Key) (*blocks.Block, error) | ||
Put(blocks.Block) error | ||
} | ||
|
||
func NewBlockstore(d ds.Datastore) Blockstore { | ||
return &blockstore{ | ||
datastore: d, | ||
} | ||
} | ||
|
||
type blockstore struct { | ||
datastore ds.Datastore | ||
} | ||
|
||
func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) { | ||
maybeData, err := bs.datastore.Get(toDatastoreKey(k)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bdata, ok := maybeData.([]byte) | ||
if !ok { | ||
return nil, ValueTypeMismatch | ||
} | ||
return blocks.NewBlock(bdata) | ||
} | ||
|
||
func (bs *blockstore) Put(block blocks.Block) error { | ||
return bs.datastore.Put(toDatastoreKey(block.Key()), block.Data) | ||
} | ||
|
||
func toDatastoreKey(k u.Key) ds.Key { | ||
return ds.NewKey(string(k)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package blockstore | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go" | ||
u "github.com/jbenet/go-ipfs/util" | ||
testutil "github.com/jbenet/go-ipfs/util/testutil" | ||
) | ||
|
||
// TODO(brian): TestGetReturnsNil | ||
|
||
func TestGetWhenKeyNotPresent(t *testing.T) { | ||
bs := NewBlockstore(ds.NewMapDatastore()) | ||
_, err := bs.Get(u.Key("not present")) | ||
|
||
if err != nil { | ||
t.Log("As expected, block is not present") | ||
return | ||
} | ||
t.Fail() | ||
} | ||
|
||
func TestPutThenGetBlock(t *testing.T) { | ||
bs := NewBlockstore(ds.NewMapDatastore()) | ||
block := testutil.NewBlockOrFail(t, "some data") | ||
|
||
err := bs.Put(block) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
blockFromBlockstore, err := bs.Get(block.Key()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Equal(block.Data, blockFromBlockstore.Data) { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestValueTypeMismatch(t *testing.T) { | ||
block := testutil.NewBlockOrFail(t, "some data") | ||
|
||
datastore := ds.NewMapDatastore() | ||
datastore.Put(toDatastoreKey(block.Key()), "data that isn't a block!") | ||
|
||
blockstore := NewBlockstore(datastore) | ||
|
||
_, err := blockstore.Get(block.Key()) | ||
if err != ValueTypeMismatch { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package testutil | ||
|
||
import ( | ||
"testing" | ||
|
||
blocks "github.com/jbenet/go-ipfs/blocks" | ||
) | ||
|
||
// NewBlockOrFail returns a block created from msgData. Signals test failure if | ||
// creation fails. | ||
// | ||
// NB: NewBlockOrFail accepts a msgData parameter to avoid non-determinism in | ||
// tests. Generating random block data could potentially result in unexpected | ||
// behavior in tests. Thus, it is left up to the caller to select the msgData | ||
// that will determine the blocks key. | ||
func NewBlockOrFail(t *testing.T, msgData string) blocks.Block { | ||
block, blockCreationErr := blocks.NewBlock([]byte(msgData)) | ||
if blockCreationErr != nil { | ||
t.Fatal(blockCreationErr) | ||
} | ||
return *block | ||
} |