From 8f01631aab5c721b439948f567b769377c0b2915 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Thu, 17 Aug 2023 11:19:54 -0700 Subject: [PATCH 1/3] code style of kvpair --- tm2/pkg/std/kvpair.go | 60 ++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/tm2/pkg/std/kvpair.go b/tm2/pkg/std/kvpair.go index 0dd6df5233b..f43f75bc18b 100644 --- a/tm2/pkg/std/kvpair.go +++ b/tm2/pkg/std/kvpair.go @@ -8,55 +8,69 @@ import ( //---------------------------------------- // KVPair +// KVPair is a key-value struct for []byte value. type KVPair struct { Key []byte Value []byte } +// KVPairs is a slice of KVPair. type KVPairs []KVPair -// Sorting -func (kvs KVPairs) Len() int { return len(kvs) } +// Len returns the length of kvs. +func (kvs KVPairs) Len() int { + return len(kvs) +} +// Less reports whether kvs[i] should be ordered before kvs[j]. func (kvs KVPairs) Less(i, j int) bool { - switch bytes.Compare(kvs[i].Key, kvs[j].Key) { - case -1: - return true - case 0: + c := bytes.Compare(kvs[i].Key, kvs[j].Key) + if c == 0 { return bytes.Compare(kvs[i].Value, kvs[j].Value) < 0 - case 1: - return false - default: - panic("invalid comparison result") } + return c < 0 +} + +// Swap swaps the elements with indexes, i and j. +func (kvs KVPairs) Swap(i, j int) { + kvs[i], kvs[j] = kvs[j], kvs[i] +} + +// Sort sorts a kvs in ascending order. +func (kvs KVPairs) Sort() { + sort.Sort(kvs) } -func (kvs KVPairs) Swap(i, j int) { kvs[i], kvs[j] = kvs[j], kvs[i] } -func (kvs KVPairs) Sort() { sort.Sort(kvs) } //---------------------------------------- // KI64Pair +// KI64Pair is a key-value struct for int64 value. type KI64Pair struct { Key []byte Value int64 } +// KI64Pairs is a slice of KI64Pair. type KI64Pairs []KI64Pair -// Sorting +// Len returns the length of kvs. func (kvs KI64Pairs) Len() int { return len(kvs) } +// Less reports whether kvs[i] should be ordered before kvs[j]. func (kvs KI64Pairs) Less(i, j int) bool { - switch bytes.Compare(kvs[i].Key, kvs[j].Key) { - case -1: - return true - case 0: + c := bytes.Compare(kvs[i].Key, kvs[j].Key) + if c == 0 { return kvs[i].Value < kvs[j].Value - case 1: - return false - default: - panic("invalid comparison result") } + return c < 0 +} + +// Swap swaps the elements with indexes, i and j. +func (kvs KI64Pairs) Swap(i, j int) { + kvs[i], kvs[j] = kvs[j], kvs[i] +} + +// Sort sorts a kvs in ascending order. +func (kvs KI64Pairs) Sort() { + sort.Sort(kvs) } -func (kvs KI64Pairs) Swap(i, j int) { kvs[i], kvs[j] = kvs[j], kvs[i] } -func (kvs KI64Pairs) Sort() { sort.Sort(kvs) } From ea84cc5ef6fe45b20c2d125ff9cc266060ecd6d2 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Thu, 17 Aug 2023 11:20:13 -0700 Subject: [PATCH 2/3] add tests for kvpair --- tm2/pkg/std/kvpair_test.go | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tm2/pkg/std/kvpair_test.go diff --git a/tm2/pkg/std/kvpair_test.go b/tm2/pkg/std/kvpair_test.go new file mode 100644 index 00000000000..d030da48053 --- /dev/null +++ b/tm2/pkg/std/kvpair_test.go @@ -0,0 +1,57 @@ +package std + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestKVPairs(t *testing.T) { + kvs := KVPairs{ + {Key: []byte("k2"), Value: []byte("")}, + {Key: []byte("k1"), Value: []byte("2")}, + {Key: []byte("k1"), Value: []byte("1")}, + {Key: []byte("k1"), Value: []byte("2")}, + } + + // Sort() essentially tests Less() and Swap() as well + assert.Equal(t, 4, kvs.Len()) + kvs.Sort() + assert.Equal(t, 4, kvs.Len()) + + kvs2 := KVPairs{ + {Key: []byte("k1"), Value: []byte("1")}, + {Key: []byte("k1"), Value: []byte("2")}, + {Key: []byte("k1"), Value: []byte("2")}, + {Key: []byte("k2"), Value: []byte("")}, + } + for i := 0; i < kvs.Len(); i++ { + assert.Equal(t, kvs[i].Key, kvs2[i].Key) + assert.Equal(t, kvs[i].Value, kvs2[i].Value) + } +} + +func TestKI64Pairs(t *testing.T) { + kvs := KI64Pairs{ + {Key: []byte("k2"), Value: 0}, + {Key: []byte("k1"), Value: 2}, + {Key: []byte("k1"), Value: 1}, + {Key: []byte("k1"), Value: 2}, + } + + // Sort() essentially tests Less() and Swap() as well + assert.Equal(t, 4, kvs.Len()) + kvs.Sort() + assert.Equal(t, 4, kvs.Len()) + + kvs2 := KI64Pairs{ + {Key: []byte("k1"), Value: 1}, + {Key: []byte("k1"), Value: 2}, + {Key: []byte("k1"), Value: 2}, + {Key: []byte("k2"), Value: 0}, + } + for i := 0; i < kvs.Len(); i++ { + assert.Equal(t, kvs[i].Key, kvs2[i].Key) + assert.Equal(t, kvs[i].Value, kvs2[i].Value) + } +} From bce824b1c88eec06f9941ae11b4973c1ae613b4a Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Thu, 24 Aug 2023 21:09:59 -0700 Subject: [PATCH 3/3] revert Less() --- tm2/pkg/std/kvpair.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tm2/pkg/std/kvpair.go b/tm2/pkg/std/kvpair.go index f43f75bc18b..868b194db8c 100644 --- a/tm2/pkg/std/kvpair.go +++ b/tm2/pkg/std/kvpair.go @@ -24,11 +24,16 @@ func (kvs KVPairs) Len() int { // Less reports whether kvs[i] should be ordered before kvs[j]. func (kvs KVPairs) Less(i, j int) bool { - c := bytes.Compare(kvs[i].Key, kvs[j].Key) - if c == 0 { + switch bytes.Compare(kvs[i].Key, kvs[j].Key) { + case -1: + return true + case 0: return bytes.Compare(kvs[i].Value, kvs[j].Value) < 0 + case 1: + return false + default: + panic("invalid comparison result") } - return c < 0 } // Swap swaps the elements with indexes, i and j. @@ -58,11 +63,16 @@ func (kvs KI64Pairs) Len() int { return len(kvs) } // Less reports whether kvs[i] should be ordered before kvs[j]. func (kvs KI64Pairs) Less(i, j int) bool { - c := bytes.Compare(kvs[i].Key, kvs[j].Key) - if c == 0 { + switch bytes.Compare(kvs[i].Key, kvs[j].Key) { + case -1: + return true + case 0: return kvs[i].Value < kvs[j].Value + case 1: + return false + default: + panic("invalid comparison result") } - return c < 0 } // Swap swaps the elements with indexes, i and j.