leveldb.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (C) 2014 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 https://mozilla.org/MPL/2.0/.
  6. package db
  7. import (
  8. "bytes"
  9. "fmt"
  10. "github.com/syncthing/syncthing/lib/protocol"
  11. )
  12. func (vl VersionList) String() string {
  13. var b bytes.Buffer
  14. var id protocol.DeviceID
  15. b.WriteString("{")
  16. for i, v := range vl.Versions {
  17. if i > 0 {
  18. b.WriteString(", ")
  19. }
  20. copy(id[:], v.Device)
  21. fmt.Fprintf(&b, "{%v, %v, %v}", v.Version, id, v.Invalid)
  22. }
  23. b.WriteString("}")
  24. return b.String()
  25. }
  26. // update brings the VersionList up to date with file. It returns the updated
  27. // VersionList, a potentially removed old FileVersion and its index, as well as
  28. // the index where the new FileVersion was inserted.
  29. func (vl VersionList) update(folder, device []byte, file protocol.FileInfo, db *instance) (_ VersionList, removedFV FileVersion, removedAt int, insertedAt int) {
  30. removedAt, insertedAt = -1, -1
  31. for i, v := range vl.Versions {
  32. if bytes.Equal(v.Device, device) {
  33. removedAt = i
  34. removedFV = v
  35. vl.Versions = append(vl.Versions[:i], vl.Versions[i+1:]...)
  36. break
  37. }
  38. }
  39. nv := FileVersion{
  40. Device: device,
  41. Version: file.Version,
  42. Invalid: file.IsInvalid(),
  43. }
  44. for i, v := range vl.Versions {
  45. switch v.Version.Compare(file.Version) {
  46. case protocol.Equal:
  47. if nv.Invalid {
  48. continue
  49. }
  50. fallthrough
  51. case protocol.Lesser:
  52. // The version at this point in the list is equal to or lesser
  53. // ("older") than us. We insert ourselves in front of it.
  54. vl = vl.insertAt(i, nv)
  55. return vl, removedFV, removedAt, i
  56. case protocol.ConcurrentLesser, protocol.ConcurrentGreater:
  57. // The version at this point is in conflict with us. We must pull
  58. // the actual file metadata to determine who wins. If we win, we
  59. // insert ourselves in front of the loser here. (The "Lesser" and
  60. // "Greater" in the condition above is just based on the device
  61. // IDs in the version vector, which is not the only thing we use
  62. // to determine the winner.)
  63. //
  64. // A surprise missing file entry here is counted as a win for us.
  65. if of, ok := db.getFile(db.keyer.GenerateDeviceFileKey(nil, folder, v.Device, []byte(file.Name))); !ok || file.WinsConflict(of) {
  66. vl = vl.insertAt(i, nv)
  67. return vl, removedFV, removedAt, i
  68. }
  69. }
  70. }
  71. // We didn't find a position for an insert above, so append to the end.
  72. vl.Versions = append(vl.Versions, nv)
  73. return vl, removedFV, removedAt, len(vl.Versions) - 1
  74. }
  75. func (vl VersionList) insertAt(i int, v FileVersion) VersionList {
  76. vl.Versions = append(vl.Versions, FileVersion{})
  77. copy(vl.Versions[i+1:], vl.Versions[i:])
  78. vl.Versions[i] = v
  79. return vl
  80. }
  81. func (vl VersionList) Get(device []byte) (FileVersion, bool) {
  82. for _, v := range vl.Versions {
  83. if bytes.Equal(v.Device, device) {
  84. return v, true
  85. }
  86. }
  87. return FileVersion{}, false
  88. }
  89. type fileList []protocol.FileInfo
  90. func (fl fileList) Len() int {
  91. return len(fl)
  92. }
  93. func (fl fileList) Swap(a, b int) {
  94. fl[a], fl[b] = fl[b], fl[a]
  95. }
  96. func (fl fileList) Less(a, b int) bool {
  97. return fl[a].Name < fl[b].Name
  98. }
  99. // Flush batches to disk when they contain this many records.
  100. const batchFlushSize = 64