Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(collections): Multi RefKeys method and reverse Triple iterator support #21496

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions collections/indexes/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import (
"context"
"errors"
"fmt"
"reflect"

Check notice

Code scanning / CodeQL

Sensitive package import Note

Certain system packages contain functions which may be a possible source of non-determinism

"cosmossdk.io/collections"
"cosmossdk.io/collections/codec"
Expand Down Expand Up @@ -121,6 +123,40 @@
return m.Iterate(ctx, collections.NewPrefixedPairRange[ReferenceKey, PrimaryKey](refKey))
}

// RefKeys returns a list of all the MultiIterator's reference keys (may contain duplicates).
// Enable the "unique" argument to get a unique list of reference keys (the reference key must be comparable)
func (m *Multi[ReferenceKey, PrimaryKey, Value]) RefKeys(ctx context.Context, unique bool) ([]ReferenceKey, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be documented as expensive call since it can end up iterating over a loooot of data.

// sanity check - enabled unique with non-comparable ReferenceKey type
if unique && !reflect.ValueOf((*ReferenceKey)(nil)).Comparable() {
return nil, fmt.Errorf("cannot retrieve unique reference keys since type is not comparable: %T", reflect.TypeOf((*ReferenceKey)(nil)))
}

iter, err := m.refKeys.Iterate(ctx, nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should RawIterate getting bytes, then use the bytes as map index. (can be efficiently done with unsafe)
then convert them to the real type later using the key codec

Copy link
Contributor Author

@oren-lava oren-lava Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I follow. IterateRaw uses an untyped range but returns a typed iterator. So we're not getting bytes out of it. Can you write a small pseudo code of what you meant? (or explain in other words?) @testinginprod

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@julienrbrt any chance you get what he meant?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. @testinginprod care to ellaborate?

if err != nil {
return nil, err
}

keys := []ReferenceKey{}
visited := map[interface{}]struct{}{}
oren-lava marked this conversation as resolved.
Show resolved Hide resolved
for ; iter.Valid(); iter.Next() {
key, err := iter.Key()
if err != nil {
return nil, err
}
refKey := key.K1()

if unique {
if _, ok := visited[refKey]; ok {
continue
}
visited[refKey] = struct{}{}
}
keys = append(keys, key.K1())
}

return keys, nil
}

func (m *Multi[K1, K2, Value]) KeyCodec() codec.KeyCodec[collections.Pair[K1, K2]] {
return m.refKeys.KeyCodec()
}
Expand Down
13 changes: 13 additions & 0 deletions collections/indexes/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func TestMultiIndex(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []uint64{1, 2}, pks)

// we get all reference keys, should only be "milan"
rks, err := mi.RefKeys(ctx, false)
require.NoError(t, err)
require.Equal(t, []string{"milan", "milan"}, rks)
rks, err = mi.RefKeys(ctx, true)
require.NoError(t, err)
require.Equal(t, []string{"milan"}, rks)

// replace
require.NoError(t, mi.Reference(ctx, 1, company{City: "new york"}, func() (company, error) { return company{City: "milan"}, nil }))

Expand All @@ -43,6 +51,11 @@ func TestMultiIndex(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []uint64{1}, pks)

// assert after replace the reference keys should be "milan" and "new york"
rks, err = mi.RefKeys(ctx, false)
require.NoError(t, err)
require.ElementsMatch(t, []string{"milan", "new york"}, rks)

// test iter methods
iter, err = mi.Iterate(ctx, nil)
require.NoError(t, err)
Expand Down
33 changes: 33 additions & 0 deletions collections/triple.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,36 @@ func NewSuperPrefixedTripleRange[K1, K2, K3 any](k1 K1, k2 K2) Ranger[Triple[K1,
end: RangeKeyPrefixEnd(key),
}
}

// NewPrefixUntilTripleRangeReversed defines a collection query which ranges until the provided Pair prefix
// in reverse order.
// Unstable: this API might change in the future.
func NewPrefixUntilTripleRangeReversed[K1, K2, K3 any](k1 K1) Ranger[Triple[K1, K2, K3]] {
key := TriplePrefix[K1, K2, K3](k1)
return &Range[Triple[K1, K2, K3]]{
end: RangeKeyPrefixEnd(key),
order: OrderDescending,
}
}

// NewPrefixedTripleRange provides a Range for all keys prefixed with the given
// first part of the Triple key in reverse order.
func NewPrefixedTripleRangeReversed[K1, K2, K3 any](k1 K1) Ranger[Triple[K1, K2, K3]] {
key := TriplePrefix[K1, K2, K3](k1)
return &Range[Triple[K1, K2, K3]]{
start: RangeKeyExact(key),
end: RangeKeyPrefixEnd(key),
order: OrderDescending,
}
}
Comment on lines +354 to +363
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Correct the function name in the comment.

The implementation of NewPrefixedTripleRangeReversed is correct and consistent with the other reverse order functions.

The comment above the function has an incorrect function name. It should be "NewPrefixedTripleRangeReversed" instead of "NewPrefixedTripleRange". Please update the comment to reflect the correct function name:

-// NewPrefixedTripleRange provides a Range for all keys prefixed with the given
+// NewPrefixedTripleRangeReversed provides a Range for all keys prefixed with the given
 // first part of the Triple key in reverse order.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// NewPrefixedTripleRange provides a Range for all keys prefixed with the given
// first part of the Triple key in reverse order.
func NewPrefixedTripleRangeReversed[K1, K2, K3 any](k1 K1) Ranger[Triple[K1, K2, K3]] {
key := TriplePrefix[K1, K2, K3](k1)
return &Range[Triple[K1, K2, K3]]{
start: RangeKeyExact(key),
end: RangeKeyPrefixEnd(key),
order: OrderDescending,
}
}
// NewPrefixedTripleRangeReversed provides a Range for all keys prefixed with the given
// first part of the Triple key in reverse order.
func NewPrefixedTripleRangeReversed[K1, K2, K3 any](k1 K1) Ranger[Triple[K1, K2, K3]] {
key := TriplePrefix[K1, K2, K3](k1)
return &Range[Triple[K1, K2, K3]]{
start: RangeKeyExact(key),
end: RangeKeyPrefixEnd(key),
order: OrderDescending,
}
}


// NewSuperPrefixedTripleRange provides a Range for all keys prefixed with the given
// first and second parts of the Triple key in reverse order.
func NewSuperPrefixedTripleRangeReversed[K1, K2, K3 any](k1 K1, k2 K2) Ranger[Triple[K1, K2, K3]] {
key := TripleSuperPrefix[K1, K2, K3](k1, k2)
return &Range[Triple[K1, K2, K3]]{
start: RangeKeyExact(key),
end: RangeKeyPrefixEnd(key),
order: OrderDescending,
}
}
Comment on lines +365 to +374
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Correct the function name in the comment.

The implementation of NewSuperPrefixedTripleRangeReversed is correct and consistent with the other reverse order functions.

The comment above the function has an incorrect function name. It should be "NewSuperPrefixedTripleRangeReversed" instead of "NewSuperPrefixedTripleRange". Please update the comment to reflect the correct function name:

-// NewSuperPrefixedTripleRange provides a Range for all keys prefixed with the given
+// NewSuperPrefixedTripleRangeReversed provides a Range for all keys prefixed with the given
 // first and second parts of the Triple key in reverse order.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// NewSuperPrefixedTripleRange provides a Range for all keys prefixed with the given
// first and second parts of the Triple key in reverse order.
func NewSuperPrefixedTripleRangeReversed[K1, K2, K3 any](k1 K1, k2 K2) Ranger[Triple[K1, K2, K3]] {
key := TripleSuperPrefix[K1, K2, K3](k1, k2)
return &Range[Triple[K1, K2, K3]]{
start: RangeKeyExact(key),
end: RangeKeyPrefixEnd(key),
order: OrderDescending,
}
}
// NewSuperPrefixedTripleRangeReversed provides a Range for all keys prefixed with the given
// first and second parts of the Triple key in reverse order.
func NewSuperPrefixedTripleRangeReversed[K1, K2, K3 any](k1 K1, k2 K2) Ranger[Triple[K1, K2, K3]] {
key := TripleSuperPrefix[K1, K2, K3](k1, k2)
return &Range[Triple[K1, K2, K3]]{
start: RangeKeyExact(key),
end: RangeKeyPrefixEnd(key),
order: OrderDescending,
}
}

22 changes: 21 additions & 1 deletion collections/triple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"cosmossdk.io/collections"
"cosmossdk.io/collections/colltest"
"cosmossdk.io/core/testing"
coretesting "cosmossdk.io/core/testing"
)

