Files
syncthing/lib/model/queue.go
T

143 lines
2.9 KiB
Go
Raw Normal View History

2014-12-01 19:23:06 +00:00
// Copyright (C) 2014 The Syncthing Authors.
//
2015-03-07 21:36:35 +01:00
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
2014-12-01 19:23:06 +00:00
package model
import (
2025-08-07 11:19:36 +02:00
"sync"
"time"
)
2014-12-01 19:23:06 +00:00
2014-12-30 09:35:21 +01:00
type jobQueue struct {
2014-12-30 09:31:34 +01:00
progress []string
queued []jobQueueEntry
2014-12-30 09:07:58 +01:00
mut sync.Mutex
2014-12-01 19:23:06 +00:00
}
type jobQueueEntry struct {
name string
size int64
modified int64
}
2014-12-30 09:35:21 +01:00
func newJobQueue() *jobQueue {
2025-08-07 11:19:36 +02:00
return &jobQueue{}
2014-12-01 19:23:06 +00:00
}
func (q *jobQueue) Push(file string, size int64, modified time.Time) {
2014-12-01 19:23:06 +00:00
q.mut.Lock()
// The range of UnixNano covers a range of reasonable timestamps.
q.queued = append(q.queued, jobQueueEntry{file, size, modified.UnixNano()})
2014-12-30 09:07:58 +01:00
q.mut.Unlock()
2014-12-01 19:23:06 +00:00
}
2014-12-30 09:35:21 +01:00
func (q *jobQueue) Pop() (string, bool) {
2014-12-01 19:23:06 +00:00
q.mut.Lock()
defer q.mut.Unlock()
2014-12-30 09:07:58 +01:00
if len(q.queued) == 0 {
2014-12-30 09:31:34 +01:00
return "", false
2014-12-01 19:23:06 +00:00
}
f := q.queued[0].name
2014-12-30 09:07:58 +01:00
q.queued = q.queued[1:]
2014-12-01 19:23:06 +00:00
q.progress = append(q.progress, f)
2014-12-30 09:31:34 +01:00
return f, true
2014-12-01 19:23:06 +00:00
}
2014-12-30 09:35:21 +01:00
func (q *jobQueue) BringToFront(filename string) {
2014-12-01 19:23:06 +00:00
q.mut.Lock()
defer q.mut.Unlock()
2015-01-02 15:45:59 +01:00
for i, cur := range q.queued {
if cur.name == filename {
2015-01-02 15:45:59 +01:00
if i > 0 {
// Shift the elements before the selected element one step to
// the right, overwriting the selected element
copy(q.queued[1:i+1], q.queued[0:])
// Put the selected element at the front
q.queued[0] = cur
}
2014-12-30 09:07:58 +01:00
return
}
2014-12-01 19:23:06 +00:00
}
}
2014-12-30 09:35:21 +01:00
func (q *jobQueue) Done(file string) {
2014-12-01 19:23:06 +00:00
q.mut.Lock()
defer q.mut.Unlock()
for i := range q.progress {
2014-12-30 09:31:34 +01:00
if q.progress[i] == file {
2014-12-01 19:23:06 +00:00
copy(q.progress[i:], q.progress[i+1:])
q.progress = q.progress[:len(q.progress)-1]
return
}
}
}
// Jobs returns a paginated list of file currently being pulled and files queued
// to be pulled. It also returns how many items were skipped.
func (q *jobQueue) Jobs(page, perpage int) ([]string, []string, int) {
2014-12-01 19:23:06 +00:00
q.mut.Lock()
defer q.mut.Unlock()
toSkip := (page - 1) * perpage
plen := len(q.progress)
qlen := len(q.queued)
2014-12-01 19:23:06 +00:00
if tot := plen + qlen; tot <= toSkip {
return nil, nil, tot
}
2014-12-01 19:23:06 +00:00
if plen >= toSkip+perpage {
progress := make([]string, perpage)
copy(progress, q.progress[toSkip:toSkip+perpage])
return progress, nil, toSkip
}
var progress []string
if plen > toSkip {
progress = make([]string, plen-toSkip)
copy(progress, q.progress[toSkip:plen])
toSkip = 0
} else {
toSkip -= plen
}
var queued []string
if qlen-toSkip < perpage-len(progress) {
queued = make([]string, qlen-toSkip)
} else {
queued = make([]string, perpage-len(progress))
}
for i := range queued {
queued[i] = q.queued[i+toSkip].name
}
return progress, queued, (page - 1) * perpage
2014-12-01 19:23:06 +00:00
}
func (q *jobQueue) Reset() {
q.mut.Lock()
defer q.mut.Unlock()
q.progress = nil
q.queued = nil
}
func (q *jobQueue) lenQueued() int {
q.mut.Lock()
defer q.mut.Unlock()
return len(q.queued)
}
func (q *jobQueue) lenProgress() int {
q.mut.Lock()
defer q.mut.Unlock()
return len(q.progress)
}