SEARCH-539 & SEARCH-206

- Added index version dynamically
This commit is contained in:
Keerthi Niranjan 2017-12-18 10:59:58 +05:30
parent f536726216
commit 6528ae8a60
2 changed files with 26 additions and 18 deletions

View File

@ -12,12 +12,19 @@ const DUMP_PATH = isDevEnv ? path.join(__dirname, '..', '..') : searchConfig.FOL
class Crypto { class Crypto {
constructor(userId, key) { /**
this.indexDataFolder = `${searchConfig.FOLDERS_CONSTANTS.PREFIX_NAME_PATH}_${userId}_${searchConfig.INDEX_VERSION}`; * Constructor
this.permanentIndexName = `${searchConfig.FOLDERS_CONSTANTS.PREFIX_NAME}_${userId}_${searchConfig.INDEX_VERSION}`; * @param userId
* @param key
* @param version
*/
constructor(userId, key, version) {
this.indexDataFolder = `${searchConfig.FOLDERS_CONSTANTS.PREFIX_NAME_PATH}_${userId}`;
this.permanentIndexName = `${searchConfig.FOLDERS_CONSTANTS.PREFIX_NAME}_${userId}`;
this.dump = DUMP_PATH; this.dump = DUMP_PATH;
this.key = key; this.key = key;
this.encryptedIndex = `${DUMP_PATH}/${this.permanentIndexName}.enc`; this.version = version;
this.encryptedIndex = `${DUMP_PATH}/${this.permanentIndexName}`;
this.dataFolder = searchConfig.FOLDERS_CONSTANTS.INDEX_PATH; this.dataFolder = searchConfig.FOLDERS_CONSTANTS.INDEX_PATH;
} }
@ -26,17 +33,17 @@ class Crypto {
* encrypting it * encrypting it
* @returns {Promise} * @returns {Promise}
*/ */
encryption(key) { encryption(key, version) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!fs.existsSync(this.indexDataFolder)){ if (!fs.existsSync(`${this.indexDataFolder}_${version}`)){
log.send(logLevels.ERROR, 'Crypto: User index folder not found'); log.send(logLevels.ERROR, 'Crypto: User index folder not found');
reject(); reject();
return; return;
} }
lz4.compression(`${searchConfig.FOLDERS_CONSTANTS.INDEX_FOLDER_NAME}/${this.permanentIndexName}`, lz4.compression(`${searchConfig.FOLDERS_CONSTANTS.INDEX_FOLDER_NAME}/${this.permanentIndexName}_${version}`,
`${this.permanentIndexName}`, (error, response) => { `${this.permanentIndexName}_${version}`, (error, response) => {
if (error) { if (error) {
log.send(logLevels.ERROR, 'Crypto: Error while compressing to lz4: ' + error); log.send(logLevels.ERROR, 'Crypto: Error while compressing to lz4: ' + error);
reject(error); reject(error);
@ -46,8 +53,8 @@ class Crypto {
if (response && response.stderr) { if (response && response.stderr) {
log.send(logLevels.WARN, 'Crypto: Child process stderr while compression, ' + response.stderr); log.send(logLevels.WARN, 'Crypto: Child process stderr while compression, ' + response.stderr);
} }
const input = fs.createReadStream(`${this.dump}/${this.permanentIndexName}${searchConfig.TAR_LZ4_EXT}`); const input = fs.createReadStream(`${this.dump}/${this.permanentIndexName}_${version}${searchConfig.TAR_LZ4_EXT}`);
const outputEncryption = fs.createWriteStream(this.encryptedIndex); const outputEncryption = fs.createWriteStream(`${this.encryptedIndex}_${version}.enc`);
let config = { let config = {
key: key key: key
}; };
@ -61,7 +68,7 @@ class Crypto {
reject(new Error(err)); reject(new Error(err));
return; return;
} }
fs.unlinkSync(`${this.dump}/${this.permanentIndexName}${searchConfig.TAR_LZ4_EXT}`); fs.unlinkSync(`${this.dump}/${this.permanentIndexName}_${version}${searchConfig.TAR_LZ4_EXT}`);
resolve('Success'); resolve('Success');
}); });
}); });
@ -76,13 +83,13 @@ class Crypto {
decryption() { decryption() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!fs.existsSync(this.encryptedIndex)){ if (!fs.existsSync(`${this.encryptedIndex}_${this.version}.enc`)){
log.send(logLevels.ERROR, 'Crypto: Encrypted file not found'); log.send(logLevels.ERROR, 'Crypto: Encrypted file not found');
reject(); reject();
return; return;
} }
const input = fs.createReadStream(this.encryptedIndex); const input = fs.createReadStream(`${this.encryptedIndex}_${this.version}.enc`);
const output = fs.createWriteStream(`${this.dump}/decrypted${searchConfig.TAR_LZ4_EXT}`); const output = fs.createWriteStream(`${this.dump}/decrypted${searchConfig.TAR_LZ4_EXT}`);
let config = { let config = {
key: this.key key: this.key

View File

@ -26,18 +26,19 @@ class Search {
* Constructor for the SymphonySearchEngine library * Constructor for the SymphonySearchEngine library
* @param userId (for the index folder name) * @param userId (for the index folder name)
* @param key * @param key
* @param version
*/ */
constructor(userId, key) { constructor(userId, key, version) {
this.isInitialized = false; this.isInitialized = false;
this.userId = userId; this.userId = userId;
this.key = key; this.key = key;
this.indexFolderName = `${searchConfig.FOLDERS_CONSTANTS.PREFIX_NAME_PATH}_${this.userId}_${searchConfig.INDEX_VERSION}`; this.indexFolderName = `${searchConfig.FOLDERS_CONSTANTS.PREFIX_NAME_PATH}_${this.userId}_${version}`;
this.dataFolder = searchConfig.FOLDERS_CONSTANTS.INDEX_PATH; this.dataFolder = searchConfig.FOLDERS_CONSTANTS.INDEX_PATH;
this.realTimeIndex = searchConfig.FOLDERS_CONSTANTS.TEMP_REAL_TIME_INDEX; this.realTimeIndex = searchConfig.FOLDERS_CONSTANTS.TEMP_REAL_TIME_INDEX;
this.batchIndex = searchConfig.FOLDERS_CONSTANTS.TEMP_BATCH_INDEX_FOLDER; this.batchIndex = searchConfig.FOLDERS_CONSTANTS.TEMP_BATCH_INDEX_FOLDER;
this.messageData = []; this.messageData = [];
this.isRealTimeIndexing = false; this.isRealTimeIndexing = false;
this.crypto = new Crypto(userId, key); this.crypto = new Crypto(userId, key, version);
this.decryptAndInit(); this.decryptAndInit();
this.collector = makeBoundTimedCollector(this.checkIsRealTimeIndexing.bind(this), this.collector = makeBoundTimedCollector(this.checkIsRealTimeIndexing.bind(this),
searchConfig.REAL_TIME_INDEXING_TIME, this.realTimeIndexing.bind(this)); searchConfig.REAL_TIME_INDEXING_TIME, this.realTimeIndexing.bind(this));
@ -234,8 +235,8 @@ class Search {
* Encrypting the index after the merging the index * Encrypting the index after the merging the index
* to the main user index * to the main user index
*/ */
encryptIndex(key) { encryptIndex(key, version) {
return this.crypto.encryption(key).then(() => { return this.crypto.encryption(key, version).then(() => {
return 'Success' return 'Success'
}).catch((e) => { }).catch((e) => {
log.send(logLevels.ERROR, 'Encrypting the index folder failed ->' + e); log.send(logLevels.ERROR, 'Encrypting the index folder failed ->' + e);