set.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // Package files provides a set type to track local/remote files with newness checks.
  2. package files
  3. import (
  4. "sync"
  5. "github.com/calmh/syncthing/cid"
  6. "github.com/calmh/syncthing/lamport"
  7. "github.com/calmh/syncthing/protocol"
  8. "github.com/calmh/syncthing/scanner"
  9. )
  10. type fileRecord struct {
  11. File scanner.File
  12. Usage int
  13. Global bool
  14. }
  15. type bitset uint64
  16. type Set struct {
  17. sync.Mutex
  18. files map[key]fileRecord
  19. remoteKey [64]map[string]key
  20. changes [64]uint64
  21. globalAvailability map[string]bitset
  22. globalKey map[string]key
  23. }
  24. func NewSet() *Set {
  25. var m = Set{
  26. files: make(map[key]fileRecord),
  27. globalAvailability: make(map[string]bitset),
  28. globalKey: make(map[string]key),
  29. }
  30. return &m
  31. }
  32. func (m *Set) Replace(id uint, fs []scanner.File) {
  33. if debug {
  34. l.Debugf("Replace(%d, [%d])", id, len(fs))
  35. }
  36. if id > 63 {
  37. panic("Connection ID must be in the range 0 - 63 inclusive")
  38. }
  39. m.Lock()
  40. if len(fs) == 0 || !m.equals(id, fs) {
  41. m.changes[id]++
  42. m.replace(id, fs)
  43. }
  44. m.Unlock()
  45. }
  46. func (m *Set) ReplaceWithDelete(id uint, fs []scanner.File) {
  47. if debug {
  48. l.Debugf("ReplaceWithDelete(%d, [%d])", id, len(fs))
  49. }
  50. if id > 63 {
  51. panic("Connection ID must be in the range 0 - 63 inclusive")
  52. }
  53. m.Lock()
  54. if len(fs) == 0 || !m.equals(id, fs) {
  55. m.changes[id]++
  56. var nf = make(map[string]key, len(fs))
  57. for _, f := range fs {
  58. nf[f.Name] = keyFor(f)
  59. }
  60. // For previously existing files not in the list, add them to the list
  61. // with the relevant delete flags etc set. Previously existing files
  62. // with the delete bit already set are not modified.
  63. for _, ck := range m.remoteKey[cid.LocalID] {
  64. if _, ok := nf[ck.Name]; !ok {
  65. cf := m.files[ck].File
  66. if !protocol.IsDeleted(cf.Flags) {
  67. cf.Flags |= protocol.FlagDeleted
  68. cf.Blocks = nil
  69. cf.Size = 0
  70. cf.Version = lamport.Default.Tick(cf.Version)
  71. }
  72. fs = append(fs, cf)
  73. if debug {
  74. l.Debugln("deleted:", ck.Name)
  75. }
  76. }
  77. }
  78. m.replace(id, fs)
  79. }
  80. m.Unlock()
  81. }
  82. func (m *Set) Update(id uint, fs []scanner.File) {
  83. if debug {
  84. l.Debugf("Update(%d, [%d])", id, len(fs))
  85. }
  86. m.Lock()
  87. m.update(id, fs)
  88. m.changes[id]++
  89. m.Unlock()
  90. }
  91. func (m *Set) Need(id uint) []scanner.File {
  92. if debug {
  93. l.Debugf("Need(%d)", id)
  94. }
  95. m.Lock()
  96. var fs = make([]scanner.File, 0, len(m.globalKey)/2) // Just a guess, but avoids too many reallocations
  97. rkID := m.remoteKey[id]
  98. for gk, gf := range m.files {
  99. if !gf.Global || gf.File.Suppressed {
  100. continue
  101. }
  102. if gk.newerThan(rkID[gk.Name]) {
  103. fs = append(fs, gf.File)
  104. }
  105. }
  106. m.Unlock()
  107. return fs
  108. }
  109. func (m *Set) Have(id uint) []scanner.File {
  110. if debug {
  111. l.Debugf("Have(%d)", id)
  112. }
  113. var fs = make([]scanner.File, 0, len(m.remoteKey[id]))
  114. m.Lock()
  115. for _, rk := range m.remoteKey[id] {
  116. fs = append(fs, m.files[rk].File)
  117. }
  118. m.Unlock()
  119. return fs
  120. }
  121. func (m *Set) Global() []scanner.File {
  122. if debug {
  123. l.Debugf("Global()")
  124. }
  125. m.Lock()
  126. var fs = make([]scanner.File, 0, len(m.globalKey))
  127. for _, file := range m.files {
  128. if file.Global {
  129. fs = append(fs, file.File)
  130. }
  131. }
  132. m.Unlock()
  133. return fs
  134. }
  135. func (m *Set) Get(id uint, file string) scanner.File {
  136. m.Lock()
  137. defer m.Unlock()
  138. if debug {
  139. l.Debugf("Get(%d, %q)", id, file)
  140. }
  141. return m.files[m.remoteKey[id][file]].File
  142. }
  143. func (m *Set) GetGlobal(file string) scanner.File {
  144. m.Lock()
  145. defer m.Unlock()
  146. if debug {
  147. l.Debugf("GetGlobal(%q)", file)
  148. }
  149. return m.files[m.globalKey[file]].File
  150. }
  151. func (m *Set) Availability(name string) bitset {
  152. m.Lock()
  153. defer m.Unlock()
  154. av := m.globalAvailability[name]
  155. if debug {
  156. l.Debugf("Availability(%q) = %0x", name, av)
  157. }
  158. return av
  159. }
  160. func (m *Set) Changes(id uint) uint64 {
  161. m.Lock()
  162. defer m.Unlock()
  163. if debug {
  164. l.Debugf("Changes(%d)", id)
  165. }
  166. return m.changes[id]
  167. }
  168. func (m *Set) equals(id uint, fs []scanner.File) bool {
  169. curWithoutDeleted := make(map[string]key)
  170. for _, k := range m.remoteKey[id] {
  171. f := m.files[k].File
  172. if !protocol.IsDeleted(f.Flags) {
  173. curWithoutDeleted[f.Name] = k
  174. }
  175. }
  176. if len(curWithoutDeleted) != len(fs) {
  177. return false
  178. }
  179. for _, f := range fs {
  180. if curWithoutDeleted[f.Name] != keyFor(f) {
  181. return false
  182. }
  183. }
  184. return true
  185. }
  186. func (m *Set) update(cid uint, fs []scanner.File) {
  187. remFiles := m.remoteKey[cid]
  188. for _, f := range fs {
  189. n := f.Name
  190. fk := keyFor(f)
  191. if ck, ok := remFiles[n]; ok && ck == fk {
  192. // The remote already has exactly this file, skip it
  193. continue
  194. }
  195. remFiles[n] = fk
  196. // Keep the block list or increment the usage
  197. if br, ok := m.files[fk]; !ok {
  198. m.files[fk] = fileRecord{
  199. Usage: 1,
  200. File: f,
  201. }
  202. } else {
  203. br.Usage++
  204. m.files[fk] = br
  205. }
  206. // Update global view
  207. gk, ok := m.globalKey[n]
  208. switch {
  209. case ok && fk == gk:
  210. av := m.globalAvailability[n]
  211. av |= 1 << cid
  212. m.globalAvailability[n] = av
  213. case fk.newerThan(gk):
  214. if ok {
  215. f := m.files[gk]
  216. f.Global = false
  217. m.files[gk] = f
  218. }
  219. f := m.files[fk]
  220. f.Global = true
  221. m.files[fk] = f
  222. m.globalKey[n] = fk
  223. m.globalAvailability[n] = 1 << cid
  224. }
  225. }
  226. }
  227. func (m *Set) replace(cid uint, fs []scanner.File) {
  228. // Decrement usage for all files belonging to this remote, and remove
  229. // those that are no longer needed.
  230. for _, fk := range m.remoteKey[cid] {
  231. br, ok := m.files[fk]
  232. switch {
  233. case ok && br.Usage == 1:
  234. delete(m.files, fk)
  235. case ok && br.Usage > 1:
  236. br.Usage--
  237. m.files[fk] = br
  238. }
  239. }
  240. // Clear existing remote remoteKey
  241. m.remoteKey[cid] = make(map[string]key)
  242. // Recalculate global based on all remaining remoteKey
  243. for n := range m.globalKey {
  244. var nk key // newest key
  245. var na bitset // newest availability
  246. for i, rem := range m.remoteKey {
  247. if rk, ok := rem[n]; ok {
  248. switch {
  249. case rk == nk:
  250. na |= 1 << uint(i)
  251. case rk.newerThan(nk):
  252. nk = rk
  253. na = 1 << uint(i)
  254. }
  255. }
  256. }
  257. if na != 0 {
  258. // Someone had the file
  259. m.globalKey[n] = nk
  260. m.globalAvailability[n] = na
  261. } else {
  262. // Noone had the file
  263. delete(m.globalKey, n)
  264. delete(m.globalAvailability, n)
  265. }
  266. }
  267. // Add new remote remoteKey to the mix
  268. m.update(cid, fs)
  269. }