You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.5 KiB

  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. }