From d2fa4f95a3c5485bfb864ce76adefcedb838869c Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Thu, 29 Oct 2015 07:09:29 +0100
Subject: [PATCH] LokiJS: file persistence

---
 app/controllers/wot.js                |  5 ++---
 app/lib/dal/fileDAL.js                |  5 ++++-
 app/lib/dal/fileDALs/AbstractLoki.js  |  8 +++++++-
 app/lib/dal/fileDALs/CertDAL.js       | 10 ++++++++++
 app/lib/dal/fileDALs/IdentityDAL.js   | 14 ++++++++++++++
 app/lib/dal/fileDALs/LinksDAL.js      |  8 ++++++++
 app/lib/dal/fileDALs/MembershipDAL.js | 13 +++++++++++++
 app/lib/dal/fileDALs/SourcesDAL.js    |  9 +++++++++
 app/lib/dal/fileDALs/TxsDAL.js        | 16 ++++++++++++++++
 app/lib/entity/certification.js       |  3 +--
 app/lib/entity/identity.js            |  2 +-
 11 files changed, 85 insertions(+), 8 deletions(-)

diff --git a/app/controllers/wot.js b/app/controllers/wot.js
index 19a1d11e4..c1ad2d9ca 100644
--- a/app/controllers/wot.js
+++ b/app/controllers/wot.js
@@ -60,7 +60,7 @@ function WOTBinding (server) {
         var signed = yield server.dal.certsFrom(idty.pubkey);
         var validSigned = [];
         for (let j = 0; j < signed.length; j++) {
-          let cert = signed[j];
+          let cert = _.clone(signed[j]);
           if (!(excluding && cert.block <= excluding.number)) {
             cert.idty = yield server.dal.getIdentityByHashOrNull(cert.target);
             validSigned.push(cert);
@@ -260,7 +260,6 @@ function WOTBinding (server) {
                     next('Not a member');
                     return;
                   }
-                  cert.pubkey = idty.pubkey;
                   cert.uid = idty.uid;
                   cert.isMember = idty.member;
                   cert.wasMember = idty.wasMember;
@@ -301,7 +300,7 @@ function WOTBinding (server) {
       };
       idty.certs.forEach(function(cert){
         json.certifications.push({
-          pubkey: cert.pubkey,
+          pubkey: cert.from,
           uid: cert.uid,
           cert_time: cert.cert_time,
           isMember: cert.isMember,
diff --git a/app/lib/dal/fileDAL.js b/app/lib/dal/fileDAL.js
index 6ec7f9690..e1adb6573 100644
--- a/app/lib/dal/fileDAL.js
+++ b/app/lib/dal/fileDAL.js
@@ -40,7 +40,10 @@ module.exports = {
           // Memory only service dals
           loki = new lokijs('temp', { autosave: false });
         } else {
-          loki = new lokijs('ucoin', { autosave: true, autosaveInterval: 300 });
+          loki = new lokijs(path.join(params.home, 'ucoin.json'), {
+            autosave: true,
+            autosaveInterval: 300
+          });
         }
         return new FileDAL(profile, params.home, "", params.fs, null, 'fileDal', null, loki);
       });
diff --git a/app/lib/dal/fileDALs/AbstractLoki.js b/app/lib/dal/fileDALs/AbstractLoki.js
index 9b842386c..e59d77eaa 100644
--- a/app/lib/dal/fileDALs/AbstractLoki.js
+++ b/app/lib/dal/fileDALs/AbstractLoki.js
@@ -113,7 +113,11 @@ function AbstractLoki(collection, fileDAL, viewFields, loki) {
     })[0];
   };
 
-  this.lokiSave = (entity) => {
+  this.lokiSave = (fullEntity) => {
+    let entity = fullEntity;
+    if (this.propsToSave) {
+      entity = _.pick(fullEntity, this.propsToSave || []);
+    }
     let existing = this.lokiExisting(entity);
     if (existing) {
       if (!fileDAL.parentDAL) {
@@ -123,6 +127,7 @@ function AbstractLoki(collection, fileDAL, viewFields, loki) {
         // Save in fork branch: overrides meta data
         existing.metaData[that.metaKey()] = _.pick(entity, this.metaProps);
       }
+      console.log(existing);
       collection.update(existing);
     } else {
       entity.metaData = {};
@@ -132,6 +137,7 @@ function AbstractLoki(collection, fileDAL, viewFields, loki) {
       _.pluck(entity, this.metaProps).forEach(function(metaProp){
         entity[metaProp] = false;
       });
+      console.log(entity);
       collection.insert(entity);
     }
     return Q(entity);
diff --git a/app/lib/dal/fileDALs/CertDAL.js b/app/lib/dal/fileDALs/CertDAL.js
index 2c3f425f8..66968eb3d 100644
--- a/app/lib/dal/fileDALs/CertDAL.js
+++ b/app/lib/dal/fileDALs/CertDAL.js
@@ -20,6 +20,16 @@ function CertDAL(fileDAL, loki) {
 
   this.idKeys = ['sig', 'from', 'target'];
   this.metaProps = ['linked'];
+  this.propsToSave = [
+    'linked',
+    'sig',
+    'block_number',
+    'block_hash',
+    'target',
+    'to',
+    'from',
+    'block'
+  ];
 
   this.init = () => null;
 
diff --git a/app/lib/dal/fileDALs/IdentityDAL.js b/app/lib/dal/fileDALs/IdentityDAL.js
index 61dd80802..1ebef4222 100644
--- a/app/lib/dal/fileDALs/IdentityDAL.js
+++ b/app/lib/dal/fileDALs/IdentityDAL.js
@@ -19,6 +19,20 @@ function IdentityDAL(fileDAL, loki) {
 
   this.idKeys = ['pubkey', 'uid', 'hash'];
   this.metaProps = ['kick', 'leaving', 'member', 'wasMember', 'written', 'currentMSN', 'revoked'];
+  this.propsToSave = [
+    'revoked',
+    'currentMSN',
+    'time',
+    'member',
+    'kick',
+    'leaving',
+    'wasMember',
+    'pubkey',
+    'uid',
+    'sig',
+    'hash',
+    'written'
+  ];
 
   this.init = () => null;
 
diff --git a/app/lib/dal/fileDALs/LinksDAL.js b/app/lib/dal/fileDALs/LinksDAL.js
index be4a81394..31401208a 100644
--- a/app/lib/dal/fileDALs/LinksDAL.js
+++ b/app/lib/dal/fileDALs/LinksDAL.js
@@ -20,6 +20,14 @@ function LinksDAL(fileDAL, loki) {
 
   this.idKeys = ['source', 'target', 'block_number', 'block_hash'];
   this.metaProps = ['obsolete'];
+  this.propsToSave = [
+    'source',
+    'target',
+    'timestamp',
+    'block_number',
+    'block_hash',
+    'obsolete'
+  ];
 
   this.init = () => null;
 
diff --git a/app/lib/dal/fileDALs/MembershipDAL.js b/app/lib/dal/fileDALs/MembershipDAL.js
index 4c36fd9b3..ad51bc21a 100644
--- a/app/lib/dal/fileDALs/MembershipDAL.js
+++ b/app/lib/dal/fileDALs/MembershipDAL.js
@@ -21,6 +21,19 @@ function MembershipDAL(fileDAL, loki) {
 
   this.idKeys = ['issuer', 'signature'];
   this.metaProps = ['written'];
+  this.propsToSave = [
+    'membership',
+    'issuer',
+    'number',
+    'blockNumber',
+    'blockHash',
+    'userid',
+    'certts',
+    'block',
+    'fpr',
+    'written',
+    'signature'
+  ];
 
   this.init = () => null;
 
diff --git a/app/lib/dal/fileDALs/SourcesDAL.js b/app/lib/dal/fileDALs/SourcesDAL.js
index 4077b26c5..1d0971287 100644
--- a/app/lib/dal/fileDALs/SourcesDAL.js
+++ b/app/lib/dal/fileDALs/SourcesDAL.js
@@ -20,6 +20,15 @@ function SourcesDAL(fileDAL, loki) {
 
   this.idKeys = ['pubkey', 'type', 'number', 'fingerprint', 'amount'];
   this.metaProps = ['consumed'];
+  this.propsToSave = [
+    'pubkey',
+    'type',
+    'number',
+    'fingerprint',
+    'amount',
+    'block_hash',
+    'consumed'
+  ];
 
   this.init = () => null;
 
diff --git a/app/lib/dal/fileDALs/TxsDAL.js b/app/lib/dal/fileDALs/TxsDAL.js
index c34a0e1c3..fdd9a830a 100644
--- a/app/lib/dal/fileDALs/TxsDAL.js
+++ b/app/lib/dal/fileDALs/TxsDAL.js
@@ -21,6 +21,22 @@ function TxsDAL(fileDAL, loki) {
 
   this.idKeys = ['hash', 'block_number'];
   this.metaProps = ['written', 'removed'];
+  this.propsToSave = [
+    'inputs',
+    'outputs',
+    'issuers',
+    'signatories',
+    'signatures',
+    'comment',
+    'hash',
+    'version',
+    'currency',
+    'block_number',
+    'time',
+    'recipients',
+    'written',
+    'removed'
+  ];
 
   this.init = () => null;
 
diff --git a/app/lib/entity/certification.js b/app/lib/entity/certification.js
index c641aa2fa..69af04ceb 100644
--- a/app/lib/entity/certification.js
+++ b/app/lib/entity/certification.js
@@ -11,8 +11,7 @@ var Certification = function(json) {
    that[key] = json[key];
   });
 
-  this.from = this.fromKey       = this.pubkey = this.from || this.fromKey || this.pubkey;
-  this.to = this.toKey           = this.to || this.toKey;
+  this.from  = this.pubkey = this.from || this.pubkey;
   this.block = this.block_number = parseInt(this.block || this.block_number);
 
   this.inline = function () {
diff --git a/app/lib/entity/identity.js b/app/lib/entity/identity.js
index 50712df81..de3fec8c6 100644
--- a/app/lib/entity/identity.js
+++ b/app/lib/entity/identity.js
@@ -33,7 +33,7 @@ var Identity = function(json) {
     var others = [];
     this.certs.forEach(function(cert){
       others.push({
-        "pubkey": cert.pubkey,
+        "pubkey": cert.from,
         "meta": {
           "block_number": cert.block_number
         },
-- 
GitLab