-
Notifications
You must be signed in to change notification settings - Fork 15
/
chain_chunker.go
102 lines (93 loc) · 2.68 KB
/
chain_chunker.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package chunker
import (
"context"
"errors"
"fmt"
"io"
"github.com/ipld/go-ipld-prime"
"github.com/ipni/go-libipni/ingest/schema"
provider "github.com/ipni/index-provider"
"github.com/multiformats/go-multihash"
)
var _ EntriesChunker = (*ChainChunker)(nil)
// ChainChunker chunks advertisement entries as a chained series of schema.EntryChunk nodes.
// See: NewChainChunker
type ChainChunker struct {
ls *ipld.LinkSystem
chunkSize int
}
// NewChainChunker instantiates a new chain chunker that given a provider.MultihashIterator it drains
// all its mulithashes and stores them in the given link system represented as a chain of
// schema.EntryChunk nodes where each chunk contains no more than chunkSize number of multihashes.
//
// See: schema.EntryChunk.
func NewChainChunker(ls *ipld.LinkSystem, chunkSize int) (*ChainChunker, error) {
if chunkSize < 1 {
return nil, fmt.Errorf("chunk size must be at least 1; got: %d", chunkSize)
}
return &ChainChunker{
ls: ls,
chunkSize: chunkSize,
}, nil
}
func NewChainChunkerFunc(chunkSize int) NewChunkerFunc {
return func(ls *ipld.LinkSystem) (EntriesChunker, error) {
return NewChainChunker(ls, chunkSize)
}
}
// Chunk chunks all the mulithashes returned by the given iterator into a chain of schema.EntryChunk
// nodes where each chunk contains no more than chunkSize number of multihashes and returns the link
// the root chunk node.
//
// See: schema.EntryChunk.
func (ls *ChainChunker) Chunk(ctx context.Context, mhi provider.MultihashIterator) (ipld.Link, error) {
mhs := make([]multihash.Multihash, 0, ls.chunkSize)
var next ipld.Link
var mhCount, chunkCount int
for {
mh, err := mhi.Next()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, err
}
mhs = append(mhs, mh)
mhCount++
if len(mhs) >= ls.chunkSize {
cNode, err := newEntriesChunkNode(mhs, next)
if err != nil {
return nil, err
}
next, err = ls.ls.Store(ipld.LinkContext{Ctx: ctx}, schema.Linkproto, cNode)
if err != nil {
return nil, err
}
chunkCount++
// NewLinkedListOfMhs makes it own copy, so safe to reuse mhs
mhs = mhs[:0]
}
}
if len(mhs) != 0 {
cNode, err := newEntriesChunkNode(mhs, next)
if err != nil {
return nil, err
}
next, err = ls.ls.Store(ipld.LinkContext{Ctx: ctx}, schema.Linkproto, cNode)
if err != nil {
return nil, err
}
chunkCount++
}
log.Infow("Generated linked chunks of multihashes", "totalMhCount", mhCount, "chunkCount", chunkCount)
return next, nil
}
func newEntriesChunkNode(mhs []multihash.Multihash, next ipld.Link) (ipld.Node, error) {
chunk := schema.EntryChunk{
Entries: mhs,
}
if next != nil {
chunk.Next = next
}
return chunk.ToNode()
}