From e1b883f69eaebb897a59ec65ed269ad3b05866dd Mon Sep 17 00:00:00 2001 From: Gert <Gert-dev@users.noreply.github.com> Date: Mon, 9 Sep 2019 21:24:41 +0200 Subject: [PATCH] Don't store indexes in project folder by default anymore due to CPU rise I wanted to place the index inside the .serenata folder, but it turns out that is a very bad idea. Atom will start firing massive amounts of change requests, due to it watching the database file, every time it is modified. We can disable this for our package, but not for other packages, which will still receive these events en masse uselessly, which not only prevents any other responses from being handled in the meantime, it also spikes CPU usage. Existing indexes will continue to work, this just sets the default for new projects to be the php-ide-serenata cache folder. You can still opt to save them in the project, by changing it, if you want. References #460 --- lib/ProjectManager.js | 19 +++++++++++++++++-- lib/ServiceContainer.js | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/ProjectManager.js b/lib/ProjectManager.js index 39887a8b..d68c0273 100644 --- a/lib/ProjectManager.js +++ b/lib/ProjectManager.js @@ -12,8 +12,9 @@ class ProjectManager /** * @param {Object} proxy */ - constructor(proxy) { + constructor(proxy, config) { this.proxy = proxy; + this.config = config; this.activeProject = null; } @@ -32,9 +33,23 @@ class ProjectManager // mainFolder = '/' + mainFolder.substr(0, 1).toLowerCase() + mainFolder.substr(1); // } + const path = require('path'); + const crypto = require('crypto'); + + const md5 = crypto.createHash('md5'); const configFileFolderPath = mainFolder + '/.serenata'; const configFilePath = configFileFolderPath + '/config.json'; + // NOTE: I wanted to place the index inside the .serenata folder, but it turns out that is a very bad idea. + // Atom will start firing massive amounts of change requests, due to it watching the database file, every time + // it is modified. We can disable this for our package, but not for other packages, which will still receive + // these events en masse uselessly, which not only prevents any other responses from being handled in the + // meantime, it also spikes CPU usage. + const indexDatabaseUri = 'file://' + path.join( + this.config.get('storagePath'), + 'index-' + md5.update(mainFolder).digest('hex') + '.sqlite' + ); + if (fs.existsSync(configFilePath)) { throw new Error( 'The currently active project was already initialized. To prevent existing settings from being ' + @@ -47,7 +62,7 @@ class ProjectManager "uris": [ "file://${mainFolder}" ], - "indexDatabaseUri": "file://${mainFolder}/.serenata/index.sqlite", + "indexDatabaseUri": "${indexDatabaseUri}", "phpVersion": 7.3, "excludedPathExpressions": [], "fileExtensions": [ diff --git a/lib/ServiceContainer.js b/lib/ServiceContainer.js index 19b1624c..6e97d3d1 100644 --- a/lib/ServiceContainer.js +++ b/lib/ServiceContainer.js @@ -74,7 +74,7 @@ class ServiceContainer getProjectManager() { if (this.projectManager === null) { const ProjectManager = require('./ProjectManager'); - this.projectManager = new ProjectManager(this.getProxy()); + this.projectManager = new ProjectManager(this.getProxy(), this.getConfiguration()); } return this.projectManager;