-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathexamples_test.go
More file actions
430 lines (350 loc) · 9.2 KB
/
examples_test.go
File metadata and controls
430 lines (350 loc) · 9.2 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package reedsolomon_test
import (
"bytes"
"fmt"
"log"
"math/rand"
"github.com/klauspost/reedsolomon"
)
func fillRandom(p []byte) {
for i := 0; i < len(p); i += 7 {
val := rand.Int63()
for j := 0; i+j < len(p) && j < 7; j++ {
p[i+j] = byte(val)
val >>= 8
}
}
}
func ExampleNew() {
// Create an encoder with 17 data and 3 parity slices.
enc, _ := reedsolomon.New(17, 3)
_ = enc
}
func ExampleEncoder_Update() {
// Create an encoder with 7 data and 3 parity slices.
enc, _ := reedsolomon.New(7, 3)
// Create a slice of data slices.
data := make([][]byte, 10)
// Create some data. All slices must have the same length.
length := 10000
for i := range data {
data[i] = make([]byte, length)
fillRandom(data[i])
}
// Encode the data.
err := enc.Encode(data)
if err != nil {
log.Fatal(err)
}
// Data is ok.
ok, err := enc.Verify(data)
fmt.Println("data ok:", ok, "err:", err)
// Change some data in shard 5.
changedData := append([]byte{}, data[5]...)
changedData[0], changedData[1], changedData[2] = 11, 22, 33
// Re-encode the data with Update.
newData := make([][]byte, 7)
// Only provide the changed data shard
newData[5] = changedData
err = enc.Update(data, newData)
if err != nil {
log.Fatal(err)
}
// Update the data shard.
data[5] = changedData
// Data is ok.
ok, err = enc.Verify(data)
fmt.Println("data ok:", ok, "err:", err)
// Output: data ok: true err: <nil>
// data ok: true err: <nil>
}
func ExampleEncoder_Reconstruct() {
// Create an encoder with 5 data and 3 parity slices.
enc, _ := reedsolomon.New(5, 3)
// Create a slice of data slices.
data := make([][]byte, 8)
// Create 5+3 slices of 50 bytes each.
for i := range data {
data[i] = make([]byte, 50)
}
// Fill in data slices with random data, leaving parity slices as zero.
for i := range 5 {
fillRandom(data[i])
}
// Encode data.
enc.Encode(data)
// Delete a data slices (but preserve the original)
original := append([][]byte{}, data...)
data[1] = nil
data[4] = nil
// Reconstruct the missing data slices.
enc.Reconstruct(data)
// Verify that reconstruction was correct.
ok := true
for i := range data {
if !bytes.Equal(original[i], data[i]) {
ok = false
}
}
fmt.Println("Reconstruction ok:", ok)
// Output: Reconstruction ok: true
}
func ExampleEncoder_Split() {
var data = make([]byte, 250000)
fillRandom(data)
// Create an encoder with 17 data and 3 parity slices.
enc, _ := reedsolomon.New(17, 3)
// Split the data into shards.
shards, _ := enc.Split(data)
// This is the output size
outputSize := len(data)
err := enc.Encode(shards)
if err != nil {
log.Fatal(err)
}
// Duplicate a data shard
shards[10] = nil
// Recover the missing shard
err = enc.Reconstruct(shards)
if err != nil {
log.Fatal(err)
}
// Join the data shards back
buf := new(bytes.Buffer)
// We write the joined data to buf
_ = enc.Join(buf, shards, outputSize)
// buf.Bytes() now contains the original data.
fmt.Println(bytes.Equal(buf.Bytes(), data))
// Output: true
}
func ExampleEncoder_Verify() {
// Create an encoder with 5 data and 3 parity slices.
enc, _ := reedsolomon.New(5, 3)
// Create a slice of data slices.
data := make([][]byte, 8)
// Create 5+3 slices of 5 bytes each.
for i := range data {
data[i] = make([]byte, 5)
}
// Fill in data slices with random data, leaving parity slices as zero.
for i := range 5 {
fillRandom(data[i])
}
// Encode data.
enc.Encode(data)
// Verify that parity slices are correct.
ok, _ := enc.Verify(data)
fmt.Println("parity ok:", ok)
// Corrupt a byte in a data shard.
data[2][1]++
// Verify that parity slices are correct.
ok, _ = enc.Verify(data)
fmt.Println("parity ok:", ok)
// Output: parity ok: true
// parity ok: false
}
func ExampleStreamEncoder() {
dataShards := 5
parShards := 2
// Create encoder (StreamEncoder is deprecated, use regular encoder)
enc, err := reedsolomon.New(dataShards, parShards)
if err != nil {
log.Fatal(err)
}
shardSize := 50000
// Create shards.
shards := make([][]byte, dataShards+parShards)
for s := range shards {
shards[s] = make([]byte, shardSize)
if s < dataShards {
fillRandom(shards[s])
}
}
// Encode parity.
err = enc.Encode(shards)
if err != nil {
log.Fatal(err)
}
// Verify the parity.
ok, err := enc.Verify(shards)
if ok {
fmt.Println("verified ok")
}
// Recover 2 lost data shards.
shards[0] = nil
shards[2] = nil
err = enc.Reconstruct(shards)
if err != nil {
log.Fatal(err)
}
// Verify the data after recovering.
ok, err = enc.Verify(shards)
if ok {
fmt.Println("recovered ok")
}
}
func ExampleEncoder_EncodeIdx() {
dataShards := 5
parityShards := 2
// Create encoder
enc, err := reedsolomon.New(dataShards, parityShards)
if err != nil {
log.Fatal(err)
}
shardSize := 50000
// Create shards.
shards := make([][]byte, dataShards+parityShards)
for s := range shards {
shards[s] = make([]byte, shardSize)
}
// Fill data shards with some data
shards[0] = bytes.Repeat([]byte{0}, shardSize)
shards[1] = bytes.Repeat([]byte{1}, shardSize)
shards[2] = bytes.Repeat([]byte{2}, shardSize)
shards[3] = bytes.Repeat([]byte{3}, shardSize)
shards[4] = bytes.Repeat([]byte{4}, shardSize)
// Encode parity, one data shard at the time using EncodeIdx.
for i := range dataShards {
err = enc.EncodeIdx(shards[i], i, shards[dataShards:])
if err != nil {
log.Fatal(err)
}
}
// Verify the parity.
ok, err := enc.Verify(shards)
if err != nil {
log.Fatal(err)
}
if ok {
fmt.Println("encode ok")
}
// Output: encode ok
}
// This shows how to use DecodeIdx to progressively reconstruct shards,
// including both data and parity shards. The new signature allows
// reconstructing multiple shards simultaneously and merging partial decodings.
func ExampleExtensions_DecodeIdx() {
const dataShards = 5
const parityShards = 3
const totalShards = dataShards + parityShards
const shardSize = 50000
// Create encoder
enc, err := reedsolomon.New(dataShards, parityShards)
if err != nil {
log.Fatal(err)
}
// DecodeIdx is available through the Extensions interface
ext := enc.(reedsolomon.Extensions)
// Create some sample data
var data = make([]byte, dataShards*shardSize)
fillRandom(data)
// Split into shards and encode
shards, err := enc.Split(data)
if err != nil {
log.Fatal(err)
}
err = enc.Encode(shards)
if err != nil {
log.Fatal(err)
}
// Save originals for verification
originals := make([][]byte, totalShards)
for i := range shards {
originals[i] = make([]byte, len(shards[i]))
copy(originals[i], shards[i])
}
// Example 1: Reconstruct multiple shards at once
// Simulate losing shards 0, 3 (data) and 7 (parity)
shards[0] = nil
shards[3] = nil
shards[7] = nil
// Set up dst with the shards we want to reconstruct
dst := make([][]byte, totalShards)
dst[0] = make([]byte, shardSize)
dst[3] = make([]byte, shardSize)
dst[7] = make([]byte, shardSize)
// Mark which shards are available as input
expectInput := make([]bool, totalShards)
expectInput[1] = true
expectInput[2] = true
expectInput[4] = true
expectInput[5] = true
expectInput[6] = true
// Provide the available shards
input := make([][]byte, totalShards)
input[1] = shards[1]
input[2] = shards[2]
input[4] = shards[4]
// Reconstruct all missing shards in two calls
err = ext.DecodeIdx(dst, expectInput, input)
if err != nil {
log.Fatal(err)
}
input = make([][]byte, totalShards)
input[5] = shards[5]
input[6] = shards[6]
err = ext.DecodeIdx(dst, expectInput, input)
if err != nil {
log.Fatal(err)
}
// Verify reconstruction
if bytes.Equal(dst[0], originals[0]) &&
bytes.Equal(dst[3], originals[3]) &&
bytes.Equal(dst[7], originals[7]) {
fmt.Println("Multiple shards reconstructed successfully")
}
// Example 2: Progressive reconstruction with merging
// Reset for progressive example
dst2 := make([][]byte, totalShards)
dst2[0] = make([]byte, shardSize)
// First partial decode using shards 1-2
input1 := make([][]byte, totalShards)
input1[1] = shards[1]
input1[2] = shards[2]
err = ext.DecodeIdx(dst2, expectInput, input1)
if err != nil {
log.Fatal(err)
}
// Second partial decode using shards 4-6
dst3 := make([][]byte, totalShards)
dst3[0] = make([]byte, shardSize)
input2 := make([][]byte, totalShards)
input2[4] = shards[4]
input2[5] = shards[5]
input2[6] = shards[6]
err = ext.DecodeIdx(dst3, expectInput, input2)
if err != nil {
log.Fatal(err)
}
// Merge the two partial decodings using nil expectInput
err = ext.DecodeIdx(dst2, nil, dst3)
if err != nil {
log.Fatal(err)
}
if bytes.Equal(dst2[0], originals[0]) {
fmt.Println("Progressive reconstruction with merge successful")
}
// Output: Multiple shards reconstructed successfully
// Progressive reconstruction with merge successful
}
func ExampleNew_maxSize() {
// Create an encoder with 17 data and 3 parity slices.
// Use a bigger max cache size
enc, err := reedsolomon.New(17, 3, reedsolomon.WithMaxGoroutines(64),
reedsolomon.WithInversionCache(true), reedsolomon.WithLeopardGF(true))
if err != nil {
log.Fatal(err)
}
// Encode some data.
data := make([][]byte, 17+3)
for i := range data {
data[i] = make([]byte, 16384)
fillRandom(data[i])
}
err = enc.Encode(data)
if err != nil {
log.Fatal(err)
}
fmt.Println("ok")
// OUTPUT: ok
}