Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
fix(fetcher): switch to node reifier
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahhoward committed Apr 2, 2021
1 parent 7fc88c4 commit d4187fb
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 51 deletions.
15 changes: 7 additions & 8 deletions fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"

"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-unixfsnode"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
Expand All @@ -17,14 +18,10 @@ import (
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
)

// AugmentChooserFunc is a function that can augment a prototype chooser at the time the Fetcher is initialized,
// which is given the linksystem the fetcher itself will use
type AugmentChooserFunc func(*ipld.LinkSystem, traversal.LinkTargetNodePrototypeChooser) traversal.LinkTargetNodePrototypeChooser

// FetcherConfig defines a configuration object from which Fetcher instances are constructed
type FetcherConfig struct {
blockService blockservice.BlockService
AugmentChooser AugmentChooserFunc
NodeReifier ipld.NodeReifier
PrototypeChooser traversal.LinkTargetNodePrototypeChooser
}

Expand Down Expand Up @@ -71,6 +68,7 @@ type FetchCallback func(result FetchResult) error
func NewFetcherConfig(blockService blockservice.BlockService) FetcherConfig {
return FetcherConfig{
blockService: blockService,
NodeReifier: DefaultReifier,
PrototypeChooser: DefaultPrototypeChooser,
}
}
Expand All @@ -83,10 +81,9 @@ func (fc FetcherConfig) NewSession(ctx context.Context) Fetcher {
// into ipld-prime
ls.TrustedStorage = true
ls.StorageReadOpener = blockOpener(ctx, blockservice.NewSession(ctx, fc.blockService))
ls.NodeReifier = fc.NodeReifier

protoChooser := fc.PrototypeChooser
if fc.AugmentChooser != nil {
protoChooser = fc.AugmentChooser(&ls, protoChooser)
}
return &fetcherSession{linkSystem: ls, protoChooser: protoChooser}
}

Expand Down Expand Up @@ -203,3 +200,5 @@ var DefaultPrototypeChooser = dagpb.AddSupportToChooser(func(lnk ipld.Link, lnkC
}
return basicnode.Prototype.Any, nil
})

var DefaultReifier = unixfsnode.Reify
42 changes: 8 additions & 34 deletions fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"
"time"

"github.com/ipld/go-ipld-prime/traversal"
"github.com/ipld/go-ipld-prime/traversal/selector"
"github.com/ipld/go-ipld-prime/traversal/selector/builder"

Expand Down Expand Up @@ -310,28 +309,7 @@ func (sl *selfLoader) LookupByString(key string) (ipld.Node, error) {
return nd, err
}

type selfLoadPrototype struct {
ctx context.Context
ls *ipld.LinkSystem
basePrototype ipld.NodePrototype
}

func (slp *selfLoadPrototype) NewBuilder() ipld.NodeBuilder {
return &selfLoadBuilder{ctx: slp.ctx, NodeBuilder: slp.basePrototype.NewBuilder(), ls: slp.ls}
}

type selfLoadBuilder struct {
ctx context.Context
ipld.NodeBuilder
ls *ipld.LinkSystem
}

func (slb *selfLoadBuilder) Build() ipld.Node {
nd := slb.NodeBuilder.Build()
return &selfLoader{nd, slb.ctx, slb.ls}
}

func TestChooserAugmentation(t *testing.T) {
func TestNodeReification(t *testing.T) {
// demonstrates how to use the augment chooser to build an ADL that self loads its own nodes
block3, node3, link3 := testutil.EncodeBlock(fluent.MustBuildMap(basicnode.Prototype__Map{}, 1, func(na fluent.MapAssembler) {
na.AssembleEntry("three").AssignBool(true)
Expand Down Expand Up @@ -364,16 +342,10 @@ func TestChooserAugmentation(t *testing.T) {

wantsGetter := blockservice.New(wantsBlock.Blockstore(), wantsBlock.Exchange)
fetcherConfig := fetcher.NewFetcherConfig(wantsGetter)
augmentChooser := func(ls *ipld.LinkSystem, base traversal.LinkTargetNodePrototypeChooser) traversal.LinkTargetNodePrototypeChooser {
return func(lnk ipld.Link, lnkCtx ipld.LinkContext) (ipld.NodePrototype, error) {
np, err := base(lnk, lnkCtx)
if err != nil {
return np, err
}
return &selfLoadPrototype{ctx: lnkCtx.Ctx, ls: ls, basePrototype: np}, nil
}
nodeReifier := func(lnkCtx ipld.LinkContext, nd ipld.Node, ls *ipld.LinkSystem) (ipld.Node, error) {
return &selfLoader{Node: nd, ctx: lnkCtx.Ctx, ls: ls}, nil
}
fetcherConfig.AugmentChooser = augmentChooser
fetcherConfig.NodeReifier = nodeReifier
session := fetcherConfig.NewSession(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
Expand All @@ -385,10 +357,12 @@ func TestChooserAugmentation(t *testing.T) {

retrievedNode3, err := retrievedNode.LookupByString("link3")
require.NoError(t, err)
assert.Equal(t, node3, retrievedNode3)
underlying3 := retrievedNode3.(*selfLoader).Node
assert.Equal(t, node3, underlying3)

retrievedNode4, err := retrievedNode.LookupByString("link4")
require.NoError(t, err)
assert.Equal(t, node4, retrievedNode4)
underlying4 := retrievedNode4.(*selfLoader).Node
assert.Equal(t, node4, underlying4)

}
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ require (
github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-ipfs-delay v0.0.1
github.com/ipfs/go-ipfs-routing v0.1.0
github.com/ipld/go-codec-dagpb v1.2.0
github.com/ipld/go-ipld-prime v0.9.1-0.20210324083106-dc342a9917db
github.com/stretchr/testify v1.6.1
github.com/ipfs/go-unixfsnode v1.0.1-0.20210402214142-de45652f269f
github.com/ipld/go-codec-dagpb v1.2.1-0.20210330082435-8ec6b0fbad18
github.com/ipld/go-ipld-prime v0.9.1-0.20210402181957-7406578571d1
github.com/polydawn/refmt v0.0.0-20201211092308-30ac6d18308e // indirect
github.com/stretchr/testify v1.7.0
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
Loading

0 comments on commit d4187fb

Please sign in to comment.