-
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 all 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() | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
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. Correct the function name in the comment. The implementation of 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
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, | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+365
to
+374
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. Correct the function name in the comment. The implementation of 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
Suggested change
|
Check notice
Code scanning / CodeQL
Sensitive package import Note