From 107fe0b20b8b0342a2e46322292e096e422806e3 Mon Sep 17 00:00:00 2001 From: Jason Dreyzehner Date: Thu, 1 Nov 2018 15:56:43 -0400 Subject: [PATCH] fix(bitcore-node): set all indexes to build in the background --- packages/bitcore-node/src/models/block.ts | 8 +++--- packages/bitcore-node/src/models/coin.ts | 19 ++++++++------ .../bitcore-node/src/models/transaction.ts | 10 ++++---- packages/bitcore-node/src/models/wallet.ts | 2 +- .../bitcore-node/src/models/walletAddress.ts | 25 ++++++++++++++----- 5 files changed, 41 insertions(+), 23 deletions(-) diff --git a/packages/bitcore-node/src/models/block.ts b/packages/bitcore-node/src/models/block.ts index 9122aac8ee0..a74f538e365 100644 --- a/packages/bitcore-node/src/models/block.ts +++ b/packages/bitcore-node/src/models/block.ts @@ -23,10 +23,10 @@ export class Block extends BaseModel { ]; async onConnect() { - this.collection.createIndex({ hash: 1 }); - this.collection.createIndex({ chain: 1, network: 1, processed: 1, height: -1 }); - this.collection.createIndex({ chain: 1, network: 1, timeNormalized: 1 }); - this.collection.createIndex({ previousBlockHash: 1 }); + this.collection.createIndex({ hash: 1 }, { background: true }); + this.collection.createIndex({ chain: 1, network: 1, processed: 1, height: -1 }, { background: true }); + this.collection.createIndex({ chain: 1, network: 1, timeNormalized: 1 }, { background: true }); + this.collection.createIndex({ previousBlockHash: 1 }, { background: true }); } async addBlock(params: { diff --git a/packages/bitcore-node/src/models/coin.ts b/packages/bitcore-node/src/models/coin.ts index 288e0831dc7..a803ce57ea6 100644 --- a/packages/bitcore-node/src/models/coin.ts +++ b/packages/bitcore-node/src/models/coin.ts @@ -48,16 +48,21 @@ class Coin extends BaseModel { ]; onConnect() { - this.collection.createIndex({ mintTxid: 1, mintIndex: 1 }); + this.collection.createIndex({ mintTxid: 1, mintIndex: 1 }, { background: true }); this.collection.createIndex( { address: 1, chain: 1, network: 1 }, - { partialFilterExpression: { spentHeight: { $lt: 0 } } } + { + background: true, + partialFilterExpression: { + spentHeight: { $lt: 0 } + } + } ); - this.collection.createIndex({ address: 1 }); - this.collection.createIndex({ mintHeight: 1, chain: 1, network: 1 }); - this.collection.createIndex({ spentTxid: 1 }, { sparse: true }); - this.collection.createIndex({ spentHeight: 1, chain: 1, network: 1 }); - this.collection.createIndex({ wallets: 1, spentHeight: 1 }, { sparse: true }); + this.collection.createIndex({ address: 1 }, { background: true }); + this.collection.createIndex({ mintHeight: 1, chain: 1, network: 1 }, { background: true }); + this.collection.createIndex({ spentTxid: 1 }, { background: true, sparse: true }); + this.collection.createIndex({ spentHeight: 1, chain: 1, network: 1 }, { background: true }); + this.collection.createIndex({ wallets: 1, spentHeight: 1 }, { background: true, sparse: true }); } getBalance(params: { query: any }) { diff --git a/packages/bitcore-node/src/models/transaction.ts b/packages/bitcore-node/src/models/transaction.ts index b3cf1a28a6b..ee4f805c136 100644 --- a/packages/bitcore-node/src/models/transaction.ts +++ b/packages/bitcore-node/src/models/transaction.ts @@ -39,11 +39,11 @@ export class Transaction extends BaseModel { allowedPaging = [{ key: 'blockHeight' as 'blockHeight', type: 'number' as 'number' }]; onConnect() { - this.collection.createIndex({ txid: 1 }); - this.collection.createIndex({ blockHeight: 1, chain: 1, network: 1 }); - this.collection.createIndex({ blockHash: 1 }); - this.collection.createIndex({ blockTimeNormalized: 1, chain: 1, network: 1 }); - this.collection.createIndex({ wallets: 1, blockTimeNormalized: 1, _id: -1 }, { sparse: true }); + this.collection.createIndex({ txid: 1 }, { background: true }); + this.collection.createIndex({ blockHeight: 1, chain: 1, network: 1 }, { background: true }); + this.collection.createIndex({ blockHash: 1 }, { background: true }); + this.collection.createIndex({ blockTimeNormalized: 1, chain: 1, network: 1 }, { background: true }); + this.collection.createIndex({ wallets: 1, blockTimeNormalized: 1, _id: -1 }, { background: true, sparse: true }); } async batchImport(params: { diff --git a/packages/bitcore-node/src/models/wallet.ts b/packages/bitcore-node/src/models/wallet.ts index 2d4d1e29ded..c1d6cc656e3 100644 --- a/packages/bitcore-node/src/models/wallet.ts +++ b/packages/bitcore-node/src/models/wallet.ts @@ -20,7 +20,7 @@ export class Wallet extends BaseModel { allowedPaging = []; onConnect() { - this.collection.createIndex({ pubKey: 1 }); + this.collection.createIndex({ pubKey: 1 }, { background: true }); } _apiTransform(wallet: IWallet, options: TransformOptions) { diff --git a/packages/bitcore-node/src/models/walletAddress.ts b/packages/bitcore-node/src/models/walletAddress.ts index a7955fc9db4..edf7fa49080 100644 --- a/packages/bitcore-node/src/models/walletAddress.ts +++ b/packages/bitcore-node/src/models/walletAddress.ts @@ -20,7 +20,7 @@ export class WalletAddress extends BaseModel { allowedPaging = []; onConnect() { - this.collection.createIndex({ address: 1, wallet: 1 }); + this.collection.createIndex({ address: 1, wallet: 1 }, { background: true }); } _apiTransform(walletAddress: { address: string }, options: TransformOptions) { @@ -65,20 +65,33 @@ export class WalletAddress extends BaseModel { return new Promise(async resolve => { for (const address of addresses) { await Promise.all([ - WalletAddressModel.collection.updateOne({ wallet: wallet._id, address }, { $set: { wallet: wallet._id, address: address, chain, network } }, { upsert: true }), - CoinModel.collection.updateMany({ chain, network, address }, { $addToSet: { wallets: wallet._id }}) + WalletAddressModel.collection.updateOne( + { wallet: wallet._id, address }, + { $set: { wallet: wallet._id, address: address, chain, network } }, + { upsert: true } + ), + CoinModel.collection.updateMany({ chain, network, address }, { $addToSet: { wallets: wallet._id } }) ]); } - let coinStream = CoinModel.collection.find({ wallets: wallet._id }).project({ spentTxid: 1, mintTxid: 1 }).addCursorFlag('noCursorTimeout', true); + let coinStream = CoinModel.collection + .find({ wallets: wallet._id }) + .project({ spentTxid: 1, mintTxid: 1 }) + .addCursorFlag('noCursorTimeout', true); let txids = {}; coinStream.on('data', (coin: ICoin) => { if (!txids[coin.mintTxid]) { - TransactionModel.collection.update({ txid: coin.mintTxid, network, chain }, { $addToSet: { wallets: wallet._id } }); + TransactionModel.collection.update( + { txid: coin.mintTxid, network, chain }, + { $addToSet: { wallets: wallet._id } } + ); } txids[coin.mintTxid] = true; if (!txids[coin.spentTxid]) { - TransactionModel.collection.update({ txid: coin.spentTxid, network, chain }, { $addToSet: { wallets: wallet._id } }); + TransactionModel.collection.update( + { txid: coin.spentTxid, network, chain }, + { $addToSet: { wallets: wallet._id } } + ); } txids[coin.spentTxid] = true; });