seaweedfs/weed/storage/idx/binary_search.go
Eric Yang ddd6bee970
ADHOC: Volume fsck use a time cutoff param (#3626)
* ADHOC: cut off volumn fsck

* more

* fix typo

* add test

* modify name

* fix comment

* fix comments

* nit

* fix typo

* Update weed/shell/command_volume_fsck.go

Co-authored-by: root <root@HQ-10MSTD3EY.roblox.local>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2022-09-10 15:29:17 -07:00

30 lines
996 B
Go

package idx
import (
"github.com/seaweedfs/seaweedfs/weed/storage/types"
)
// firstInvalidIndex find the first index the failed lessThanOrEqualToFn function's requirement.
func FirstInvalidIndex(bytes []byte, lessThanOrEqualToFn func(key types.NeedleId, offset types.Offset, size types.Size) (bool, error)) (int, error) {
left, right := 0, len(bytes)/types.NeedleMapEntrySize-1
index := right + 1
for left <= right {
mid := left + (right-left)>>1
loc := mid * types.NeedleMapEntrySize
key := types.BytesToNeedleId(bytes[loc : loc+types.NeedleIdSize])
offset := types.BytesToOffset(bytes[loc+types.NeedleIdSize : loc+types.NeedleIdSize+types.OffsetSize])
size := types.BytesToSize(bytes[loc+types.NeedleIdSize+types.OffsetSize : loc+types.NeedleIdSize+types.OffsetSize+types.SizeSize])
res, err := lessThanOrEqualToFn(key, offset, size)
if err != nil {
return -1, err
}
if res {
left = mid + 1
} else {
index = mid
right = mid - 1
}
}
return index, nil
}