You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
996 B

  1. package idx
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  4. )
  5. // firstInvalidIndex find the first index the failed lessThanOrEqualToFn function's requirement.
  6. func FirstInvalidIndex(bytes []byte, lessThanOrEqualToFn func(key types.NeedleId, offset types.Offset, size types.Size) (bool, error)) (int, error) {
  7. left, right := 0, len(bytes)/types.NeedleMapEntrySize-1
  8. index := right + 1
  9. for left <= right {
  10. mid := left + (right-left)>>1
  11. loc := mid * types.NeedleMapEntrySize
  12. key := types.BytesToNeedleId(bytes[loc : loc+types.NeedleIdSize])
  13. offset := types.BytesToOffset(bytes[loc+types.NeedleIdSize : loc+types.NeedleIdSize+types.OffsetSize])
  14. size := types.BytesToSize(bytes[loc+types.NeedleIdSize+types.OffsetSize : loc+types.NeedleIdSize+types.OffsetSize+types.SizeSize])
  15. res, err := lessThanOrEqualToFn(key, offset, size)
  16. if err != nil {
  17. return -1, err
  18. }
  19. if res {
  20. left = mid + 1
  21. } else {
  22. index = mid
  23. right = mid - 1
  24. }
  25. }
  26. return index, nil
  27. }