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