Commit 33ef70f7 authored by Seth Hoenig's avatar Seth Hoenig
Browse files

dnm: playing with ulids

parent f8111692
Showing with 103 additions and 0 deletions
+103 -0
......@@ -215,6 +215,7 @@ require (
github.com/nicolai86/scaleway-sdk v1.10.2-0.20180628010248-798f60e20bb2 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/oklog/run v1.0.1-0.20180308005104-6934b124db28 // indirect
github.com/oklog/ulid/v2 v2.0.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/selinux v1.8.2 // indirect
......
......@@ -981,7 +981,10 @@ github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/oklog/run v1.0.1-0.20180308005104-6934b124db28 h1:R9vmquWCeGmxTHUVnTQJrU4oPlgEn9+x48nwXSqkIKg=
github.com/oklog/run v1.0.1-0.20180308005104-6934b124db28/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/oklog/ulid/v2 v2.0.2 h1:r4fFzBm+bv0wNKNh5eXTwU7i85y5x+uwkxCUTNVQqLc=
github.com/oklog/ulid/v2 v2.0.2/go.mod h1:mtBL0Qe/0HAx6/a4Z30qxVIAL1eQDweXq5lxOEiwQ68=
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/onsi/ginkgo v0.0.0-20151202141238-7f8ab55aaf3b/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
......@@ -1038,6 +1041,7 @@ github.com/packethost/packngo v0.1.1-0.20180711074735-b9cb5096f54c/go.mod h1:otz
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
......
package ids
func Head(id string) string {
return id[0:4]
}
func Tail(id string) string {
return id[len(id)-4:]
}
package ids
import (
"crypto/rand"
"fmt"
"time"
"github.com/hashicorp/go-uuid"
"oss.indeed.com/go/libtime"
)
// NewULID creates a new pseudo-ULID based on the ulid/spec. The output format
// is that of a UUID rather than the compact form of a ULID.
//
// Specification of ULID:
// https://github.com/ulid/spec
//
// This implementation *does not* guarantee monotonic increasing IDs within the
// same millisecond.
//
// This implementation *is* safe to use across goroutines.
func NewULID() string {
b := make([]byte, 16)
// first 6 bytes are based on timestamp
ms := libtime.ToMilliseconds(time.Now().UTC())
b[0] = byte(ms >> 40)
b[1] = byte(ms >> 32)
b[2] = byte(ms >> 24)
b[3] = byte(ms >> 16)
b[4] = byte(ms >> 8)
b[5] = byte(ms)
// last 10 bytes are based on true random
n, rndErr := rand.Read(b[6:])
if rndErr != nil {
panic(fmt.Errorf("failed to generate ulid: %v", rndErr))
}
if n != 10 {
panic("failed to generate ulid: not enough random bytes")
}
// we like our ULIDs formatted as UUIDs
s, fmtErr := uuid.FormatUUID(b)
if fmtErr != nil {
panic(fmt.Errorf("failed to format ulid as uuid: %v", fmtErr))
}
return s
}
package ids
import (
"fmt"
"testing"
"time"
)
func Test_NewULID(t *testing.T) {
// create 10 ULIDs quickly; the first few bytes should be the same
// and the last few bytes should be completely random
var ulids [10]string
for i := 0; i < 10; i++ {
ulids[i] = NewULID()
time.Sleep(1*time.Millisecond + 1)
fmt.Println(ulids[i])
}
}
package ids
import (
"github.com/hashicorp/go-uuid"
)
type UUID = string
func NewUUID() UUID {
id, err := uuid.GenerateUUID()
if err != nil {
panic(err)
}
return id
}
func ShortUUID(id UUID) string {
return NewUUID()[0:4]
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment