queue.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 http://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "math/rand"
  9. "sort"
  10. "github.com/syncthing/syncthing/lib/sync"
  11. )
  12. type jobQueue struct {
  13. progress []string
  14. queued []jobQueueEntry
  15. mut sync.Mutex
  16. }
  17. type jobQueueEntry struct {
  18. name string
  19. size int64
  20. modified int64
  21. }
  22. func newJobQueue() *jobQueue {
  23. return &jobQueue{
  24. mut: sync.NewMutex(),
  25. }
  26. }
  27. func (q *jobQueue) Push(file string, size, modified int64) {
  28. q.mut.Lock()
  29. q.queued = append(q.queued, jobQueueEntry{file, size, modified})
  30. q.mut.Unlock()
  31. }
  32. func (q *jobQueue) Pop() (string, bool) {
  33. q.mut.Lock()
  34. defer q.mut.Unlock()
  35. if len(q.queued) == 0 {
  36. return "", false
  37. }
  38. f := q.queued[0].name
  39. q.queued = q.queued[1:]
  40. q.progress = append(q.progress, f)
  41. return f, true
  42. }
  43. func (q *jobQueue) BringToFront(filename string) {
  44. q.mut.Lock()
  45. defer q.mut.Unlock()
  46. for i, cur := range q.queued {
  47. if cur.name == filename {
  48. if i > 0 {
  49. // Shift the elements before the selected element one step to
  50. // the right, overwriting the selected element
  51. copy(q.queued[1:i+1], q.queued[0:])
  52. // Put the selected element at the front
  53. q.queued[0] = cur
  54. }
  55. return
  56. }
  57. }
  58. }
  59. func (q *jobQueue) Done(file string) {
  60. q.mut.Lock()
  61. defer q.mut.Unlock()
  62. for i := range q.progress {
  63. if q.progress[i] == file {
  64. copy(q.progress[i:], q.progress[i+1:])
  65. q.progress = q.progress[:len(q.progress)-1]
  66. return
  67. }
  68. }
  69. }
  70. func (q *jobQueue) Jobs() ([]string, []string) {
  71. q.mut.Lock()
  72. defer q.mut.Unlock()
  73. progress := make([]string, len(q.progress))
  74. copy(progress, q.progress)
  75. queued := make([]string, len(q.queued))
  76. for i := range q.queued {
  77. queued[i] = q.queued[i].name
  78. }
  79. return progress, queued
  80. }
  81. func (q *jobQueue) Shuffle() {
  82. q.mut.Lock()
  83. defer q.mut.Unlock()
  84. l := len(q.queued)
  85. for i := range q.queued {
  86. r := rand.Intn(l)
  87. q.queued[i], q.queued[r] = q.queued[r], q.queued[i]
  88. }
  89. }
  90. func (q *jobQueue) lenQueued() int {
  91. q.mut.Lock()
  92. defer q.mut.Unlock()
  93. return len(q.queued)
  94. }
  95. func (q *jobQueue) lenProgress() int {
  96. q.mut.Lock()
  97. defer q.mut.Unlock()
  98. return len(q.progress)
  99. }
  100. func (q *jobQueue) SortSmallestFirst() {
  101. q.mut.Lock()
  102. defer q.mut.Unlock()
  103. sort.Sort(smallestFirst(q.queued))
  104. }
  105. func (q *jobQueue) SortLargestFirst() {
  106. q.mut.Lock()
  107. defer q.mut.Unlock()
  108. sort.Sort(sort.Reverse(smallestFirst(q.queued)))
  109. }
  110. func (q *jobQueue) SortOldestFirst() {
  111. q.mut.Lock()
  112. defer q.mut.Unlock()
  113. sort.Sort(oldestFirst(q.queued))
  114. }
  115. func (q *jobQueue) SortNewestFirst() {
  116. q.mut.Lock()
  117. defer q.mut.Unlock()
  118. sort.Sort(sort.Reverse(oldestFirst(q.queued)))
  119. }
  120. // The usual sort.Interface boilerplate
  121. type smallestFirst []jobQueueEntry
  122. func (q smallestFirst) Len() int { return len(q) }
  123. func (q smallestFirst) Less(a, b int) bool { return q[a].size < q[b].size }
  124. func (q smallestFirst) Swap(a, b int) { q[a], q[b] = q[b], q[a] }
  125. type oldestFirst []jobQueueEntry
  126. func (q oldestFirst) Len() int { return len(q) }
  127. func (q oldestFirst) Less(a, b int) bool { return q[a].modified < q[b].modified }
  128. func (q oldestFirst) Swap(a, b int) { q[a], q[b] = q[b], q[a] }