leveldb.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 http://mozilla.org/MPL/2.0/.
  6. package db
  7. import (
  8. "bytes"
  9. "fmt"
  10. "github.com/syncthing/syncthing/lib/protocol"
  11. "github.com/syndtr/goleveldb/leveldb"
  12. "github.com/syndtr/goleveldb/leveldb/opt"
  13. )
  14. const (
  15. KeyTypeDevice = iota
  16. KeyTypeGlobal
  17. KeyTypeBlock
  18. KeyTypeDeviceStatistic
  19. KeyTypeFolderStatistic
  20. KeyTypeVirtualMtime
  21. KeyTypeFolderIdx
  22. KeyTypeDeviceIdx
  23. KeyTypeIndexID
  24. )
  25. func (l VersionList) String() string {
  26. var b bytes.Buffer
  27. var id protocol.DeviceID
  28. b.WriteString("{")
  29. for i, v := range l.Versions {
  30. if i > 0 {
  31. b.WriteString(", ")
  32. }
  33. copy(id[:], v.Device)
  34. fmt.Fprintf(&b, "{%d, %v}", v.Version, id)
  35. }
  36. b.WriteString("}")
  37. return b.String()
  38. }
  39. type fileList []protocol.FileInfo
  40. func (l fileList) Len() int {
  41. return len(l)
  42. }
  43. func (l fileList) Swap(a, b int) {
  44. l[a], l[b] = l[b], l[a]
  45. }
  46. func (l fileList) Less(a, b int) bool {
  47. return l[a].Name < l[b].Name
  48. }
  49. type dbReader interface {
  50. Get([]byte, *opt.ReadOptions) ([]byte, error)
  51. }
  52. // Flush batches to disk when they contain this many records.
  53. const batchFlushSize = 64
  54. func getFile(db dbReader, key []byte) (protocol.FileInfo, bool) {
  55. bs, err := db.Get(key, nil)
  56. if err == leveldb.ErrNotFound {
  57. return protocol.FileInfo{}, false
  58. }
  59. if err != nil {
  60. panic(err)
  61. }
  62. var f protocol.FileInfo
  63. err = f.Unmarshal(bs)
  64. if err != nil {
  65. panic(err)
  66. }
  67. return f, true
  68. }