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 58c7cc8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 51 deletions.
11 changes: 3 additions & 8 deletions fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,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 @@ -83,10 +79,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
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)

}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ require (
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/ipld/go-ipld-prime v0.9.1-0.20210402181957-7406578571d1
github.com/stretchr/testify v1.6.1
)
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,12 @@ github.com/ipfs/go-peertaskqueue v0.2.0 h1:2cSr7exUGKYyDeUyQ7P/nHPs9P7Ht/B+ROrpN
github.com/ipfs/go-peertaskqueue v0.2.0/go.mod h1:5/eNrBEbtSKWCG+kQK8K8fGNixoYUnr+P7jivavs9lY=
github.com/ipfs/go-verifcid v0.0.1 h1:m2HI7zIuR5TFyQ1b79Da5N9dnnCP1vcu2QqawmWlK2E=
github.com/ipfs/go-verifcid v0.0.1/go.mod h1:5Hrva5KBeIog4A+UpqlaIU+DEstipcJYQQZc0g37pY0=
github.com/ipld/go-codec-dagpb v1.1.0 h1:MO3Fa6ZHiXUy6f6o6hSyHmItfKKNyBfBSk838kXDZmI=
github.com/ipld/go-codec-dagpb v1.1.0/go.mod h1:6nBN7X7h8EOsEejZGqC7tej5drsdBAXbMHyBT+Fne5s=
github.com/ipld/go-codec-dagpb v1.1.1-0.20210316175912-b641b0ff139c h1:AItYb6pmxQ4tHXqE33BKcb5ejowTwQScX4XfdaVafHY=
github.com/ipld/go-codec-dagpb v1.1.1-0.20210316175912-b641b0ff139c/go.mod h1:6nBN7X7h8EOsEejZGqC7tej5drsdBAXbMHyBT+Fne5s=
github.com/ipld/go-codec-dagpb v1.2.0 h1:2umV7ud8HBMkRuJgd8gXw95cLhwmcYrihS3cQEy9zpI=
github.com/ipld/go-codec-dagpb v1.2.0/go.mod h1:6nBN7X7h8EOsEejZGqC7tej5drsdBAXbMHyBT+Fne5s=
github.com/ipld/go-ipld-prime v0.9.0 h1:N2OjJMb+fhyFPwPnVvJcWU/NsumP8etal+d2v3G4eww=
github.com/ipld/go-ipld-prime v0.9.0/go.mod h1:KvBLMr4PX1gWptgkzRjVZCrLmSGcZCb/jioOQwCqZN8=
github.com/ipld/go-ipld-prime v0.9.1-0.20210324083106-dc342a9917db h1:kFwGn8rXa/Z31ev1OFNQsYeNKNCdifnTPl/NvPy5L38=
github.com/ipld/go-ipld-prime v0.9.1-0.20210324083106-dc342a9917db/go.mod h1:KvBLMr4PX1gWptgkzRjVZCrLmSGcZCb/jioOQwCqZN8=
github.com/ipld/go-ipld-prime v0.9.1-0.20210402181957-7406578571d1 h1:dIKSj9r+CCXz9t6p4TfbDx34CfSjLfasRZf37SXlNjg=
github.com/ipld/go-ipld-prime v0.9.1-0.20210402181957-7406578571d1/go.mod h1:KvBLMr4PX1gWptgkzRjVZCrLmSGcZCb/jioOQwCqZN8=
github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA=
github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
Expand Down Expand Up @@ -534,8 +530,6 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/assertions v1.0.0 h1:UVQPSSmc3qtTi+zPPkCXvZX9VvW/xT/NsRvKfwY81a8=
github.com/smartystreets/assertions v1.0.0/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/smola/gocompat v0.2.0/go.mod h1:1B0MlxbmoZNo3h8guHp8HztB3BSYR5itql9qtVc0ypY=
Expand Down

0 comments on commit 58c7cc8

Please sign in to comment.