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

Commit

Permalink
better channel behavior and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
acruikshank committed Mar 2, 2021
1 parent 4ff0dac commit 7ec6d73
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
10 changes: 8 additions & 2 deletions fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type FetcherConfig struct {
type Fetcher interface {
// NodeMatching traverses a node graph starting with the provided node using the given selector and possibly crossing
// block boundaries. Each matched node is sent to the FetchResult channel.
// The results and error channels will be closed on query completion or error. The error channel is buffered,
// will emit at most one error and must be checked after processing the results.
NodeMatching(ctx context.Context, node ipld.Node, match selector.Selector) (<-chan FetchResult, <-chan error)

// BlockOfType fetches a node graph of the provided type corresponding to single block by link.
Expand All @@ -34,6 +36,8 @@ type Fetcher interface {
// BlockMatchingOfType traverses a node graph starting with the given link using the given selector and possibly
// crossing block boundaries. The nodes will be typed using the provided prototype. Each matched node is sent to
// the FetchResult channel.
// The results and error channels will be closed on query completion or error. The error channel is buffered,
// will emit at most one error and must be checked after processing the results.
BlockMatchingOfType(ctx context.Context, root ipld.Link, match selector.Selector, ptype ipld.NodePrototype) (<-chan FetchResult, <-chan error)
}

Expand Down Expand Up @@ -75,10 +79,11 @@ func (f *fetcherSession) BlockOfType(ctx context.Context, link ipld.Link, ptype

func (f *fetcherSession) NodeMatching(ctx context.Context, node ipld.Node, match selector.Selector) (<-chan FetchResult, <-chan error) {
results := make(chan FetchResult)
errors := make(chan error)
errors := make(chan error, 1)

go func() {
defer close(results)
defer close(errors)

err := f.fetch(ctx, node, match, results)
if err != nil {
Expand All @@ -92,10 +97,11 @@ func (f *fetcherSession) NodeMatching(ctx context.Context, node ipld.Node, match

func (f *fetcherSession) BlockMatchingOfType(ctx context.Context, root ipld.Link, match selector.Selector, ptype ipld.NodePrototype) (<-chan FetchResult, <-chan error) {
results := make(chan FetchResult)
errors := make(chan error)
errors := make(chan error, 1)

go func() {
defer close(results)
defer close(errors)

// retrieve first node
node, err := f.BlockOfType(ctx, root, ptype)
Expand Down
24 changes: 8 additions & 16 deletions fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,24 +208,16 @@ func TestHelpers(t *testing.T) {

func assertNodesInOrder(t *testing.T, nodeCh <-chan fetcher.FetchResult, errCh <-chan error, nodeCount int, nodes map[int]ipld.Node) {
order := 0
Loop:
for {
select {
case res, ok := <-nodeCh:
if !ok {
break Loop
}

expectedNode, ok := nodes[order]
if ok {
assert.Equal(t, expectedNode, res.Node)
}

order++
case err := <-errCh:
require.FailNow(t, err.Error())
for res := range nodeCh {
expectedNode, ok := nodes[order]
if ok {
assert.Equal(t, expectedNode, res.Node)
}
order++
}

err := <-errCh
require.NoError(t, err)
assert.Equal(t, nodeCount, order)
}

Expand Down

0 comments on commit 7ec6d73

Please sign in to comment.