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.

103 lines
2.8 KiB

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