interface.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(folder string, hash []byte) (iter.Seq[BlockMapEntry], func() error)
  38. // Cleanup
  39. DropAllFiles(folder string, device protocol.DeviceID) error
  40. DropDevice(device protocol.DeviceID) error
  41. DropFilesNamed(folder string, device protocol.DeviceID, names []string) error
  42. DropFolder(folder string) error
  43. // Various metadata
  44. GetDeviceSequence(folder string, device protocol.DeviceID) (int64, error)
  45. ListFolders() ([]string, error)
  46. ListDevicesForFolder(folder string) ([]protocol.DeviceID, error)
  47. RemoteSequences(folder string) (map[protocol.DeviceID]int64, error)
  48. // Counts
  49. CountGlobal(folder string) (Counts, error)
  50. CountLocal(folder string, device protocol.DeviceID) (Counts, error)
  51. CountNeed(folder string, device protocol.DeviceID) (Counts, error)
  52. CountReceiveOnlyChanged(folder string) (Counts, error)
  53. // Index IDs
  54. DropAllIndexIDs() error
  55. GetIndexID(folder string, device protocol.DeviceID) (protocol.IndexID, error)
  56. SetIndexID(folder string, device protocol.DeviceID, id protocol.IndexID) error
  57. // MtimeFS
  58. DeleteMtime(folder, name string) error
  59. GetMtime(folder, name string) (ondisk, virtual time.Time)
  60. PutMtime(folder, name string, ondisk, virtual time.Time) error
  61. KV
  62. }
  63. // Generic KV store
  64. type KV interface {
  65. GetKV(key string) ([]byte, error)
  66. PutKV(key string, val []byte) error
  67. DeleteKV(key string) error
  68. PrefixKV(prefix string) (iter.Seq[KeyValue], func() error)
  69. }
  70. type BlockMapEntry struct {
  71. BlocklistHash []byte
  72. Offset int64
  73. BlockIndex int
  74. Size int
  75. FileName string
  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 protocol.FlagLocal
  87. Type protocol.FileInfoType
  88. Deleted bool
  89. }
  90. func (f *FileMetadata) ModTime() time.Time {
  91. return time.Unix(0, f.ModNanos)
  92. }
  93. func (f *FileMetadata) IsReceiveOnlyChanged() bool {
  94. return f.LocalFlags&protocol.FlagLocalReceiveOnly != 0
  95. }
  96. func (f *FileMetadata) IsDirectory() bool {
  97. return f.Type == protocol.FileInfoTypeDirectory
  98. }
  99. func (f *FileMetadata) ShouldConflict() bool {
  100. return f.LocalFlags&protocol.LocalConflictFlags != 0
  101. }
  102. func (f *FileMetadata) IsInvalid() bool {
  103. return f.LocalFlags.IsInvalid()
  104. }