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.

61 lines
1.3 KiB

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