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

fix: use limit/offset instead of a cursor #252

Merged
merged 2 commits into from
Oct 11, 2023
Merged
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
35 changes: 21 additions & 14 deletions src/pg/brc20/brc20-pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,27 @@ export class Brc20PgStore extends BasePgStoreModule {
for (let blockHeight = startBlock; blockHeight <= endBlock; blockHeight++) {
logger.info(`Brc20PgStore scanning block ${blockHeight}`);
await this.sqlWriteTransaction(async sql => {
const cursor = sql<DbBrc20ScannedInscription[]>`
SELECT
EXISTS(SELECT location_id FROM genesis_locations WHERE location_id = l.id) AS genesis,
l.id, l.inscription_id, l.block_height, l.tx_id, l.tx_index, l.address
FROM locations AS l
INNER JOIN inscriptions AS i ON l.inscription_id = i.id
WHERE l.block_height = ${blockHeight}
AND i.number >= 0
AND i.mime_type IN ('application/json', 'text/plain')
ORDER BY tx_index ASC
`.cursor(100_000);
for await (const chunk of cursor) {
await this.insertOperations(chunk);
}
const limit = 100_000;
let offset = 0;
do {
const block = await sql<DbBrc20ScannedInscription[]>`
SELECT
EXISTS(SELECT location_id FROM genesis_locations WHERE location_id = l.id) AS genesis,
l.id, l.inscription_id, l.block_height, l.tx_id, l.tx_index, l.address
FROM locations AS l
INNER JOIN inscriptions AS i ON l.inscription_id = i.id
WHERE l.block_height = ${blockHeight}
AND i.number >= 0
AND i.mime_type IN ('application/json', 'text/plain')
ORDER BY tx_index ASC
LIMIT ${limit}
OFFSET ${offset}
`;
if (block.count === 0) break;
await this.insertOperations(block);
if (block.count < limit) break;
offset += limit;
} while (true);
});
}
}
Expand Down
Loading