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.

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