/
countinglinksystem.go
52 lines (46 loc) · 1.35 KB
/
countinglinksystem.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
package bitswaphelpers
import (
"io"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/linking"
"github.com/libp2p/go-libp2p/core/peer"
)
type cumulativeCountWriter struct {
io.Writer
totalWritten uint64
committer linking.BlockWriteCommitter
cb func(count uint64)
}
func (ccw *cumulativeCountWriter) Write(p []byte) (n int, err error) {
written, err := ccw.Writer.Write(p)
if err != nil {
return written, err
}
ccw.totalWritten += uint64(written)
return written, nil
}
func (ccw *cumulativeCountWriter) Commit(link datamodel.Link) error {
err := ccw.committer(link)
if err != nil {
return err
}
ccw.cb(ccw.totalWritten)
return nil
}
func NewByteCountingLinkSystem(lsys *linking.LinkSystem, blockWritten func(from *peer.ID, count uint64)) *linking.LinkSystem {
newLsys := *lsys // copy all values from old system
oldWriteOpener := lsys.StorageWriteOpener
newLsys.StorageWriteOpener = func(lctx linking.LinkContext) (io.Writer, linking.BlockWriteCommitter, error) {
var from *peer.ID = nil
if p, ok := lctx.Ctx.Value(peerIdContextKey).(peer.ID); ok {
from = &p
}
w, committer, err := oldWriteOpener(lctx)
if err != nil {
return w, committer, err
}
ccw := &cumulativeCountWriter{w, 0, committer, func(count uint64) { blockWritten(from, count) }}
return ccw, ccw.Commit, err
}
return &newLsys
}