counts.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (C) 2014 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. "fmt"
  9. "strings"
  10. "github.com/syncthing/syncthing/lib/protocol"
  11. )
  12. type Counts struct {
  13. Files int
  14. Directories int
  15. Symlinks int
  16. Deleted int
  17. Bytes int64
  18. Sequence int64 // zero for the global state
  19. DeviceID protocol.DeviceID // device ID for remote devices, or special values for local/global
  20. LocalFlags protocol.FlagLocal // the local flag for this count bucket
  21. }
  22. func (c Counts) Add(other Counts) Counts {
  23. return Counts{
  24. Files: c.Files + other.Files,
  25. Directories: c.Directories + other.Directories,
  26. Symlinks: c.Symlinks + other.Symlinks,
  27. Deleted: c.Deleted + other.Deleted,
  28. Bytes: c.Bytes + other.Bytes,
  29. Sequence: c.Sequence + other.Sequence,
  30. DeviceID: protocol.EmptyDeviceID,
  31. LocalFlags: c.LocalFlags | other.LocalFlags,
  32. }
  33. }
  34. func (c Counts) TotalItems() int {
  35. return c.Files + c.Directories + c.Symlinks + c.Deleted
  36. }
  37. func (c Counts) String() string {
  38. var flags strings.Builder
  39. if c.LocalFlags&protocol.FlagLocalNeeded != 0 {
  40. flags.WriteString("Need")
  41. }
  42. if c.LocalFlags&protocol.FlagLocalIgnored != 0 {
  43. flags.WriteString("Ignored")
  44. }
  45. if c.LocalFlags&protocol.FlagLocalMustRescan != 0 {
  46. flags.WriteString("Rescan")
  47. }
  48. if c.LocalFlags&protocol.FlagLocalReceiveOnly != 0 {
  49. flags.WriteString("Recvonly")
  50. }
  51. if c.LocalFlags&protocol.FlagLocalUnsupported != 0 {
  52. flags.WriteString("Unsupported")
  53. }
  54. if c.LocalFlags != 0 {
  55. flags.WriteString(fmt.Sprintf("(%x)", c.LocalFlags))
  56. }
  57. if flags.Len() == 0 {
  58. flags.WriteString("---")
  59. }
  60. return fmt.Sprintf("{Device:%v, Files:%d, Dirs:%d, Symlinks:%d, Del:%d, Bytes:%d, Seq:%d, Flags:%s}", c.DeviceID, c.Files, c.Directories, c.Symlinks, c.Deleted, c.Bytes, c.Sequence, flags.String())
  61. }
  62. // Equal compares the numbers only, not sequence/dev/flags.
  63. func (c Counts) Equal(o Counts) bool {
  64. return c.Files == o.Files && c.Directories == o.Directories && c.Symlinks == o.Symlinks && c.Deleted == o.Deleted && c.Bytes == o.Bytes
  65. }