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.

64 lines
1.2 KiB

  1. package topology
  2. import (
  3. "fmt"
  4. )
  5. type VolumeLocationList struct {
  6. list []*DataNode
  7. }
  8. func NewVolumeLocationList() *VolumeLocationList {
  9. return &VolumeLocationList{}
  10. }
  11. func (dnll *VolumeLocationList) String() string {
  12. return fmt.Sprintf("%v", dnll.list)
  13. }
  14. func (dnll *VolumeLocationList) Head() *DataNode {
  15. return dnll.list[0]
  16. }
  17. func (dnll *VolumeLocationList) Length() int {
  18. return len(dnll.list)
  19. }
  20. func (dnll *VolumeLocationList) Set(loc *DataNode) {
  21. for i := 0; i < len(dnll.list); i++ {
  22. if loc.Ip == dnll.list[i].Ip && loc.Port == dnll.list[i].Port {
  23. dnll.list[i] = loc
  24. return
  25. }
  26. }
  27. dnll.list = append(dnll.list, loc)
  28. }
  29. func (dnll *VolumeLocationList) Remove(loc *DataNode) bool {
  30. for i, dnl := range dnll.list {
  31. if loc.Ip == dnl.Ip && loc.Port == dnl.Port {
  32. dnll.list = append(dnll.list[:i], dnll.list[i+1:]...)
  33. return true
  34. }
  35. }
  36. return false
  37. }
  38. func (dnll *VolumeLocationList) Refresh(freshThreshHold int64) {
  39. var changed bool
  40. for _, dnl := range dnll.list {
  41. if dnl.LastSeen < freshThreshHold {
  42. changed = true
  43. break
  44. }
  45. }
  46. if changed {
  47. var l []*DataNode
  48. for _, dnl := range dnll.list {
  49. if dnl.LastSeen >= freshThreshHold {
  50. l = append(l, dnl)
  51. }
  52. }
  53. dnll.list = l
  54. }
  55. }