devicedownloadstate.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright (C) 2015 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. "github.com/syncthing/syncthing/lib/protocol"
  9. "github.com/syncthing/syncthing/lib/sync"
  10. )
  11. // deviceFolderFileDownloadState holds current download state of a file that
  12. // a remote device has advertised. blockIndexes represends indexes within
  13. // FileInfo.Blocks that the remote device already has, and version represents
  14. // the version of the file that the remote device is downloading.
  15. type deviceFolderFileDownloadState struct {
  16. blockIndexes []int32
  17. version protocol.Vector
  18. }
  19. // deviceFolderDownloadState holds current download state of all files that
  20. // a remote device is currently downloading in a specific folder.
  21. type deviceFolderDownloadState struct {
  22. mut sync.RWMutex
  23. files map[string]deviceFolderFileDownloadState
  24. numberOfBlocksInProgress int
  25. }
  26. // Has returns wether a block at that specific index, and that specific version of the file
  27. // is currently available on the remote device for pulling from a temporary file.
  28. func (p *deviceFolderDownloadState) Has(file string, version protocol.Vector, index int32) bool {
  29. p.mut.RLock()
  30. defer p.mut.RUnlock()
  31. local, ok := p.files[file]
  32. if !ok || !local.version.Equal(version) {
  33. return false
  34. }
  35. for _, existingIndex := range local.blockIndexes {
  36. if existingIndex == index {
  37. return true
  38. }
  39. }
  40. return false
  41. }
  42. // Update updates internal state of what has been downloaded into the temporary
  43. // files by the remote device for this specific folder.
  44. func (p *deviceFolderDownloadState) Update(updates []protocol.FileDownloadProgressUpdate) {
  45. p.mut.Lock()
  46. defer p.mut.Unlock()
  47. for _, update := range updates {
  48. local, ok := p.files[update.Name]
  49. if update.UpdateType == protocol.UpdateTypeForget && ok && local.version.Equal(update.Version) {
  50. p.numberOfBlocksInProgress -= len(local.blockIndexes)
  51. delete(p.files, update.Name)
  52. } else if update.UpdateType == protocol.UpdateTypeAppend {
  53. if !ok {
  54. local = deviceFolderFileDownloadState{
  55. blockIndexes: update.BlockIndexes,
  56. version: update.Version,
  57. }
  58. } else if !local.version.Equal(update.Version) {
  59. p.numberOfBlocksInProgress -= len(local.blockIndexes)
  60. local.blockIndexes = append(local.blockIndexes[:0], update.BlockIndexes...)
  61. local.version = update.Version
  62. } else {
  63. local.blockIndexes = append(local.blockIndexes, update.BlockIndexes...)
  64. }
  65. p.files[update.Name] = local
  66. p.numberOfBlocksInProgress += len(update.BlockIndexes)
  67. }
  68. }
  69. }
  70. // NumberOfBlocksInProgress returns the number of blocks the device has downloaded
  71. // for a specific folder.
  72. func (p *deviceFolderDownloadState) NumberOfBlocksInProgress() int {
  73. p.mut.RLock()
  74. n := p.numberOfBlocksInProgress
  75. p.mut.RUnlock()
  76. return n
  77. }
  78. // deviceDownloadState represents the state of all in progress downloads
  79. // for all folders of a specific device.
  80. type deviceDownloadState struct {
  81. mut sync.RWMutex
  82. folders map[string]*deviceFolderDownloadState
  83. numberOfBlocksInProgress int
  84. }
  85. // Update updates internal state of what has been downloaded into the temporary
  86. // files by the remote device for this specific folder.
  87. func (t *deviceDownloadState) Update(folder string, updates []protocol.FileDownloadProgressUpdate) {
  88. t.mut.RLock()
  89. f, ok := t.folders[folder]
  90. t.mut.RUnlock()
  91. if !ok {
  92. f = &deviceFolderDownloadState{
  93. mut: sync.NewRWMutex(),
  94. files: make(map[string]deviceFolderFileDownloadState),
  95. }
  96. t.mut.Lock()
  97. t.folders[folder] = f
  98. t.mut.Unlock()
  99. }
  100. f.Update(updates)
  101. }
  102. // Has returns wether block at that specific index, and that specific version of the file
  103. // is currently available on the remote device for pulling from a temporary file.
  104. func (t *deviceDownloadState) Has(folder, file string, version protocol.Vector, index int32) bool {
  105. if t == nil {
  106. return false
  107. }
  108. t.mut.RLock()
  109. f, ok := t.folders[folder]
  110. t.mut.RUnlock()
  111. if !ok {
  112. return false
  113. }
  114. return f.Has(file, version, index)
  115. }
  116. // NumberOfBlocksInProgress returns the number of blocks the device has downloaded
  117. // for all folders.
  118. func (t *deviceDownloadState) NumberOfBlocksInProgress() int {
  119. if t == nil {
  120. return 0
  121. }
  122. n := 0
  123. t.mut.RLock()
  124. for _, folder := range t.folders {
  125. n += folder.NumberOfBlocksInProgress()
  126. }
  127. t.mut.RUnlock()
  128. return n
  129. }
  130. func newDeviceDownloadState() *deviceDownloadState {
  131. return &deviceDownloadState{
  132. mut: sync.NewRWMutex(),
  133. folders: make(map[string]*deviceFolderDownloadState),
  134. }
  135. }