seaweedfs/weed/storage/erasure_coding/ec_volume_info.go

114 lines
2.5 KiB
Go
Raw Normal View History

package erasure_coding
import (
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
"github.com/chrislusf/seaweedfs/weed/storage/needle"
)
// data structure used in master
type EcVolumeInfo struct {
VolumeId needle.VolumeId
Collection string
ShardBits ShardBits
}
func NewEcVolumeInfo(collection string, vid needle.VolumeId, shardBits ShardBits) *EcVolumeInfo {
return &EcVolumeInfo{
Collection: collection,
VolumeId: vid,
ShardBits: shardBits,
}
}
func (ecInfo *EcVolumeInfo) AddShardId(id ShardId) {
ecInfo.ShardBits = ecInfo.ShardBits.AddShardId(id)
}
func (ecInfo *EcVolumeInfo) RemoveShardId(id ShardId) {
ecInfo.ShardBits = ecInfo.ShardBits.RemoveShardId(id)
}
func (ecInfo *EcVolumeInfo) HasShardId(id ShardId) bool {
return ecInfo.ShardBits.HasShardId(id)
}
func (ecInfo *EcVolumeInfo) ShardIds() (ret []ShardId) {
return ecInfo.ShardBits.ShardIds()
}
2019-05-25 09:02:44 +00:00
func (ecInfo *EcVolumeInfo) ShardIdCount() (count int) {
return ecInfo.ShardBits.ShardIdCount()
}
2019-05-29 04:29:07 +00:00
func (ecInfo *EcVolumeInfo) Minus(other *EcVolumeInfo) *EcVolumeInfo {
2019-05-24 05:51:18 +00:00
ret := &EcVolumeInfo{
VolumeId: ecInfo.VolumeId,
Collection: ecInfo.Collection,
ShardBits: ecInfo.ShardBits.Minus(other.ShardBits),
2019-05-24 05:51:18 +00:00
}
return ret
}
func (ecInfo *EcVolumeInfo) ToVolumeEcShardInformationMessage() (ret *master_pb.VolumeEcShardInformationMessage) {
return &master_pb.VolumeEcShardInformationMessage{
Id: uint32(ecInfo.VolumeId),
EcIndexBits: uint32(ecInfo.ShardBits),
Collection: ecInfo.Collection,
}
}
type ShardBits uint32 // use bits to indicate the shard id, use 32 bits just for possible future extension
func (b ShardBits) AddShardId(id ShardId) ShardBits {
return b | (1 << id)
}
func (b ShardBits) RemoveShardId(id ShardId) ShardBits {
return b &^ (1 << id)
}
func (b ShardBits) HasShardId(id ShardId) bool {
return b&(1<<id) > 0
}
func (b ShardBits) ShardIds() (ret []ShardId) {
2019-05-25 09:02:44 +00:00
for i := ShardId(0); i < TotalShardsCount; i++ {
if b.HasShardId(i) {
ret = append(ret, i)
}
}
return
}
2019-05-24 18:52:23 +00:00
2019-12-23 20:48:20 +00:00
func (b ShardBits) ToUint32Slice() (ret []uint32) {
for i := uint32(0); i < TotalShardsCount; i++ {
if b.HasShardId(ShardId(i)) {
ret = append(ret, i)
}
}
return
}
2019-05-25 09:02:44 +00:00
func (b ShardBits) ShardIdCount() (count int) {
for count = 0; b > 0; count++ {
b &= b - 1
}
return
}
2019-05-29 04:29:07 +00:00
func (b ShardBits) Minus(other ShardBits) ShardBits {
return b &^ other
2019-05-24 18:52:23 +00:00
}
2019-05-29 04:29:07 +00:00
func (b ShardBits) Plus(other ShardBits) ShardBits {
return b | other
}
2019-12-24 02:06:13 +00:00
func (b ShardBits) MinusParityShards() ShardBits {
for i := DataShardsCount; i < TotalShardsCount; i++ {
b = b.RemoveShardId(ShardId(i))
}
return b
}