needle_types.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package types
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/chrislusf/seaweedfs/weed/util"
  6. )
  7. type Offset struct {
  8. OffsetHigher
  9. OffsetLower
  10. }
  11. type Size int32
  12. func (s Size) IsDeleted() bool {
  13. return s < 0 || s == TombstoneFileSize
  14. }
  15. func (s Size) IsValid() bool {
  16. return s > 0 && s != TombstoneFileSize
  17. }
  18. type OffsetLower struct {
  19. b3 byte
  20. b2 byte
  21. b1 byte
  22. b0 byte // the smaller byte
  23. }
  24. type Cookie uint32
  25. const (
  26. SizeSize = 4 // uint32 size
  27. NeedleHeaderSize = CookieSize + NeedleIdSize + SizeSize
  28. NeedleMapEntrySize = NeedleIdSize + OffsetSize + SizeSize
  29. TimestampSize = 8 // int64 size
  30. NeedlePaddingSize = 8
  31. TombstoneFileSize = Size(-1)
  32. CookieSize = 4
  33. )
  34. func CookieToBytes(bytes []byte, cookie Cookie) {
  35. util.Uint32toBytes(bytes, uint32(cookie))
  36. }
  37. func Uint32ToCookie(cookie uint32) Cookie {
  38. return Cookie(cookie)
  39. }
  40. func BytesToCookie(bytes []byte) Cookie {
  41. return Cookie(util.BytesToUint32(bytes[0:4]))
  42. }
  43. func ParseCookie(cookieString string) (Cookie, error) {
  44. cookie, err := strconv.ParseUint(cookieString, 16, 32)
  45. if err != nil {
  46. return 0, fmt.Errorf("needle cookie %s format error: %v", cookieString, err)
  47. }
  48. return Cookie(cookie), nil
  49. }
  50. func BytesToSize(bytes []byte) Size {
  51. return Size(util.BytesToUint32(bytes))
  52. }
  53. func SizeToBytes(bytes []byte, size Size) {
  54. util.Uint32toBytes(bytes, uint32(size))
  55. }