queue.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "sync"
  9. "time"
  10. )
  11. type jobQueue struct {
  12. progress []string
  13. queued []jobQueueEntry
  14. mut sync.Mutex
  15. }
  16. type jobQueueEntry struct {
  17. name string
  18. size int64
  19. modified int64
  20. }
  21. func newJobQueue() *jobQueue {
  22. return &jobQueue{}
  23. }
  24. func (q *jobQueue) Push(file string, size int64, modified time.Time) {
  25. q.mut.Lock()
  26. // The range of UnixNano covers a range of reasonable timestamps.
  27. q.queued = append(q.queued, jobQueueEntry{file, size, modified.UnixNano()})
  28. q.mut.Unlock()
  29. }
  30. func (q *jobQueue) Pop() (string, bool) {
  31. q.mut.Lock()
  32. defer q.mut.Unlock()
  33. if len(q.queued) == 0 {
  34. return "", false
  35. }
  36. f := q.queued[0].name
  37. q.queued = q.queued[1:]
  38. q.progress = append(q.progress, f)
  39. return f, true
  40. }
  41. func (q *jobQueue) BringToFront(filename string) {
  42. q.mut.Lock()
  43. defer q.mut.Unlock()
  44. for i, cur := range q.queued {
  45. if cur.name == filename {
  46. if i > 0 {
  47. // Shift the elements before the selected element one step to
  48. // the right, overwriting the selected element
  49. copy(q.queued[1:i+1], q.queued[0:])
  50. // Put the selected element at the front
  51. q.queued[0] = cur
  52. }
  53. return
  54. }
  55. }
  56. }
  57. func (q *jobQueue) Done(file string) {
  58. q.mut.Lock()
  59. defer q.mut.Unlock()
  60. for i := range q.progress {
  61. if q.progress[i] == file {
  62. copy(q.progress[i:], q.progress[i+1:])
  63. q.progress = q.progress[:len(q.progress)-1]
  64. return
  65. }
  66. }
  67. }
  68. // Jobs returns a paginated list of file currently being pulled and files queued
  69. // to be pulled. It also returns how many items were skipped.
  70. func (q *jobQueue) Jobs(page, perpage int) ([]string, []string, int) {
  71. q.mut.Lock()
  72. defer q.mut.Unlock()
  73. toSkip := (page - 1) * perpage
  74. plen := len(q.progress)
  75. qlen := len(q.queued)
  76. if tot := plen + qlen; tot <= toSkip {
  77. return nil, nil, tot
  78. }
  79. if plen >= toSkip+perpage {
  80. progress := make([]string, perpage)
  81. copy(progress, q.progress[toSkip:toSkip+perpage])
  82. return progress, nil, toSkip
  83. }
  84. var progress []string
  85. if plen > toSkip {
  86. progress = make([]string, plen-toSkip)
  87. copy(progress, q.progress[toSkip:plen])
  88. toSkip = 0
  89. } else {
  90. toSkip -= plen
  91. }
  92. var queued []string
  93. if qlen-toSkip < perpage-len(progress) {
  94. queued = make([]string, qlen-toSkip)
  95. } else {
  96. queued = make([]string, perpage-len(progress))
  97. }
  98. for i := range queued {
  99. queued[i] = q.queued[i+toSkip].name
  100. }
  101. return progress, queued, (page - 1) * perpage
  102. }
  103. func (q *jobQueue) Reset() {
  104. q.mut.Lock()
  105. defer q.mut.Unlock()
  106. q.progress = nil
  107. q.queued = nil
  108. }
  109. func (q *jobQueue) lenQueued() int {
  110. q.mut.Lock()
  111. defer q.mut.Unlock()
  112. return len(q.queued)
  113. }
  114. func (q *jobQueue) lenProgress() int {
  115. q.mut.Lock()
  116. defer q.mut.Unlock()
  117. return len(q.progress)
  118. }