needle_id_type.go 878 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package types
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/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 ParseNeedleId(idString string) (NeedleId, error) {
  29. key, err := strconv.ParseUint(idString, 16, 64)
  30. if err != nil {
  31. return 0, fmt.Errorf("needle id %s format error: %v", idString, err)
  32. }
  33. return NeedleId(key), nil
  34. }