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 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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"
Fixed Show fixed Hide fixed

"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) {
oren-lava marked this conversation as resolved.
Show resolved Hide resolved
// 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@testinginprod trying again...
The work is almost done, I would really like see this code merged, and it's stuck for some time (@julienrbrt)

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for being slow!

So:

  1. we cannot use reflection.
  2. we should iterate the map using the raw bytes and comparing the raw bytes for equality

Copy link
Contributor

Choose a reason for hiding this comment

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

Also maybe to fast track merging, we could split the PR in two,and push the multi refkeys method to another PR since it is more contentious than the triple reverse range

Copy link
Contributor Author

@oren-lava oren-lava Oct 12, 2024

Choose a reason for hiding this comment

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

now comparing bytes for equality and not using reflect :) @testinginprod
Hope it's ok, see here: oren-lava@a56e943

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 @@ -301,3 +301,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,
}
}
oren-lava marked this conversation as resolved.
Show resolved Hide resolved

// 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,
}
}
oren-lava marked this conversation as resolved.
Show resolved Hide resolved
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