From 2144223879ee68aa787d5eac510d1a96bf8429ac Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 16 Nov 2023 16:36:54 +0100 Subject: [PATCH 1/5] feat(examples): add p/demo/squid --- examples/gno.land/p/demo/squid/README.md | 36 ++++++++++++ examples/gno.land/p/demo/squid/gno.mod | 1 + examples/gno.land/p/demo/squid/squid.gno | 57 +++++++++++++++++++ examples/gno.land/p/demo/squid/squid_test.gno | 43 ++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 examples/gno.land/p/demo/squid/README.md create mode 100644 examples/gno.land/p/demo/squid/gno.mod create mode 100644 examples/gno.land/p/demo/squid/squid.gno create mode 100644 examples/gno.land/p/demo/squid/squid_test.gno diff --git a/examples/gno.land/p/demo/squid/README.md b/examples/gno.land/p/demo/squid/README.md new file mode 100644 index 00000000000..7fac2e5f8e8 --- /dev/null +++ b/examples/gno.land/p/demo/squid/README.md @@ -0,0 +1,36 @@ +# squid + +``` +package squid // import "gno.land/p/demo/squid" + +Package squid provides a simple way to have sequential IDs which will be ordered +correctly when inserted in an AVL tree. + +Sample usage: + + var id squid.ID + var users avl.Tree + + func NewUser() { + users.Set(id.Next().Binary(), &User{ ... }) + } + +TYPES + +type ID uint64 + An ID is a simple sequential ID generator. + +func FromBinary(b string) (ID, bool) + FromBinary creates a new ID from the given string. + +func (i ID) Binary() string + Binary returns a big-endian binary representation of the ID, suitable to be + used as an AVL key. + +func (i *ID) Next() ID + Next advances the ID i. It will panic if increasing ID would overflow. + +func (i *ID) TryNext() (ID, bool) + TryNext increases i by 1 and returns its value. It returns true if + successful, or false if the increment would result in an overflow. +``` diff --git a/examples/gno.land/p/demo/squid/gno.mod b/examples/gno.land/p/demo/squid/gno.mod new file mode 100644 index 00000000000..3c553138ca6 --- /dev/null +++ b/examples/gno.land/p/demo/squid/gno.mod @@ -0,0 +1 @@ +module gno.land/p/demo/squid diff --git a/examples/gno.land/p/demo/squid/squid.gno b/examples/gno.land/p/demo/squid/squid.gno new file mode 100644 index 00000000000..73eb4ecbeb2 --- /dev/null +++ b/examples/gno.land/p/demo/squid/squid.gno @@ -0,0 +1,57 @@ +// Package squid provides a simple way to have sequential IDs which will be +// ordered correctly when inserted in an AVL tree. +// +// Sample usage: +// +// var id squid.ID +// var users avl.Tree +// +// func NewUser() { +// users.Set(id.Next().Binary(), &User{ ... }) +// } +package squid + +import "encoding/binary" + +// An ID is a simple sequential ID generator. +type ID uint64 + +// Next advances the ID i. +// It will panic if increasing ID would overflow. +func (i *ID) Next() ID { + next, ok := i.TryNext() + if !ok { + panic("squid: next ID overflows uint64") + } + return next +} + +const maxUint64 uint64 = 1<<64 - 1 + +// TryNext increases i by 1 and returns its value. +// It returns true if successful, or false if the increment would result in +// an overflow. +func (i *ID) TryNext() (ID, bool) { + if *i == maxUint64 { + // Addition will overflow. + return 0, false + } + *i++ + return *i, true +} + +// Binary returns a big-endian binary representation of the ID, +// suitable to be used as an AVL key. +func (i ID) Binary() string { + buf := make([]byte, 8) + binary.BigEndian.PutUint64(buf, uint64(i)) + return string(buf) +} + +// FromBinary creates a new ID from the given string. +func FromBinary(b string) (ID, bool) { + if len(b) != 8 { + return 0, false + } + return ID(binary.BigEndian.Uint64([]byte(b))), true +} diff --git a/examples/gno.land/p/demo/squid/squid_test.gno b/examples/gno.land/p/demo/squid/squid_test.gno new file mode 100644 index 00000000000..167e1fee135 --- /dev/null +++ b/examples/gno.land/p/demo/squid/squid_test.gno @@ -0,0 +1,43 @@ +package squid + +import ( + "strings" + "testing" + "fmt" +) + +func TestID(t *testing.T) { + var i ID + + for j := 0; j < 100; j++ { + i.Next() + } + if i != 100 { + t.Fatalf("invalid: wanted %d got %d", 100, i) + } +} + +func TestID_Overflow(t *testing.T) { + i := ID(maxUint64) + + defer func() { + err := recover() + if !strings.Contains(fmt.Sprint(err), "next ID overflows") { + t.Errorf("did not overflow") + } + }() + + i.Next() +} + +func TestID_Binary(t *testing.T) { + var i ID + prev := i.Binary() + + for j := 0; j < 1000; j++ { + cur := i.Next().Binary() + if cur <= prev { + t.Fatalf("cur %x <= prev %x", cur, prev) + } + } +} From f92f17cb6de75218b9bcc088cc2afcc941af52c3 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 16 Nov 2023 16:52:12 +0100 Subject: [PATCH 2/5] the linter check I wrote is turning against me! --- examples/gno.land/p/demo/squid/squid_test.gno | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gno.land/p/demo/squid/squid_test.gno b/examples/gno.land/p/demo/squid/squid_test.gno index 167e1fee135..4b5240a82bd 100644 --- a/examples/gno.land/p/demo/squid/squid_test.gno +++ b/examples/gno.land/p/demo/squid/squid_test.gno @@ -1,9 +1,9 @@ package squid import ( + "fmt" "strings" "testing" - "fmt" ) func TestID(t *testing.T) { From f2ef17aff95e00a1a81689a314bad97f0f6e6fda Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 16 Nov 2023 16:57:44 +0100 Subject: [PATCH 3/5] maxUint64 -> maxID --- examples/gno.land/p/demo/squid/squid.gno | 4 ++-- examples/gno.land/p/demo/squid/squid_test.gno | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/gno.land/p/demo/squid/squid.gno b/examples/gno.land/p/demo/squid/squid.gno index 73eb4ecbeb2..b2275cd4e3f 100644 --- a/examples/gno.land/p/demo/squid/squid.gno +++ b/examples/gno.land/p/demo/squid/squid.gno @@ -26,13 +26,13 @@ func (i *ID) Next() ID { return next } -const maxUint64 uint64 = 1<<64 - 1 +const maxID ID = 1<<64 - 1 // TryNext increases i by 1 and returns its value. // It returns true if successful, or false if the increment would result in // an overflow. func (i *ID) TryNext() (ID, bool) { - if *i == maxUint64 { + if *i == maxID { // Addition will overflow. return 0, false } diff --git a/examples/gno.land/p/demo/squid/squid_test.gno b/examples/gno.land/p/demo/squid/squid_test.gno index 4b5240a82bd..1b14ec58ad5 100644 --- a/examples/gno.land/p/demo/squid/squid_test.gno +++ b/examples/gno.land/p/demo/squid/squid_test.gno @@ -18,7 +18,7 @@ func TestID(t *testing.T) { } func TestID_Overflow(t *testing.T) { - i := ID(maxUint64) + i := ID(maxID) defer func() { err := recover() From b1c4f5d4d718e6b9c3520d18bd61628fb454958b Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 16 Nov 2023 18:32:48 +0100 Subject: [PATCH 4/5] squid -> seqid, by popular consensus --- examples/gno.land/p/demo/{squid => seqid}/README.md | 8 ++++---- examples/gno.land/p/demo/seqid/gno.mod | 1 + examples/gno.land/p/demo/{squid => seqid}/squid.gno | 8 ++++---- examples/gno.land/p/demo/{squid => seqid}/squid_test.gno | 2 +- examples/gno.land/p/demo/squid/gno.mod | 1 - 5 files changed, 10 insertions(+), 10 deletions(-) rename examples/gno.land/p/demo/{squid => seqid}/README.md (84%) create mode 100644 examples/gno.land/p/demo/seqid/gno.mod rename examples/gno.land/p/demo/{squid => seqid}/squid.gno (89%) rename examples/gno.land/p/demo/{squid => seqid}/squid_test.gno (97%) delete mode 100644 examples/gno.land/p/demo/squid/gno.mod diff --git a/examples/gno.land/p/demo/squid/README.md b/examples/gno.land/p/demo/seqid/README.md similarity index 84% rename from examples/gno.land/p/demo/squid/README.md rename to examples/gno.land/p/demo/seqid/README.md index 7fac2e5f8e8..8b96f99cfed 100644 --- a/examples/gno.land/p/demo/squid/README.md +++ b/examples/gno.land/p/demo/seqid/README.md @@ -1,14 +1,14 @@ -# squid +# seqid ``` -package squid // import "gno.land/p/demo/squid" +package seqid // import "gno.land/p/demo/seqid" -Package squid provides a simple way to have sequential IDs which will be ordered +Package seqid provides a simple way to have sequential IDs which will be ordered correctly when inserted in an AVL tree. Sample usage: - var id squid.ID + var id seqid.ID var users avl.Tree func NewUser() { diff --git a/examples/gno.land/p/demo/seqid/gno.mod b/examples/gno.land/p/demo/seqid/gno.mod new file mode 100644 index 00000000000..63e6a1fb551 --- /dev/null +++ b/examples/gno.land/p/demo/seqid/gno.mod @@ -0,0 +1 @@ +module gno.land/p/demo/seqid diff --git a/examples/gno.land/p/demo/squid/squid.gno b/examples/gno.land/p/demo/seqid/squid.gno similarity index 89% rename from examples/gno.land/p/demo/squid/squid.gno rename to examples/gno.land/p/demo/seqid/squid.gno index b2275cd4e3f..8cb5366ef44 100644 --- a/examples/gno.land/p/demo/squid/squid.gno +++ b/examples/gno.land/p/demo/seqid/squid.gno @@ -1,15 +1,15 @@ -// Package squid provides a simple way to have sequential IDs which will be +// Package seqid provides a simple way to have sequential IDs which will be // ordered correctly when inserted in an AVL tree. // // Sample usage: // -// var id squid.ID +// var id seqid.ID // var users avl.Tree // // func NewUser() { // users.Set(id.Next().Binary(), &User{ ... }) // } -package squid +package seqid import "encoding/binary" @@ -21,7 +21,7 @@ type ID uint64 func (i *ID) Next() ID { next, ok := i.TryNext() if !ok { - panic("squid: next ID overflows uint64") + panic("seqid: next ID overflows uint64") } return next } diff --git a/examples/gno.land/p/demo/squid/squid_test.gno b/examples/gno.land/p/demo/seqid/squid_test.gno similarity index 97% rename from examples/gno.land/p/demo/squid/squid_test.gno rename to examples/gno.land/p/demo/seqid/squid_test.gno index 1b14ec58ad5..c6f57960177 100644 --- a/examples/gno.land/p/demo/squid/squid_test.gno +++ b/examples/gno.land/p/demo/seqid/squid_test.gno @@ -1,4 +1,4 @@ -package squid +package seqid import ( "fmt" diff --git a/examples/gno.land/p/demo/squid/gno.mod b/examples/gno.land/p/demo/squid/gno.mod deleted file mode 100644 index 3c553138ca6..00000000000 --- a/examples/gno.land/p/demo/squid/gno.mod +++ /dev/null @@ -1 +0,0 @@ -module gno.land/p/demo/squid From a4511f9fe921f69b33008d582785be922644f443 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 16 Nov 2023 18:37:47 +0100 Subject: [PATCH 5/5] mv files --- examples/gno.land/p/demo/seqid/{squid.gno => seqid.gno} | 0 examples/gno.land/p/demo/seqid/{squid_test.gno => seqid_test.gno} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename examples/gno.land/p/demo/seqid/{squid.gno => seqid.gno} (100%) rename examples/gno.land/p/demo/seqid/{squid_test.gno => seqid_test.gno} (100%) diff --git a/examples/gno.land/p/demo/seqid/squid.gno b/examples/gno.land/p/demo/seqid/seqid.gno similarity index 100% rename from examples/gno.land/p/demo/seqid/squid.gno rename to examples/gno.land/p/demo/seqid/seqid.gno diff --git a/examples/gno.land/p/demo/seqid/squid_test.gno b/examples/gno.land/p/demo/seqid/seqid_test.gno similarity index 100% rename from examples/gno.land/p/demo/seqid/squid_test.gno rename to examples/gno.land/p/demo/seqid/seqid_test.gno