<commit_before>'use strict';

const fs = require('fs');
const randomString = require('randomstring');

const electron = require('electron');
const childProcess = require('child_process');
const app = electron.app;
const path = require('path');
const isDevEnv = require('../utils/misc.js').isDevEnv;
const isMac = require('../utils/misc.js').isMac;

// Search library
const libSymphonySearch = require('./searchLibrary');

// Path for the exec file and the user data folder
let userData = path.join(app.getPath('userData'));
let execPath = path.dirname(app.getPath('exe'));

// Constants paths for temp indexing folders
const TEMP_BATCH_INDEX_FOLDER = isDevEnv ? './data/temp_batch_indexes' : path.join(userData, 'data/temp_batch_indexes');
const TEMP_REALTIME_INDEX = isDevEnv ? './data/temp_realtime_index' : path.join(userData, 'data/temp_realtime_index');
const INDEX_PREFIX = isDevEnv ? './data/search_index' : path.join(userData, 'data/search_index');
const INDEX_DATA_FOLDER = isDevEnv ? './data' : path.join(userData, 'data');
const SEARCH_PERIOD_SUBTRACTOR = 3 * 31 * 24 * 60 * 60 * 1000;//3 months
const MINIMUM_DATE = '0000000000000';
const MAXIMUM_DATE = '9999999999999';
const INDEX_VERSION = 'v1';

const rootPath = isMac ? 'indexvalidator.exec' : 'indexvalidator.exe';
let productionPath = path.join(execPath, isMac ? '..' : '', rootPath);
let devPath = path.join(__dirname, '..', '..', rootPath);
let libraryPath = isDevEnv ? devPath : productionPath;

let INDEX_VALIDATOR = libraryPath;

const SORT_BY_SCORE = 0;

const BATCH_RANDOM_INDEX_PATH_LENGTH = 20;

class Search {
    //TODO: fix eslint class-methods-use-this
    /*eslint-disable class-methods-use-this */

    constructor(userId) {
        this.isInitialized = false;
        this.userId = userId;
        this.startIndexingFromDate = (new Date().getTime() - SEARCH_PERIOD_SUBTRACTOR).toString();
        this.indexFolderName = INDEX_PREFIX + '_' + userId + '_' + INDEX_VERSION;
        this.init();
    }

    initLib() {
        return new Promise((resolve) => {
            if (!this.isInitialized) {
                this.isInitialized = true;
            }
            resolve(libSymphonySearch.symSEInit());
        });
    }

    isLibInit() {
        return this.initLib();
    }

    init() {
        libSymphonySearch.symSEInit();
        Search.indexValidator(INDEX_DATA_FOLDER);
        libSymphonySearch.symSERemoveFolder(TEMP_REALTIME_INDEX);
        libSymphonySearch.symSERemoveFolder(TEMP_BATCH_INDEX_FOLDER);
        Search.indexValidator(this.indexFolderName);
        Search.indexValidator(TEMP_REALTIME_INDEX);
        let indexDateStartFrom = new Date().getTime() - SEARCH_PERIOD_SUBTRACTOR;
        libSymphonySearch.symSEDeleteMessages(this.indexFolderName, null,
            MINIMUM_DATE, indexDateStartFrom.toString());
        this.isInitialized = true;
    }

    indexBatch(messages) {
        return new Promise((resolve, reject) => {
            if (!this.isInitialized) {
                reject()
            }
            let indexId = randomString.generate(BATCH_RANDOM_INDEX_PATH_LENGTH);
            libSymphonySearch.symSECreatePartialIndexAsync(TEMP_BATCH_INDEX_FOLDER, indexId, JSON.stringify(messages), function (err, res) {
                if (err) reject(err);
                resolve(res);
            });
        });
    }

    mergeIndexBatches() {
        libSymphonySearch.symSEMergePartialIndexAsync(this.indexFolderName, TEMP_BATCH_INDEX_FOLDER, function (err) {
            if (err) throw err;
            libSymphonySearch.symSERemoveFolder(TEMP_BATCH_INDEX_FOLDER)
        });
    }

    realTimeIndexing(message) {
        libSymphonySearch.symSEIndexRealTime(TEMP_REALTIME_INDEX, JSON.stringify(message));
    }

