| 1 |
package reedsolomon_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"log" |
| 7 |
"math/rand" |
| 8 |
|
| 9 |
"github.com/klauspost/reedsolomon" |
| 10 |
) |
| 11 |
|
| 12 |
func fillRandom(p []byte) { |
| 13 |
for i := 0; i < len(p); i += 7 { |
| 14 |
val := rand.Int63() |
| 15 |
for j := 0; i+j < len(p) && j < 7; j++ { |
| 16 |
p[i+j] = byte(val) |
| 17 |
val >>= 8 |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
func ExampleNew() { |
| 23 |
// Create an encoder with 17 data and 3 parity slices. |
| 24 |
enc, _ := reedsolomon.New(17, 3) |
| 25 |
|
| 26 |
_ = enc |
| 27 |
} |
| 28 |
|
| 29 |
func ExampleEncoder_Update() { |
| 30 |
// Create an encoder with 7 data and 3 parity slices. |
| 31 |
enc, _ := reedsolomon.New(7, 3) |
| 32 |
|
| 33 |
// Create a slice of data slices. |
| 34 |
data := make([][]byte, 10) |
| 35 |
|
| 36 |
// Create some data. All slices must have the same length. |
| 37 |
length := 10000 |
| 38 |
for i := range data { |
| 39 |
data[i] = make([]byte, length) |
| 40 |
fillRandom(data[i]) |
| 41 |
} |
| 42 |
|
| 43 |
// Encode the data. |
| 44 |
err := enc.Encode(data) |
| 45 |
if err != nil { |
| 46 |
log.Fatal(err) |
| 47 |
} |
| 48 |
// Data is ok. |
| 49 |
ok, err := enc.Verify(data) |
| 50 |
fmt.Println("data ok:", ok, "err:", err) |
| 51 |
|
| 52 |
// Change some data in shard 5. |
| 53 |
changedData := append([]byte{}, data[5]...) |
| 54 |
changedData[0], changedData[1], changedData[2] = 11, 22, 33 |
| 55 |
|
| 56 |
// Re-encode the data with Update. |
| 57 |
newData := make([][]byte, 7) |
| 58 |
|
| 59 |
// Only provide the changed data shard |
| 60 |
newData[5] = changedData |
| 61 |
err = enc.Update(data, newData) |
| 62 |
if err != nil { |
| 63 |
log.Fatal(err) |
| 64 |
} |
| 65 |
// Update the data shard. |
| 66 |
data[5] = changedData |
| 67 |
|
| 68 |
// Data is ok. |
| 69 |
ok, err = enc.Verify(data) |
| 70 |
fmt.Println("data ok:", ok, "err:", err) |
| 71 |
|
| 72 |
// Output: data ok: true err: <nil> |
| 73 |
// data ok: true err: <nil> |
| 74 |
} |
| 75 |
|
| 76 |
func ExampleEncoder_Reconstruct() { |
| 77 |
// Create an encoder with 5 data and 3 parity slices. |
| 78 |
enc, _ := reedsolomon.New(5, 3) |
| 79 |
|
| 80 |
// Create a slice of data slices. |
| 81 |
data := make([][]byte, 8) |
| 82 |
|
| 83 |
// Create 5+3 slices of 50 bytes each. |
| 84 |
for i := range data { |
| 85 |
data[i] = make([]byte, 50) |
| 86 |
} |
| 87 |
|
| 88 |
// Fill in data slices with random data, leaving parity slices as zero. |
| 89 |
for i := range 5 { |
| 90 |
fillRandom(data[i]) |
| 91 |
} |
| 92 |
|
| 93 |
// Encode data. |
| 94 |
enc.Encode(data) |
| 95 |
|
| 96 |
// Delete a data slices (but preserve the original) |
| 97 |
original := append([][]byte{}, data...) |
| 98 |
data[1] = nil |
| 99 |
data[4] = nil |
| 100 |
|
| 101 |
// Reconstruct the missing data slices. |
| 102 |
enc.Reconstruct(data) |
| 103 |
|
| 104 |
// Verify that reconstruction was correct. |
| 105 |
ok := true |
| 106 |
for i := range data { |
| 107 |
if !bytes.Equal(original[i], data[i]) { |
| 108 |
ok = false |
| 109 |
} |
| 110 |
} |
| 111 |
fmt.Println("Reconstruction ok:", ok) |
| 112 |
// Output: Reconstruction ok: true |
| 113 |
} |
| 114 |
|
| 115 |
func ExampleEncoder_Split() { |
| 116 |
var data = make([]byte, 250000) |
| 117 |
fillRandom(data) |
| 118 |
|
| 119 |
// Create an encoder with 17 data and 3 parity slices. |
| 120 |
enc, _ := reedsolomon.New(17, 3) |
| 121 |
|
| 122 |
// Split the data into shards. |
| 123 |
shards, _ := enc.Split(data) |
| 124 |
|
| 125 |
// This is the output size |
| 126 |
outputSize := len(data) |
| 127 |
|
| 128 |
err := enc.Encode(shards) |
| 129 |
if err != nil { |
| 130 |
log.Fatal(err) |
| 131 |
} |
| 132 |
// Duplicate a data shard |
| 133 |
shards[10] = nil |
| 134 |
|
| 135 |
// Recover the missing shard |
| 136 |
err = enc.Reconstruct(shards) |
| 137 |
if err != nil { |
| 138 |
log.Fatal(err) |
| 139 |
} |
| 140 |
// Join the data shards back |
| 141 |
buf := new(bytes.Buffer) |
| 142 |
|
| 143 |
// We write the joined data to buf |
| 144 |
_ = enc.Join(buf, shards, outputSize) |
| 145 |
|
| 146 |
// buf.Bytes() now contains the original data. |
| 147 |
fmt.Println(bytes.Equal(buf.Bytes(), data)) |
| 148 |
// Output: true |
| 149 |
} |
| 150 |
|
| 151 |
func ExampleEncoder_Verify() { |
| 152 |
// Create an encoder with 5 data and 3 parity slices. |
| 153 |
enc, _ := reedsolomon.New(5, 3) |
| 154 |
|
| 155 |
// Create a slice of data slices. |
| 156 |
data := make([][]byte, 8) |
| 157 |
|
| 158 |
// Create 5+3 slices of 5 bytes each. |
| 159 |
for i := range data { |
| 160 |
data[i] = make([]byte, 5) |
| 161 |
} |
| 162 |
|
| 163 |
// Fill in data slices with random data, leaving parity slices as zero. |
| 164 |
for i := range 5 { |
| 165 |
fillRandom(data[i]) |
| 166 |
} |
| 167 |
|
| 168 |
// Encode data. |
| 169 |
enc.Encode(data) |
| 170 |
|
| 171 |
// Verify that parity slices are correct. |
| 172 |
ok, _ := enc.Verify(data) |
| 173 |
fmt.Println("parity ok:", ok) |
| 174 |
|
| 175 |
// Corrupt a byte in a data shard. |
| 176 |
data[2][1]++ |
| 177 |
|
| 178 |
// Verify that parity slices are correct. |
| 179 |
ok, _ = enc.Verify(data) |
| 180 |
fmt.Println("parity ok:", ok) |
| 181 |
// Output: parity ok: true |
| 182 |
// parity ok: false |
| 183 |
} |
| 184 |
|
| 185 |
func ExampleStreamEncoder() { |
| 186 |
dataShards := 5 |
| 187 |
parShards := 2 |
| 188 |
|
| 189 |
// Create encoder (StreamEncoder is deprecated, use regular encoder) |
| 190 |
enc, err := reedsolomon.New(dataShards, parShards) |
| 191 |
if err != nil { |
| 192 |
log.Fatal(err) |
| 193 |
} |
| 194 |
|
| 195 |
shardSize := 50000 |
| 196 |
|
| 197 |
// Create shards. |
| 198 |
shards := make([][]byte, dataShards+parShards) |
| 199 |
for s := range shards { |
| 200 |
shards[s] = make([]byte, shardSize) |
| 201 |
if s < dataShards { |
| 202 |
fillRandom(shards[s]) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// Encode parity. |
| 207 |
err = enc.Encode(shards) |
| 208 |
if err != nil { |
| 209 |
log.Fatal(err) |
| 210 |
} |
| 211 |
|
| 212 |
// Verify the parity. |
| 213 |
ok, err := enc.Verify(shards) |
| 214 |
if ok { |
| 215 |
fmt.Println("verified ok") |
| 216 |
} |
| 217 |
|
| 218 |
// Recover 2 lost data shards. |
| 219 |
shards[0] = nil |
| 220 |
shards[2] = nil |
| 221 |
err = enc.Reconstruct(shards) |
| 222 |
if err != nil { |
| 223 |
log.Fatal(err) |
| 224 |
} |
| 225 |
|
| 226 |
// Verify the data after recovering. |
| 227 |
ok, err = enc.Verify(shards) |
| 228 |
if ok { |
| 229 |
fmt.Println("recovered ok") |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
func ExampleEncoder_EncodeIdx() { |
| 234 |
dataShards := 5 |
| 235 |
parityShards := 2 |
| 236 |
|
| 237 |
// Create encoder |
| 238 |
enc, err := reedsolomon.New(dataShards, parityShards) |
| 239 |
if err != nil { |
| 240 |
log.Fatal(err) |
| 241 |
} |
| 242 |
|
| 243 |
shardSize := 50000 |
| 244 |
|
| 245 |
// Create shards. |
| 246 |
shards := make([][]byte, dataShards+parityShards) |
| 247 |
for s := range shards { |
| 248 |
shards[s] = make([]byte, shardSize) |
| 249 |
} |
| 250 |
|
| 251 |
// Fill data shards with some data |
| 252 |
shards[0] = bytes.Repeat([]byte{0}, shardSize) |
| 253 |
shards[1] = bytes.Repeat([]byte{1}, shardSize) |
| 254 |
shards[2] = bytes.Repeat([]byte{2}, shardSize) |
| 255 |
shards[3] = bytes.Repeat([]byte{3}, shardSize) |
| 256 |
shards[4] = bytes.Repeat([]byte{4}, shardSize) |
| 257 |
|
| 258 |
// Encode parity, one data shard at the time using EncodeIdx. |
| 259 |
for i := range dataShards { |
| 260 |
err = enc.EncodeIdx(shards[i], i, shards[dataShards:]) |
| 261 |
if err != nil { |
| 262 |
log.Fatal(err) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
// Verify the parity. |
| 267 |
ok, err := enc.Verify(shards) |
| 268 |
if err != nil { |
| 269 |
log.Fatal(err) |
| 270 |
} |
| 271 |
if ok { |
| 272 |
fmt.Println("encode ok") |
| 273 |
} |
| 274 |
|
| 275 |
// Output: encode ok |
| 276 |
} |
| 277 |
|
| 278 |
// This shows how to use DecodeIdx to progressively reconstruct shards, |
| 279 |
// including both data and parity shards. The new signature allows |
| 280 |
// reconstructing multiple shards simultaneously and merging partial decodings. |
| 281 |
func ExampleExtensions_DecodeIdx() { |
| 282 |
const dataShards = 5 |
| 283 |
const parityShards = 3 |
| 284 |
const totalShards = dataShards + parityShards |
| 285 |
const shardSize = 50000 |
| 286 |
|
| 287 |
// Create encoder |
| 288 |
enc, err := reedsolomon.New(dataShards, parityShards) |
| 289 |
if err != nil { |
| 290 |
log.Fatal(err) |
| 291 |
} |
| 292 |
|
| 293 |
// DecodeIdx is available through the Extensions interface |
| 294 |
ext := enc.(reedsolomon.Extensions) |
| 295 |
|
| 296 |
// Create some sample data |
| 297 |
var data = make([]byte, dataShards*shardSize) |
| 298 |
fillRandom(data) |
| 299 |
|
| 300 |
// Split into shards and encode |
| 301 |
shards, err := enc.Split(data) |
| 302 |
if err != nil { |
| 303 |
log.Fatal(err) |
| 304 |
} |
| 305 |
err = enc.Encode(shards) |
| 306 |
if err != nil { |
| 307 |
log.Fatal(err) |
| 308 |
} |
| 309 |
|
| 310 |
// Save originals for verification |
| 311 |
originals := make([][]byte, totalShards) |
| 312 |
for i := range shards { |
| 313 |
originals[i] = make([]byte, len(shards[i])) |
| 314 |
copy(originals[i], shards[i]) |
| 315 |
} |
| 316 |
|
| 317 |
// Example 1: Reconstruct multiple shards at once |
| 318 |
// Simulate losing shards 0, 3 (data) and 7 (parity) |
| 319 |
shards[0] = nil |
| 320 |
shards[3] = nil |
| 321 |
shards[7] = nil |
| 322 |
|
| 323 |
// Set up dst with the shards we want to reconstruct |
| 324 |
dst := make([][]byte, totalShards) |
| 325 |
dst[0] = make([]byte, shardSize) |
| 326 |
dst[3] = make([]byte, shardSize) |
| 327 |
dst[7] = make([]byte, shardSize) |
| 328 |
|
| 329 |
// Mark which shards are available as input |
| 330 |
expectInput := make([]bool, totalShards) |
| 331 |
expectInput[1] = true |
| 332 |
expectInput[2] = true |
| 333 |
expectInput[4] = true |
| 334 |
expectInput[5] = true |
| 335 |
expectInput[6] = true |
| 336 |
|
| 337 |
// Provide the available shards |
| 338 |
input := make([][]byte, totalShards) |
| 339 |
input[1] = shards[1] |
| 340 |
input[2] = shards[2] |
| 341 |
input[4] = shards[4] |
| 342 |
|
| 343 |
// Reconstruct all missing shards in two calls |
| 344 |
err = ext.DecodeIdx(dst, expectInput, input) |
| 345 |
if err != nil { |
| 346 |
log.Fatal(err) |
| 347 |
} |
| 348 |
|
| 349 |
input = make([][]byte, totalShards) |
| 350 |
input[5] = shards[5] |
| 351 |
input[6] = shards[6] |
| 352 |
err = ext.DecodeIdx(dst, expectInput, input) |
| 353 |
if err != nil { |
| 354 |
log.Fatal(err) |
| 355 |
} |
| 356 |
|
| 357 |
// Verify reconstruction |
| 358 |
if bytes.Equal(dst[0], originals[0]) && |
| 359 |
bytes.Equal(dst[3], originals[3]) && |
| 360 |
bytes.Equal(dst[7], originals[7]) { |
| 361 |
fmt.Println("Multiple shards reconstructed successfully") |
| 362 |
} |
| 363 |
|
| 364 |
// Example 2: Progressive reconstruction with merging |
| 365 |
// Reset for progressive example |
| 366 |
dst2 := make([][]byte, totalShards) |
| 367 |
dst2[0] = make([]byte, shardSize) |
| 368 |
|
| 369 |
// First partial decode using shards 1-2 |
| 370 |
input1 := make([][]byte, totalShards) |
| 371 |
input1[1] = shards[1] |
| 372 |
input1[2] = shards[2] |
| 373 |
|
| 374 |
err = ext.DecodeIdx(dst2, expectInput, input1) |
| 375 |
if err != nil { |
| 376 |
log.Fatal(err) |
| 377 |
} |
| 378 |
|
| 379 |
// Second partial decode using shards 4-6 |
| 380 |
dst3 := make([][]byte, totalShards) |
| 381 |
dst3[0] = make([]byte, shardSize) |
| 382 |
|
| 383 |
input2 := make([][]byte, totalShards) |
| 384 |
input2[4] = shards[4] |
| 385 |
input2[5] = shards[5] |
| 386 |
input2[6] = shards[6] |
| 387 |
|
| 388 |
err = ext.DecodeIdx(dst3, expectInput, input2) |
| 389 |
if err != nil { |
| 390 |
log.Fatal(err) |
| 391 |
} |
| 392 |
|
| 393 |
// Merge the two partial decodings using nil expectInput |
| 394 |
err = ext.DecodeIdx(dst2, nil, dst3) |
| 395 |
if err != nil { |
| 396 |
log.Fatal(err) |
| 397 |
} |
| 398 |
|
| 399 |
if bytes.Equal(dst2[0], originals[0]) { |
| 400 |
fmt.Println("Progressive reconstruction with merge successful") |
| 401 |
} |
| 402 |
|
| 403 |
// Output: Multiple shards reconstructed successfully |
| 404 |
// Progressive reconstruction with merge successful |
| 405 |
} |
| 406 |
|
| 407 |
func ExampleNew_maxSize() { |
| 408 |
// Create an encoder with 17 data and 3 parity slices. |
| 409 |
// Use a bigger max cache size |
| 410 |
enc, err := reedsolomon.New(17, 3, reedsolomon.WithMaxGoroutines(64), |
| 411 |
reedsolomon.WithInversionCache(true), reedsolomon.WithLeopardGF(true)) |
| 412 |
if err != nil { |
| 413 |
log.Fatal(err) |
| 414 |
} |
| 415 |
|
| 416 |
// Encode some data. |
| 417 |
data := make([][]byte, 17+3) |
| 418 |
for i := range data { |
| 419 |
data[i] = make([]byte, 16384) |
| 420 |
fillRandom(data[i]) |
| 421 |
} |
| 422 |
|
| 423 |
err = enc.Encode(data) |
| 424 |
if err != nil { |
| 425 |
log.Fatal(err) |
| 426 |
} |
| 427 |
|
| 428 |
fmt.Println("ok") |
| 429 |
// OUTPUT: ok |
| 430 |
} |