ec_volume_info.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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) ShardIdCount() (count int) {
  68. for count = 0; b > 0; count++ {
  69. b &= b - 1
  70. }
  71. return
  72. }
  73. func (b ShardBits) Minus(other ShardBits) ShardBits {
  74. return b &^ other
  75. }
  76. func (b ShardBits) Plus(other ShardBits) ShardBits {
  77. return b | other
  78. }