    readJson(batch) {
        return new Promise((resolve, reject) => {
            let dirPath = path.join(execPath, isMac ? '..' : '', 'Resources/msgsjson', batch);
            let messageFolderPath = isDevEnv ? path.join('./msgsjson', batch) : dirPath;<commit_msg>removed the Resources path<commit_after>'use strict';

const fs = require('fs');
const randomString = require('randomstring');

const electron = require('electron');
const childProcess = require('child_process');
const app = electron.app;
const path = require('path');
const isDevEnv = require('../utils/misc.js').isDevEnv;
const isMac = require('../utils/misc.js').isMac;

// Search library
const libSymphonySearch = require('./searchLibrary');

// Path for the exec file and the user data folder
let userData = path.join(app.getPath('userData'));
let execPath = path.dirname(app.getPath('exe'));

// Constants paths for temp indexing folders
const TEMP_BATCH_INDEX_FOLDER = isDevEnv ? './data/temp_batch_indexes' : path.join(userData, 'data/temp_batch_indexes');
const TEMP_REALTIME_INDEX = isDevEnv ? './data/temp_realtime_index' : path.join(userData, 'data/temp_realtime_index');
const INDEX_PREFIX = isDevEnv ? './data/search_index' : path.join(userData, 'data/search_index');
const INDEX_DATA_FOLDER = isDevEnv ? './data' : path.join(userData, 'data');
const SEARCH_PERIOD_SUBTRACTOR = 3 * 31 * 24 * 60 * 60 * 1000;//3 months
const MINIMUM_DATE = '0000000000000';
const MAXIMUM_DATE = '9999999999999';
const INDEX_VERSION = 'v1';

const rootPath = isMac ? 'indexvalidator.exec' : 'indexvalidator.exe';
let productionPath = path.join(execPath, isMac ? '..' : '', rootPath);
let devPath = path.join(__dirname, '..', '..', rootPath);
let libraryPath = isDevEnv ? devPath : productionPath;

let INDEX_VALIDATOR = libraryPath;

const SORT_BY_SCORE = 0;

const BATCH_RANDOM_INDEX_PATH_LENGTH = 20;

class Search {
    //TODO: fix eslint class-methods-use-this
    /*eslint-disable class-methods-use-this */

    constructor(userId) {
        this.isInitialized = false;
        this.userId = userId;
        this.startIndexingFromDate = (new Date().getTime() - SEARCH_PERIOD_SUBTRACTOR).toString();
        this.indexFolderName = INDEX_PREFIX + '_' + userId + '_' + INDEX_VERSION;
        this.init();
    }

    initLib() {
        return new Promise((resolve) => {
            if (!this.isInitialized) {
                this.isInitialized = true;
            }
            resolve(libSymphonySearch.symSEInit());
        });
    }

    isLibInit() {
        return this.initLib();
    }

    init() {
        libSymphonySearch.symSEInit();
        Search.indexValidator(INDEX_DATA_FOLDER);
        libSymphonySearch.symSERemoveFolder(TEMP_REALTIME_INDEX);
        libSymphonySearch.symSERemoveFolder(TEMP_BATCH_INDEX_FOLDER);
        Search.indexValidator(this.indexFolderName);
        Search.indexValidator(TEMP_REALTIME_INDEX);
        let indexDateStartFrom = new Date().getTime() - SEARCH_PERIOD_SUBTRACTOR;
        libSymphonySearch.symSEDeleteMessages(this.indexFolderName, null,
            MINIMUM_DATE, indexDateStartFrom.toString());
        this.isInitialized = true;
    }

    indexBatch(messages) {
        return new Promise((resolve, reject) => {
            if (!this.isInitialized) {
                reject()
            }
            let indexId = randomString.generate(BATCH_RANDOM_INDEX_PATH_LENGTH);
            libSymphonySearch.symSECreatePartialIndexAsync(TEMP_BATCH_INDEX_FOLDER, indexId, JSON.stringify(messages), function (err, res) {
                if (err) reject(err);
                resolve(res);
            });
        });
    }

    mergeIndexBatches() {
        libSymphonySearch.symSEMergePartialIndexAsync(this.indexFolderName, TEMP_BATCH_INDEX_FOLDER, function (err) {
            if (err) throw err;
            libSymphonySearch.symSERemoveFolder(TEMP_BATCH_INDEX_FOLDER)
        });
    }

    realTimeIndexing(message) {
        libSymphonySearch.symSEIndexRealTime(TEMP_REALTIME_INDEX, JSON.stringify(message));
    }

    readJson(batch) {
        return new Promise((resolve, reject) => {
            let dirPath = path.join(execPath, isMac ? '..' : '', 'msgsjson', batch);
            let messageFolderPath = isDevEnv ? path.join('./msgsjson', batch) : dirPath;
{'fraction_non_alphanumeric': 0.12351988034401097, 'fraction_numerical': 0.01096846566122398, 'mean_word_length': 3.302412868632708, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3994394', 'n_tokens_mistral': 2550, 'n_tokens_neox': 2485, 'n_words': 454}