Skip to content
Snippets Groups Projects
Commit 9af81aec authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

fix(format)

parent 0dfa8abd
Branches
Tags
1 merge request!1429fix(1442): Optimize BMA /tx/history
Pipeline #32240 failed
...@@ -29,10 +29,18 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO { ...@@ -29,10 +29,18 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO {
inputs: new SqlNullableFieldDefinition("JSON", false), inputs: new SqlNullableFieldDefinition("JSON", false),
unlocks: new SqlNullableFieldDefinition("JSON", false), unlocks: new SqlNullableFieldDefinition("JSON", false),
outputs: new SqlNullableFieldDefinition("JSON", false), outputs: new SqlNullableFieldDefinition("JSON", false),
issuer: new SqlNullableFieldDefinition("VARCHAR", true, 50), /* computed column - need by getTxHistoryXxx() */ issuer: new SqlNullableFieldDefinition(
"VARCHAR",
true,
50
) /* computed column - need by getTxHistoryXxx() */,
issuers: new SqlNullableFieldDefinition("JSON", false), issuers: new SqlNullableFieldDefinition("JSON", false),
signatures: new SqlNullableFieldDefinition("JSON", false), signatures: new SqlNullableFieldDefinition("JSON", false),
recipient: new SqlNullableFieldDefinition("VARCHAR", true, 50), /* computed column - need by getTxHistoryXxx() */ recipient: new SqlNullableFieldDefinition(
"VARCHAR",
true,
50
) /* computed column - need by getTxHistoryXxx() */,
recipients: new SqlNullableFieldDefinition("JSON", false), recipients: new SqlNullableFieldDefinition("JSON", false),
written: new SqlNotNullableFieldDefinition("BOOLEAN", true), written: new SqlNotNullableFieldDefinition("BOOLEAN", true),
removed: new SqlNotNullableFieldDefinition("BOOLEAN", true), removed: new SqlNotNullableFieldDefinition("BOOLEAN", true),
...@@ -93,7 +101,7 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO { ...@@ -93,7 +101,7 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO {
@MonitorExecutionTime() @MonitorExecutionTime()
async saveBatch(records: DBTx[]): Promise<void> { async saveBatch(records: DBTx[]): Promise<void> {
if (records.length) { if (records.length) {
await this.removeByHashBatch(records.map(t => t.hash)); await this.removeByHashBatch(records.map((t) => t.hash));
await this.insertBatch(records); await this.insertBatch(records);
} }
} }
...@@ -172,8 +180,18 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO { ...@@ -172,8 +180,18 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO {
to: number to: number
): Promise<{ sent: DBTx[]; received: DBTx[] }> { ): Promise<{ sent: DBTx[]; received: DBTx[] }> {
return { return {
sent: await this.getLinkedWithIssuerByRange('block_number', pubkey, from, to), sent: await this.getLinkedWithIssuerByRange(
received: await this.getLinkedWithRecipientByRange('block_number', pubkey, from, to), "block_number",
pubkey,
from,
to
),
received: await this.getLinkedWithRecipientByRange(
"block_number",
pubkey,
from,
to
),
}; };
} }
...@@ -183,8 +201,13 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO { ...@@ -183,8 +201,13 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO {
to: number to: number
): Promise<{ sent: DBTx[]; received: DBTx[] }> { ): Promise<{ sent: DBTx[]; received: DBTx[] }> {
return { return {
sent: await this.getLinkedWithIssuerByRange('time', pubkey, from, to), sent: await this.getLinkedWithIssuerByRange("time", pubkey, from, to),
received: await this.getLinkedWithRecipientByRange('time', pubkey, from, to) received: await this.getLinkedWithRecipientByRange(
"time",
pubkey,
from,
to
),
}; };
} }
...@@ -198,7 +221,8 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO { ...@@ -198,7 +221,8 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO {
} }
getLinkedWithIssuer(pubkey: string): Promise<DBTx[]> { getLinkedWithIssuer(pubkey: string): Promise<DBTx[]> {
return this.findEntities(`SELECT * FROM txs return this.findEntities(
`SELECT * FROM txs
WHERE written WHERE written
AND ( AND (
issuer = ? issuer = ?
...@@ -208,8 +232,14 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO { ...@@ -208,8 +232,14 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO {
); );
} }
getLinkedWithIssuerByRange(rangeFieldName: keyof DBTx, pubkey: string, from: number, to: number): Promise<DBTx[]> { getLinkedWithIssuerByRange(
return this.findEntities(`SELECT * FROM txs rangeFieldName: keyof DBTx,
pubkey: string,
from: number,
to: number
): Promise<DBTx[]> {
return this.findEntities(
`SELECT * FROM txs
WHERE written WHERE written
AND ( AND (
issuer = ? issuer = ?
...@@ -222,7 +252,8 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO { ...@@ -222,7 +252,8 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO {
} }
getLinkedWithRecipient(pubkey: string): Promise<DBTx[]> { getLinkedWithRecipient(pubkey: string): Promise<DBTx[]> {
return this.findEntities(`SELECT * FROM txs return this.findEntities(
`SELECT * FROM txs
WHERE written WHERE written
AND ( AND (
recipient = ? recipient = ?
...@@ -232,8 +263,14 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO { ...@@ -232,8 +263,14 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO {
); );
} }
getLinkedWithRecipientByRange(rangeColumnName: string, pubkey: string, from: number, to: number): Promise<DBTx[]> { getLinkedWithRecipientByRange(
return this.findEntities(`SELECT * FROM txs rangeColumnName: string,
pubkey: string,
from: number,
to: number
): Promise<DBTx[]> {
return this.findEntities(
`SELECT * FROM txs
WHERE written WHERE written
AND ( AND (
recipient = ? recipient = ?
...@@ -246,7 +283,8 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO { ...@@ -246,7 +283,8 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO {
} }
getPendingWithIssuer(pubkey: string): Promise<DBTx[]> { getPendingWithIssuer(pubkey: string): Promise<DBTx[]> {
return this.findEntities(`SELECT * FROM txs return this.findEntities(
`SELECT * FROM txs
WHERE NOT written WHERE NOT written
AND ( AND (
issuer = ? issuer = ?
...@@ -257,7 +295,8 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO { ...@@ -257,7 +295,8 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO {
} }
getPendingWithRecipient(pubkey: string): Promise<DBTx[]> { getPendingWithRecipient(pubkey: string): Promise<DBTx[]> {
return this.findEntities(`SELECT * FROM txs return this.findEntities(
`SELECT * FROM txs
WHERE NOT written WHERE NOT written
AND ( AND (
recipient = ? recipient = ?
...@@ -268,7 +307,7 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO { ...@@ -268,7 +307,7 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO {
} }
async existsByHash(hash: string): Promise<boolean> { async existsByHash(hash: string): Promise<boolean> {
return (await this.countBy('hash', hash)) > 0; return (await this.countBy("hash", hash)) > 0;
} }
async getTX(hash: string): Promise<DBTx> { async getTX(hash: string): Promise<DBTx> {
...@@ -298,7 +337,10 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO { ...@@ -298,7 +337,10 @@ export class SqliteTransactions extends SqliteTable<DBTx> implements TxsDAO {
// Delete by slice of 500 items (because SQLite IN operator is limited) // Delete by slice of 500 items (because SQLite IN operator is limited)
while (i < hashArray.length - 1) { while (i < hashArray.length - 1) {
const slice = hashArray.slice(i, i + 500); const slice = hashArray.slice(i, i + 500);
await this.driver.sqlWrite(`DELETE FROM txs WHERE hash IN (${slice.map(_ => '?').join(', ')})`, slice); await this.driver.sqlWrite(
`DELETE FROM txs WHERE hash IN (${slice.map((_) => "?").join(", ")})`,
slice
);
i += 500; i += 500;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment