Skip to content

Commit 107fe0b

Browse files
committed
fix(bitcore-node): set all indexes to build in the background
1 parent 3d50203 commit 107fe0b

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

packages/bitcore-node/src/models/block.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ export class Block extends BaseModel<IBlock> {
2323
];
2424

2525
async onConnect() {
26-
this.collection.createIndex({ hash: 1 });
27-
this.collection.createIndex({ chain: 1, network: 1, processed: 1, height: -1 });
28-
this.collection.createIndex({ chain: 1, network: 1, timeNormalized: 1 });
29-
this.collection.createIndex({ previousBlockHash: 1 });
26+
this.collection.createIndex({ hash: 1 }, { background: true });
27+
this.collection.createIndex({ chain: 1, network: 1, processed: 1, height: -1 }, { background: true });
28+
this.collection.createIndex({ chain: 1, network: 1, timeNormalized: 1 }, { background: true });
29+
this.collection.createIndex({ previousBlockHash: 1 }, { background: true });
3030
}
3131

3232
async addBlock(params: {

packages/bitcore-node/src/models/coin.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,21 @@ class Coin extends BaseModel<ICoin> {
4848
];
4949

5050
onConnect() {
51-
this.collection.createIndex({ mintTxid: 1, mintIndex: 1 });
51+
this.collection.createIndex({ mintTxid: 1, mintIndex: 1 }, { background: true });
5252
this.collection.createIndex(
5353
{ address: 1, chain: 1, network: 1 },
54-
{ partialFilterExpression: { spentHeight: { $lt: 0 } } }
54+
{
55+
background: true,
56+
partialFilterExpression: {
57+
spentHeight: { $lt: 0 }
58+
}
59+
}
5560
);
56-
this.collection.createIndex({ address: 1 });
57-
this.collection.createIndex({ mintHeight: 1, chain: 1, network: 1 });
58-
this.collection.createIndex({ spentTxid: 1 }, { sparse: true });
59-
this.collection.createIndex({ spentHeight: 1, chain: 1, network: 1 });
60-
this.collection.createIndex({ wallets: 1, spentHeight: 1 }, { sparse: true });
61+
this.collection.createIndex({ address: 1 }, { background: true });
62+
this.collection.createIndex({ mintHeight: 1, chain: 1, network: 1 }, { background: true });
63+
this.collection.createIndex({ spentTxid: 1 }, { background: true, sparse: true });
64+
this.collection.createIndex({ spentHeight: 1, chain: 1, network: 1 }, { background: true });
65+
this.collection.createIndex({ wallets: 1, spentHeight: 1 }, { background: true, sparse: true });
6166
}
6267

6368
getBalance(params: { query: any }) {

packages/bitcore-node/src/models/transaction.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ export class Transaction extends BaseModel<ITransaction> {
3939
allowedPaging = [{ key: 'blockHeight' as 'blockHeight', type: 'number' as 'number' }];
4040

4141
onConnect() {
42-
this.collection.createIndex({ txid: 1 });
43-
this.collection.createIndex({ blockHeight: 1, chain: 1, network: 1 });
44-
this.collection.createIndex({ blockHash: 1 });
45-
this.collection.createIndex({ blockTimeNormalized: 1, chain: 1, network: 1 });
46-
this.collection.createIndex({ wallets: 1, blockTimeNormalized: 1, _id: -1 }, { sparse: true });
42+
this.collection.createIndex({ txid: 1 }, { background: true });
43+
this.collection.createIndex({ blockHeight: 1, chain: 1, network: 1 }, { background: true });
44+
this.collection.createIndex({ blockHash: 1 }, { background: true });
45+
this.collection.createIndex({ blockTimeNormalized: 1, chain: 1, network: 1 }, { background: true });
46+
this.collection.createIndex({ wallets: 1, blockTimeNormalized: 1, _id: -1 }, { background: true, sparse: true });
4747
}
4848

4949
async batchImport(params: {

packages/bitcore-node/src/models/wallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class Wallet extends BaseModel<IWallet> {
2020
allowedPaging = [];
2121

2222
onConnect() {
23-
this.collection.createIndex({ pubKey: 1 });
23+
this.collection.createIndex({ pubKey: 1 }, { background: true });
2424
}
2525

2626
_apiTransform(wallet: IWallet, options: TransformOptions) {

packages/bitcore-node/src/models/walletAddress.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class WalletAddress extends BaseModel<IWalletAddress> {
2020
allowedPaging = [];
2121

2222
onConnect() {
23-
this.collection.createIndex({ address: 1, wallet: 1 });
23+
this.collection.createIndex({ address: 1, wallet: 1 }, { background: true });
2424
}
2525

2626
_apiTransform(walletAddress: { address: string }, options: TransformOptions) {
@@ -65,20 +65,33 @@ export class WalletAddress extends BaseModel<IWalletAddress> {
6565
return new Promise(async resolve => {
6666
for (const address of addresses) {
6767
await Promise.all([
68-
WalletAddressModel.collection.updateOne({ wallet: wallet._id, address }, { $set: { wallet: wallet._id, address: address, chain, network } }, { upsert: true }),
69-
CoinModel.collection.updateMany({ chain, network, address }, { $addToSet: { wallets: wallet._id }})
68+
WalletAddressModel.collection.updateOne(
69+
{ wallet: wallet._id, address },
70+
{ $set: { wallet: wallet._id, address: address, chain, network } },
71+
{ upsert: true }
72+
),
73+
CoinModel.collection.updateMany({ chain, network, address }, { $addToSet: { wallets: wallet._id } })
7074
]);
7175
}
7276

73-
let coinStream = CoinModel.collection.find({ wallets: wallet._id }).project({ spentTxid: 1, mintTxid: 1 }).addCursorFlag('noCursorTimeout', true);
77+
let coinStream = CoinModel.collection
78+
.find({ wallets: wallet._id })
79+
.project({ spentTxid: 1, mintTxid: 1 })
80+
.addCursorFlag('noCursorTimeout', true);
7481
let txids = {};
7582
coinStream.on('data', (coin: ICoin) => {
7683
if (!txids[coin.mintTxid]) {
77-
TransactionModel.collection.update({ txid: coin.mintTxid, network, chain }, { $addToSet: { wallets: wallet._id } });
84+
TransactionModel.collection.update(
85+
{ txid: coin.mintTxid, network, chain },
86+
{ $addToSet: { wallets: wallet._id } }
87+
);
7888
}
7989
txids[coin.mintTxid] = true;
8090
if (!txids[coin.spentTxid]) {
81-
TransactionModel.collection.update({ txid: coin.spentTxid, network, chain }, { $addToSet: { wallets: wallet._id } });
91+
TransactionModel.collection.update(
92+
{ txid: coin.spentTxid, network, chain },
93+
{ $addToSet: { wallets: wallet._id } }
94+
);
8295
}
8396
txids[coin.spentTxid] = true;
8497
});

0 commit comments

Comments
 (0)