interface.go 4.1 KB

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