Skip to content

Commit b64b285

Browse files
committed
Feat: removed count to make queries faster
1 parent 5bdcbe4 commit b64b285

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

src/services/indexer-services/utxo-indexer.service.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
108108
* @param blockNumber
109109
* @returns
110110
*/
111-
public async confirmedBlockAt(
112-
blockNumber: number,
113-
): Promise<ApiDBBlock> {
111+
public async confirmedBlockAt(blockNumber: number): Promise<ApiDBBlock> {
114112
const query = this.manager
115113
.createQueryBuilder(this.blockTable, 'block')
116114
.andWhere('block.block_number = :blockNumber', { blockNumber });
@@ -124,9 +122,15 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
124122
/**
125123
* Gets a confirmed block from the indexer database in the given block number range and pagination props.
126124
*/
127-
public async listBlock({ from, to }: QueryBlock): Promise<PaginatedList<ApiDBBlock>> {
125+
public async listBlock({
126+
from,
127+
to,
128+
}: QueryBlock): Promise<PaginatedList<ApiDBBlock>> {
129+
// TODO: (Luka) add pagination
128130
let theLimit = this.indexerServerPageLimit;
129-
let query = this.manager.createQueryBuilder(this.blockTable, 'block').orderBy('block.block_number', 'ASC');
131+
let query = this.manager
132+
.createQueryBuilder(this.blockTable, 'block')
133+
.orderBy('block.block_number', 'ASC');
130134
const count = await query.getCount();
131135
theLimit = Math.min(theLimit, count);
132136

@@ -139,9 +143,11 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
139143
}
140144
if (to !== undefined) {
141145
if (from === undefined) {
142-
query = query.andWhere('block.block_number <= :to', { to }).take(theLimit);
146+
query = query
147+
.andWhere('block.block_number <= :to', { to })
148+
.take(theLimit);
143149
} else {
144-
const tempTo = Math.min(to, from + theLimit - 1)
150+
const tempTo = Math.min(to, from + theLimit - 1);
145151
theLimit = tempTo - from + 1;
146152
query = query.andWhere('block.block_number <= :tempTo', { tempTo });
147153
}
@@ -154,7 +160,7 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
154160
return res.toApiDBBlock();
155161
});
156162

157-
return new PaginatedList(items, count, theLimit, 0);
163+
return new PaginatedList(items, theLimit, 0);
158164
}
159165

160166
/**
@@ -171,7 +177,6 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
171177
return res.toApiDBBlock();
172178
}
173179
throw new Error('Block not found');
174-
175180
}
176181

177182
/**
@@ -218,23 +223,20 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
218223
if (returnResponse) {
219224
query = this.joinTransactionQuery(query);
220225
}
221-
const count = await query.getCount();
222226
const results = await query.getMany();
223227
const items = results.map((res) => {
224228
return res.toApiDBTransaction(this.chainType, returnResponse);
225229
});
226230

227-
return new PaginatedList(items, count, theLimit, theOffset);
231+
return new PaginatedList(items, theLimit, theOffset);
228232
}
229233

230234
/**
231235
* Gets the confirmed transaction from the indexer database for a given transaction id (hash).
232236
* @param txHash
233237
* @returns
234238
*/
235-
public async getTransaction(
236-
txHash: string,
237-
): Promise<ApiDBTransaction> {
239+
public async getTransaction(txHash: string): Promise<ApiDBTransaction> {
238240
const query = this.joinTransactionQuery(
239241
this.manager
240242
.createQueryBuilder(this.transactionTable, 'transaction')

src/services/indexer-services/xrp-indexer.service.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ export class XrpExternalIndexerEngineService extends IIndexerEngineService {
8282

8383
public async listBlock({
8484
from,
85-
to
85+
to,
8686
}: QueryBlock): Promise<PaginatedList<ApiDBBlock>> {
87+
// TODO: (Luka) add pagination
8788
let theLimit = this.indexerServerPageLimit;
88-
let query = this.manager.createQueryBuilder(this.blockTable, 'block').orderBy('block.block_number', 'ASC');
89+
let query = this.manager
90+
.createQueryBuilder(this.blockTable, 'block')
91+
.orderBy('block.block_number', 'ASC');
8992
const count = await query.getCount();
9093
theLimit = Math.min(theLimit, count);
9194

@@ -98,9 +101,11 @@ export class XrpExternalIndexerEngineService extends IIndexerEngineService {
98101
}
99102
if (to !== undefined) {
100103
if (from === undefined) {
101-
query = query.andWhere('block.block_number <= :to', { to }).take(theLimit);
104+
query = query
105+
.andWhere('block.block_number <= :to', { to })
106+
.take(theLimit);
102107
} else {
103-
const tempTo = Math.min(to, from + theLimit - 1)
108+
const tempTo = Math.min(to, from + theLimit - 1);
104109
theLimit = tempTo - from + 1;
105110
query = query.andWhere('block.block_number <= :tempTo', { tempTo });
106111
}
@@ -113,7 +118,7 @@ export class XrpExternalIndexerEngineService extends IIndexerEngineService {
113118
return res.toApiDBBlock();
114119
});
115120

116-
return new PaginatedList(items, count, theLimit, 0);
121+
return new PaginatedList(items, theLimit, 0);
117122
}
118123

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

169-
const count = await query.getCount();
170174
const results = await query.getMany();
171175
const items = results.map((res) => {
172176
return res.toApiDBTransaction(returnResponse);
173177
});
174178

175-
return new PaginatedList(items, count, theLimit, theOffset);
179+
return new PaginatedList(items, theLimit, theOffset);
176180
}
177181

178-
public async getTransaction(
179-
txHash: string,
180-
): Promise<ApiDBTransaction> {
182+
public async getTransaction(txHash: string): Promise<ApiDBTransaction> {
181183
const query = this.manager
182184
.createQueryBuilder(this.transactionTable, 'transaction')
183185
.andWhere('transaction.hash = :txHash', { txHash });
@@ -188,7 +190,6 @@ export class XrpExternalIndexerEngineService extends IIndexerEngineService {
188190
throw new Error('Transaction not found');
189191
}
190192

191-
192193
public async getTransactionBlock(txHash: string): Promise<ApiDBBlock | null> {
193194
const tx = await this.getTransaction(txHash);
194195
if (!tx) {

src/utils/api-models/PaginatedList.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { ApiProperty } from "@nestjs/swagger";
22

33
export class PaginatedList<T> {
4-
/**
5-
* Count of all items satisfying 'paginatable' request.
6-
*/
7-
@ApiProperty()
8-
count: number;
4+
// /**
5+
// * Count of all items satisfying 'paginatable' request.
6+
// */
7+
// @ApiProperty()
8+
// count: number;
99
/**
1010
* Response items.
1111
*/
@@ -21,9 +21,8 @@ export class PaginatedList<T> {
2121
@ApiProperty()
2222
offset: number;
2323

24-
constructor(items: T[], count: number, limit: number, offset: number) {
24+
constructor(items: T[], limit: number, offset: number) {
2525
this.items = items;
26-
this.count = count;
2726
this.limit = limit;
2827
this.offset = offset;
2928
}

0 commit comments

Comments
 (0)