needle_id_type.go 991 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package types
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/util"
  5. "strconv"
  6. )
  7. type NeedleId uint64
  8. const (
  9. NeedleIdSize = 8
  10. NeedleIdEmpty = 0
  11. )
  12. func NeedleIdToBytes(bytes []byte, needleId NeedleId) {
  13. util.Uint64toBytes(bytes, uint64(needleId))
  14. }
  15. // NeedleIdToUint64 used to send max needle id to master
  16. func NeedleIdToUint64(needleId NeedleId) uint64 {
  17. return uint64(needleId)
  18. }
  19. func Uint64ToNeedleId(needleId uint64) NeedleId {
  20. return NeedleId(needleId)
  21. }
  22. func BytesToNeedleId(bytes []byte) NeedleId {
  23. return NeedleId(util.BytesToUint64(bytes))
  24. }
  25. func (k NeedleId) String() string {
  26. return strconv.FormatUint(uint64(k), 16)
  27. }
  28. func (k NeedleId) FileId(volumeId uint32) string {
  29. return fmt.Sprintf("%d,%s00000000", volumeId, k.String())
  30. }
  31. func ParseNeedleId(idString string) (NeedleId, error) {
  32. key, err := strconv.ParseUint(idString, 16, 64)
  33. if err != nil {
  34. return 0, fmt.Errorf("needle id %s format error: %v", idString, err)
  35. }
  36. return NeedleId(key), nil
  37. }