interface.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 db
  7. import (
  8. "iter"
  9. "time"
  10. "github.com/syncthing/syncthing/lib/config"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. "github.com/thejerf/suture/v4"
  13. )
  14. type DBService interface {
  15. suture.Service
  16. // Starts maintenance asynchronously, if not already running
  17. StartMaintenance()
  18. }
  19. type DB interface {
  20. // Create a service that performs database maintenance periodically (no
  21. // more often than the requested interval)
  22. Service(maintenanceInterval time.Duration) DBService
  23. // Basics
  24. Update(folder string, device protocol.DeviceID, fs []protocol.FileInfo) error
  25. Close() error
  26. // Single files
  27. GetDeviceFile(folder string, device protocol.DeviceID, file string) (protocol.FileInfo, bool, error)
  28. GetGlobalAvailability(folder, file string) ([]protocol.DeviceID, error)
  29. GetGlobalFile(folder string, file string) (protocol.FileInfo, bool, error)
  30. // File iterators
  31. //
  32. // n.b. there is a slight inconsistency in the return types where some
  33. // return a FileInfo iterator and some a FileMetadata iterator. The
  34. // latter is more lightweight, and the discrepancy depends on how the
  35. // functions tend to be used. We can introduce more variations as
  36. // required.
  37. AllGlobalFiles(folder string) (iter.Seq[FileMetadata], func() error)
  38. AllGlobalFilesPrefix(folder string, prefix string) (iter.Seq[FileMetadata], func() error)
  39. AllLocalFiles(folder string, device protocol.DeviceID) (iter.Seq[protocol.FileInfo], func() error)
  40. AllLocalFilesBySequence(folder string, device protocol.DeviceID, startSeq int64, limit int) (iter.Seq[protocol.FileInfo], func() error)
  41. AllLocalFilesWithPrefix(folder string, device protocol.DeviceID, prefix string) (iter.Seq[protocol.FileInfo], func() error)
  42. AllLocalFilesWithBlocksHash(folder string, h []byte) (iter.Seq[FileMetadata], func() error)
  43. AllNeededGlobalFiles(folder string, device protocol.DeviceID, order config.PullOrder, limit, offset int) (iter.Seq[protocol.FileInfo], func() error)
  44. AllLocalBlocksWithHash(folder string, hash []byte) (iter.Seq[BlockMapEntry], func() error)
  45. // Cleanup
  46. DropAllFiles(folder string, device protocol.DeviceID) error
  47. DropDevice(device protocol.DeviceID) error
  48. DropFilesNamed(folder string, device protocol.DeviceID, names []string) error
  49. DropFolder(folder string) error
  50. // Various metadata
  51. GetDeviceSequence(folder string, device protocol.DeviceID) (int64, error)
  52. ListFolders() ([]string, error)
  53. ListDevicesForFolder(folder string) ([]protocol.DeviceID, error)
  54. RemoteSequences(folder string) (map[protocol.DeviceID]int64, error)
  55. // Counts
  56. CountGlobal(folder string) (Counts, error)
  57. CountLocal(folder string, device protocol.DeviceID) (Counts, error)
  58. CountNeed(folder string, device protocol.DeviceID) (Counts, error)
  59. CountReceiveOnlyChanged(folder string) (Counts, error)
  60. // Index IDs
  61. DropAllIndexIDs() error
  62. GetIndexID(folder string, device protocol.DeviceID) (protocol.IndexID, error)
  63. SetIndexID(folder string, device protocol.DeviceID, id protocol.IndexID) error
  64. // MtimeFS
  65. DeleteMtime(folder, name string) error
  66. GetMtime(folder, name string) (ondisk, virtual time.Time)
  67. PutMtime(folder, name string, ondisk, virtual time.Time) error
  68. KV
  69. }
  70. // Generic KV store
  71. type KV interface {
  72. GetKV(key string) ([]byte, error)
  73. PutKV(key string, val []byte) error
  74. DeleteKV(key string) error
  75. PrefixKV(prefix string) (iter.Seq[KeyValue], func() error)
  76. }
  77. type BlockMapEntry struct {
  78. BlocklistHash []byte
  79. Offset int64
  80. BlockIndex int
  81. Size int
  82. FileName string
  83. }
  84. type KeyValue struct {
  85. Key string
  86. Value []byte
  87. }
  88. type FileMetadata struct {
  89. Name string
  90. Sequence int64
  91. ModNanos int64
  92. Size int64
  93. LocalFlags protocol.FlagLocal
  94. Type protocol.FileInfoType
  95. Deleted bool
  96. }
  97. func (f *FileMetadata) ModTime() time.Time {
  98. return time.Unix(0, f.ModNanos)
  99. }
  100. func (f *FileMetadata) IsReceiveOnlyChanged() bool {
  101. return f.LocalFlags&protocol.FlagLocalReceiveOnly != 0
  102. }
  103. func (f *FileMetadata) IsDirectory() bool {
  104. return f.Type == protocol.FileInfoTypeDirectory
  105. }
  106. func (f *FileMetadata) ShouldConflict() bool {
  107. return f.LocalFlags&protocol.LocalConflictFlags != 0
  108. }
  109. func (f *FileMetadata) IsInvalid() bool {
  110. return f.LocalFlags.IsInvalid()
  111. }