From db7ea8131ebea3bfd1a37c02ab816cef837e38a7 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Fri, 25 Mar 2016 12:43:43 +0100
Subject: [PATCH] Data reset was not working on Windows

---
 app/controllers/webmin.controller.js    |  5 +----
 app/lib/dal/fileDAL.js                  | 14 +++++++++++++-
 app/lib/dal/sqliteDAL/AbstractSQLite.js |  2 ++
 app/lib/dal/sqliteDAL/BlockDAL.js       |  3 +--
 server.js                               |  8 +++++---
 5 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/app/controllers/webmin.controller.js b/app/controllers/webmin.controller.js
index 8bafd2e8a..9e2f93ca1 100644
--- a/app/controllers/webmin.controller.js
+++ b/app/controllers/webmin.controller.js
@@ -374,10 +374,7 @@ function WebAdmin (dbConf, overConf) {
     yield pluggedDALP;
     // We have to wait for a non-breaking window to process reset
     yield server.BlockchainService.pushFIFO(() => co(function *() {
-      yield server.unPlugFileSystem();
-      yield server.resetData();
-      pluggedConfP = plugForConf();
-      pluggedDALP = plugForDAL();
+      yield server.softResetData();
     }));
     return {};
   });
diff --git a/app/lib/dal/fileDAL.js b/app/lib/dal/fileDAL.js
index 9e20945d9..bf8345eb5 100644
--- a/app/lib/dal/fileDAL.js
+++ b/app/lib/dal/fileDAL.js
@@ -1114,9 +1114,21 @@ function FileDAL(params) {
   this.loadStats = that.statDAL.loadStats;
   this.getStat = that.statDAL.getStat;
   this.pushStats = that.statDAL.pushStats;
+  
+  this.cleanCaches = () => co(function *() {
+    yield _.values(that.newDals).map((dal) => dal.cleanCache && dal.cleanCache());
+  });
+
+  this.cleanDBData = () => co(function *() {
+    yield _.values(that.newDals).map((dal) => dal.cleanData && dal.cleanData());
+    that.wotb.resetWoT();
+    var files = ['stats', 'cores', 'current'];
+    var dirs  = ['blocks', 'ud_history', 'branches', 'certs', 'txs', 'cores', 'sources', 'links', 'ms', 'identities', 'peers', 'indicators', 'leveldb'];
+    return resetFiles(files, dirs);
+  });
 
   this.close = () => co(function *() {
-    yield _.values(that.newDals).map((dal) => dal.close && dal.close());
+    yield _.values(that.newDals).map((dal) => dal.cleanCache && dal.cleanCache());
     return Q.nbind(sqlite.close, sqlite);
   });
 
diff --git a/app/lib/dal/sqliteDAL/AbstractSQLite.js b/app/lib/dal/sqliteDAL/AbstractSQLite.js
index 095abb005..c6ec9d309 100644
--- a/app/lib/dal/sqliteDAL/AbstractSQLite.js
+++ b/app/lib/dal/sqliteDAL/AbstractSQLite.js
@@ -49,6 +49,8 @@ function AbstractSQLite(db) {
     }
   });
 
+  this.cleanData = () => this.query("DELETE FROM " + this.table);
+
   this.sqlListAll = () => this.query("SELECT * FROM " + this.table);
 
   this.sqlDeleteAll = () => this.exec("DELETE FROM " + this.table);
diff --git a/app/lib/dal/sqliteDAL/BlockDAL.js b/app/lib/dal/sqliteDAL/BlockDAL.js
index d5b40b535..84844c872 100644
--- a/app/lib/dal/sqliteDAL/BlockDAL.js
+++ b/app/lib/dal/sqliteDAL/BlockDAL.js
@@ -67,8 +67,7 @@ function BlockDAL(db) {
       'COMMIT;', []);
   });
 
-  this.close = () =>
-    current = null;
+  this.cleanCache = () => current = null;
 
   this.getCurrent = () => co(function *() {
     if (!current) {
diff --git a/server.js b/server.js
index 8c1fab75d..cdf2580fd 100644
--- a/server.js
+++ b/server.js
@@ -57,9 +57,11 @@ function Server (dbConf, overrideConf) {
     that.dal = fileDAL(params);
   });
 
-  this.unPlugFileSystem = () => co(function *() {
-    logger.debug('Unplugging file system...');
-    yield that.dal.close();
+  this.softResetData = () => co(function *() {
+    logger.debug('Soft data reset... [cache]');
+    yield that.dal.cleanCaches();
+    logger.debug('Soft data reset... [data]');
+    yield that.dal.cleanDBData();
   });
 
   this.loadConf = (useDefaultConf) => co(function *() {
-- 
GitLab