From f81fbd5ed0d6cf2792db138dd585add93bfd710e Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Wed, 28 Oct 2015 19:10:04 +0100
Subject: [PATCH] LokiJS: refactoring

---
 app/lib/dal/fileDALs/AbstractLoki.js  | 63 ++++++++++++++++++++++-----
 app/lib/dal/fileDALs/CertDAL.js       | 30 ++-----------
 app/lib/dal/fileDALs/LinksDAL.js      | 32 +++-----------
 app/lib/dal/fileDALs/MembershipDAL.js | 37 ++--------------
 app/lib/dal/fileDALs/SourcesDAL.js    | 30 ++-----------
 app/lib/dal/fileDALs/TxsDAL.js        | 25 ++---------
 test/integration/tools/node.js        |  2 +-
 7 files changed, 73 insertions(+), 146 deletions(-)

diff --git a/app/lib/dal/fileDALs/AbstractLoki.js b/app/lib/dal/fileDALs/AbstractLoki.js
index a08d671a6..9b842386c 100644
--- a/app/lib/dal/fileDALs/AbstractLoki.js
+++ b/app/lib/dal/fileDALs/AbstractLoki.js
@@ -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
diff --git a/app/lib/dal/fileDALs/CertDAL.js b/app/lib/dal/fileDALs/CertDAL.js
index 2fbfe328f..2c3f425f8 100644
--- a/app/lib/dal/fileDALs/CertDAL.js
+++ b/app/lib/dal/fileDALs/CertDAL.js
@@ -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'];
diff --git a/app/lib/dal/fileDALs/LinksDAL.js b/app/lib/dal/fileDALs/LinksDAL.js
index 1b0fbe0bd..be4a81394 100644
--- a/app/lib/dal/fileDALs/LinksDAL.js
+++ b/app/lib/dal/fileDALs/LinksDAL.js
@@ -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++) {
diff --git a/app/lib/dal/fileDALs/MembershipDAL.js b/app/lib/dal/fileDALs/MembershipDAL.js
index b5c859219..4c36fd9b3 100644
--- a/app/lib/dal/fileDALs/MembershipDAL.js
+++ b/app/lib/dal/fileDALs/MembershipDAL.js
@@ -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'];
diff --git a/app/lib/dal/fileDALs/SourcesDAL.js b/app/lib/dal/fileDALs/SourcesDAL.js
index eb4a1604e..4077b26c5 100644
--- a/app/lib/dal/fileDALs/SourcesDAL.js
+++ b/app/lib/dal/fileDALs/SourcesDAL.js
@@ -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'];
diff --git a/app/lib/dal/fileDALs/TxsDAL.js b/app/lib/dal/fileDALs/TxsDAL.js
index c6e44f909..c34a0e1c3 100644
--- a/app/lib/dal/fileDALs/TxsDAL.js
+++ b/app/lib/dal/fileDALs/TxsDAL.js
@@ -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'];
diff --git a/test/integration/tools/node.js b/test/integration/tools/node.js
index 74aabc9c8..5320c37b7 100644
--- a/test/integration/tools/node.js
+++ b/test/integration/tools/node.js
@@ -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);
-- 
GitLab