-
Notifications
You must be signed in to change notification settings - Fork 1
/
parallelcbor.go
55 lines (48 loc) · 1.13 KB
/
parallelcbor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"context"
"sync"
"github.com/filecoin-project/go-hamt-ipld/v3"
"github.com/ipfs/go-cid"
ipldcbor "github.com/ipfs/go-ipld-cbor"
)
type getManyCborStore struct {
*ipldcbor.BasicIpldStore
}
func (g *getManyCborStore) GetMany(ctx context.Context, cids []cid.Cid, outs []interface{}) <-chan *hamt.OptionalInteger {
outCh := make(chan *hamt.OptionalInteger)
wg := sync.WaitGroup{}
processingCh := make(chan int)
go func() {
for i := 0; i < len(cids); i++ {
processingCh <- i
}
close(processingCh)
}()
const concurrency = 8
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
go func() {
defer wg.Done()
for index := range processingCh {
c := cids[index]
o := outs[index]
err := g.Get(ctx, c, o)
if err != nil {
outCh <- &hamt.OptionalInteger{Error: err}
} else {
outCh <- &hamt.OptionalInteger{Value: index}
}
}
}()
}
go func() {
wg.Wait()
close(outCh)
}()
return outCh
}
type getManyIPLDStore interface {
GetMany(ctx context.Context, cids []cid.Cid, outs []interface{}) <-chan *hamt.OptionalInteger
}
var _ getManyIPLDStore = (*getManyCborStore)(nil)