queue.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "time"
  9. "github.com/syncthing/syncthing/lib/sync"
  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. mut: sync.NewMutex(),
  24. }
  25. }
  26. func (q *jobQueue) Push(file string, size int64, modified time.Time) {
  27. q.mut.Lock()
  28. // The range of UnixNano covers a range of reasonable timestamps.
  29. q.queued = append(q.queued, jobQueueEntry{file, size, modified.UnixNano()})
  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. // Jobs returns a paginated list of file currently being pulled and files queued
  71. // to be pulled. It also returns how many items were skipped.
  72. func (q *jobQueue) Jobs(page, perpage int) ([]string, []string, int) {
  73. q.mut.Lock()
  74. defer q.mut.Unlock()
  75. toSkip := (page - 1) * perpage
  76. plen := len(q.progress)
  77. qlen := len(q.queued)
  78. if tot := plen + qlen; tot <= toSkip {
  79. return nil, nil, tot
  80. }
  81. if plen >= toSkip+perpage {
  82. progress := make([]string, perpage)
  83. copy(progress, q.progress[toSkip:toSkip+perpage])
  84. return progress, nil, toSkip
  85. }
  86. var progress []string
  87. if plen > toSkip {
  88. progress = make([]string, plen-toSkip)
  89. copy(progress, q.progress[toSkip:plen])
  90. toSkip = 0
  91. } else {
  92. toSkip -= plen
  93. }
  94. var queued []string
  95. if qlen-toSkip < perpage-len(progress) {
  96. queued = make([]string, qlen-toSkip)
  97. } else {
  98. queued = make([]string, perpage-len(progress))
  99. }
  100. for i := range queued {
  101. queued[i] = q.queued[i+toSkip].name
  102. }
  103. return progress, queued, (page - 1) * perpage
  104. }
  105. func (q *jobQueue) Reset() {
  106. q.mut.Lock()
  107. defer q.mut.Unlock()
  108. q.progress = nil
  109. q.queued = nil
  110. }
  111. func (q *jobQueue) lenQueued() int {
  112. q.mut.Lock()
  113. defer q.mut.Unlock()
  114. return len(q.queued)
  115. }
  116. func (q *jobQueue) lenProgress() int {
  117. q.mut.Lock()
  118. defer q.mut.Unlock()
  119. return len(q.progress)
  120. }