lease_queue.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2018 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package lease
  15. // LeaseWithTime contains lease object with a time.
  16. // For the lessor's lease heap, time identifies the lease expiration time.
  17. // For the lessor's lease checkpoint heap, the time identifies the next lease checkpoint time.
  18. type LeaseWithTime struct {
  19. id LeaseID
  20. // Unix nanos timestamp.
  21. time int64
  22. index int
  23. }
  24. type LeaseQueue []*LeaseWithTime
  25. func (pq LeaseQueue) Len() int { return len(pq) }
  26. func (pq LeaseQueue) Less(i, j int) bool {
  27. return pq[i].time < pq[j].time
  28. }
  29. func (pq LeaseQueue) Swap(i, j int) {
  30. pq[i], pq[j] = pq[j], pq[i]
  31. pq[i].index = i
  32. pq[j].index = j
  33. }
  34. func (pq *LeaseQueue) Push(x interface{}) {
  35. n := len(*pq)
  36. item := x.(*LeaseWithTime)
  37. item.index = n
  38. *pq = append(*pq, item)
  39. }
  40. func (pq *LeaseQueue) Pop() interface{} {
  41. old := *pq
  42. n := len(old)
  43. item := old[n-1]
  44. item.index = -1 // for safety
  45. *pq = old[0 : n-1]
  46. return item
  47. }