queue.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. "sort"
  9. "time"
  10. "github.com/syncthing/syncthing/lib/rand"
  11. "github.com/syncthing/syncthing/lib/sync"
  12. )
  13. type jobQueue struct {
  14. progress []string
  15. queued []jobQueueEntry
  16. mut sync.Mutex
  17. }
  18. type jobQueueEntry struct {
  19. name string
  20. size int64
  21. modified time.Time
  22. }
  23. func newJobQueue() *jobQueue {
  24. return &jobQueue{
  25. mut: sync.NewMutex(),
  26. }
  27. }
  28. func (q *jobQueue) Push(file string, size int64, modified time.Time) {
  29. q.mut.Lock()
  30. q.queued = append(q.queued, jobQueueEntry{file, size, modified})
  31. q.mut.Unlock()
  32. }
  33. func (q *jobQueue) Pop() (string, bool) {
  34. q.mut.Lock()
  35. defer q.mut.Unlock()
  36. if len(q.queued) == 0 {
  37. return "", false
  38. }
  39. f := q.queued[0].name
  40. q.queued = q.queued[1:]
  41. q.progress = append(q.progress, f)
  42. return f, true
  43. }
  44. func (q *jobQueue) BringToFront(filename string) {
  45. q.mut.Lock()
  46. defer q.mut.Unlock()
  47. for i, cur := range q.queued {
  48. if cur.name == filename {
  49. if i > 0 {
  50. // Shift the elements before the selected element one step to
  51. // the right, overwriting the selected element
  52. copy(q.queued[1:i+1], q.queued[0:])
  53. // Put the selected element at the front
  54. q.queued[0] = cur
  55. }
  56. return
  57. }
  58. }
  59. }
  60. func (q *jobQueue) Done(file string) {
  61. q.mut.Lock()
  62. defer q.mut.Unlock()
  63. for i := range q.progress {
  64. if q.progress[i] == file {
  65. copy(q.progress[i:], q.progress[i+1:])
  66. q.progress = q.progress[:len(q.progress)-1]
  67. return
  68. }
  69. }
  70. }
  71. // Jobs returns a paginated list of file currently being pulled and files queued
  72. // to be pulled. It also returns how many items were skipped.
  73. func (q *jobQueue) Jobs(page, perpage int) ([]string, []string, int) {
  74. q.mut.Lock()
  75. defer q.mut.Unlock()
  76. toSkip := (page - 1) * perpage
  77. plen := len(q.progress)
  78. qlen := len(q.queued)
  79. if tot := plen + qlen; tot <= toSkip {
  80. return nil, nil, tot
  81. }
  82. if plen >= toSkip+perpage {
  83. progress := make([]string, perpage)
  84. copy(progress, q.progress[toSkip:toSkip+perpage])
  85. return progress, nil, toSkip
  86. }
  87. var progress []string
  88. if plen > toSkip {
  89. progress = make([]string, plen-toSkip)
  90. copy(progress, q.progress[toSkip:plen])
  91. toSkip = 0
  92. } else {
  93. toSkip -= plen
  94. }
  95. var queued []string
  96. if qlen-toSkip < perpage-len(progress) {
  97. queued = make([]string, qlen-toSkip)
  98. } else {
  99. queued = make([]string, perpage-len(progress))
  100. }
  101. for i := range queued {
  102. queued[i] = q.queued[i+toSkip].name
  103. }
  104. return progress, queued, (page - 1) * perpage
  105. }
  106. func (q *jobQueue) Shuffle() {
  107. q.mut.Lock()
  108. defer q.mut.Unlock()
  109. rand.Shuffle(q.queued)
  110. }
  111. func (q *jobQueue) Reset() {
  112. q.mut.Lock()
  113. defer q.mut.Unlock()
  114. q.progress = nil
  115. q.queued = nil
  116. }
  117. func (q *jobQueue) lenQueued() int {
  118. q.mut.Lock()
  119. defer q.mut.Unlock()
  120. return len(q.queued)
  121. }
  122. func (q *jobQueue) lenProgress() int {
  123. q.mut.Lock()
  124. defer q.mut.Unlock()
  125. return len(q.progress)
  126. }
  127. func (q *jobQueue) SortSmallestFirst() {
  128. q.mut.Lock()
  129. defer q.mut.Unlock()
  130. sort.Sort(smallestFirst(q.queued))
  131. }
  132. func (q *jobQueue) SortLargestFirst() {
  133. q.mut.Lock()
  134. defer q.mut.Unlock()
  135. sort.Sort(sort.Reverse(smallestFirst(q.queued)))
  136. }
  137. func (q *jobQueue) SortOldestFirst() {
  138. q.mut.Lock()
  139. defer q.mut.Unlock()
  140. sort.Sort(oldestFirst(q.queued))
  141. }
  142. func (q *jobQueue) SortNewestFirst() {
  143. q.mut.Lock()
  144. defer q.mut.Unlock()
  145. sort.Sort(sort.Reverse(oldestFirst(q.queued)))
  146. }
  147. // The usual sort.Interface boilerplate
  148. type smallestFirst []jobQueueEntry
  149. func (q smallestFirst) Len() int { return len(q) }
  150. func (q smallestFirst) Less(a, b int) bool { return q[a].size < q[b].size }
  151. func (q smallestFirst) Swap(a, b int) { q[a], q[b] = q[b], q[a] }
  152. type oldestFirst []jobQueueEntry
  153. func (q oldestFirst) Len() int { return len(q) }
  154. func (q oldestFirst) Less(a, b int) bool { return q[a].modified.Before(q[b].modified) }
  155. func (q oldestFirst) Swap(a, b int) { q[a], q[b] = q[b], q[a] }