mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-12 15:51:01 +00:00
added checking to SSUID
This commit is contained in:
parent
abcd0b648c
commit
3980fb1fb3
2 changed files with 20 additions and 1 deletions
14
util/id.go
14
util/id.go
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -18,6 +19,9 @@ func init() {
|
|||
|
||||
type SUUID uint64
|
||||
|
||||
func leftPad(s string, padStr string, pLen int) string {
|
||||
return strings.Repeat(padStr, pLen) + s
|
||||
}
|
||||
func Id() SUUID {
|
||||
millis := uint64(time.Now().UTC().UnixNano())
|
||||
id := millis << (64 - 41)
|
||||
|
|
@ -33,7 +37,15 @@ func SUUID_to_Hex(a SUUID) string {
|
|||
return hex.EncodeToString(buf.Bytes())
|
||||
}
|
||||
func Hex_to_SUUID(str string) SUUID {
|
||||
b, _ := hex.DecodeString(str)
|
||||
l := len(str)
|
||||
var m string
|
||||
if l < 16 {
|
||||
m = leftPad(str, "0", 16-l)
|
||||
} else {
|
||||
m = str
|
||||
}
|
||||
|
||||
b, _ := hex.DecodeString(m)
|
||||
num := binary.BigEndian.Uint64(b)
|
||||
return SUUID(num)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,13 @@ func init() {
|
|||
}
|
||||
*/
|
||||
func TestId(t *testing.T) {
|
||||
Convey("Test Small", t, func() {
|
||||
s := "1"
|
||||
//s := "0000000000000001"
|
||||
//s := "000000000000001"
|
||||
b2 := Hex_to_SUUID(s)
|
||||
So(1, ShouldEqual, b2)
|
||||
})
|
||||
Convey("Basic Usage", t, func() {
|
||||
bc1 := Id()
|
||||
println(SUUID_to_Hex(bc1))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue