Skip to content

Commit

Permalink
fix: use limit/offset instead of a cursor (#252)
Browse files Browse the repository at this point in the history
* fix: use limit/offset instead of a cursor

* fix: jump early
  • Loading branch information
rafaelcr committed Oct 11, 2023
1 parent c15185d commit e4e9819
Showing 1 changed file with 21 additions and 14 deletions.
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

0 comments on commit e4e9819

Please sign in to comment.