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.

58 lines
1.4 KiB

  1. package super_block
  2. import (
  3. "fmt"
  4. )
  5. type ReplicaPlacement struct {
  6. SameRackCount int `json:"node,omitempty"`
  7. DiffRackCount int `json:"rack,omitempty"`
  8. DiffDataCenterCount int `json:"dc,omitempty"`
  9. }
  10. func NewReplicaPlacementFromString(t string) (*ReplicaPlacement, error) {
  11. rp := &ReplicaPlacement{}
  12. for i, c := range t {
  13. count := int(c - '0')
  14. if count < 0 {
  15. return rp, fmt.Errorf("unknown replication type: %s", t)
  16. }
  17. switch i {
  18. case 0:
  19. rp.DiffDataCenterCount = count
  20. case 1:
  21. rp.DiffRackCount = count
  22. case 2:
  23. rp.SameRackCount = count
  24. }
  25. }
  26. value := rp.DiffDataCenterCount*100 + rp.DiffRackCount*10 + rp.SameRackCount
  27. if value > 255 {
  28. return rp, fmt.Errorf("unexpected replication type: %s", t)
  29. }
  30. return rp, nil
  31. }
  32. func NewReplicaPlacementFromByte(b byte) (*ReplicaPlacement, error) {
  33. return NewReplicaPlacementFromString(fmt.Sprintf("%03d", b))
  34. }
  35. func (rp *ReplicaPlacement) Byte() byte {
  36. if rp == nil {
  37. return 0
  38. }
  39. ret := rp.DiffDataCenterCount*100 + rp.DiffRackCount*10 + rp.SameRackCount
  40. return byte(ret)
  41. }
  42. func (rp *ReplicaPlacement) String() string {
  43. b := make([]byte, 3)
  44. b[0] = byte(rp.DiffDataCenterCount + '0')
  45. b[1] = byte(rp.DiffRackCount + '0')
  46. b[2] = byte(rp.SameRackCount + '0')
  47. return string(b)
  48. }
  49. func (rp *ReplicaPlacement) GetCopyCount() int {
  50. return rp.DiffDataCenterCount + rp.DiffRackCount + rp.SameRackCount + 1
  51. }