volume_info.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package storage
  2. import (
  3. "fmt"
  4. "sort"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  8. )
  9. type VolumeInfo struct {
  10. Id needle.VolumeId
  11. Size uint64
  12. ReplicaPlacement *super_block.ReplicaPlacement
  13. Ttl *needle.TTL
  14. DiskType string
  15. Collection string
  16. Version needle.Version
  17. FileCount int
  18. DeleteCount int
  19. DeletedByteCount uint64
  20. ReadOnly bool
  21. CompactRevision uint32
  22. ModifiedAtSecond int64
  23. RemoteStorageName string
  24. RemoteStorageKey string
  25. }
  26. func NewVolumeInfo(m *master_pb.VolumeInformationMessage) (vi VolumeInfo, err error) {
  27. vi = VolumeInfo{
  28. Id: needle.VolumeId(m.Id),
  29. Size: m.Size,
  30. Collection: m.Collection,
  31. FileCount: int(m.FileCount),
  32. DeleteCount: int(m.DeleteCount),
  33. DeletedByteCount: m.DeletedByteCount,
  34. ReadOnly: m.ReadOnly,
  35. Version: needle.Version(m.Version),
  36. CompactRevision: m.CompactRevision,
  37. ModifiedAtSecond: m.ModifiedAtSecond,
  38. RemoteStorageName: m.RemoteStorageName,
  39. RemoteStorageKey: m.RemoteStorageKey,
  40. DiskType: m.DiskType,
  41. }
  42. rp, e := super_block.NewReplicaPlacementFromByte(byte(m.ReplicaPlacement))
  43. if e != nil {
  44. return vi, e
  45. }
  46. vi.ReplicaPlacement = rp
  47. vi.Ttl = needle.LoadTTLFromUint32(m.Ttl)
  48. return vi, nil
  49. }
  50. func NewVolumeInfoFromShort(m *master_pb.VolumeShortInformationMessage) (vi VolumeInfo, err error) {
  51. vi = VolumeInfo{
  52. Id: needle.VolumeId(m.Id),
  53. Collection: m.Collection,
  54. Version: needle.Version(m.Version),
  55. }
  56. rp, e := super_block.NewReplicaPlacementFromByte(byte(m.ReplicaPlacement))
  57. if e != nil {
  58. return vi, e
  59. }
  60. vi.ReplicaPlacement = rp
  61. vi.Ttl = needle.LoadTTLFromUint32(m.Ttl)
  62. vi.DiskType = m.DiskType
  63. return vi, nil
  64. }
  65. func (vi VolumeInfo) IsRemote() bool {
  66. return vi.RemoteStorageName != ""
  67. }
  68. func (vi VolumeInfo) String() string {
  69. return fmt.Sprintf("Id:%d, Size:%d, ReplicaPlacement:%s, Collection:%s, Version:%v, FileCount:%d, DeleteCount:%d, DeletedByteCount:%d, ReadOnly:%v",
  70. vi.Id, vi.Size, vi.ReplicaPlacement, vi.Collection, vi.Version, vi.FileCount, vi.DeleteCount, vi.DeletedByteCount, vi.ReadOnly)
  71. }
  72. func (vi VolumeInfo) ToVolumeInformationMessage() *master_pb.VolumeInformationMessage {
  73. return &master_pb.VolumeInformationMessage{
  74. Id: uint32(vi.Id),
  75. Size: uint64(vi.Size),
  76. Collection: vi.Collection,
  77. FileCount: uint64(vi.FileCount),
  78. DeleteCount: uint64(vi.DeleteCount),
  79. DeletedByteCount: vi.DeletedByteCount,
  80. ReadOnly: vi.ReadOnly,
  81. ReplicaPlacement: uint32(vi.ReplicaPlacement.Byte()),
  82. Version: uint32(vi.Version),
  83. Ttl: vi.Ttl.ToUint32(),
  84. CompactRevision: vi.CompactRevision,
  85. ModifiedAtSecond: vi.ModifiedAtSecond,
  86. RemoteStorageName: vi.RemoteStorageName,
  87. RemoteStorageKey: vi.RemoteStorageKey,
  88. DiskType: vi.DiskType,
  89. }
  90. }
  91. /*VolumesInfo sorting*/
  92. type volumeInfos []*VolumeInfo
  93. func (vis volumeInfos) Len() int {
  94. return len(vis)
  95. }
  96. func (vis volumeInfos) Less(i, j int) bool {
  97. return vis[i].Id < vis[j].Id
  98. }
  99. func (vis volumeInfos) Swap(i, j int) {
  100. vis[i], vis[j] = vis[j], vis[i]
  101. }
  102. func sortVolumeInfos(vis volumeInfos) {
  103. sort.Sort(vis)
  104. }