Skip to content

Commit

Permalink
fix(bitcore-node): set all indexes to build in the background
Browse files Browse the repository at this point in the history
  • Loading branch information
bitjson committed Nov 1, 2018
1 parent 3d50203 commit 107fe0b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
8 changes: 4 additions & 4 deletions packages/bitcore-node/src/models/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export class Block extends BaseModel<IBlock> {
];

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: {
Expand Down
19 changes: 12 additions & 7 deletions packages/bitcore-node/src/models/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,21 @@ class Coin extends BaseModel<ICoin> {
];

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 }) {
Expand Down
10 changes: 5 additions & 5 deletions packages/bitcore-node/src/models/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export class Transaction extends BaseModel<ITransaction> {
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: {
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcore-node/src/models/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Wallet extends BaseModel<IWallet> {
allowedPaging = [];

onConnect() {
this.collection.createIndex({ pubKey: 1 });
this.collection.createIndex({ pubKey: 1 }, { background: true });
}

_apiTransform(wallet: IWallet, options: TransformOptions) {
Expand Down
25 changes: 19 additions & 6 deletions packages/bitcore-node/src/models/walletAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class WalletAddress extends BaseModel<IWalletAddress> {
allowedPaging = [];

onConnect() {
this.collection.createIndex({ address: 1, wallet: 1 });
this.collection.createIndex({ address: 1, wallet: 1 }, { background: true });
}

_apiTransform(walletAddress: { address: string }, options: TransformOptions) {
Expand Down Expand Up @@ -65,20 +65,33 @@ export class WalletAddress extends BaseModel<IWalletAddress> {
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;
});
Expand Down

0 comments on commit 107fe0b

Please sign in to comment.