queue.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package model
  16. import (
  17. "container/list"
  18. "sync"
  19. "github.com/syncthing/syncthing/internal/protocol"
  20. )
  21. type JobQueue struct {
  22. progress []*protocol.FileInfo
  23. queued *list.List
  24. lookup map[string]*list.Element // O(1) lookups
  25. mut sync.Mutex
  26. }
  27. func NewJobQueue() *JobQueue {
  28. return &JobQueue{
  29. progress: []*protocol.FileInfo{},
  30. queued: list.New(),
  31. lookup: make(map[string]*list.Element),
  32. }
  33. }
  34. func (q *JobQueue) Push(file *protocol.FileInfo) {
  35. q.mut.Lock()
  36. defer q.mut.Unlock()
  37. q.lookup[file.Name] = q.queued.PushBack(file)
  38. }
  39. func (q *JobQueue) Pop() *protocol.FileInfo {
  40. q.mut.Lock()
  41. defer q.mut.Unlock()
  42. if q.queued.Len() == 0 {
  43. return nil
  44. }
  45. f := q.queued.Remove(q.queued.Front()).(*protocol.FileInfo)
  46. delete(q.lookup, f.Name)
  47. q.progress = append(q.progress, f)
  48. return f
  49. }
  50. func (q *JobQueue) Bump(filename string) {
  51. q.mut.Lock()
  52. defer q.mut.Unlock()
  53. ele, ok := q.lookup[filename]
  54. if ok {
  55. q.queued.MoveToFront(ele)
  56. }
  57. }
  58. func (q *JobQueue) Done(file *protocol.FileInfo) {
  59. q.mut.Lock()
  60. defer q.mut.Unlock()
  61. for i := range q.progress {
  62. if q.progress[i].Name == file.Name {
  63. copy(q.progress[i:], q.progress[i+1:])
  64. q.progress[len(q.progress)-1] = nil
  65. q.progress = q.progress[:len(q.progress)-1]
  66. return
  67. }
  68. }
  69. }
  70. func (q *JobQueue) Jobs() ([]protocol.FileInfoTruncated, []protocol.FileInfoTruncated) {
  71. q.mut.Lock()
  72. defer q.mut.Unlock()
  73. progress := make([]protocol.FileInfoTruncated, len(q.progress))
  74. for i := range q.progress {
  75. progress[i] = q.progress[i].ToTruncated()
  76. }
  77. queued := make([]protocol.FileInfoTruncated, q.queued.Len())
  78. i := 0
  79. for e := q.queued.Front(); e != nil; e = e.Next() {
  80. fi := e.Value.(*protocol.FileInfo)
  81. queued[i] = fi.ToTruncated()
  82. i++
  83. }
  84. return progress, queued
  85. }