lowlevel.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. db, err := leveldb.OpenFile(location, opts)
  41. if leveldbIsCorrupted(err) {
  42. db, err = leveldb.RecoverFile(location, opts)
  43. }
  44. if leveldbIsCorrupted(err) {
  45. // The database is corrupted, and we've tried to recover it but it
  46. // didn't work. At this point there isn't much to do beyond dropping
  47. // the database and reindexing...
  48. l.Infoln("Database corruption detected, unable to recover. Reinitializing...")
  49. if err := os.RemoveAll(location); err != nil {
  50. return nil, errorSuggestion{err, "failed to delete corrupted database"}
  51. }
  52. db, err = leveldb.OpenFile(location, opts)
  53. }
  54. if err != nil {
  55. return nil, errorSuggestion{err, "is another instance of Syncthing running?"}
  56. }
  57. return NewLowlevel(db, location), nil
  58. }
  59. // OpenMemory returns a new Lowlevel referencing an in-memory database.
  60. func OpenMemory() *Lowlevel {
  61. db, _ := leveldb.Open(storage.NewMemStorage(), nil)
  62. return NewLowlevel(db, "<memory>")
  63. }
  64. // Location returns the filesystem path where the database is stored
  65. func (db *Lowlevel) Location() string {
  66. return db.location
  67. }
  68. // ListFolders returns the list of folders currently in the database
  69. func (db *Lowlevel) ListFolders() []string {
  70. return db.folderIdx.Values()
  71. }
  72. // Committed returns the number of items committed to the database since startup
  73. func (db *Lowlevel) Committed() int64 {
  74. return atomic.LoadInt64(&db.committed)
  75. }
  76. func (db *Lowlevel) Put(key, val []byte, wo *opt.WriteOptions) error {
  77. atomic.AddInt64(&db.committed, 1)
  78. return db.DB.Put(key, val, wo)
  79. }
  80. func (db *Lowlevel) Delete(key []byte, wo *opt.WriteOptions) error {
  81. atomic.AddInt64(&db.committed, 1)
  82. return db.DB.Delete(key, wo)
  83. }
  84. // NewLowlevel wraps the given *leveldb.DB into a *lowlevel
  85. func NewLowlevel(db *leveldb.DB, location string) *Lowlevel {
  86. return &Lowlevel{
  87. DB: db,
  88. location: location,
  89. folderIdx: newSmallIndex(db, []byte{KeyTypeFolderIdx}),
  90. deviceIdx: newSmallIndex(db, []byte{KeyTypeDeviceIdx}),
  91. }
  92. }
  93. // A "better" version of leveldb's errors.IsCorrupted.
  94. func leveldbIsCorrupted(err error) bool {
  95. switch {
  96. case err == nil:
  97. return false
  98. case errors.IsCorrupted(err):
  99. return true
  100. case strings.Contains(err.Error(), "corrupted"):
  101. return true
  102. }
  103. return false
  104. }