github.com/ethersphere/bee/v2@v2.2.0/pkg/file/span.go (about) 1 // Copyright 2020 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 file 6 7 import ( 8 "math" 9 10 "github.com/ethersphere/bee/v2/pkg/swarm" 11 ) 12 13 var Spans []int64 14 15 // nolint:gochecknoinits 16 func init() { 17 Spans = GenerateSpanSizes(9, swarm.Branches) 18 } 19 20 // GenerateSpanSizes generates a dictionary of maximum span lengths per level represented by one SectionSize() of data 21 func GenerateSpanSizes(levels, branches int) []int64 { 22 spans := make([]int64, levels) 23 branchesSixtyfour := int64(branches) 24 var span int64 = 1 25 for i := 0; i < 9; i++ { 26 spans[i] = span 27 span *= branchesSixtyfour 28 } 29 return spans 30 } 31 32 // Levels calculates the last level index which a particular data section count will result in. 33 // The returned level will be the level of the root hash. 34 func Levels(length int64, sectionSize, branches int) int { 35 s := int64(sectionSize) 36 b := int64(branches) 37 if length == 0 { 38 return 0 39 } else if length <= s*b { 40 return 1 41 } 42 c := (length - 1) / s 43 44 return int(math.Log(float64(c))/math.Log(float64(b)) + 1) 45 }