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.

68 lines
1.9 KiB

  1. package erasure_coding
  2. type Interval struct {
  3. blockIndex int
  4. innerBlockOffset int64
  5. size uint32
  6. isLargeBlock bool
  7. }
  8. func locateData(largeBlockLength, smallBlockLength int64, datSize int64, offset int64, size uint32) (intervals []Interval) {
  9. blockIndex, isLargeBlock, innerBlockOffset := locateOffset(largeBlockLength, smallBlockLength, datSize, offset)
  10. nLargeBlockRows := int(datSize / (largeBlockLength * DataShardsCount))
  11. for size > 0 {
  12. interval := Interval{
  13. blockIndex: blockIndex,
  14. innerBlockOffset: innerBlockOffset,
  15. isLargeBlock: isLargeBlock,
  16. }
  17. blockRemaining := largeBlockLength - innerBlockOffset
  18. if !isLargeBlock {
  19. blockRemaining = smallBlockLength - innerBlockOffset
  20. }
  21. if int64(size) <= blockRemaining {
  22. interval.size = size
  23. intervals = append(intervals, interval)
  24. return
  25. }
  26. interval.size = uint32(blockRemaining)
  27. intervals = append(intervals, interval)
  28. size -= interval.size
  29. blockIndex += 1
  30. if isLargeBlock && blockIndex == nLargeBlockRows*DataShardsCount {
  31. isLargeBlock = false
  32. blockIndex = 0
  33. }
  34. innerBlockOffset = 0
  35. }
  36. return
  37. }
  38. func locateOffset(largeBlockLength, smallBlockLength int64, datSize int64, offset int64) (blockIndex int, isLargeBlock bool, innerBlockOffset int64) {
  39. largeRowSize := largeBlockLength * DataShardsCount
  40. nLargeBlockRows := datSize / (largeBlockLength * DataShardsCount)
  41. // if offset is within the large block area
  42. if offset < nLargeBlockRows*largeRowSize {
  43. isLargeBlock = true
  44. blockIndex, innerBlockOffset = locateOffsetWithinBlocks(largeBlockLength, offset)
  45. return
  46. }
  47. isLargeBlock = false
  48. offset -= nLargeBlockRows * largeRowSize
  49. blockIndex, innerBlockOffset = locateOffsetWithinBlocks(smallBlockLength, offset)
  50. return
  51. }
  52. func locateOffsetWithinBlocks(blockLength int64, offset int64) (blockIndex int, innerBlockOffset int64) {
  53. blockIndex = int(offset / blockLength)
  54. innerBlockOffset = offset % blockLength
  55. return
  56. }