From f62a0b8eae84a526428305c1a9468b0fbd376322 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Tue, 3 Jul 2018 17:55:18 +0200
Subject: [PATCH] [enh] sync: allow to disable Loki changesAPI dynamically

---
 app/lib/dal/fileDAL.ts                        | 28 +++++++++++++++++--
 app/lib/dal/indexDAL/abstract/GenericDAO.ts   |  3 +-
 app/lib/dal/indexDAL/abstract/PeerDAO.ts      |  3 +-
 app/lib/dal/indexDAL/abstract/WalletDAO.ts    |  3 +-
 app/lib/dal/indexDAL/loki/LokiCollection.ts   |  6 +++-
 .../indexDAL/loki/LokiCollectionManager.ts    | 11 +++++++-
 app/lib/dal/indexDAL/loki/LokiDAO.ts          |  7 +++++
 app/lib/dal/indexDAL/loki/LokiTypes.ts        |  2 ++
 app/modules/crawler/index.ts                  |  6 +++-
 9 files changed, 61 insertions(+), 8 deletions(-)
 create mode 100644 app/lib/dal/indexDAL/loki/LokiDAO.ts

diff --git a/app/lib/dal/fileDAL.ts b/app/lib/dal/fileDAL.ts
index de8284271..138c48ee3 100644
--- a/app/lib/dal/fileDAL.ts
+++ b/app/lib/dal/fileDAL.ts
@@ -79,6 +79,8 @@ import {cliprogram} from "../common-libs/programOptions"
 import {DividendDAO, UDSource} from "./indexDAL/abstract/DividendDAO"
 import {LokiDividend} from "./indexDAL/loki/LokiDividend"
 import {HttpSource, HttpUD} from "../../modules/bma/lib/dtos"
+import {GenericDAO} from "./indexDAL/abstract/GenericDAO"
+import {LokiDAO} from "./indexDAL/loki/LokiDAO"
 
 const readline = require('readline')
 const indexer = require('../indexer').Indexer
@@ -133,6 +135,8 @@ export class FileDAL {
   cindexDAL:CIndexDAO
   dividendDAL:DividendDAO
   newDals:{ [k:string]: Initiable }
+  private dals:(BlockchainArchiveDAO<any>|PeerDAO|WalletDAO|GenericDAO<any>)[]
+  private daos:LokiDAO[]
 
   loadConfHook: (conf:ConfDTO) => Promise<void>
   saveConfHook: (conf:ConfDTO) => Promise<ConfDTO>
@@ -187,10 +191,30 @@ export class FileDAL {
     }
   }
 
