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.

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