Skip to content

Instantly share code, notes, and snippets.

@makasim
Last active April 29, 2024 17:56
Show Gist options
  • Save makasim/36f8bb33ab2dd1edba591e9d2018628d to your computer and use it in GitHub Desktop.
Save makasim/36f8bb33ab2dd1edba591e9d2018628d to your computer and use it in GitHub Desktop.

Revisions

  1. makasim revised this gist Apr 29, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion example.go
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ import (
    "fmt"
    "time"

    "git.solidgate.com/lib/go-opsctl/bar/intulid"
    "intulid"
    "github.com/oklog/ulid/v2"
    )

  2. makasim revised this gist Mar 31, 2024. No changes.
  3. makasim revised this gist Mar 31, 2024. 1 changed file with 45 additions and 0 deletions.
    45 changes: 45 additions & 0 deletions example.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    package main

    import (
    "fmt"
    "time"

    "git.solidgate.com/lib/go-opsctl/bar/intulid"
    "github.com/oklog/ulid/v2"
    )

    type Event struct {
    id int64
    createdAt time.Time
    Foo string
    Bar string
    }

    func main() {
    t := time.Unix(123, 567)

    uniq := make(map[string]struct{})

    for i := 0; i < 1000000000; i++ {
    e := Event{
    id: int64(i),
    createdAt: t,
    Foo: "foo",
    Bar: "bar",
    }

    id, err := intulid.New(ulid.Timestamp(e.createdAt), e.id, []byte(`foo`))
    if err != nil {
    panic(err)
    }

    fmt.Printf("%d -> %s\n", e.id, id.String())

    if _, ok := uniq[id.String()]; ok {
    fmt.Printf("%d -> %s\n", e.id, id.String())
    panic("duplicate id")
    } else {
    uniq[id.String()] = struct{}{}
    }
    }
    }
  4. makasim revised this gist Mar 31, 2024. 2 changed files with 28 additions and 60 deletions.
    28 changes: 28 additions & 0 deletions int.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    package intulid

    import (
    "encoding/binary"
    "fmt"

    "github.com/oklog/ulid/v2"
    "golang.org/x/crypto/sha3"
    )

    type entropy []byte

    func New(ms uint64, num int64, salt []byte) (ulid.ULID, error) {
    if len(salt) > 120 {
    return ulid.ULID{}, fmt.Errorf("salt len could not be longer than 120 bytes")
    }

    e := make(entropy, 8, 128)
    binary.LittleEndian.PutUint64(e[:8], uint64(num))
    e = append(e, salt...)

    return ulid.New(ms, e)
    }

    func (e entropy) Read(p []byte) (n int, err error) {
    sha3.ShakeSum256(p, e)
    return len(p), nil
    }
    60 changes: 0 additions & 60 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -1,60 +0,0 @@
    package main

    import (
    "encoding/binary"
    "fmt"
    "time"

    "github.com/oklog/ulid/v2"
    "golang.org/x/crypto/sha3"
    )

    type Event struct {
    id int64
    createdAt time.Time
    Foo string
    Bar string
    }

    type IDEntropy int64

    func (ide IDEntropy) Read(p []byte) (n int, err error) {
    data := make([]byte, 8)
    binary.LittleEndian.PutUint64(data, uint64(ide))

    hash := make([]byte, len(p))
    sha3.ShakeSum256(hash, data)

    p = append(p[:0], hash...)

    return len(p), nil
    }

    func main() {
    t := time.Unix(123, 567)

    uniq := make(map[string]struct{})

    for i := 0; i < 1000000000; i++ {
    e := Event{
    id: int64(i),
    createdAt: t,
    Foo: "foo",
    Bar: "bar",
    }

    id, err := ulid.New(ulid.Timestamp(e.createdAt), IDEntropy(e.id))
    if err != nil {
    panic(err)
    }

    fmt.Printf("%d -> %s\n", e.id, id.String())

    if _, ok := uniq[id.String()]; ok {
    fmt.Printf("%d -> %s\n", e.id, id.String())
    panic("duplicate id")
    } else {
    uniq[id.String()] = struct{}{}
    }
    }
    }
  5. makasim revised this gist Mar 30, 2024. 1 changed file with 20 additions and 2 deletions.
    22 changes: 20 additions & 2 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@ import (
    "time"

    "github.com/oklog/ulid/v2"
    "golang.org/x/crypto/sha3"
    )

    type Event struct {
    @@ -18,14 +19,23 @@ type Event struct {
    type IDEntropy int64

    func (ide IDEntropy) Read(p []byte) (n int, err error) {
    binary.LittleEndian.PutUint64(p[:], uint64(ide))
    data := make([]byte, 8)
    binary.LittleEndian.PutUint64(data, uint64(ide))

    hash := make([]byte, len(p))
    sha3.ShakeSum256(hash, data)

    p = append(p[:0], hash...)

    return len(p), nil
    }

    func main() {
    t := time.Unix(123, 567)

    for i := 0; i < 10000000000; i++ {
    uniq := make(map[string]struct{})

    for i := 0; i < 1000000000; i++ {
    e := Event{
    id: int64(i),
    createdAt: t,
    @@ -37,6 +47,14 @@ func main() {
    if err != nil {
    panic(err)
    }

    fmt.Printf("%d -> %s\n", e.id, id.String())

    if _, ok := uniq[id.String()]; ok {
    fmt.Printf("%d -> %s\n", e.id, id.String())
    panic("duplicate id")
    } else {
    uniq[id.String()] = struct{}{}
    }
    }
    }
  6. makasim revised this gist Mar 30, 2024. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@ package main
    import (
    "encoding/binary"
    "fmt"
    "log"
    "time"

    "github.com/oklog/ulid/v2"
    @@ -20,15 +19,12 @@ type IDEntropy int64

    func (ide IDEntropy) Read(p []byte) (n int, err error) {
    binary.LittleEndian.PutUint64(p[:], uint64(ide))
    log.Println(p)
    return len(p), nil
    }

    func main() {
    t := time.Unix(123, 567)

    unq := make(map[string]struct{})

    for i := 0; i < 10000000000; i++ {
    e := Event{
    id: int64(i),
  7. makasim revised this gist Mar 30, 2024. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ package main

    import (
    "encoding/binary"
    "fmt"
    "log"
    "time"

    @@ -19,13 +20,16 @@ type IDEntropy int64

    func (ide IDEntropy) Read(p []byte) (n int, err error) {
    binary.LittleEndian.PutUint64(p[:], uint64(ide))
    log.Println(p)
    return len(p), nil
    }

    func main() {
    t := time.Unix(123, 567)

    for i := 0; i < 10; i++ {
    unq := make(map[string]struct{})

    for i := 0; i < 10000000000; i++ {
    e := Event{
    id: int64(i),
    createdAt: t,
    @@ -37,6 +41,6 @@ func main() {
    if err != nil {
    panic(err)
    }
    log.Println(id.String())
    fmt.Printf("%d -> %s\n", e.id, id.String())
    }
    }
  8. makasim created this gist Mar 30, 2024.
    42 changes: 42 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    package main

    import (
    "encoding/binary"
    "log"
    "time"

    "github.com/oklog/ulid/v2"
    )

    type Event struct {
    id int64
    createdAt time.Time
    Foo string
    Bar string
    }

    type IDEntropy int64

    func (ide IDEntropy) Read(p []byte) (n int, err error) {
    binary.LittleEndian.PutUint64(p[:], uint64(ide))
    return len(p), nil
    }

    func main() {
    t := time.Unix(123, 567)

    for i := 0; i < 10; i++ {
    e := Event{
    id: int64(i),
    createdAt: t,
    Foo: "foo",
    Bar: "bar",
    }

    id, err := ulid.New(ulid.Timestamp(e.createdAt), IDEntropy(e.id))
    if err != nil {
    panic(err)
    }
    log.Println(id.String())
    }
    }