Skip to content

Commit

Permalink
feat: Optimize output db query
Browse files Browse the repository at this point in the history
  • Loading branch information
ashchan committed Oct 28, 2019
1 parent 9de193b commit 6e36550
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {MigrationInterface, QueryRunner, TableIndex} from "typeorm";

export class AddOutputIndex1572226722928 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.createIndex("output", new TableIndex({ columnNames: ["lockHash", "status", "hasData", "typeScript"] }))
}

public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.dropIndex("output", new TableIndex({ columnNames: ["lockHash", "status", "hasData", "typeScript"] }))
}

}
4 changes: 3 additions & 1 deletion packages/neuron-wallet/src/database/chain/ormconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ChangeHasDataDefault1568621556467 } from './migrations/1568621556467-Ch
import { AddLockToInput1570522869590 } from './migrations/1570522869590-AddLockToInput'
import { AddIndices1572006450765 } from './migrations/1572006450765-AddIndices'
import { AddIndexToTxTimestamp1572137226866 } from './migrations/1572137226866-AddIndexToTxTimestamp'
import { AddOutputIndex1572226722928 } from './migrations/1572226722928-AddOutputIndex'

export const CONNECTION_NOT_FOUND_NAME = 'ConnectionNotFoundError'

Expand All @@ -41,7 +42,8 @@ const connectOptions = async (genesisBlockHash: string): Promise<SqliteConnectio
ChangeHasDataDefault1568621556467,
AddLockToInput1570522869590,
AddIndices1572006450765,
AddIndexToTxTimestamp1572137226866
AddIndexToTxTimestamp1572137226866,
AddOutputIndex1572226722928
],
logging,
maxQueryExecutionTime: 30
Expand Down
13 changes: 10 additions & 3 deletions packages/neuron-wallet/src/services/cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ export default class CellsService {

const cells: OutputEntity[] = await getConnection()
.getRepository(OutputEntity)
.find({
where: queryParams,
})
.createQueryBuilder('output')
.select([
"output.lockHash",
"output.status",
"output.hasData",
"output.typeScript",
"output.capacity"
])
.where(queryParams)
.getMany()

const capacity: bigint = cells.map(c => BigInt(c.capacity)).reduce((result, c) => result + c, BigInt(0))

Expand Down

0 comments on commit 6e36550

Please sign in to comment.