Last active
April 29, 2024 17:56
-
-
Save makasim/36f8bb33ab2dd1edba591e9d2018628d to your computer and use it in GitHub Desktop.
Revisions
-
makasim revised this gist
Apr 29, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import ( "fmt" "time" "intulid" "github.com/oklog/ulid/v2" ) -
makasim revised this gist
Mar 31, 2024 . No changes.There are no files selected for viewing
-
makasim revised this gist
Mar 31, 2024 . 1 changed file with 45 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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{}{} } } } -
makasim revised this gist
Mar 31, 2024 . 2 changed files with 28 additions and 60 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,60 +0,0 @@ -
makasim revised this gist
Mar 30, 2024 . 1 changed file with 20 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) { 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, @@ -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{}{} } } } -
makasim revised this gist
Mar 30, 2024 . 1 changed file with 0 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,6 @@ package main import ( "encoding/binary" "fmt" "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)) return len(p), nil } func main() { t := time.Unix(123, 567) for i := 0; i < 10000000000; i++ { e := Event{ id: int64(i), -
makasim revised this gist
Mar 30, 2024 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) 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) } fmt.Printf("%d -> %s\n", e.id, id.String()) } } -
makasim created this gist
Mar 30, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()) } }