func TestTriple(t *testing.T) {
Expand Down Expand Up @@ -45,10 +45,30 @@ func TestTripleRange(t *testing.T) {
require.NoError(t, err)
require.Equal(t, keys[:3], gotKeys)

// we prefix over (1) with "reverse" enabled, we expect 3 results in reverse order
iter, err = keySet.Iterate(ctx, collections.NewPrefixedTripleRangeReversed[uint64, string, []byte](uint64(1)))
require.NoError(t, err)
gotKeys, err = iter.Keys()
require.NoError(t, err)
require.Len(t, gotKeys, 3)
for i := range gotKeys {
require.Equal(t, gotKeys[i], keys[len(gotKeys)-i-1])
}

// we super prefix over Join(1, "A") we expect 2 results
iter, err = keySet.Iterate(ctx, collections.NewSuperPrefixedTripleRange[uint64, string, []byte](1, "A"))
require.NoError(t, err)
gotKeys, err = iter.Keys()
require.NoError(t, err)
require.Equal(t, keys[:2], gotKeys)

// we prefix over Join(1, "A") with "reverse" enabled, we expect 2 results in reverse order
iter, err = keySet.Iterate(ctx, collections.NewSuperPrefixedTripleRangeReversed[uint64, string, []byte](1, "A"))
require.NoError(t, err)
gotKeys, err = iter.Keys()
require.NoError(t, err)
require.Len(t, gotKeys, 2)
for i := range gotKeys {
require.Equal(t, gotKeys[i], keys[len(gotKeys)-i-1])
}
}
Loading