Commit ba37c07a authored by hannahhoward's avatar hannahhoward

feat(storeutil): construct loader & storer from store

Utility functions to construct loaders/storers from a go-blockstore
parent edbfd032
...@@ -4,13 +4,17 @@ go 1.12 ...@@ -4,13 +4,17 @@ go 1.12
require ( require (
github.com/btcsuite/btcd v0.0.0-20190629003639-c26ffa870fd8 // indirect github.com/btcsuite/btcd v0.0.0-20190629003639-c26ffa870fd8 // indirect
github.com/dgraph-io/badger v2.0.0-rc.2+incompatible // indirect
github.com/gogo/protobuf v1.2.1 github.com/gogo/protobuf v1.2.1
github.com/golang/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.3.2 // indirect
github.com/ipfs/go-block-format v0.0.2 github.com/ipfs/go-block-format v0.0.2
github.com/ipfs/go-cid v0.0.3 github.com/ipfs/go-cid v0.0.3
github.com/ipfs/go-datastore v0.0.5
github.com/ipfs/go-ipfs v0.4.21 // indirect
github.com/ipfs/go-ipfs-blockstore v0.0.1
github.com/ipfs/go-ipfs-blocksutil v0.0.1 github.com/ipfs/go-ipfs-blocksutil v0.0.1
github.com/ipfs/go-log v0.0.1 github.com/ipfs/go-log v0.0.1
github.com/ipfs/go-peertaskqueue v0.0.1 github.com/ipfs/go-peertaskqueue v0.0.4
github.com/ipld/go-ipld-prime v0.0.0-20190730002952-369bb56ad071 github.com/ipld/go-ipld-prime v0.0.0-20190730002952-369bb56ad071
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
github.com/libp2p/go-eventbus v0.0.3 // indirect github.com/libp2p/go-eventbus v0.0.3 // indirect
...@@ -18,12 +22,12 @@ require ( ...@@ -18,12 +22,12 @@ require (
github.com/libp2p/go-libp2p-core v0.0.9 github.com/libp2p/go-libp2p-core v0.0.9
github.com/libp2p/go-libp2p-net v0.1.0 // indirect github.com/libp2p/go-libp2p-net v0.1.0 // indirect
github.com/libp2p/go-libp2p-secio v0.1.1 // indirect github.com/libp2p/go-libp2p-secio v0.1.1 // indirect
github.com/libp2p/go-libp2p-transport v0.0.5 // indirect
github.com/libp2p/go-msgio v0.0.4 // indirect github.com/libp2p/go-msgio v0.0.4 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect github.com/mattn/go-colorable v0.1.2 // indirect
github.com/multiformats/go-multiaddr v0.0.4 github.com/multiformats/go-multiaddr v0.0.4
github.com/multiformats/go-multiaddr-dns v0.0.3 // indirect github.com/multiformats/go-multiaddr-dns v0.0.3 // indirect
github.com/multiformats/go-multihash v0.0.6 github.com/multiformats/go-multihash v0.0.6
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/smartystreets/goconvey v0.0.0-20190710185942-9d28bd7c0945 // indirect github.com/smartystreets/goconvey v0.0.0-20190710185942-9d28bd7c0945 // indirect
go.opencensus.io v0.22.0 // indirect go.opencensus.io v0.22.0 // indirect
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
......
This diff is collapsed.
package storeutil
import (
"bytes"
"fmt"
"io"
blocks "github.com/ipfs/go-block-format"
bstore "github.com/ipfs/go-ipfs-blockstore"
ipld "github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
)
// LoaderForBlockstore returns an IPLD Loader function compatible with graphsync
// from an IPFS blockstore
func LoaderForBlockstore(bs bstore.Blockstore) ipld.Loader {
return func(lnk ipld.Link, lnkCtx ipld.LinkContext) (io.Reader, error) {
asCidLink, ok := lnk.(cidlink.Link)
if !ok {
return nil, fmt.Errorf("Unsupported Link Type")
}
block, err := bs.Get(asCidLink.Cid)
if err != nil {
return nil, err
}
return bytes.NewReader(block.RawData()), nil
}
}
// StorerForBlockstore returns an IPLD Storer function compatible with graphsync
// from an IPFS blockstore
func StorerForBlockstore(bs bstore.Blockstore) ipld.Storer {
return func(lnkCtx ipld.LinkContext) (io.Writer, ipld.StoreCommitter, error) {
var buffer bytes.Buffer
committer := func(lnk ipld.Link) error {
asCidLink, ok := lnk.(cidlink.Link)
if !ok {
return fmt.Errorf("Unsupported Link Type")
}
block, err := blocks.NewBlockWithCid(buffer.Bytes(), asCidLink.Cid)
if err != nil {
return err
}
return bs.Put(block)
}
return &buffer, committer, nil
}
}
package storeutil
import (
"io/ioutil"
"testing"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-datastore"
dss "github.com/ipfs/go-datastore/sync"
bstore "github.com/ipfs/go-ipfs-blockstore"
ipld "github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipfs/go-graphsync/testutil"
)
func TestLoader(t *testing.T) {
store := bstore.NewBlockstore(dss.MutexWrap(datastore.NewMapDatastore()))
blk := testutil.GenerateBlocksOfSize(1, 1000)[0]
err := store.Put(blk)
if err != nil {
t.Fatal("Unable to put block to store")
}
loader := LoaderForBlockstore(store)
data, err := loader(cidlink.Link{Cid: blk.Cid()}, ipld.LinkContext{})
if err != nil {
t.Fatal("Unable to load block with loader")
}
bytes, err := ioutil.ReadAll(data)
if err != nil {
t.Fatal("Unable to read bytes from reader returned by loader")
}
returnedBlock := blocks.NewBlock(bytes)
if returnedBlock.Cid() != blk.Cid() {
t.Fatal("Did not return correct block with loader")
}
}
func TestStorer(t *testing.T) {
store := bstore.NewBlockstore(dss.MutexWrap(datastore.NewMapDatastore()))
blk := testutil.GenerateBlocksOfSize(1, 1000)[0]
storer := StorerForBlockstore(store)
buffer, commit, err := storer(ipld.LinkContext{})
if err != nil {
t.Fatal("Unable to setup buffer")
}
_, err = buffer.Write(blk.RawData())
if err != nil {
t.Fatal("Unable to write data to buffer")
}
err = commit(cidlink.Link{Cid: blk.Cid()})
if err != nil {
t.Fatal("Unable to commit with storer function")
}
_, err = store.Get(blk.Cid())
if err != nil {
t.Fatal("Block not written to store")
}
}
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