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.

89 lines
2.3 KiB

  1. package erasure_coding
  2. import (
  3. "math"
  4. "sort"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  7. )
  8. type EcVolumeShards []*EcVolumeShard
  9. func (shards *EcVolumeShards) AddEcVolumeShard(ecVolumeShard *EcVolumeShard) bool {
  10. for _, s := range *shards {
  11. if s.ShardId == ecVolumeShard.ShardId {
  12. return false
  13. }
  14. }
  15. *shards = append(*shards, ecVolumeShard)
  16. sort.Slice(shards, func(i, j int) bool {
  17. return (*shards)[i].VolumeId < (*shards)[j].VolumeId ||
  18. (*shards)[i].VolumeId == (*shards)[j].VolumeId && (*shards)[i].ShardId < (*shards)[j].ShardId
  19. })
  20. return true
  21. }
  22. func (shards *EcVolumeShards) DeleteEcVolumeShard(ecVolumeShard *EcVolumeShard) bool {
  23. foundPosition := -1
  24. for i, s := range *shards {
  25. if s.ShardId == ecVolumeShard.ShardId {
  26. foundPosition = i
  27. }
  28. }
  29. if foundPosition < 0 {
  30. return false
  31. }
  32. *shards = append((*shards)[:foundPosition], (*shards)[foundPosition+1:]...)
  33. return true
  34. }
  35. func (shards *EcVolumeShards) FindEcVolumeShard(shardId ShardId) (ecVolumeShard *EcVolumeShard, found bool) {
  36. for _, s := range *shards {
  37. if s.ShardId == shardId {
  38. return s, true
  39. }
  40. }
  41. return nil, false
  42. }
  43. func (shards *EcVolumeShards) Close() {
  44. for _, s := range *shards {
  45. s.Close()
  46. }
  47. }
  48. func (shards *EcVolumeShards) ToVolumeEcShardInformationMessage() (messages []*master_pb.VolumeEcShardInformationMessage) {
  49. prevVolumeId := needle.VolumeId(math.MaxUint32)
  50. var m *master_pb.VolumeEcShardInformationMessage
  51. for _, s := range *shards {
  52. if s.VolumeId != prevVolumeId {
  53. m = &master_pb.VolumeEcShardInformationMessage{
  54. Id: uint32(s.VolumeId),
  55. Collection: s.Collection,
  56. }
  57. messages = append(messages, m)
  58. }
  59. prevVolumeId = s.VolumeId
  60. m.EcIndexBits = uint32(ShardBits(m.EcIndexBits).AddShardId(s.ShardId))
  61. }
  62. return
  63. }
  64. func (shards *EcVolumeShards) ReadEcShardNeedle(n *needle.Needle) (int, error) {
  65. shard := (*shards)[0]
  66. // find the needle from ecx file
  67. offset, size, err := shard.findNeedleFromEcx(n.Id)
  68. if err != nil {
  69. return 0, err
  70. }
  71. // calculate the locations in the ec shards
  72. intervals := LocateData(ErasureCodingLargeBlockSize, ErasureCodingSmallBlockSize, shard.ecxFileSize, offset.ToAcutalOffset(), size)
  73. // TODO read the intervals
  74. return len(intervals), nil
  75. }