-
Notifications
You must be signed in to change notification settings - Fork 0
/
scatter.go
181 lines (169 loc) · 3.41 KB
/
scatter.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package gopar3
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/klauspost/reedsolomon"
"golang.org/x/sync/errgroup"
)
// Scatter acts the same as [Inflate], but outputs each shard
// as its own file. Recovery is the same, as long as the quorum
// of files is available.
func Scatter(
ctx context.Context,
destination string,
source string,
withOptions ...Option,
) (err error) {
options, err := newOptions(withOptions...)
if err != nil {
return err
}
f, err := os.Stat(destination)
if err != nil {
return err
}
if !f.IsDir() {
return errors.New("destination must be a directory")
}
f, err = os.Stat(source)
if err != nil {
return err
}
if f.IsDir() {
return errors.New("cannot inflate a directory")
}
r, err := os.Open(source)
if err != nil {
return err
}
defer func() {
err = errors.Join(err, r.Close())
}()
var (
l = &BatchLoader{
Quorum: int(options.QuorumShards),
Shards: int(options.QuorumShards + options.RedundantShards),
ShardSize: options.ShardSize,
}
)
rs, err := reedsolomon.New(
l.Quorum, l.Shards-l.Quorum,
reedsolomon.WithAutoGoroutines(options.ShardSize),
)
if err != nil {
return err
}
tag, err := NewTag(ctx, r, options.QuorumShards)
if err != nil {
return err
}
if _, err = r.Seek(0, io.SeekStart); err != nil {
return err
}
wg, ctx := errgroup.WithContext(ctx)
batches := make(chan [][]byte, 4)
wg.Go(func() (err error) {
defer close(batches)
var batch [][]byte
for {
batch, _, err = l.Load(r)
if err != nil {
// panic(fmt.Sprintf("%+v", batch))
if err == io.EOF {
return nil
}
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case batches <- batch:
}
}
})
batchesWithParity := make(chan [][]byte, 4)
wg.Go(func() (err error) {
defer close(batchesWithParity)
for batch := range batches {
if err = rs.Reconstruct(batch); err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case batchesWithParity <- batch:
}
}
return nil
})
splitShards := make([]chan []byte, l.Shards)
for i := range splitShards {
splitShards[i] = make(chan []byte, 4)
}
wg.Go(func() (err error) {
for batch := range batchesWithParity {
for i, shard := range batch {
select {
case <-ctx.Done():
return ctx.Err()
case splitShards[i] <- shard:
}
}
}
for i := range splitShards {
close(splitShards[i])
}
return nil
})
ext := filepath.Ext(source)
base := strings.TrimSuffix(filepath.Base(source), ext)
lastBatch := tag.Batches(int64(options.ShardSize)) - 1
for i := range splitShards {
i := i
wg.Go(func() (err error) {
splitTag := tag
splitTag.ShardOrder = uint8(i)
for shard := range splitShards[i] {
if err = writeOneShard(
filepath.Join(
destination,
fmt.Sprintf(
`%s%x%s.%dof%dof%d.gopar3`,
base,
tag.SourceCRC,
ext,
i, splitTag.ShardBatch, lastBatch,
),
),
splitTag.Bytes(),
shard,
options.Telomeres,
); err != nil {
return err
}
splitTag.ShardBatch++
}
return nil
})
}
return wg.Wait()
}
func writeOneShard(destination string, tag, shard []byte, telomereCount int) (err error) {
w, closer, err := NewShardWriter(
destination,
telomereCount,
nil,
)
if err != nil {
return err
}
defer func() {
err = errors.Join(err, closer())
}()
return w.Write(tag, shard)
}