1
0

leveldb_dbinstance_updateschema.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (C) 2018 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. "strings"
  9. "github.com/syncthing/syncthing/lib/protocol"
  10. "github.com/syndtr/goleveldb/leveldb/util"
  11. )
  12. const dbVersion = 3
  13. func (db *Instance) updateSchema() {
  14. miscDB := NewNamespacedKV(db, string(KeyTypeMiscData))
  15. prevVersion, _ := miscDB.Int64("dbVersion")
  16. if prevVersion >= dbVersion {
  17. return
  18. }
  19. l.Infof("Updating database schema version from %v to %v...", prevVersion, dbVersion)
  20. if prevVersion < 1 {
  21. db.updateSchema0to1()
  22. }
  23. if prevVersion < 2 {
  24. db.updateSchema1to2()
  25. }
  26. if prevVersion < 3 {
  27. db.updateSchema2to3()
  28. }
  29. miscDB.PutInt64("dbVersion", dbVersion)
  30. }
  31. func (db *Instance) updateSchema0to1() {
  32. t := db.newReadWriteTransaction()
  33. defer t.close()
  34. dbi := t.NewIterator(util.BytesPrefix([]byte{KeyTypeDevice}), nil)
  35. defer dbi.Release()
  36. symlinkConv := 0
  37. changedFolders := make(map[string]struct{})
  38. ignAdded := 0
  39. meta := newMetadataTracker() // dummy metadata tracker
  40. var gk []byte
  41. for dbi.Next() {
  42. folder := db.deviceKeyFolder(dbi.Key())
  43. device := db.deviceKeyDevice(dbi.Key())
  44. name := db.deviceKeyName(dbi.Key())
  45. // Remove files with absolute path (see #4799)
  46. if strings.HasPrefix(string(name), "/") {
  47. if _, ok := changedFolders[string(folder)]; !ok {
  48. changedFolders[string(folder)] = struct{}{}
  49. }
  50. gk = db.globalKeyInto(gk, folder, name)
  51. t.removeFromGlobal(gk, folder, device, nil, nil)
  52. t.Delete(dbi.Key())
  53. t.checkFlush()
  54. continue
  55. }
  56. // Change SYMLINK_FILE and SYMLINK_DIRECTORY types to the current SYMLINK
  57. // type (previously SYMLINK_UNKNOWN). It does this for all devices, both
  58. // local and remote, and does not reset delta indexes. It shouldn't really
  59. // matter what the symlink type is, but this cleans it up for a possible
  60. // future when SYMLINK_FILE and SYMLINK_DIRECTORY are no longer understood.
  61. var f protocol.FileInfo
  62. if err := f.Unmarshal(dbi.Value()); err != nil {
  63. // probably can't happen
  64. continue
  65. }
  66. if f.Type == protocol.FileInfoTypeDeprecatedSymlinkDirectory || f.Type == protocol.FileInfoTypeDeprecatedSymlinkFile {
  67. f.Type = protocol.FileInfoTypeSymlink
  68. bs, err := f.Marshal()
  69. if err != nil {
  70. panic("can't happen: " + err.Error())
  71. }
  72. t.Put(dbi.Key(), bs)
  73. t.checkFlush()
  74. symlinkConv++
  75. }
  76. // Add invalid files to global list
  77. if f.Invalid {
  78. gk = db.globalKeyInto(gk, folder, name)
  79. if t.updateGlobal(gk, folder, device, f, meta) {
  80. if _, ok := changedFolders[string(folder)]; !ok {
  81. changedFolders[string(folder)] = struct{}{}
  82. }
  83. ignAdded++
  84. }
  85. }
  86. }
  87. for folder := range changedFolders {
  88. db.dropFolderMeta([]byte(folder))
  89. }
  90. }
  91. // updateSchema1to2 introduces a sequenceKey->deviceKey bucket for local items
  92. // to allow iteration in sequence order (simplifies sending indexes).
  93. func (db *Instance) updateSchema1to2() {
  94. t := db.newReadWriteTransaction()
  95. defer t.close()
  96. var sk []byte
  97. var dk []byte
  98. for _, folderStr := range db.ListFolders() {
  99. folder := []byte(folderStr)
  100. db.withHave(folder, protocol.LocalDeviceID[:], nil, true, func(f FileIntf) bool {
  101. sk = db.sequenceKeyInto(sk, folder, f.SequenceNo())
  102. dk = db.deviceKeyInto(dk, folder, protocol.LocalDeviceID[:], []byte(f.FileName()))
  103. t.Put(sk, dk)
  104. t.checkFlush()
  105. return true
  106. })
  107. }
  108. }
  109. // updateSchema2to3 introduces a needKey->nil bucket for locally needed files.
  110. func (db *Instance) updateSchema2to3() {
  111. t := db.newReadWriteTransaction()
  112. defer t.close()
  113. var nk []byte
  114. var dk []byte
  115. for _, folderStr := range db.ListFolders() {
  116. folder := []byte(folderStr)
  117. db.withGlobal(folder, nil, true, func(f FileIntf) bool {
  118. name := []byte(f.FileName())
  119. dk = db.deviceKeyInto(dk, folder, protocol.LocalDeviceID[:], name)
  120. var v protocol.Vector
  121. haveFile, ok := db.getFileTrunc(dk, true)
  122. if ok {
  123. v = haveFile.FileVersion()
  124. }
  125. if !need(f, ok, v) {
  126. return true
  127. }
  128. nk = t.db.needKeyInto(nk, folder, []byte(f.FileName()))
  129. t.Put(nk, nil)
  130. t.checkFlush()
  131. return true
  132. })
  133. }
  134. }