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

proposed additional QueryKeys API method to support getting events of different types in log index order #944

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions pkg/types/contract_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type ContractReader interface {
// QueryKey provides fetching chain agnostic events (Sequence) with general querying capability.
QueryKey(ctx context.Context, contract BoundContract, filter query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]Sequence, error)

// QueryKeys provides fetching chain agnostic events of different types (SequenceWithKey) with general querying capability.
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]SequenceWithKey, error)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about a map of Key -> Sequence?

Suggested change
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]SequenceWithKey, error)
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) (map[string]Sequence, error)

If you are worried about ordering, could consider a sequence:

Suggested change
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]SequenceWithKey, error)
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) (iter.Seq2[string, Sequence], error)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem with map[string]Sequence is it would lose the order, the key thing is that the order across the various event types needs to be maintained.

no strong feelings on using iter, however the result isn't streamed so no strong advantage to using iter either as far as I can see, and I think it would add a bit of complexity to the loop implementation. On balance my pref would be for []SequenceWithKey

Copy link
Collaborator

Choose a reason for hiding this comment

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

Streaming is not the only reason to use an iter.Seq, and this API is a layer above the GRPC protos. The advantage is that we can express what we need naturally and directly without declaring extra hyper-specific types. Another similar approach could be a generic entry/pair/etc.:

type Entry[K comparable, V any] struct {
	K K
	V V
}
Suggested change
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]SequenceWithKey, error)
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]Entry[string, Sequence], error)

Could also consider documenting via a type:

// SortedEntries is a slice of [Entry]s sorted by ascending keys.
type SortedEntries[K comparable, V any] []Entry[K, V]
Suggested change
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]SequenceWithKey, error)
QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) (SortedEntries[string, Sequence], error)


mustEmbedUnimplementedContractReader()
}

Expand Down Expand Up @@ -112,6 +115,11 @@ type Sequence struct {
Data any
}

type SequenceWithKey struct {
Sequence
Key string
}

type BoundContract struct {
Address string
Name string
Expand Down Expand Up @@ -153,6 +161,10 @@ func (UnimplementedContractReader) QueryKey(ctx context.Context, boundContract B
return nil, UnimplementedError("ContractReader.QueryKey unimplemented")
}

func (UnimplementedContractReader) QueryKeys(ctx context.Context, contract BoundContract, filters []query.KeyFilter, limitAndSort query.LimitAndSort, sequenceDataType any) ([]SequenceWithKey, error) {
return nil, UnimplementedError("ContractReader.QueryKeys unimplemented")
}

func (UnimplementedContractReader) Start(context.Context) error {
return UnimplementedError("ContractReader.Start unimplemented")
}
Expand Down
Loading