-
Notifications
You must be signed in to change notification settings - Fork 8
/
writer.go
158 lines (131 loc) · 3.56 KB
/
writer.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package writer
import (
"bytes"
"math/bits"
"runtime"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
ffi "github.com/filecoin-project/filecoin-ffi"
"github.com/filecoin-project/go-padreader"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-commp-utils/ffiwrapper"
"github.com/filecoin-project/go-commp-utils/zerocomm"
)
type DataCIDSize struct {
PayloadSize int64
PieceSize abi.PaddedPieceSize
PieceCID cid.Cid
}
const commPBufPad = abi.PaddedPieceSize(8 << 20)
const CommPBuf = abi.UnpaddedPieceSize(commPBufPad - (commPBufPad / 128)) // can't use .Unpadded() for const
type ciderr struct {
c cid.Cid
err error
}
type Writer struct {
len int64
buf [CommPBuf]byte
leaves []chan ciderr
tbufs [][CommPBuf]byte
throttle chan int
}
func (w *Writer) Write(p []byte) (int, error) {
if w.throttle == nil {
w.throttle = make(chan int, runtime.NumCPU())
for i := 0; i < cap(w.throttle); i++ {
w.throttle <- i
}
}
if w.tbufs == nil {
w.tbufs = make([][CommPBuf]byte, cap(w.throttle))
}
n := len(p)
for len(p) > 0 {
buffered := int(w.len % int64(len(w.buf)))
toBuffer := len(w.buf) - buffered
if toBuffer > len(p) {
toBuffer = len(p)
}
copied := copy(w.buf[buffered:], p[:toBuffer])
p = p[copied:]
w.len += int64(copied)
if copied > 0 && w.len%int64(len(w.buf)) == 0 {
leaf := make(chan ciderr, 1)
bufIdx := <-w.throttle
copy(w.tbufs[bufIdx][:], w.buf[:])
go func() {
defer func() {
w.throttle <- bufIdx
}()
l, err := ffiwrapper.GeneratePieceCIDFromFile(abi.RegisteredSealProof_StackedDrg32GiBV1, bytes.NewReader(w.tbufs[bufIdx][:]), CommPBuf)
leaf <- ciderr{
c: l,
err: err,
}
}()
w.leaves = append(w.leaves, leaf)
}
}
return n, nil
}
func (w *Writer) Sum() (DataCIDSize, error) {
// process last non-zero leaf if exists
lastLen := w.len % int64(len(w.buf))
rawLen := w.len
leaves := make([]cid.Cid, len(w.leaves))
for i, leaf := range w.leaves {
r := <- leaf
if r.err != nil {
return DataCIDSize{}, xerrors.Errorf("processing leaf %d: %w", i, r.err)
}
leaves[i] = r.c
}
// process remaining bit of data
if lastLen != 0 {
if len(leaves) != 0 {
copy(w.buf[lastLen:], make([]byte, int(int64(CommPBuf)-lastLen)))
lastLen = int64(CommPBuf)
}
r, sz := padreader.New(bytes.NewReader(w.buf[:lastLen]), uint64(lastLen))
p, err := ffiwrapper.GeneratePieceCIDFromFile(abi.RegisteredSealProof_StackedDrg32GiBV1, r, sz)
if err != nil {
return DataCIDSize{}, err
}
if sz < CommPBuf { // special case for pieces smaller than 16MiB
return DataCIDSize{
PayloadSize: w.len,
PieceSize: sz.Padded(),
PieceCID: p,
}, nil
}
leaves = append(leaves, p)
}
// pad with zero pieces to power-of-two size
fillerLeaves := (1 << (bits.Len(uint(len(leaves) - 1)))) - len(leaves)
for i := 0; i < fillerLeaves; i++ {
leaves = append(leaves, zerocomm.ZeroPieceCommitment(CommPBuf))
}
if len(leaves) == 1 {
return DataCIDSize{
PayloadSize: rawLen,
PieceSize: abi.PaddedPieceSize(len(leaves)) * commPBufPad,
PieceCID: leaves[0],
}, nil
}
pieces := make([]abi.PieceInfo, len(leaves))
for i, leaf := range leaves {
pieces[i] = abi.PieceInfo{
Size: commPBufPad,
PieceCID: leaf,
}
}
p, err := ffi.GenerateUnsealedCID(abi.RegisteredSealProof_StackedDrg32GiBV1, pieces)
if err != nil {
return DataCIDSize{}, xerrors.Errorf("generating unsealed CID: %w", err)
}
return DataCIDSize{
PayloadSize: rawLen,
PieceSize: abi.PaddedPieceSize(len(leaves)) * commPBufPad,
PieceCID: p,
}, nil
}