ec_volume_info.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package erasure_coding
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  4. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  5. )
  6. // data structure used in master
  7. type EcVolumeInfo struct {
  8. VolumeId needle.VolumeId
  9. Collection string
  10. ShardBits ShardBits
  11. }
  12. func NewEcVolumeInfo(collection string, vid needle.VolumeId, shardBits ShardBits) *EcVolumeInfo {
  13. return &EcVolumeInfo{
  14. Collection: collection,
  15. VolumeId: vid,
  16. ShardBits: shardBits,
  17. }
  18. }
  19. func (ecInfo *EcVolumeInfo) AddShardId(id ShardId) {
  20. ecInfo.ShardBits = ecInfo.ShardBits.AddShardId(id)
  21. }
  22. func (ecInfo *EcVolumeInfo) RemoveShardId(id ShardId) {
  23. ecInfo.ShardBits = ecInfo.ShardBits.RemoveShardId(id)
  24. }
  25. func (ecInfo *EcVolumeInfo) HasShardId(id ShardId) bool {
  26. return ecInfo.ShardBits.HasShardId(id)
  27. }
  28. func (ecInfo *EcVolumeInfo) ShardIds() (ret []ShardId) {
  29. return ecInfo.ShardBits.ShardIds()
  30. }
  31. func (ecInfo *EcVolumeInfo) ShardIdCount() (count int) {
  32. return ecInfo.ShardBits.ShardIdCount()
  33. }
  34. func (ecInfo *EcVolumeInfo) Minus(other *EcVolumeInfo) *EcVolumeInfo {
  35. ret := &EcVolumeInfo{
  36. VolumeId: ecInfo.VolumeId,
  37. Collection: ecInfo.Collection,
  38. ShardBits: ecInfo.ShardBits.Minus(other.ShardBits),
  39. }
  40. return ret
  41. }
  42. func (ecInfo *EcVolumeInfo) ToVolumeEcShardInformationMessage() (ret *master_pb.VolumeEcShardInformationMessage) {
  43. return &master_pb.VolumeEcShardInformationMessage{
  44. Id: uint32(ecInfo.VolumeId),
  45. EcIndexBits: uint32(ecInfo.ShardBits),
  46. Collection: ecInfo.Collection,
  47. }
  48. }
  49. type ShardBits uint32 // use bits to indicate the shard id, use 32 bits just for possible future extension
  50. func (b ShardBits) AddShardId(id ShardId) ShardBits {
  51. return b | (1 << id)
  52. }
  53. func (b ShardBits) RemoveShardId(id ShardId) ShardBits {
  54. return b &^ (1 << id)
  55. }
  56. func (b ShardBits) HasShardId(id ShardId) bool {
  57. return b&(1<<id) > 0
  58. }
  59. func (b ShardBits) ShardIds() (ret []ShardId) {
  60. for i := ShardId(0); i < TotalShardsCount; i++ {
  61. if b.HasShardId(i) {
  62. ret = append(ret, i)
  63. }
  64. }
  65. return
  66. }
  67. func (b ShardBits) ToUint32Slice() (ret []uint32) {
  68. for i := uint32(0); i < TotalShardsCount; i++ {
  69. if b.HasShardId(ShardId(i)) {
  70. ret = append(ret, i)
  71. }
  72. }
  73. return
  74. }
  75. func (b ShardBits) ShardIdCount() (count int) {
  76. for count = 0; b > 0; count++ {
  77. b &= b - 1
  78. }
  79. return
  80. }
  81. func (b ShardBits) Minus(other ShardBits) ShardBits {
  82. return b &^ other
  83. }
  84. func (b ShardBits) Plus(other ShardBits) ShardBits {
  85. return b | other
  86. }
  87. func (b ShardBits) MinusParityShards() ShardBits {
  88. for i := DataShardsCount; i < TotalShardsCount; i++ {
  89. b = b.RemoveShardId(ShardId(i))
  90. }
  91. return b
  92. }