Skip to content

Commit

Permalink
Feat: removed count to make queries faster
Browse files Browse the repository at this point in the history
  • Loading branch information
AvbrehtLuka committed Dec 23, 2024
1 parent 5bdcbe4 commit b64b285
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
30 changes: 16 additions & 14 deletions src/services/indexer-services/utxo-indexer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
* @param blockNumber
* @returns
*/
public async confirmedBlockAt(
blockNumber: number,
): Promise<ApiDBBlock> {
public async confirmedBlockAt(blockNumber: number): Promise<ApiDBBlock> {
const query = this.manager
.createQueryBuilder(this.blockTable, 'block')
.andWhere('block.block_number = :blockNumber', { blockNumber });
Expand All @@ -124,9 +122,15 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
/**
* Gets a confirmed block from the indexer database in the given block number range and pagination props.
*/
public async listBlock({ from, to }: QueryBlock): Promise<PaginatedList<ApiDBBlock>> {
public async listBlock({
from,
to,
}: QueryBlock): Promise<PaginatedList<ApiDBBlock>> {
// TODO: (Luka) add pagination
let theLimit = this.indexerServerPageLimit;
let query = this.manager.createQueryBuilder(this.blockTable, 'block').orderBy('block.block_number', 'ASC');
let query = this.manager
.createQueryBuilder(this.blockTable, 'block')
.orderBy('block.block_number', 'ASC');
const count = await query.getCount();
theLimit = Math.min(theLimit, count);

Expand All @@ -139,9 +143,11 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
}
if (to !== undefined) {
if (from === undefined) {
query = query.andWhere('block.block_number <= :to', { to }).take(theLimit);
query = query
.andWhere('block.block_number <= :to', { to })
.take(theLimit);
} else {
const tempTo = Math.min(to, from + theLimit - 1)
const tempTo = Math.min(to, from + theLimit - 1);
theLimit = tempTo - from + 1;
query = query.andWhere('block.block_number <= :tempTo', { tempTo });
}
Expand All @@ -154,7 +160,7 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
return res.toApiDBBlock();
});

return new PaginatedList(items, count, theLimit, 0);
return new PaginatedList(items, theLimit, 0);
}

/**
Expand All @@ -171,7 +177,6 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
return res.toApiDBBlock();
}
throw new Error('Block not found');

}

/**
Expand Down Expand Up @@ -218,23 +223,20 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
if (returnResponse) {
query = this.joinTransactionQuery(query);
}
const count = await query.getCount();
const results = await query.getMany();
const items = results.map((res) => {
return res.toApiDBTransaction(this.chainType, returnResponse);
});

return new PaginatedList(items, count, theLimit, theOffset);
return new PaginatedList(items, theLimit, theOffset);
}

/**
* Gets the confirmed transaction from the indexer database for a given transaction id (hash).
* @param txHash
* @returns
*/
public async getTransaction(
txHash: string,
): Promise<ApiDBTransaction> {
public async getTransaction(txHash: string): Promise<ApiDBTransaction> {
const query = this.joinTransactionQuery(
this.manager
.createQueryBuilder(this.transactionTable, 'transaction')
Expand Down
23 changes: 12 additions & 11 deletions src/services/indexer-services/xrp-indexer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ export class XrpExternalIndexerEngineService extends IIndexerEngineService {

public async listBlock({
from,
to
to,
}: QueryBlock): Promise<PaginatedList<ApiDBBlock>> {
// TODO: (Luka) add pagination
let theLimit = this.indexerServerPageLimit;
let query = this.manager.createQueryBuilder(this.blockTable, 'block').orderBy('block.block_number', 'ASC');
let query = this.manager
.createQueryBuilder(this.blockTable, 'block')
.orderBy('block.block_number', 'ASC');
const count = await query.getCount();
theLimit = Math.min(theLimit, count);

Expand All @@ -98,9 +101,11 @@ export class XrpExternalIndexerEngineService extends IIndexerEngineService {
}
if (to !== undefined) {
if (from === undefined) {
query = query.andWhere('block.block_number <= :to', { to }).take(theLimit);
query = query
.andWhere('block.block_number <= :to', { to })
.take(theLimit);
} else {
const tempTo = Math.min(to, from + theLimit - 1)
const tempTo = Math.min(to, from + theLimit - 1);
theLimit = tempTo - from + 1;
query = query.andWhere('block.block_number <= :tempTo', { tempTo });
}
Expand All @@ -113,7 +118,7 @@ export class XrpExternalIndexerEngineService extends IIndexerEngineService {
return res.toApiDBBlock();
});

return new PaginatedList(items, count, theLimit, 0);
return new PaginatedList(items, theLimit, 0);
}

public async getBlock(blockHash: string): Promise<ApiDBBlock> {
Expand Down Expand Up @@ -166,18 +171,15 @@ export class XrpExternalIndexerEngineService extends IIndexerEngineService {
.limit(theLimit)
.offset(theOffset);

const count = await query.getCount();
const results = await query.getMany();
const items = results.map((res) => {
return res.toApiDBTransaction(returnResponse);
});

return new PaginatedList(items, count, theLimit, theOffset);
return new PaginatedList(items, theLimit, theOffset);
}

public async getTransaction(
txHash: string,
): Promise<ApiDBTransaction> {
public async getTransaction(txHash: string): Promise<ApiDBTransaction> {
const query = this.manager
.createQueryBuilder(this.transactionTable, 'transaction')
.andWhere('transaction.hash = :txHash', { txHash });
Expand All @@ -188,7 +190,6 @@ export class XrpExternalIndexerEngineService extends IIndexerEngineService {
throw new Error('Transaction not found');
}


public async getTransactionBlock(txHash: string): Promise<ApiDBBlock | null> {
const tx = await this.getTransaction(txHash);
if (!tx) {
Expand Down
13 changes: 6 additions & 7 deletions src/utils/api-models/PaginatedList.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ApiProperty } from "@nestjs/swagger";

export class PaginatedList<T> {
/**
* Count of all items satisfying 'paginatable' request.
*/
@ApiProperty()
count: number;
// /**
// * Count of all items satisfying 'paginatable' request.
// */
// @ApiProperty()
// count: number;
/**
* Response items.
*/
Expand All @@ -21,9 +21,8 @@ export class PaginatedList<T> {
@ApiProperty()
offset: number;

constructor(items: T[], count: number, limit: number, offset: number) {
constructor(items: T[], limit: number, offset: number) {
this.items = items;
this.count = count;
this.limit = limit;
this.offset = offset;
}
Expand Down

0 comments on commit b64b285

Please sign in to comment.