From 9f9f24c6249913357ae92e85df11bd1faaf35965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 17 Jun 2020 17:12:13 +0200 Subject: [PATCH 1/2] BaseEventIndexManager: Add support to read/write user versions. --- src/indexing/BaseEventIndexManager.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/indexing/BaseEventIndexManager.ts b/src/indexing/BaseEventIndexManager.ts index c40d1300ea..c59ed9b20e 100644 --- a/src/indexing/BaseEventIndexManager.ts +++ b/src/indexing/BaseEventIndexManager.ts @@ -144,6 +144,29 @@ export default abstract class BaseEventIndexManager { throw new Error("Unimplemented"); } + + /** + * Get the user version of the database. + * @return {Promise} A promise that will resolve to the user stored + * version number. + */ + async getUserVersion(): Promise { + throw new Error("Unimplemented"); + } + + /** + * Set the user stored version to the given version number. + * + * @param {number} version The new version that should be stored in the + * database. + * + * @return {Promise} A promise that will resolve once the new version + * is stored. + */ + async setUserVersion(version: number): Promise { + throw new Error("Unimplemented"); + } + /** * Commit the previously queued up events to the index. * From 2aa00cbf4142b92146f63850e9d62280fa33b4b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 17 Jun 2020 17:13:25 +0200 Subject: [PATCH 2/2] EventIndex: Bump our user version and delete the db if it's an old db. --- src/indexing/EventIndex.js | 3 --- src/indexing/EventIndexPeg.js | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/indexing/EventIndex.js b/src/indexing/EventIndex.js index d4e8ab0117..ca974dd4fc 100644 --- a/src/indexing/EventIndex.js +++ b/src/indexing/EventIndex.js @@ -42,9 +42,6 @@ export default class EventIndex extends EventEmitter { async init() { const indexManager = PlatformPeg.get().getEventIndexingManager(); - await indexManager.initEventIndex(); - console.log("EventIndex: Successfully initialized the event index"); - this.crawlerCheckpoints = await indexManager.loadCheckpoints(); console.log("EventIndex: Loaded checkpoints", this.crawlerCheckpoints); diff --git a/src/indexing/EventIndexPeg.js b/src/indexing/EventIndexPeg.js index ae4c14dafd..20e05f985d 100644 --- a/src/indexing/EventIndexPeg.js +++ b/src/indexing/EventIndexPeg.js @@ -23,6 +23,8 @@ import PlatformPeg from "../PlatformPeg"; import EventIndex from "../indexing/EventIndex"; import SettingsStore, {SettingLevel} from '../settings/SettingsStore'; +const INDEX_VERSION = 1; + class EventIndexPeg { constructor() { this.index = null; @@ -66,8 +68,25 @@ class EventIndexPeg { */ async initEventIndex() { const index = new EventIndex(); + const indexManager = PlatformPeg.get().getEventIndexingManager(); try { + await indexManager.initEventIndex(); + + const userVersion = await indexManager.getUserVersion(); + const eventIndexIsEmpty = await indexManager.isEventIndexEmpty(); + + if (eventIndexIsEmpty) { + await indexManager.setUserVersion(INDEX_VERSION); + } else if (userVersion === 0 && !eventIndexIsEmpty) { + await indexManager.closeEventIndex(); + await this.deleteEventIndex(); + + await indexManager.initEventIndex(); + await indexManager.setUserVersion(INDEX_VERSION); + } + + console.log("EventIndex: Successfully initialized the event index"); await index.init(); } catch (e) { console.log("EventIndex: Error initializing the event index", e);