Workers can now search in multiple specified db-s

This commit is contained in:
mrfry 2020-12-19 12:07:31 +01:00
parent 68dcbff846
commit 19b8fdc5ab
5 changed files with 57 additions and 52 deletions

View file

@ -495,33 +495,48 @@ if (!isMainThread) {
parentPort.on('message', (msg) => {
if (msg.type === 'work') {
const { searchIn, subjName, question, questionData } = msg.data
const { subjName, question, questionData } = msg.data
const index = msg.index
const searchIn = msg.data.searchIn
console.log(
`[THREAD #${workerIndex}]: staring work${
!isNaN(index) ? ` on job index #${index}` : ''
}`
)
let searchResult = null
const currQdb = qdbs.find((qdb) => {
return searchIn[0] === qdb.index
})
let searchResult = []
try {
searchResult = doSearch(currQdb.data, subjName, question, questionData)
qdbs.forEach((qdb) => {
if (searchIn === 'all' || searchIn.includes(qdb.index)) {
const res = doSearch(qdb.data, subjName, question, questionData)
searchResult = [...searchResult, ...res]
}
})
} catch (err) {
logger.Log('Error in worker thread!', logger.GetColor('redbg'))
console.error(err)
}
// sorting
const sortedResult = searchResult.sort((q1, q2) => {
if (q1.match < q2.match) {
return 1
} else if (q1.match > q2.match) {
return -1
} else {
return 0
}
})
// ONDONE:
parentPort.postMessage({
msg: `From thread #${workerIndex}: job ${
!isNaN(index) ? `#${index}` : ''
}done`,
workerIndex: workerIndex,
result: searchResult,
result: sortedResult,
})
console.log(