-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
e241646
564cc8e
af2f94c
e1a1811
1b1d1a7
bc0f060
3d5e125
7e970f2
c04262d
ab38ee4
855d950
f74d090
7fb9afc
d3437ca
a723a09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
|
||
"cosmossdk.io/collections" | ||
"cosmossdk.io/collections/codec" | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure I follow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @julienrbrt any chance you get what he meant? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
} | ||
|
Check notice
Code scanning / CodeQL
Sensitive package import Note