aboutsummaryrefslogtreecommitdiff
path: root/weed/storage/erasure_coding/ec_shard_size_helper.go
blob: 43d9a4f2f188a2fa9faf2668f8284bc8ae9a98bc (plain)
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
package erasure_coding

import (
	"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
)

// GetShardSize returns the size of a specific shard from VolumeEcShardInformationMessage
// Returns the size and true if the shard exists, 0 and false if not present
func GetShardSize(msg *master_pb.VolumeEcShardInformationMessage, shardId ShardId) (size int64, found bool) {
	if msg == nil || msg.ShardSizes == nil {
		return 0, false
	}

	shardBits := ShardBits(msg.EcIndexBits)
	index, found := shardBits.ShardIdToIndex(shardId)
	if !found || index >= len(msg.ShardSizes) {
		return 0, false
	}

	return msg.ShardSizes[index], true
}

// SetShardSize sets the size of a specific shard in VolumeEcShardInformationMessage
// Returns true if successful, false if the shard is not present in EcIndexBits
func SetShardSize(msg *master_pb.VolumeEcShardInformationMessage, shardId ShardId, size int64) bool {
	if msg == nil {
		return false
	}

	shardBits := ShardBits(msg.EcIndexBits)
	index, found := shardBits.ShardIdToIndex(shardId)
	if !found {
		return false
	}

	// Initialize ShardSizes slice if needed
	expectedLength := shardBits.ShardIdCount()
	if msg.ShardSizes == nil {
		msg.ShardSizes = make([]int64, expectedLength)
	} else if len(msg.ShardSizes) != expectedLength {
		// Resize the slice to match the expected length
		newSizes := make([]int64, expectedLength)
		copy(newSizes, msg.ShardSizes)
		msg.ShardSizes = newSizes
	}

	if index >= len(msg.ShardSizes) {
		return false
	}

	msg.ShardSizes[index] = size
	return true
}

// InitializeShardSizes initializes the ShardSizes slice based on EcIndexBits
// This ensures the slice has the correct length for all present shards
func InitializeShardSizes(msg *master_pb.VolumeEcShardInformationMessage) {
	if msg == nil {
		return
	}

	shardBits := ShardBits(msg.EcIndexBits)
	expectedLength := shardBits.ShardIdCount()

	if msg.ShardSizes == nil || len(msg.ShardSizes) != expectedLength {
		msg.ShardSizes = make([]int64, expectedLength)
	}
}