blockmap.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. "encoding/binary"
  9. "fmt"
  10. "github.com/syncthing/syncthing/lib/osutil"
  11. )
  12. var blockFinder *BlockFinder
  13. type BlockFinder struct {
  14. db *Lowlevel
  15. }
  16. func NewBlockFinder(db *Lowlevel) *BlockFinder {
  17. if blockFinder != nil {
  18. return blockFinder
  19. }
  20. return &BlockFinder{
  21. db: db,
  22. }
  23. }
  24. func (f *BlockFinder) String() string {
  25. return fmt.Sprintf("BlockFinder@%p", f)
  26. }
  27. // Iterate takes an iterator function which iterates over all matching blocks
  28. // for the given hash. The iterator function has to return either true (if
  29. // they are happy with the block) or false to continue iterating for whatever
  30. // reason. The iterator finally returns the result, whether or not a
  31. // satisfying block was eventually found.
  32. func (f *BlockFinder) Iterate(folders []string, hash []byte, iterFn func(string, string, int32) bool) bool {
  33. t, err := f.db.newReadOnlyTransaction()
  34. if err != nil {
  35. return false
  36. }
  37. defer t.close()
  38. var key []byte
  39. for _, folder := range folders {
  40. key, err = f.db.keyer.GenerateBlockMapKey(key, []byte(folder), hash, nil)
  41. if err != nil {
  42. return false
  43. }
  44. iter, err := t.NewPrefixIterator(key)
  45. if err != nil {
  46. return false
  47. }
  48. for iter.Next() && iter.Error() == nil {
  49. file := string(f.db.keyer.NameFromBlockMapKey(iter.Key()))
  50. index := int32(binary.BigEndian.Uint32(iter.Value()))
  51. if iterFn(folder, osutil.NativeFilename(file), index) {
  52. iter.Release()
  53. return true
  54. }
  55. }
  56. iter.Release()
  57. }
  58. return false
  59. }