db_open.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 2025 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 sqlite
  7. import (
  8. "os"
  9. "path/filepath"
  10. "sync"
  11. "time"
  12. "github.com/syncthing/syncthing/internal/db"
  13. )
  14. const maxDBConns = 16
  15. type DB struct {
  16. pathBase string
  17. deleteRetention time.Duration
  18. *baseDB
  19. folderDBsMut sync.RWMutex
  20. folderDBs map[string]*folderDB
  21. folderDBOpener func(folder, path string, deleteRetention time.Duration) (*folderDB, error)
  22. }
  23. var _ db.DB = (*DB)(nil)
  24. type Option func(*DB)
  25. func WithDeleteRetention(d time.Duration) Option {
  26. return func(s *DB) {
  27. s.deleteRetention = d
  28. }
  29. }
  30. func Open(path string, opts ...Option) (*DB, error) {
  31. pragmas := []string{
  32. "journal_mode = WAL",
  33. "optimize = 0x10002",
  34. "auto_vacuum = INCREMENTAL",
  35. "default_temp_store = MEMORY",
  36. "temp_store = MEMORY",
  37. }
  38. schemas := []string{
  39. "sql/schema/common/*",
  40. "sql/schema/main/*",
  41. }
  42. os.MkdirAll(path, 0o700)
  43. mainPath := filepath.Join(path, "main.db")
  44. mainBase, err := openBase(mainPath, maxDBConns, pragmas, schemas, nil)
  45. if err != nil {
  46. return nil, err
  47. }
  48. db := &DB{
  49. pathBase: path,
  50. baseDB: mainBase,
  51. folderDBs: make(map[string]*folderDB),
  52. folderDBOpener: openFolderDB,
  53. }
  54. return db, nil
  55. }
  56. // Open the database with options suitable for the migration inserts. This
  57. // is not a safe mode of operation for normal processing, use only for bulk
  58. // inserts with a close afterwards.
  59. func OpenForMigration(path string) (*DB, error) {
  60. pragmas := []string{
  61. "journal_mode = OFF",
  62. "default_temp_store = MEMORY",
  63. "temp_store = MEMORY",
  64. "foreign_keys = 0",
  65. "synchronous = 0",
  66. "locking_mode = EXCLUSIVE",
  67. }
  68. schemas := []string{
  69. "sql/schema/common/*",
  70. "sql/schema/main/*",
  71. }
  72. os.MkdirAll(path, 0o700)
  73. mainPath := filepath.Join(path, "main.db")
  74. mainBase, err := openBase(mainPath, 1, pragmas, schemas, nil)
  75. if err != nil {
  76. return nil, err
  77. }
  78. db := &DB{
  79. pathBase: path,
  80. baseDB: mainBase,
  81. folderDBs: make(map[string]*folderDB),
  82. folderDBOpener: openFolderDBForMigration,
  83. }
  84. // // Touch device IDs that should always exist and have a low index
  85. // // numbers, and will never change
  86. // db.localDeviceIdx, _ = db.deviceIdxLocked(protocol.LocalDeviceID)
  87. // db.tplInput["LocalDeviceIdx"] = db.localDeviceIdx
  88. return db, nil
  89. }
  90. func OpenTemp() (*DB, error) {
  91. // SQLite has a memory mode, but it works differently with concurrency
  92. // compared to what we need with the WAL mode. So, no memory databases
  93. // for now.
  94. dir, err := os.MkdirTemp("", "syncthing-db")
  95. if err != nil {
  96. return nil, wrap(err)
  97. }
  98. path := filepath.Join(dir, "db")
  99. l.Debugln("Test DB in", path)
  100. return Open(path)
  101. }
  102. func (s *DB) Close() error {
  103. s.folderDBsMut.Lock()
  104. defer s.folderDBsMut.Unlock()
  105. for folder, fdb := range s.folderDBs {
  106. fdb.Close()
  107. delete(s.folderDBs, folder)
  108. }
  109. return wrap(s.baseDB.Close())
  110. }