queue.go 858 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2020-2021 InfluxData, Inc. All rights reserved.
  2. // Use of this source code is governed by MIT
  3. // license that can be found in the LICENSE file.
  4. package write
  5. import (
  6. "container/list"
  7. )
  8. type queue struct {
  9. list *list.List
  10. limit int
  11. }
  12. func newQueue(limit int) *queue {
  13. return &queue{list: list.New(), limit: limit}
  14. }
  15. func (q *queue) push(batch *Batch) bool {
  16. overWrite := false
  17. if q.list.Len() == q.limit {
  18. q.pop()
  19. overWrite = true
  20. }
  21. q.list.PushBack(batch)
  22. return overWrite
  23. }
  24. func (q *queue) pop() *Batch {
  25. el := q.list.Front()
  26. if el != nil {
  27. q.list.Remove(el)
  28. batch := el.Value.(*Batch)
  29. batch.Evicted = true
  30. return batch
  31. }
  32. return nil
  33. }
  34. func (q *queue) first() *Batch {
  35. el := q.list.Front()
  36. if el != nil {
  37. return el.Value.(*Batch)
  38. }
  39. return nil
  40. }
  41. func (q *queue) isEmpty() bool {
  42. return q.list.Len() == 0
  43. }