rain 6bc7e6d372 Update collection and segment id type and Refactor channel controller
Signed-off-by: rain <boyan.wang@zilliz.com>
2020-09-18 15:55:33 +08:00

44 lines
639 B
Go

package id
import (
"encoding/binary"
"github.com/rs/xid"
"github.com/czs007/suvlim/errors"
)
type ID struct {
xid.ID
}
func BytesToUint64(b []byte) (uint64, error) {
if len(b) != 12 {
return 0, errors.Errorf("invalid data, must 12 bytes, but %d", len(b))
}
return binary.BigEndian.Uint64(b), nil
}
// Uint64ToBytes converts uint64 to a byte slice.
func Uint64ToBytes(v uint64) []byte {
b := make([]byte, 12)
binary.BigEndian.PutUint64(b, v)
return b
}
func New() ID {
return ID{
xid.New(),
}
}
func (id ID) Uint64() uint64 {
b := id.Bytes()
if len(b) != 12 {
return 0
}
return binary.BigEndian.Uint64(b)
}