folderdb_open.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "time"
  9. "github.com/syncthing/syncthing/lib/protocol"
  10. )
  11. type folderDB struct {
  12. folderID string
  13. *baseDB
  14. localDeviceIdx int64
  15. deleteRetention time.Duration
  16. }
  17. func openFolderDB(folder, path string, deleteRetention time.Duration) (*folderDB, error) {
  18. pragmas := []string{
  19. "journal_mode = WAL",
  20. "optimize = 0x10002",
  21. "auto_vacuum = INCREMENTAL",
  22. "default_temp_store = MEMORY",
  23. "temp_store = MEMORY",
  24. }
  25. schemas := []string{
  26. "sql/schema/common/*",
  27. "sql/schema/folder/*",
  28. }
  29. migrations := []string{
  30. "sql/migrations/common/*",
  31. "sql/migrations/folder/*",
  32. }
  33. base, err := openBase(path, maxDBConns, pragmas, schemas, migrations)
  34. if err != nil {
  35. return nil, err
  36. }
  37. fdb := &folderDB{
  38. folderID: folder,
  39. baseDB: base,
  40. deleteRetention: deleteRetention,
  41. }
  42. _ = fdb.PutKV("folderID", []byte(folder))
  43. // Touch device IDs that should always exist and have a low index
  44. // numbers, and will never change
  45. fdb.localDeviceIdx, _ = fdb.deviceIdxLocked(protocol.LocalDeviceID)
  46. fdb.tplInput["LocalDeviceIdx"] = fdb.localDeviceIdx
  47. return fdb, nil
  48. }
  49. // Open the database with options suitable for the migration inserts. This
  50. // is not a safe mode of operation for normal processing, use only for bulk
  51. // inserts with a close afterwards.
  52. func openFolderDBForMigration(folder, path string, deleteRetention time.Duration) (*folderDB, error) {
  53. pragmas := []string{
  54. "journal_mode = OFF",
  55. "default_temp_store = MEMORY",
  56. "temp_store = MEMORY",
  57. "foreign_keys = 0",
  58. "synchronous = 0",
  59. "locking_mode = EXCLUSIVE",
  60. }
  61. schemas := []string{
  62. "sql/schema/common/*",
  63. "sql/schema/folder/*",
  64. }
  65. base, err := openBase(path, 1, pragmas, schemas, nil)
  66. if err != nil {
  67. return nil, err
  68. }
  69. fdb := &folderDB{
  70. folderID: folder,
  71. baseDB: base,
  72. deleteRetention: deleteRetention,
  73. }
  74. // Touch device IDs that should always exist and have a low index
  75. // numbers, and will never change
  76. fdb.localDeviceIdx, _ = fdb.deviceIdxLocked(protocol.LocalDeviceID)
  77. fdb.tplInput["LocalDeviceIdx"] = fdb.localDeviceIdx
  78. return fdb, nil
  79. }
  80. func (s *folderDB) deviceIdxLocked(deviceID protocol.DeviceID) (int64, error) {
  81. devStr := deviceID.String()
  82. if _, err := s.stmt(`
  83. INSERT OR IGNORE INTO devices(device_id)
  84. VALUES (?)
  85. `).Exec(devStr); err != nil {
  86. return 0, wrap(err)
  87. }
  88. var idx int64
  89. if err := s.stmt(`
  90. SELECT idx FROM devices
  91. WHERE device_id = ?
  92. `).Get(&idx, devStr); err != nil {
  93. return 0, wrap(err)
  94. }
  95. return idx, nil
  96. }