Commit 7c2d481e authored by Jeromy's avatar Jeromy

Use a bitswap session for 'Cat'

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent 129535d0
package merkledag
import (
"context"
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)
// ErrorService implements ipld.DAGService, returning 'Err' for every call.
type ErrorService struct {
Err error
}
var _ ipld.DAGService = (*ErrorService)(nil)
func (cs *ErrorService) Add(ctx context.Context, nd ipld.Node) error {
return cs.Err
}
func (cs *ErrorService) AddMany(ctx context.Context, nds []ipld.Node) error {
return cs.Err
}
func (cs *ErrorService) Get(ctx context.Context, c *cid.Cid) (ipld.Node, error) {
return nil, cs.Err
}
func (cs *ErrorService) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *ipld.NodeOption {
ch := make(chan *ipld.NodeOption)
close(ch)
return ch
}
func (cs *ErrorService) Remove(ctx context.Context, c *cid.Cid) error {
return cs.Err
}
func (cs *ErrorService) RemoveMany(ctx context.Context, cids []*cid.Cid) error {
return cs.Err
}
......@@ -146,6 +146,10 @@ func (sg *sesGetter) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *ipld.
return getNodesFromBG(ctx, sg.bs, keys)
}
func (ds *dagService) Session(ctx context.Context) ipld.NodeGetter {
return &sesGetter{bserv.NewSession(ctx, ds.Blocks)}
}
// FetchGraph fetches all nodes that are children of the given node
func FetchGraph(ctx context.Context, root *cid.Cid, serv ipld.DAGService) error {
var ng ipld.NodeGetter = serv
......
package merkledag
import (
"fmt"
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)
var ErrReadOnly = fmt.Errorf("cannot write to readonly DAGService")
func NewReadOnlyDagService(ng ipld.NodeGetter) ipld.DAGService {
return &ComboService{
Read: ng,
Write: &ErrorService{ErrReadOnly},
}
}
package merkledag_test
import (
"context"
"testing"
. "github.com/ipfs/go-ipfs/merkledag"
dstest "github.com/ipfs/go-ipfs/merkledag/test"
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)
func TestReadonlyProperties(t *testing.T) {
ds := dstest.Mock()
ro := NewReadOnlyDagService(ds)
ctx := context.Background()
nds := []ipld.Node{
NewRawNode([]byte("foo1")),
NewRawNode([]byte("foo2")),
NewRawNode([]byte("foo3")),
NewRawNode([]byte("foo4")),
}
cids := []*cid.Cid{
nds[0].Cid(),
nds[1].Cid(),
nds[2].Cid(),
nds[3].Cid(),
}
// add to the actual underlying datastore
if err := ds.Add(ctx, nds[2]); err != nil {
t.Fatal(err)
}
if err := ds.Add(ctx, nds[3]); err != nil {
t.Fatal(err)
}
if err := ro.Add(ctx, nds[0]); err != ErrReadOnly {
t.Fatal("expected ErrReadOnly")
}
if err := ro.Add(ctx, nds[2]); err != ErrReadOnly {
t.Fatal("expected ErrReadOnly")
}
if err := ro.AddMany(ctx, nds[0:1]); err != ErrReadOnly {
t.Fatal("expected ErrReadOnly")
}
if err := ro.Remove(ctx, cids[3]); err != ErrReadOnly {
t.Fatal("expected ErrReadOnly")
}
if err := ro.RemoveMany(ctx, cids[1:2]); err != ErrReadOnly {
t.Fatal("expected ErrReadOnly")
}
if _, err := ro.Get(ctx, cids[0]); err != ipld.ErrNotFound {
t.Fatal("expected ErrNotFound")
}
if _, err := ro.Get(ctx, cids[3]); err != nil {
t.Fatal(err)
}
}
package merkledag
import (
"context"
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)
// ComboService implements ipld.DAGService, using 'Read' for all fetch methods,
// and 'Write' for all methods that add new objects.
type ComboService struct {
Read ipld.NodeGetter
Write ipld.DAGService
}
var _ ipld.DAGService = (*ComboService)(nil)
func (cs *ComboService) Add(ctx context.Context, nd ipld.Node) error {
return cs.Write.Add(ctx, nd)
}
func (cs *ComboService) AddMany(ctx context.Context, nds []ipld.Node) error {
return cs.Write.AddMany(ctx, nds)
}
func (cs *ComboService) Get(ctx context.Context, c *cid.Cid) (ipld.Node, error) {
return cs.Read.Get(ctx, c)
}
func (cs *ComboService) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *ipld.NodeOption {
return cs.Read.GetMany(ctx, cids)
}
func (cs *ComboService) Remove(ctx context.Context, c *cid.Cid) error {
return cs.Write.Remove(ctx, c)
}
func (cs *ComboService) RemoveMany(ctx context.Context, cids []*cid.Cid) error {
return cs.Write.RemoveMany(ctx, cids)
}
package merkledag
import (
"context"
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)
type SessionMaker interface {
Session(context.Context) ipld.NodeGetter
}
func NewSession(ctx context.Context, g ipld.NodeGetter) ipld.NodeGetter {
if sm, ok := g.(SessionMaker); ok {
return sm.Session(ctx)
}
return g
}
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