+  public enableChangesAPI() {
+    this.daos.map(d => d.enableChangesAPI())
+  }
+
+  public disableChangesAPI() {
+    this.daos.map(d => d.disableChangesAPI())
+  }
+
   async init(conf:ConfDTO) {
     // Init LokiJS
     await this.loki.loadDatabase()
-    const dals = [
+    this.daos = [
+      this.blockDAL,
+      this.txsDAL,
+      this.peerDAL,
+      this.walletDAL,
+      this.bindexDAL,
+      this.mindexDAL,
+      this.iindexDAL,
+      this.sindexDAL,
+      this.cindexDAL,
+      this.dividendDAL
+    ]
+    this.dals = [
       this.blockDAL,
       this.txsDAL,
       this.peerDAL,
@@ -203,7 +227,7 @@ export class FileDAL {
       this.dividendDAL,
       this.blockchainArchiveDAL,
     ]
-    for (const indexDAL of dals) {
+    for (const indexDAL of this.dals) {
       indexDAL.triggerInit()
     }
     const dalNames = Underscore.keys(this.newDals);
diff --git a/app/lib/dal/indexDAL/abstract/GenericDAO.ts b/app/lib/dal/indexDAL/abstract/GenericDAO.ts
index 12ada2d28..ff2fd53c1 100644
--- a/app/lib/dal/indexDAL/abstract/GenericDAO.ts
+++ b/app/lib/dal/indexDAL/abstract/GenericDAO.ts
@@ -1,6 +1,7 @@
 import {Initiable} from "../../sqliteDAL/Initiable"
+import {LokiDAO} from "../loki/LokiDAO"
 
-export interface GenericDAO<T> extends Initiable {
+export interface GenericDAO<T> extends Initiable, LokiDAO {
 
   /**
    * Trigger the initialization of the DAO. Called when the underlying DB is ready.
diff --git a/app/lib/dal/indexDAL/abstract/PeerDAO.ts b/app/lib/dal/indexDAL/abstract/PeerDAO.ts
index 6cedd6230..97756b422 100644
--- a/app/lib/dal/indexDAL/abstract/PeerDAO.ts
+++ b/app/lib/dal/indexDAL/abstract/PeerDAO.ts
@@ -1,7 +1,8 @@
 import {Initiable} from "../../sqliteDAL/Initiable"
 import {DBPeer} from "../../../db/DBPeer"
+import {LokiDAO} from "../loki/LokiDAO"
 
-export interface PeerDAO extends Initiable {
+export interface PeerDAO extends Initiable, LokiDAO {
 
   /**
    * Trigger the initialization of the DAO. Called when the underlying DB is ready.
diff --git a/app/lib/dal/indexDAL/abstract/WalletDAO.ts b/app/lib/dal/indexDAL/abstract/WalletDAO.ts
index 4774e6943..dfb0d0f50 100644
--- a/app/lib/dal/indexDAL/abstract/WalletDAO.ts
+++ b/app/lib/dal/indexDAL/abstract/WalletDAO.ts
@@ -1,7 +1,8 @@
 import {Initiable} from "../../sqliteDAL/Initiable"
 import {DBWallet} from "../../../db/DBWallet"
+import {LokiDAO} from "../loki/LokiDAO"
 
-export interface WalletDAO extends Initiable {
+export interface WalletDAO extends Initiable, LokiDAO {
 
   /**
    * Trigger the initialization of the DAO. Called when the underlying DB is ready.
diff --git a/app/lib/dal/indexDAL/loki/LokiCollection.ts b/app/lib/dal/indexDAL/loki/LokiCollection.ts
index 6b22804f8..515ff9c8c 100644
--- a/app/lib/dal/indexDAL/loki/LokiCollection.ts
+++ b/app/lib/dal/indexDAL/loki/LokiCollection.ts
@@ -1,6 +1,6 @@
 import {LokiChainableFind, LokiCollection} from "./LokiTypes"
 import {NewLogger} from "../../../logger"
-import {getDurationInMicroSeconds, getMicrosecondsTime} from "../../../../ProcessCpuProfiler"
+import {getMicrosecondsTime} from "../../../../ProcessCpuProfiler"
 
 const logger = NewLogger()
 
@@ -35,4 +35,8 @@ export class LokiProxyCollection<T> implements LokiCollection<T> {
   chain(): LokiChainableFind<T> {
     return this.collection.chain()
   }
+
+  setDisableChangesAPI(disable: boolean) {
+    this.collection.setDisableChangesAPI(disable)
+  }
 }
diff --git a/app/lib/dal/indexDAL/loki/LokiCollectionManager.ts b/app/lib/dal/indexDAL/loki/LokiCollectionManager.ts
index 442b31863..38c696f68 100755
--- a/app/lib/dal/indexDAL/loki/LokiCollectionManager.ts
+++ b/app/lib/dal/indexDAL/loki/LokiCollectionManager.ts
@@ -1,11 +1,12 @@
 import {LokiCollection} from "./LokiTypes"
 import {LokiProxyCollection} from "./LokiCollection"
 import {NewLogger} from "../../../logger"
+import {LokiDAO} from "./LokiDAO"
 import {cliprogram} from "../../../common-libs/programOptions"
 
 const logger = NewLogger()
 
-export abstract class LokiCollectionManager<T> {
+export abstract class LokiCollectionManager<T> implements LokiDAO {
 
   protected collection:LokiCollection<T>
   protected collectionIsInitialized: Promise<void>
@@ -27,6 +28,14 @@ export abstract class LokiCollectionManager<T> {
     this.resolveCollection()
   }
 
+  public enableChangesAPI() {
+    this.collection.setDisableChangesAPI(false)
+  }
+
+  public disableChangesAPI() {
+    this.collection.setDisableChangesAPI(true)
+  }
+
   async init(): Promise<void> {
     await this.collectionIsInitialized
     logger.info('Collection %s ready', this.collectionName)
diff --git a/app/lib/dal/indexDAL/loki/LokiDAO.ts b/app/lib/dal/indexDAL/loki/LokiDAO.ts
new file mode 100644
index 000000000..9c0a9ffba
--- /dev/null
+++ b/app/lib/dal/indexDAL/loki/LokiDAO.ts
@@ -0,0 +1,7 @@
+
+export interface LokiDAO {
+
+  enableChangesAPI(): void
+
+  disableChangesAPI(): void
+}
diff --git a/app/lib/dal/indexDAL/loki/LokiTypes.ts b/app/lib/dal/indexDAL/loki/LokiTypes.ts
index 26150bbe1..4e4f87d15 100644
--- a/app/lib/dal/indexDAL/loki/LokiTypes.ts
+++ b/app/lib/dal/indexDAL/loki/LokiTypes.ts
@@ -12,6 +12,8 @@ export interface LokiCollection<T> {
   find(criterion:{ [t in keyof T|'$or'|'$and']?: any }): T[]
 
   chain(): LokiChainableFind<T>
+
+  setDisableChangesAPI(disable: boolean): void
 }
 
 export interface LokiChainableFind<T> {
diff --git a/app/modules/crawler/index.ts b/app/modules/crawler/index.ts
index 1e4fc8e74..a86cfeb99 100644
--- a/app/modules/crawler/index.ts
+++ b/app/modules/crawler/index.ts
@@ -54,7 +54,11 @@ export const CrawlerDependency = {
         const syncPromise = remote.sync(upTo, chunkLength)
         return {
           flow: remote,
-          syncPromise: syncPromise
+          syncPromise: (async () => {
+            await server.dal.disableChangesAPI()
+            await syncPromise
+            await server.dal.enableChangesAPI()
+          })()
         };
       },
 
-- 
GitLab