offset_5bytes.go 1.7 KB

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