queue.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. "math/rand"
  9. "sort"
  10. "time"
  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. func (q *jobQueue) Jobs() ([]string, []string) {
  72. q.mut.Lock()
  73. defer q.mut.Unlock()
  74. progress := make([]string, len(q.progress))
  75. copy(progress, q.progress)
  76. queued := make([]string, len(q.queued))
  77. for i := range q.queued {
  78. queued[i] = q.queued[i].name
  79. }
  80. return progress, queued
  81. }
  82. func (q *jobQueue) Shuffle() {
  83. q.mut.Lock()
  84. defer q.mut.Unlock()
  85. l := len(q.queued)
  86. for i := range q.queued {
  87. r := rand.Intn(l)
  88. q.queued[i], q.queued[r] = q.queued[r], q.queued[i]
  89. }
  90. }
  91. func (q *jobQueue) lenQueued() int {
  92. q.mut.Lock()
  93. defer q.mut.Unlock()
  94. return len(q.queued)
  95. }
  96. func (q *jobQueue) lenProgress() int {
  97. q.mut.Lock()
  98. defer q.mut.Unlock()
  99. return len(q.progress)
  100. }
  101. func (q *jobQueue) SortSmallestFirst() {
  102. q.mut.Lock()
  103. defer q.mut.Unlock()
  104. sort.Sort(smallestFirst(q.queued))
  105. }
  106. func (q *jobQueue) SortLargestFirst() {
  107. q.mut.Lock()
  108. defer q.mut.Unlock()
  109. sort.Sort(sort.Reverse(smallestFirst(q.queued)))
  110. }
  111. func (q *jobQueue) SortOldestFirst() {
  112. q.mut.Lock()
  113. defer q.mut.Unlock()
  114. sort.Sort(oldestFirst(q.queued))
  115. }
  116. func (q *jobQueue) SortNewestFirst() {
  117. q.mut.Lock()
  118. defer q.mut.Unlock()
  119. sort.Sort(sort.Reverse(oldestFirst(q.queued)))
  120. }
  121. // The usual sort.Interface boilerplate
  122. type smallestFirst []jobQueueEntry
  123. func (q smallestFirst) Len() int { return len(q) }
  124. func (q smallestFirst) Less(a, b int) bool { return q[a].size < q[b].size }
  125. func (q smallestFirst) Swap(a, b int) { q[a], q[b] = q[b], q[a] }
  126. type oldestFirst []jobQueueEntry
  127. func (q oldestFirst) Len() int { return len(q) }
  128. func (q oldestFirst) Less(a, b int) bool { return q[a].modified.Before(q[b].modified) }
  129. func (q oldestFirst) Swap(a, b int) { q[a], q[b] = q[b], q[a] }