github.com/ethersphere/bee/v2@v2.2.0/pkg/file/redundancy/table.go (about)

     1  // Copyright 2023 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package redundancy
     6  
     7  type erasureTable struct {
     8  	shards   []int
     9  	parities []int
    10  }
    11  
    12  // newErasureTable initializes a shards<->parities table
    13  //
    14  //	 the value order must be strictly descending in both arrays
    15  //		example usage:
    16  //			shards := []int{94, 68, 46, 28, 14, 5, 1}
    17  //			parities := []int{9, 8, 7, 6, 5, 4, 3}
    18  //			var et = newErasureTable(shards, parities)
    19  func newErasureTable(shards, parities []int) erasureTable {
    20  	if len(shards) != len(parities) {
    21  		panic("redundancy table: shards and parities arrays must be of equal size")
    22  	}
    23  
    24  	maxShards := shards[0]
    25  	maxParities := parities[0]
    26  	for k := 1; k < len(shards); k++ {
    27  		s := shards[k]
    28  		if maxShards <= s {
    29  			panic("redundancy table: shards should be in strictly descending order")
    30  		}
    31  		p := parities[k]
    32  		if maxParities <= p {
    33  			panic("redundancy table: parities should be in strictly descending order")
    34  		}
    35  		maxShards, maxParities = s, p
    36  	}
    37  
    38  	return erasureTable{
    39  		shards:   shards,
    40  		parities: parities,
    41  	}
    42  }
    43  
    44  // getParities gives back the optimal parity number for a given shard
    45  func (et *erasureTable) getParities(maxShards int) int {
    46  	for k, s := range et.shards {
    47  		if maxShards >= s {
    48  			return et.parities[k]
    49  		}
    50  	}
    51  	return 0
    52  }