Merge pull request #291 from keerthi16/SEARCH-569

SEARCH-569 (Index Version)
This commit is contained in:
Vikas Shashidhar 2018-01-18 14:33:36 +05:30 committed by GitHub
commit cb864b3f1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 34 deletions

View File

@ -52,6 +52,7 @@ const searchConfig = {
MINIMUM_DATE: '0000000000000', MINIMUM_DATE: '0000000000000',
MAXIMUM_DATE: '9999999999999', MAXIMUM_DATE: '9999999999999',
SORT_BY_SCORE: 0, SORT_BY_SCORE: 0,
INDEX_VERSION: 'v1',
BATCH_RANDOM_INDEX_PATH_LENGTH: 20, BATCH_RANDOM_INDEX_PATH_LENGTH: 20,
LIBRARY_CONSTANTS: libraryPaths, LIBRARY_CONSTANTS: libraryPaths,
FOLDERS_CONSTANTS: folderPaths, FOLDERS_CONSTANTS: folderPaths,

View File

@ -21,7 +21,11 @@ class SearchUtils {
checkFreeSpace() { checkFreeSpace() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!isMac) { if (!isMac) {
this.path = this.path.substring(0, 2); try {
this.path = this.path.substring(0, 2);
} catch (e) {
reject(new Error('Invalid Path : ' + e));
}
} }
checkDiskSpace(this.path, resolve, reject); checkDiskSpace(this.path, resolve, reject);
}); });
@ -65,7 +69,7 @@ function readFile(userId, resolve, reject) {
if (err) { if (err) {
return reject(new Error('Error reading the ')) return reject(new Error('Error reading the '))
} }
let usersConfig = []; let usersConfig = {};
try { try {
usersConfig = JSON.parse(data); usersConfig = JSON.parse(data);
} catch (e) { } catch (e) {
@ -111,12 +115,16 @@ function createUser(userId, oldConfig) {
* @param data * @param data
*/ */
function createUserConfigFile(userId, data) { function createUserConfigFile(userId, data) {
let userData = data;
let createStream = fs.createWriteStream(searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE); let createStream = fs.createWriteStream(searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE);
if (data) { if (userData) {
let jsonData; if (!userData.indexVersion) {
userData.indexVersion = searchConfig.INDEX_VERSION;
}
try { try {
jsonData = JSON.stringify(data); userData = JSON.stringify(userData);
createStream.write(`{"${userId}": ${jsonData}}`); createStream.write(`{"${userId}": ${userData}}`);
} catch (e) { } catch (e) {
createStream.write(`{"${userId}": {}}`); createStream.write(`{"${userId}": {}}`);
} }
@ -135,9 +143,15 @@ function createUserConfigFile(userId, data) {
* @returns {*} * @returns {*}
*/ */
function updateConfig(userId, data, resolve, reject) { function updateConfig(userId, data, resolve, reject) {
let userData = data;
if (userData && !userData.indexVersion) {
userData.indexVersion = searchConfig.INDEX_VERSION;
}
let configPath = searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE; let configPath = searchConfig.FOLDERS_CONSTANTS.USER_CONFIG_FILE;
if (!fs.existsSync(configPath)) { if (!fs.existsSync(configPath)) {
createUserConfigFile(userId, data); createUserConfigFile(userId, userData);
return reject(null); return reject(null);
} }

View File

@ -1,6 +1,7 @@
const childProcess = require('child_process'); const childProcess = require('child_process');
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const { isMac } = require('../js/utils/misc.js');
let executionPath = null; let executionPath = null;
let userConfigDir = null; let userConfigDir = null;
@ -45,6 +46,9 @@ describe('Tests for Search', function() {
key = 'jjjehdnctsjyieoalskcjdhsnahsadndfnusdfsdfsd='; key = 'jjjehdnctsjyieoalskcjdhsnahsadndfnusdfsdfsd=';
executionPath = path.join(__dirname, 'library'); executionPath = path.join(__dirname, 'library');
if (!isMac) {
executionPath = path.join(__dirname, '..', 'library');
}
userConfigDir = path.join(__dirname, '..'); userConfigDir = path.join(__dirname, '..');
searchConfig = require('../js/search/searchConfig.js'); searchConfig = require('../js/search/searchConfig.js');
@ -53,7 +57,7 @@ describe('Tests for Search', function() {
realTimeIndexPath = path.join(userConfigDir, 'data', 'temp_realtime_index'); realTimeIndexPath = path.join(userConfigDir, 'data', 'temp_realtime_index');
tempBatchPath = path.join(userConfigDir, 'data', 'temp_batch_indexes'); tempBatchPath = path.join(userConfigDir, 'data', 'temp_batch_indexes');
dataFolderPath = path.join(searchConfig.FOLDERS_CONSTANTS.EXEC_PATH, '..', 'data'); dataFolderPath = path.join(userConfigDir, 'data');
if (fs.existsSync(dataFolderPath)) { if (fs.existsSync(dataFolderPath)) {
fs.unlinkSync(dataFolderPath) fs.unlinkSync(dataFolderPath)
} }
@ -65,7 +69,7 @@ describe('Tests for Search', function() {
setTimeout(function () { setTimeout(function () {
deleteIndexFolders(dataFolderPath); deleteIndexFolders(dataFolderPath);
let root = path.join(searchConfig.FOLDERS_CONSTANTS.EXEC_PATH, '..', `${searchConfig.FOLDERS_CONSTANTS.PREFIX_NAME}_${userId}.enc`); let root = path.join(userConfigDir, `${searchConfig.FOLDERS_CONSTANTS.PREFIX_NAME}_${userId}.enc`);
if (fs.existsSync(root)) { if (fs.existsSync(root)) {
fs.unlinkSync(root); fs.unlinkSync(root);
} }
@ -77,7 +81,7 @@ describe('Tests for Search', function() {
function deleteIndexFolders(location) { function deleteIndexFolders(location) {
if (fs.existsSync(location)) { if (fs.existsSync(location)) {
fs.readdirSync(location).forEach(function(file) { fs.readdirSync(location).forEach(function(file) {
let curPath = location + "/" + file; let curPath = path.join(location, file);
if (fs.lstatSync(curPath).isDirectory()) { if (fs.lstatSync(curPath).isDirectory()) {
deleteIndexFolders(curPath); deleteIndexFolders(curPath);
} else { } else {
@ -421,11 +425,10 @@ describe('Tests for Search', function() {
}); });
it('should not get the latest timestamp', function (done) { it('should not get the latest timestamp', function (done) {
SearchApi.indexFolderName = '';
const getLatestMessageTimestamp = jest.spyOn(SearchApi, 'getLatestMessageTimestamp'); const getLatestMessageTimestamp = jest.spyOn(SearchApi, 'getLatestMessageTimestamp');
deleteIndexFolders(dataFolderPath);
SearchApi.getLatestMessageTimestamp().catch(function (err) { SearchApi.getLatestMessageTimestamp().catch(function (err) {
expect(err).toEqual(new Error('Index folder does not exist.')); expect(err).toEqual(new Error('Index folder does not exist.'));
SearchApi.indexFolderName = `${dataFolderPath}/${searchConfig.FOLDERS_CONSTANTS.PREFIX_NAME}_${userId}`;
expect(getLatestMessageTimestamp).toHaveBeenCalled(); expect(getLatestMessageTimestamp).toHaveBeenCalled();
expect(getLatestMessageTimestamp).toHaveBeenCalledTimes(3); expect(getLatestMessageTimestamp).toHaveBeenCalledTimes(3);
done(); done();
@ -435,11 +438,13 @@ describe('Tests for Search', function() {
describe('Test to decrypt the index', function () { describe('Test to decrypt the index', function () {
it('should decrypt the index', function () { it('should decrypt the index', function (done) {
deleteIndexFolders(dataFolderPath); setTimeout(function () {
const decryptAndInit = jest.spyOn(SearchApi, 'decryptAndInit'); const decryptAndInit = jest.spyOn(SearchApi, 'decryptAndInit');
SearchApi.decryptAndInit(); SearchApi.decryptAndInit();
expect(decryptAndInit).toHaveBeenCalled(); expect(decryptAndInit).toHaveBeenCalled();
done();
}, 3000);
}); });
it('should get message from the decrypted index', function (done) { it('should get message from the decrypted index', function (done) {
@ -480,17 +485,19 @@ describe('Tests for Search', function() {
}); });
it('should search fails index folder not fund', function (done) { it('should search fails index folder not fund', function (done) {
const searchQuery = jest.spyOn(SearchApi, 'searchQuery');
deleteIndexFolders(dataFolderPath); deleteIndexFolders(dataFolderPath);
SearchApi.searchQuery('it works', [], [], '', '', '', 25, 0, 0).catch(function (err) { setTimeout(function () {
expect(err).toEqual(new Error('Index folder does not exist.')); const searchQuery = jest.spyOn(SearchApi, 'searchQuery');
expect(searchQuery).toHaveBeenCalledTimes(8); SearchApi.searchQuery('it works', [], [], '', '', '', 25, 0, 0).catch(function (err) {
expect(searchQuery).toHaveBeenCalled(); expect(err).toEqual(new Error('Index folder does not exist.'));
SearchApi = undefined; expect(searchQuery).toHaveBeenCalledTimes(8);
const { Search } = require('../js/search/search.js'); expect(searchQuery).toHaveBeenCalled();
SearchApi = new Search(userId, key); SearchApi = undefined;
done(); const { Search } = require('../js/search/search.js');
}); SearchApi = new Search(userId, key);
done();
})
}, 3000);
}); });
it('should search fails query is undefined', function (done) { it('should search fails query is undefined', function (done) {

View File

@ -1,5 +1,6 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const { isMac } = require('../js/utils/misc.js');
let executionPath = null; let executionPath = null;
let userConfigDir = null; let userConfigDir = null;
@ -60,20 +61,32 @@ describe('Tests for Search Utils', function() {
it('should return error', function (done) { it('should return error', function (done) {
const checkFreeSpace = jest.spyOn(SearchUtilsAPI, 'checkFreeSpace'); const checkFreeSpace = jest.spyOn(SearchUtilsAPI, 'checkFreeSpace');
SearchUtilsAPI.path = undefined; if (isMac) {
SearchUtilsAPI.checkFreeSpace().catch(function (err) { SearchUtilsAPI.path = undefined;
expect(err).toEqual(new Error("Please provide path")); SearchUtilsAPI.checkFreeSpace().catch(function (err) {
expect(checkFreeSpace).toHaveBeenCalled(); expect(err).toEqual(new Error("Please provide path"));
done(); expect(checkFreeSpace).toHaveBeenCalled();
}); done();
});
} else {
SearchUtilsAPI.path = undefined;
SearchUtilsAPI.checkFreeSpace().catch(function (err) {
expect(err).toBeTruthy();
expect(checkFreeSpace).toHaveBeenCalled();
done();
});
}
}); });
it('should return error invalid path', function (done) { it('should return error invalid path', function (done) {
const checkFreeSpace = jest.spyOn(SearchUtilsAPI, 'checkFreeSpace'); const checkFreeSpace = jest.spyOn(SearchUtilsAPI, 'checkFreeSpace');
SearchUtilsAPI.path = './tp'; SearchUtilsAPI.path = './tp';
if (!isMac) {
SearchUtilsAPI.path = 'A://test';
}
SearchUtilsAPI.checkFreeSpace().catch(function (err) { SearchUtilsAPI.checkFreeSpace().catch(function (err) {
expect(checkFreeSpace).toHaveBeenCalled(); expect(checkFreeSpace).toHaveBeenCalled();
expect(err).toEqual(err); expect(err).toBeTruthy();
done(); done();
}); });
}); });
@ -111,6 +124,7 @@ describe('Tests for Search Utils', function() {
language: 'en' language: 'en'
}; };
SearchUtilsAPI.updateUserConfig(1234567891011, data).then(function (res) { SearchUtilsAPI.updateUserConfig(1234567891011, data).then(function (res) {
data.indexVersion = 'v1';
expect(res).toEqual(data); expect(res).toEqual(data);
done(); done();
}) })
@ -124,6 +138,7 @@ describe('Tests for Search Utils', function() {
}; };
SearchUtilsAPI.updateUserConfig(1234567891011, data).then(function (res) { SearchUtilsAPI.updateUserConfig(1234567891011, data).then(function (res) {
expect(res.rotationId).toEqual(1); expect(res.rotationId).toEqual(1);
expect(res.indexVersion).toEqual('v1');
done(); done();
}) })
}); });