2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-10-06 13:57:33 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-10-06 13:57:33 -07:00
|
|
|
|
2015-01-12 06:50:30 -07:00
|
|
|
package db
|
2014-10-06 13:57:33 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2015-06-03 00:47:39 -07:00
|
|
|
"fmt"
|
2014-10-06 13:57:33 -07:00
|
|
|
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2014-10-06 13:57:33 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type BlockFinder struct {
|
2019-12-02 00:18:04 -07:00
|
|
|
db *Lowlevel
|
2014-10-06 13:57:33 -07:00
|
|
|
}
|
|
|
|
|
2018-10-10 02:34:24 -07:00
|
|
|
func NewBlockFinder(db *Lowlevel) *BlockFinder {
|
2019-01-23 02:22:33 -07:00
|
|
|
return &BlockFinder{
|
2019-12-02 00:18:04 -07:00
|
|
|
db: db,
|
2014-10-06 13:57:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 00:47:39 -07:00
|
|
|
func (f *BlockFinder) String() string {
|
|
|
|
return fmt.Sprintf("BlockFinder@%p", f)
|
2014-10-06 13:57:33 -07:00
|
|
|
}
|
|
|
|
|
2015-04-28 13:32:10 -07:00
|
|
|
// Iterate takes an iterator function which iterates over all matching blocks
|
|
|
|
// for the given hash. The iterator function has to return either true (if
|
|
|
|
// they are happy with the block) or false to continue iterating for whatever
|
|
|
|
// reason. The iterator finally returns the result, whether or not a
|
|
|
|
// satisfying block was eventually found.
|
2015-09-04 03:01:00 -07:00
|
|
|
func (f *BlockFinder) Iterate(folders []string, hash []byte, iterFn func(string, string, int32) bool) bool {
|
2019-11-29 01:11:52 -07:00
|
|
|
t, err := f.db.newReadOnlyTransaction()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-01-23 02:22:33 -07:00
|
|
|
defer t.close()
|
|
|
|
|
2015-10-21 13:55:40 -07:00
|
|
|
var key []byte
|
2014-10-06 13:57:33 -07:00
|
|
|
for _, folder := range folders {
|
2019-11-29 01:11:52 -07:00
|
|
|
key, err = f.db.keyer.GenerateBlockMapKey(key, []byte(folder), hash, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
iter, err := t.NewPrefixIterator(key)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2014-10-06 13:57:33 -07:00
|
|
|
|
|
|
|
for iter.Next() && iter.Error() == nil {
|
2019-01-23 02:22:33 -07:00
|
|
|
file := string(f.db.keyer.NameFromBlockMapKey(iter.Key()))
|
2015-01-17 18:12:06 -07:00
|
|
|
index := int32(binary.BigEndian.Uint32(iter.Value()))
|
2014-11-05 16:41:51 -07:00
|
|
|
if iterFn(folder, osutil.NativeFilename(file), index) {
|
2019-01-23 02:22:33 -07:00
|
|
|
iter.Release()
|
2014-10-06 13:57:33 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 02:22:33 -07:00
|
|
|
iter.Release()
|
2014-10-06 13:57:33 -07:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|