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

LokiJS: refactoring

parent eb026d15
......@@ -7,10 +7,14 @@ var _ = require('underscore');
module.exports = AbstractLoki;
function AbstractLoki(collection, fileDAL, view) {
function AbstractLoki(collection, fileDAL, viewFields, loki) {
"use strict";
let that = this;
let cores = getCores();
let view = getView();
function find(conditons) {
if (view) {
return view.branchResultset().find(conditons).data();
......@@ -18,16 +22,6 @@ function AbstractLoki(collection, fileDAL, view) {
return collection.find(conditons);
}
let that = this;
let cores = [], p = fileDAL;
while (p) {
if (p.core) {
cores.push(p.core);
}
p = p.parentDAL;
}
cores = _.sortBy(cores, (b) => b.forkPointNumber);
this.IMMUTABLE_FIELDS = true;
this.collection = collection;
......@@ -142,4 +136,51 @@ function AbstractLoki(collection, fileDAL, view) {
}
return Q(entity);
};
function getCores() {
let theCores = [], p = fileDAL;
while (p) {
if (p.core) {
theCores.push(p.core);
}
p = p.parentDAL;
}
return _.sortBy(theCores, (b) => b.forkPointNumber);
}
function getView() {
let branchView;
if (viewFields && loki) {
let blockCollection = loki.getCollection('blocks');
let current = blockCollection.chain().find({ fork: false }).simplesort('number', true).limit(1).data()[0];
let conditions = cores.map((b) => {
let objNumber = {}, objHash = {};
objNumber[viewFields.block_number] = b.forkPointNumber;
objHash[viewFields.block_hash] = b.forkPointHash;
if (viewFields.block_number && viewFields.block_hash) {
return { $and: [objNumber, objHash] };
} else if (viewFields.block_hash) {
return objHash;
} else {
return objNumber;
}
});
if (!current) {
conditions.unshift({
$and: [{
block_number: 0
}, {
block_hash: 'DA39A3EE5E6B4B0D3255BFEF95601890AFD80709'
}]
});
}
conditions.unshift({
block_number: { $lte: current ? current.number : -1 }
});
branchView = collection.addDynamicView(['branch', fileDAL.name].join('_'));
branchView.applyFind({ '$or': conditions });
branchView.conditions = conditions;
}
return branchView;
}
}
\ No newline at end of file
......@@ -12,33 +12,11 @@ function CertDAL(fileDAL, loki) {
"use strict";
let collection = loki.getCollection('certs') || loki.addCollection('certs', { indices: ['from', 'target', 'linked', 'written'] });
let blockCollection = loki.getCollection('blocks');
let current = blockCollection.chain().find({ fork: false }).simplesort('number', true).limit(1).data()[0];
let blocks = [], p = fileDAL;
let branchView;
while (p) {
if (p.core) {
blocks.push(p.core);
}
p = p.parentDAL;
}
let conditions = blocks.map((b) => {
return {
$and: [{
block_number: b.forkPointNumber
}, {
block_hash: b.forkPointHash
}]
};
});
conditions.unshift({
block_number: { $lte: current ? current.number : -1 }
});
branchView = collection.addDynamicView(['branch', fileDAL.name].join('_'));
branchView.applyFind({ '$or': conditions });
branchView.conditions = conditions;
AbstractLoki.call(this, collection, fileDAL);
AbstractLoki.call(this, collection, fileDAL, {
block_number: 'block_number',
block_hash: 'block_hash'
}, loki);
this.idKeys = ['sig', 'from', 'target'];
this.metaProps = ['linked'];
......
......@@ -12,33 +12,11 @@ function LinksDAL(fileDAL, loki) {
"use strict";
let collection = loki.getCollection('links') || loki.addCollection('links', { indices: ['source', 'target', 'block_number', 'block_hash', 'timestamp'] });
let blockCollection = loki.getCollection('blocks');
let current = blockCollection.chain().find({ fork: false }).simplesort('number', true).limit(1).data()[0];
let blocks = [], p = fileDAL;
let branchView;
while (p) {
if (p.core) {
blocks.push(p.core);
}
p = p.parentDAL;
}
let conditions = blocks.map((b) => {
return {
$and: [{
block_number: b.forkPointNumber
}, {
block_hash: b.forkPointHash
}]
};
});
conditions.unshift({
block_number: { $lte: current ? current.number : -1 }
});
branchView = collection.addDynamicView(['branchl', fileDAL.name].join('_'));
branchView.applyFind({ '$or': conditions });
branchView.conditions = conditions;
AbstractLoki.call(this, collection, fileDAL, branchView);
AbstractLoki.call(this, collection, fileDAL, {
block_number: 'block_number',
block_hash: 'block_hash'
}, loki);
this.idKeys = ['source', 'target', 'block_number', 'block_hash'];
this.metaProps = ['obsolete'];
......@@ -68,7 +46,7 @@ function LinksDAL(fileDAL, loki) {
});
this.obsoletesLinks = (minTimestamp) => {
let toObsolete = branchView.branchResultset().find({
let toObsolete = this.lokiFind({
timestamp: { $lte: minTimestamp }
});
for (let i = 0; i < toObsolete.length; i++) {
......
......@@ -13,40 +13,11 @@ function MembershipDAL(fileDAL, loki) {
"use strict";
let collection = loki.getCollection('memberships') || loki.addCollection('memberships', { indices: ['membership', 'issuer', 'number', 'blockNumber', 'blockHash', 'userid', 'certts', 'block', 'fpr', 'written', 'signature'] });
let blockCollection = loki.getCollection('blocks');
let current = blockCollection.chain().find({ fork: false }).simplesort('number', true).limit(1).data()[0];
let blocks = [], p = fileDAL;
let branchView;
while (p) {
if (p.core) {
blocks.push(p.core);
}
p = p.parentDAL;
}
let conditions = blocks.map((b) => {
return {
$and: [{
blockNumber: b.forkPointNumber
}, {
blockHash: b.forkPointHash
}]
};
});
conditions.unshift({
blockNumber: { $lte: current ? current.number : -1 }
});
conditions.unshift({
$and: [{
blockNumber: '0'
}, {
blockHash: 'DA39A3EE5E6B4B0D3255BFEF95601890AFD80709'
}]
});
branchView = collection.addDynamicView(['branch', fileDAL.name].join('_'));
branchView.applyFind({ '$or': conditions });
branchView.conditions = conditions;
AbstractLoki.call(this, collection, fileDAL, branchView);
AbstractLoki.call(this, collection, fileDAL, {
block_number: 'blockNumber',
block_hash: 'blockHash'
}, loki);
this.idKeys = ['issuer', 'signature'];
this.metaProps = ['written'];
......
......@@ -12,33 +12,11 @@ function SourcesDAL(fileDAL, loki) {
"use strict";
let collection = loki.getCollection('sources') || loki.addCollection('sources', { indices: ['pubkey', 'type', 'number', 'fingerprint', 'amount', 'block_hash'] });
let blockCollection = loki.getCollection('blocks');
let current = blockCollection.chain().find({ fork: false }).simplesort('number', true).limit(1).data()[0];
let blocks = [], p = fileDAL;
let branchView;
while (p) {
if (p.core) {
blocks.push(p.core);
}
p = p.parentDAL;
}
let conditions = blocks.map((b) => {
return {
$and: [{
number: b.forkPointNumber
}, {
block_hash: b.forkPointHash
}]
};
});
conditions.unshift({
block_number: { $lte: current ? current.number : -1 }
});
branchView = collection.addDynamicView(['branch', fileDAL.name].join('_'));
branchView.applyFind({ '$or': conditions });
branchView.conditions = conditions;
AbstractLoki.call(this, collection, fileDAL, branchView);
AbstractLoki.call(this, collection, fileDAL, {
block_number: 'number',
block_hash: 'block_hash'
}, loki);
this.idKeys = ['pubkey', 'type', 'number', 'fingerprint', 'amount'];
this.metaProps = ['consumed'];
......
......@@ -14,29 +14,10 @@ function TxsDAL(fileDAL, loki) {
let that = this;
let collection = loki.getCollection('txs') || loki.addCollection('txs', { indices: ['hash', 'block_number', 'written', 'signature', 'recipients'] });
let blockCollection = loki.getCollection('blocks');
let current = blockCollection.chain().find({ fork: false }).simplesort('number', true).limit(1).data()[0];
let blocks = [], p = fileDAL;
let branchView;
while (p) {
if (p.core) {
blocks.push(p.core);
}
p = p.parentDAL;
}
let conditions = blocks.map((b) => {
return {
block_number: b.forkPointNumber
};
});
conditions.unshift({
block_number: { $lte: current ? current.number : -1 }
});
branchView = collection.addDynamicView(['branch', fileDAL.name].join('_'));
branchView.applyFind({ '$or': conditions });
branchView.conditions = conditions;
AbstractLoki.call(this, collection, fileDAL, branchView);
AbstractLoki.call(this, collection, fileDAL, {
block_number: 'block_number'
}, loki);
this.idKeys = ['hash', 'block_number'];
this.metaProps = ['written', 'removed'];
......
......@@ -100,7 +100,7 @@ function Node (dbName, options) {
BlockchainService.prove(block, sigFunc, difficulty, next);
},
function (provenBlock, next){
logger.debug(provenBlock.getRawSigned());
provenBlock && provenBlock.getRawSigned && logger.debug(provenBlock.getRawSigned());
post('/blockchain/block', {
"block": provenBlock.getRawSigned()
}, next);
......
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