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.

117 lines
3.3 KiB

6 years ago
6 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "sort"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  7. "github.com/chrislusf/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. return vi, nil
  63. }
  64. func (vi VolumeInfo) IsRemote() bool {
  65. return vi.RemoteStorageName != ""
  66. }
  67. func (vi VolumeInfo) String() string {
  68. return fmt.Sprintf("Id:%d, Size:%d, ReplicaPlacement:%s, Collection:%s, Version:%v, FileCount:%d, DeleteCount:%d, DeletedByteCount:%d, ReadOnly:%v",
  69. vi.Id, vi.Size, vi.ReplicaPlacement, vi.Collection, vi.Version, vi.FileCount, vi.DeleteCount, vi.DeletedByteCount, vi.ReadOnly)
  70. }
  71. func (vi VolumeInfo) ToVolumeInformationMessage() *master_pb.VolumeInformationMessage {
  72. return &master_pb.VolumeInformationMessage{
  73. Id: uint32(vi.Id),
  74. Size: uint64(vi.Size),
  75. Collection: vi.Collection,
  76. FileCount: uint64(vi.FileCount),
  77. DeleteCount: uint64(vi.DeleteCount),
  78. DeletedByteCount: vi.DeletedByteCount,
  79. ReadOnly: vi.ReadOnly,
  80. ReplicaPlacement: uint32(vi.ReplicaPlacement.Byte()),
  81. Version: uint32(vi.Version),
  82. Ttl: vi.Ttl.ToUint32(),
  83. CompactRevision: vi.CompactRevision,
  84. ModifiedAtSecond: vi.ModifiedAtSecond,
  85. RemoteStorageName: vi.RemoteStorageName,
  86. RemoteStorageKey: vi.RemoteStorageKey,
  87. DiskType: vi.DiskType,
  88. }
  89. }
  90. /*VolumesInfo sorting*/
  91. type volumeInfos []*VolumeInfo
  92. func (vis volumeInfos) Len() int {
  93. return len(vis)
  94. }
  95. func (vis volumeInfos) Less(i, j int) bool {
  96. return vis[i].Id < vis[j].Id
  97. }
  98. func (vis volumeInfos) Swap(i, j int) {
  99. vis[i], vis[j] = vis[j], vis[i]
  100. }
  101. func sortVolumeInfos(vis volumeInfos) {
  102. sort.Sort(vis)
  103. }