lowlevel.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "os"
  9. "strings"
  10. "sync/atomic"
  11. "github.com/syndtr/goleveldb/leveldb"
  12. "github.com/syndtr/goleveldb/leveldb/errors"
  13. "github.com/syndtr/goleveldb/leveldb/opt"
  14. "github.com/syndtr/goleveldb/leveldb/storage"
  15. )
  16. const (
  17. dbMaxOpenFiles = 100
  18. dbWriteBuffer = 4 << 20
  19. )
  20. // Lowlevel is the lowest level database interface. It has a very simple
  21. // purpose: hold the actual *leveldb.DB database, and the in-memory state
  22. // that belong to that database. In the same way that a single on disk
  23. // database can only be opened once, there should be only one Lowlevel for
  24. // any given *leveldb.DB.
  25. type Lowlevel struct {
  26. committed int64 // atomic, must come first
  27. *leveldb.DB
  28. location string
  29. folderIdx *smallIndex
  30. deviceIdx *smallIndex
  31. }
  32. // Open attempts to open the database at the given location, and runs
  33. // recovery on it if opening fails. Worst case, if recovery is not possible,
  34. // the database is erased and created from scratch.
  35. func Open(location string) (*Lowlevel, error) {
  36. opts := &opt.Options{
  37. OpenFilesCacheCapacity: dbMaxOpenFiles,
  38. WriteBuffer: dbWriteBuffer,
  39. }
  40. return open(location, opts)
  41. }
  42. // OpenRO attempts to open the database at the given location, read only.
  43. func OpenRO(location string) (*Lowlevel, error) {
  44. opts := &opt.Options{
  45. OpenFilesCacheCapacity: dbMaxOpenFiles,
  46. ReadOnly: true,
  47. }
  48. return open(location, opts)
  49. }
  50. func open(location string, opts *opt.Options) (*Lowlevel, error) {
  51. db, err := leveldb.OpenFile(location, opts)
  52. if leveldbIsCorrupted(err) {
  53. db, err = leveldb.RecoverFile(location, opts)
  54. }
  55. if leveldbIsCorrupted(err) {
  56. // The database is corrupted, and we've tried to recover it but it
  57. // didn't work. At this point there isn't much to do beyond dropping
  58. // the database and reindexing...
  59. l.Infoln("Database corruption detected, unable to recover. Reinitializing...")
  60. if err := os.RemoveAll(location); err != nil {
  61. return nil, errorSuggestion{err, "failed to delete corrupted database"}
  62. }
  63. db, err = leveldb.OpenFile(location, opts)
  64. }
  65. if err != nil {
  66. return nil, errorSuggestion{err, "is another instance of Syncthing running?"}
  67. }
  68. return NewLowlevel(db, location), nil
  69. }
  70. // OpenMemory returns a new Lowlevel referencing an in-memory database.
  71. func OpenMemory() *Lowlevel {
  72. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  73. return NewLowlevel(db, "<memory>")
  74. }
  75. // ListFolders returns the list of folders currently in the database
  76. func (db *Lowlevel) ListFolders() []string {
  77. return db.folderIdx.Values()
  78. }
  79. // Committed returns the number of items committed to the database since startup
  80. func (db *Lowlevel) Committed() int64 {
  81. return atomic.LoadInt64(&db.committed)
  82. }
  83. func (db *Lowlevel) Put(key, val []byte, wo *opt.WriteOptions) error {
  84. atomic.AddInt64(&db.committed, 1)
  85. return db.DB.Put(key, val, wo)
  86. }
  87. func (db *Lowlevel) Delete(key []byte, wo *opt.WriteOptions) error {
  88. atomic.AddInt64(&db.committed, 1)
  89. return db.DB.Delete(key, wo)
  90. }
  91. // NewLowlevel wraps the given *leveldb.DB into a *lowlevel
  92. func NewLowlevel(db *leveldb.DB, location string) *Lowlevel {
  93. return &Lowlevel{
  94. DB: db,
  95. location: location,
  96. folderIdx: newSmallIndex(db, []byte{KeyTypeFolderIdx}),
  97. deviceIdx: newSmallIndex(db, []byte{KeyTypeDeviceIdx}),
  98. }
  99. }
  100. // A "better" version of leveldb's errors.IsCorrupted.
  101. func leveldbIsCorrupted(err error) bool {
  102. switch {
  103. case err == nil:
  104. return false
  105. case errors.IsCorrupted(err):
  106. return true
  107. case strings.Contains(err.Error(), "corrupted"):
  108. return true
  109. }
  110. return false
  111. }