-
Notifications
You must be signed in to change notification settings - Fork 11
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
example: More powerful non fungible ids query #777
Draft
dhedey
wants to merge
2
commits into
develop
Choose a base branch
from
example/more-powerful-non-fungible-ids-query
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
246 changes: 246 additions & 0 deletions
246
....NetworkGateway.PostgresIntegration/Services/Queries/NonFungibleIdsInResourcePageQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
using Dapper; | ||
using RadixDlt.NetworkGateway.Abstractions; | ||
using System.Collections.Generic; | ||
using System.Data.Common; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GatewayModel = RadixDlt.NetworkGateway.GatewayApiSdk.Model; | ||
|
||
namespace RadixDlt.NetworkGateway.PostgresIntegration.Services.Queries; | ||
|
||
// ReSharper disable NotAccessedPositionalProperty.Global | ||
internal static class NonFungibleIdsInResourcePageQuery | ||
{ | ||
internal readonly record struct QueryConfiguration( | ||
GatewayModel.IdBoundaryCoursor? ExclusiveCursor, | ||
bool IsAscending, | ||
bool IncludeDeleted, | ||
bool IncludeValue, | ||
int MaxPageSize, | ||
int MaxDefinitionsToRead | ||
); | ||
|
||
// Raw result form the query - easiest to keep this relatively | ||
// standardised across different PageQuerys, but you might need | ||
// to change the type of Key, Value and the Totals | ||
private readonly record struct QueryResultRow( | ||
long EntityId, | ||
string EntityAddress, | ||
long TotalEntriesExcludingDeleted, | ||
long TotalEntriesIncludingDeleted, | ||
bool FilterOut, | ||
long? DefinitionId, | ||
string? Key, | ||
long? KeyFirstSeenStateVersion, | ||
byte[]? Value, | ||
bool? IsLocked, | ||
bool? IsDeleted, | ||
long? LastUpdatedStateVersion, | ||
long? NextExclusiveCursorStateVersion, | ||
long? NextExclusiveCursorDefinitionId | ||
); | ||
|
||
// Query-specific results model - mapping QueryResultRow back out | ||
internal readonly record struct PerEntityResult( | ||
Check failure on line 45 in src/RadixDlt.NetworkGateway.PostgresIntegration/Services/Queries/NonFungibleIdsInResourcePageQuery.cs GitHub Actions / build
Check failure on line 45 in src/RadixDlt.NetworkGateway.PostgresIntegration/Services/Queries/NonFungibleIdsInResourcePageQuery.cs GitHub Actions / build
|
||
long EntityId, | ||
EntityAddress NonFungibleEntityAddress, | ||
GatewayModel.IdBoundaryCoursor? NextCursor, | ||
long TotalEntriesGivenPagingParameters, | ||
long TotalMinted, | ||
long TotalSupply, | ||
List<PageItem> PageItems | ||
) | ||
{ | ||
internal GatewayModel.NonFungibleIdsCollection ToNonFungibleIdsCollection() | ||
{ | ||
return new GatewayModel.NonFungibleIdsCollection( | ||
totalCount: TotalEntriesGivenPagingParameters, | ||
nextCursor: NextCursor?.ToCursorString(), | ||
items: PageItems.Select(i => i.NonFungibleId).ToList()); | ||
} | ||
} | ||
|
||
internal readonly record struct PageItem( | ||
long DefinitionId, | ||
string NonFungibleId, | ||
long KeyFirstSeenStateVersion, | ||
byte[]? Data, | ||
bool IsLocked, | ||
bool IsDeleted, | ||
long DataLastUpdatedStateVersion | ||
); | ||
|
||
internal static async Task<Dictionary<EntityAddress, PerEntityResult>> ReadPages( | ||
DbConnection dbConnection, | ||
IDapperWrapper dapperWrapper, | ||
GatewayModel.LedgerState ledgerState, | ||
List<EntityAddress> nonFungibleResources, | ||
QueryConfiguration queryConfiguration, | ||
CancellationToken token = default) | ||
{ | ||
// See `query_conventions.md` for details about how this query structure works | ||
var queryParameters = new | ||
{ | ||
rootEntityAddresses = nonFungibleResources, | ||
useCursor = queryConfiguration.ExclusiveCursor is not null, | ||
stateVersion = ledgerState.StateVersion, | ||
exclusiveCursorStateVersion = queryConfiguration.ExclusiveCursor?.StateVersionBoundary ?? 0, | ||
exclusiveCursorDefinitionId = queryConfiguration.ExclusiveCursor?.IdBoundary ?? 0, | ||
pageLimit = queryConfiguration.MaxPageSize, | ||
definitionReadLimit = queryConfiguration.MaxDefinitionsToRead, | ||
}; | ||
|
||
var commandDefinition = new CommandDefinition( | ||
commandText: $@" | ||
WITH vars AS ( | ||
SELECT | ||
CAST(@rootEntityAddresses AS text[]) AS entity_addresses, | ||
-- If use_cursor is false, the cursor is ignored, so just set it to (0, 0) | ||
CAST(@useCursor AS bool) AS use_cursor, | ||
-- This cursor is (from_state_version, definition_id) exclusive | ||
ROW(CAST(@exclusiveCursorStateVersion AS bigint), CAST(@exclusiveCursorDefinitionId AS bigint)) AS start_cursor_exclusive, | ||
CAST(@stateVersion AS bigint) AS current_state_version | ||
), | ||
definitions_with_cursor AS ( | ||
SELECT | ||
d.*, | ||
(d.from_state_version, d.id) AS cursor | ||
FROM non_fungible_id_definition d | ||
), | ||
entries_per_entity AS ( | ||
SELECT | ||
entities.id AS EntityId, | ||
entities.address AS EntityAddress, | ||
entity_totals.total_entries_excluding_deleted AS TotalEntriesExcludingDeleted, | ||
entity_totals.total_entries_including_deleted AS TotalEntriesIncludingDeleted, | ||
COALESCE(filter_out, TRUE) AS FilterOut, | ||
CASE WHEN COALESCE(filter_out, TRUE) THEN NULL ELSE definition_id END AS DefinitionId, | ||
CASE WHEN COALESCE(filter_out, TRUE) THEN NULL ELSE key END AS Key, | ||
CASE WHEN COALESCE(filter_out, TRUE) THEN NULL ELSE key_first_seen_state_version END AS KeyFirstSeenStateVersion, | ||
CASE WHEN COALESCE(filter_out, TRUE) THEN NULL ELSE value END AS Value, | ||
CASE WHEN COALESCE(filter_out, TRUE) THEN NULL ELSE is_locked END AS IsLocked, | ||
CASE WHEN COALESCE(filter_out, TRUE) THEN NULL ELSE is_deleted END AS IsDeleted, | ||
CASE WHEN COALESCE(filter_out, TRUE) THEN NULL ELSE last_updated_state_version END AS LastUpdatedStateVersion, | ||
next_cursor_exclusive.f1 AS NextExclusiveCursorStateVersion, | ||
next_cursor_exclusive.f2 AS NextExclusiveCursorDefinitionId | ||
FROM vars | ||
INNER JOIN LATERAL ( | ||
SELECT | ||
UNNEST(vars.entity_addresses) AS address | ||
) addresses ON TRUE | ||
INNER JOIN entities | ||
ON e.address = addresses.address | ||
AND e.from_state_version <= vars.current_state_version | ||
-- In general, this can be replaced by some XXX_totals_history table, or removed if we don't have any relevant totals table | ||
INNER JOIN LATERAL ( | ||
SELECT | ||
t.total_supply AS total_entries_excluding_deleted, | ||
t.total_minted AS total_entries_including_deleted | ||
FROM resource_entity_supply_history t | ||
WHERE | ||
t.resource_entity_id = entities.id | ||
AND t.from_state_version <= vars.current_state_version | ||
ORDER BY | ||
t.from_state_version DESC | ||
LIMIT 1 | ||
) entity_totals ON TRUE | ||
LEFT JOIN LATERAL ( -- LEFT JOIN so we always return a row where we can join on the totals | ||
SELECT | ||
definitions.id as definition_id, | ||
definitions.non_fungible_id, | ||
definitions.key_first_seen_state_version, | ||
definitions.cursor, | ||
entries.*, | ||
CASE WHEN | ||
-- Add cursor to last row returned only | ||
-- > EITHER because we have filled a page (row num = limit) | ||
-- > OR because we have reached the last sub-query item (definitions.is_last_subquery_item) | ||
-- | ||
-- NOTE: The last row should be ignored if filter_out is TRUE - in which case it's just being returned for the cursor | ||
(ROW_NUMBER() OVER (ORDER BY definitions.cursor {(queryConfiguration.IsAscending ? "ASC" : "DESC")})) = @pageLimit | ||
OR definitions.is_last_subquery_item | ||
THEN definitions.cursor ELSE NULL END AS next_cursor_exclusive | ||
FROM ( | ||
SELECT | ||
d.id AS id, | ||
d.non_fungible_id AS key, -- The key | ||
d.from_state_version AS key_first_seen_state_version, | ||
d.cursor, | ||
(ROW_NUMBER() OVER (ORDER BY d.cursor {(queryConfiguration.IsAscending ? "ASC" : "DESC")})) = @definitionReadLimit AS is_last_subquery_item | ||
FROM definitions_with_cursor d | ||
WHERE | ||
d.non_fungible_resource_entity_id = entities.id | ||
AND ( | ||
(NOT vars.use_cursor) OR | ||
d.cursor {(queryConfiguration.IsAscending ? ">" : "<")} vars.start_cursor_exclusive | ||
) | ||
ORDER BY | ||
d.cursor {(queryConfiguration.IsAscending ? "ASC" : "DESC")} | ||
LIMIT @definitionReadLimit | ||
) definitions | ||
INNER JOIN LATERAL ( | ||
SELECT | ||
h.from_state_version AS last_updated_state_version, | ||
{(queryConfiguration.IncludeValue ? "h.data" : "NULL")} AS value, | ||
h.is_locked, | ||
h.is_deleted, | ||
{(queryConfiguration.IncludeDeleted ? "FALSE" : "h.is_deleted")} AS filter_out | ||
FROM non_fungible_id_data_history h | ||
WHERE | ||
h.non_fungible_id_definition_id = definitions.id | ||
AND h.from_state_version <= vars.current_state_version | ||
ORDER BY | ||
h.from_state_version DESC | ||
LIMIT 1 | ||
) entries ON TRUE | ||
WHERE | ||
(NOT entries.filter_out) | ||
OR definitions.is_last_subquery_item | ||
ORDER BY | ||
definitions.cursor {(queryConfiguration.IsAscending ? "ASC" : "DESC")} | ||
LIMIT @pageLimit | ||
) entries_per_entity ON TRUE | ||
) | ||
SELECT * FROM entries_per_entity | ||
;", | ||
parameters: queryParameters, | ||
cancellationToken: token); | ||
|
||
var results = await dapperWrapper.QueryAsync<QueryResultRow>(dbConnection, commandDefinition); | ||
|
||
// NOTE: In some other instances where we have sub-pages, we may need to find roots for sub-pages here | ||
// and do a call to load them as a dictionary, before creating the data models, reading off the sub-page roots. | ||
|
||
return results | ||
.GroupBy(r => r.EntityId) | ||
.Select(g => | ||
{ | ||
var rows = g.ToList(); | ||
var finalRow = rows.Last(); | ||
var nextCursor = finalRow.NextExclusiveCursorStateVersion.HasValue | ||
? new GatewayModel.IdBoundaryCoursor(finalRow.NextExclusiveCursorStateVersion, finalRow.NextExclusiveCursorDefinitionId) | ||
: null; | ||
return new PerEntityResult( | ||
EntityId: finalRow.EntityId, | ||
NonFungibleEntityAddress: (EntityAddress)finalRow.EntityAddress, | ||
NextCursor: nextCursor, | ||
TotalEntriesGivenPagingParameters: queryConfiguration.IncludeDeleted ? finalRow.TotalEntriesIncludingDeleted : finalRow.TotalEntriesExcludingDeleted, | ||
TotalMinted: finalRow.TotalEntriesIncludingDeleted, | ||
TotalSupply: finalRow.TotalEntriesExcludingDeleted, | ||
PageItems: rows | ||
.Where(f => !f.FilterOut) | ||
.Select(row => new PageItem( | ||
DefinitionId: row.DefinitionId!.Value, | ||
NonFungibleId: row.Key!, | ||
KeyFirstSeenStateVersion: row.KeyFirstSeenStateVersion!.Value, | ||
Data: row.Value, // Will be null if !IncludeValue | ||
IsLocked: row.IsLocked!.Value, | ||
IsDeleted: row.IsDeleted!.Value, | ||
DataLastUpdatedStateVersion: row.LastUpdatedStateVersion!.Value | ||
)).ToList() | ||
); | ||
}) | ||
.ToDictionary(r => r.NonFungibleEntityAddress); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1ng
substring won't be observed inpackage_1loc2dsadaxxxx1ngdasdasdas
or something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
REP-71
if we'd ratherEntityAddress
to work over a toolkit address