Commit 96a45e31 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Optimisations for /lockchain/current (#197)

parent ee085835
......@@ -13,18 +13,16 @@ function BlockDAL(fileDAL, loki) {
let collection = loki.getCollection('blocks') || loki.addCollection('blocks', { indices: ['fork', 'number', 'hash'] });
let blocksDB = getView();
let udView = collection.addDynamicView('udView');
udView.applyFind({
$and: [{
fork: false,
}]
});
udView.applySimpleSort('number', true);
let current = null;
this.init = () => null;
this.getCurrent = () => Q(blocksDB.branchResultset().simplesort('number', true).limit(1).data()[0]);
this.getCurrent = () => {
if (!current) {
current = blocksDB.branchResultset().simplesort('number', true).limit(1).data()[0];
}
return Q(current);
};
this.getBlock = (number) => Q(blocksDB.branchResultset().find({ number: parseInt(number) }).data()[0]);
......@@ -53,7 +51,14 @@ function BlockDAL(fileDAL, loki) {
return Q(blocksOfIssuer[0]);
};
this.saveBunch = (blocks) => {
collection.insert(blocks);
};
this.saveBlock = (block) => {
if (!current || current.number < block.number) {
current = block;
}
block.fork = !!fileDAL.parentDAL;
let existing;
existing = collection.find({
......
......@@ -1414,7 +1414,7 @@ function BlockchainService (conf, mainDAL, pair) {
});
});
}
mainDAL.blockDAL.collection.insert(blocks);
mainDAL.blockDAL.saveBunch(blocks);
//console.log('Saved');
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment