lib/db, lib/model: Keep need stats in metadata (ref #5899) (#6413)

This commit is contained in:
Simon Frei
2020-05-11 15:07:06 +02:00
committed by GitHub
parent 7dc290c3ed
commit a94951becd
21 changed files with 971 additions and 358 deletions
+33
View File
@@ -18,6 +18,7 @@ import (
type deviceFolderFileDownloadState struct {
blockIndexes []int32
version protocol.Vector
blockSize int
}
// deviceFolderDownloadState holds current download state of all files that
@@ -62,10 +63,12 @@ func (p *deviceFolderDownloadState) Update(updates []protocol.FileDownloadProgre
local = deviceFolderFileDownloadState{
blockIndexes: update.BlockIndexes,
version: update.Version,
blockSize: int(update.BlockSize),
}
} else if !local.version.Equal(update.Version) {
local.blockIndexes = append(local.blockIndexes[:0], update.BlockIndexes...)
local.version = update.Version
local.blockSize = int(update.BlockSize)
} else {
local.blockIndexes = append(local.blockIndexes, update.BlockIndexes...)
}
@@ -74,6 +77,20 @@ func (p *deviceFolderDownloadState) Update(updates []protocol.FileDownloadProgre
}
}
func (p *deviceFolderDownloadState) BytesDownloaded() int64 {
var res int64
for _, state := range p.files {
// BlockSize is a new field introduced in 1.4.1, thus a fallback
// is required (will potentially underrepresent downloaded bytes).
if state.blockSize != 0 {
res += int64(len(state.blockIndexes) * state.blockSize)
} else {
res += int64(len(state.blockIndexes) * protocol.MinBlockSize)
}
}
return res
}
// GetBlockCounts returns a map filename -> number of blocks downloaded.
func (p *deviceFolderDownloadState) GetBlockCounts() map[string]int {
p.mut.RLock()
@@ -150,6 +167,22 @@ func (t *deviceDownloadState) GetBlockCounts(folder string) map[string]int {
return nil
}
func (t *deviceDownloadState) BytesDownloaded(folder string) int64 {
if t == nil {
return 0
}
t.mut.RLock()
defer t.mut.RUnlock()
for name, state := range t.folders {
if name == folder {
return state.BytesDownloaded()
}
}
return 0
}
func newDeviceDownloadState() *deviceDownloadState {
return &deviceDownloadState{
mut: sync.NewRWMutex(),