offset_5bytes.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // +build 5BytesOffset
  2. package types
  3. import (
  4. "fmt"
  5. )
  6. type OffsetHigher struct {
  7. b4 byte
  8. }
  9. const (
  10. OffsetSize = 4 + 1
  11. MaxPossibleVolumeSize uint64 = 4 * 1024 * 1024 * 1024 * 8 * 256 /* 256 is from the extra byte */ // 8TB
  12. )
  13. func OffsetToBytes(bytes []byte, offset Offset) {
  14. bytes[4] = offset.b4
  15. bytes[3] = offset.b0
  16. bytes[2] = offset.b1
  17. bytes[1] = offset.b2
  18. bytes[0] = offset.b3
  19. }
  20. // only for testing, will be removed later.
  21. func Uint32ToOffset(offset uint32) Offset {
  22. return Offset{
  23. OffsetHigher: OffsetHigher{
  24. b4: byte(offset >> 32),
  25. },
  26. OffsetLower: OffsetLower{
  27. b0: byte(offset),
  28. b1: byte(offset >> 8),
  29. b2: byte(offset >> 16),
  30. b3: byte(offset >> 24),
  31. },
  32. }
  33. }
  34. func BytesToOffset(bytes []byte) Offset {
  35. return Offset{
  36. OffsetHigher: OffsetHigher{
  37. b4: bytes[4],
  38. },
  39. OffsetLower: OffsetLower{
  40. b0: bytes[3],
  41. b1: bytes[2],
  42. b2: bytes[1],
  43. b3: bytes[0],
  44. },
  45. }
  46. }
  47. func (offset Offset) IsZero() bool {
  48. return offset.b0 == 0 && offset.b1 == 0 && offset.b2 == 0 && offset.b3 == 0 && offset.b4 == 0
  49. }
  50. func ToOffset(offset int64) Offset {
  51. smaller := offset / int64(NeedlePaddingSize)
  52. return Offset{
  53. OffsetHigher: OffsetHigher{
  54. b4: byte(smaller >> 32),
  55. },
  56. OffsetLower: OffsetLower{
  57. b0: byte(smaller),
  58. b1: byte(smaller >> 8),
  59. b2: byte(smaller >> 16),
  60. b3: byte(smaller >> 24),
  61. },
  62. }
  63. }
  64. func (offset Offset) ToActualOffset() (actualOffset int64) {
  65. return (int64(offset.b0) + int64(offset.b1)<<8 + int64(offset.b2)<<16 + int64(offset.b3)<<24 + int64(offset.b4)<<32) * int64(NeedlePaddingSize)
  66. }
  67. func (offset Offset) String() string {
  68. return fmt.Sprintf("%d", int64(offset.b0)+int64(offset.b1)<<8+int64(offset.b2)<<16+int64(offset.b3)<<24+int64(offset.b4)<<32)
  69. }