From 42514f2ed77eb9523b560c9d96ed0d493c69b2ee Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Tue, 16 Sep 2014 03:31:35 -0700 Subject: [PATCH 1/3] feat(blockstore): implement blockstore --- bitswap/blockstore/blockstore.go | 47 +++++++++++++++++++++++ bitswap/blockstore/blockstore_test.go | 55 +++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 bitswap/blockstore/blockstore.go create mode 100644 bitswap/blockstore/blockstore_test.go diff --git a/bitswap/blockstore/blockstore.go b/bitswap/blockstore/blockstore.go new file mode 100644 index 00000000000..a4fc1f65d85 --- /dev/null +++ b/bitswap/blockstore/blockstore.go @@ -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)) +} diff --git a/bitswap/blockstore/blockstore_test.go b/bitswap/blockstore/blockstore_test.go new file mode 100644 index 00000000000..4b0909d7547 --- /dev/null +++ b/bitswap/blockstore/blockstore_test.go @@ -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) + } +} From be835662f24852ca374a61d27c8f816c8c3c2bf2 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Tue, 16 Sep 2014 03:53:57 -0700 Subject: [PATCH 2/3] style(blockstore) move to top-level --- {bitswap/blockstore => blockstore}/blockstore.go | 0 {bitswap/blockstore => blockstore}/blockstore_test.go | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {bitswap/blockstore => blockstore}/blockstore.go (100%) rename {bitswap/blockstore => blockstore}/blockstore_test.go (100%) diff --git a/bitswap/blockstore/blockstore.go b/blockstore/blockstore.go similarity index 100% rename from bitswap/blockstore/blockstore.go rename to blockstore/blockstore.go diff --git a/bitswap/blockstore/blockstore_test.go b/blockstore/blockstore_test.go similarity index 100% rename from bitswap/blockstore/blockstore_test.go rename to blockstore/blockstore_test.go From 697625a9e5f405bcc5cb106a05b66c74698d1054 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Tue, 16 Sep 2014 04:03:30 -0700 Subject: [PATCH 3/3] feat(util:testutil) add NewBlockOrFail(t, data) factory method for use in tests --- util/testutil/blocks.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 util/testutil/blocks.go diff --git a/util/testutil/blocks.go b/util/testutil/blocks.go new file mode 100644 index 00000000000..05b69239998 --- /dev/null +++ b/util/testutil/blocks.go @@ -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 +}