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.

56 lines
1.1 KiB

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