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',
MAXIMUM_DATE: '9999999999999',
SORT_BY_SCORE: 0,
INDEX_VERSION: 'v1',
BATCH_RANDOM_INDEX_PATH_LENGTH: 20,
LIBRARY_CONSTANTS: libraryPaths,
FOLDERS_CONSTANTS: folderPaths,

View File

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

View File

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

View File

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