Skip to content

Commit

Permalink
feat(search): part words search feature
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jul 7, 2018
1 parent a725053 commit 8836607
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"electron-log": "^2.2.14",
"electron-updater": "^2.21.10",
"fs-jetpack": "^1.2.0",
"glob": "^7.1.2",
"iconv-lite": "^0.4.19",
"ipaddr.js": "^1.5.4",
"json-socket": "^0.3.0",
Expand Down
5 changes: 5 additions & 0 deletions src/background/asyncForEach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
73 changes: 71 additions & 2 deletions src/background/dbPatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ const { BrowserWindow } = require("electron");
const url = require('url')
const path = require('path')
const fs = require('fs')
const glob = require("glob")
const asyncForEach = require('./asyncForEach')

const {torrentTypeDetect} = require('../app/content');
const getTorrent = require('./gettorrent')
const startSphinx = require('./sphinx')


const currentVersion = 4
const currentVersion = 5


module.exports = async (callback, mainWindow, sphinxApp) => {
const sphinx = await single().waitConnection()
let sphinx = await single().waitConnection()

const setVersion = async (version) => {
await sphinx.query(`delete from version where id = 1`)
Expand Down Expand Up @@ -186,6 +189,72 @@ module.exports = async (callback, mainWindow, sphinxApp) => {

await setVersion(4)
}
case 4:
{
openPatchWindow()

let i = 1
const torrents = (await sphinx.query("SELECT COUNT(*) AS c FROM torrents"))[0].c

const torrentsArray = []

await forBigTable(sphinx, 'torrents', async (torrent) => {
console.log('remember index', torrent.id, torrent.name, '[', i, 'of', torrents, ']')
if(patchWindow)
patchWindow.webContents.send('reindex', {field: torrent.name, index: i++, all: torrents, torrent: true})

torrentsArray.push(torrent)
})

// stop sphinx
await new Promise((resolve) => {
// reopen sphinx
sphinx.destroy() // destory connection
sphinxApp.stop(resolve, true)
})

console.log('sphinx stoped for patching')

await new Promise((resolve) => {
glob(`${sphinxApp.directoryPathDb}/torrents.*`, function (er, files) {
files.forEach(file => {
console.log('clear torrents file', file)
fs.unlinkSync(path.resolve(file))
})
resolve()
})
})

console.log('cleaned torrents db structure, rectreating again')
i = 1
await new Promise((resolve) => {
// reopen sphinx
sphinxApp = sphinxApp.start(async () => {
sphinx = await single().waitConnection()
resolve()
}) // same args
})

console.log('sphinx restarted, patch db now')

await asyncForEach(torrentsArray, async (torrent) => {
console.log('update index', torrent.id, torrent.name, '[', i, 'of', torrents, ']')
if(patchWindow)
patchWindow.webContents.send('reindex', {field: torrent.name, index: i++, all: torrents, torrent: true})

torrent.nameIndex = torrent.name
await sphinx.query(`DELETE FROM torrents WHERE id = ${torrent.id}`)
await sphinx.insertValues('torrents', torrent)
})

console.log('optimizing torrents')
if(patchWindow)
patchWindow.webContents.send('optimize', {field: 'torrents'})
sphinx.query(`OPTIMIZE INDEX torrents`)
await sphinxApp.waitOptimized('torrents')

await setVersion(5)
}
}
console.log('db patch done')
sphinx.destroy()
Expand Down
24 changes: 20 additions & 4 deletions src/background/sphinx.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const writeSphinxConfig = (path, dbPath) => {
index torrents
{
type = rt
path = ${dbPath}/database/torrents
path = ${dbPath}/database/torrents
min_prefix_len = 3
expand_keywords = 1
rt_attr_string = hash
rt_attr_string = name
Expand Down Expand Up @@ -70,7 +73,7 @@ const writeSphinxConfig = (path, dbPath) => {
{
type = rt
path = ${dbPath}/database/feed
rt_field = feedIndex
rt_attr_json = data
}
Expand Down Expand Up @@ -138,6 +141,8 @@ const writeSphinxConfig = (path, dbPath) => {
}

module.exports = (callback, dataDirectory, onClose) => {
const start = (callback) => {

const sphinxPath = path.resolve(appPath('searchd'))
console.log('Sphinx Path:', sphinxPath)

Expand All @@ -159,6 +164,7 @@ module.exports = (callback, dataDirectory, onClose) => {
}
const sphinx = spawn(sphinxPath, options)
// remeber initizalizing of db
sphinx.start = start
sphinx.isInitDb = isInitDb
sphinx.directoryPath = appConfig.dbPath
sphinx.directoryPathDb = appConfig.dbPath + '/database'
Expand Down Expand Up @@ -186,12 +192,18 @@ module.exports = (callback, dataDirectory, onClose) => {

sphinx.on('close', (code, signal) => {
console.log(`sphinx closed with code ${code} and signal ${signal}`)
if(onClose)
if(onClose && !sphinx.replaceOnClose) // sometime we don't want to call default callback
onClose()
if(sphinx.onClose)
sphinx.onClose()
})

sphinx.stop = () => {
sphinx.stop = (onFinish, replaceFinish) => {
console.log('sphinx closing...')
if(onFinish)
sphinx.onClose = onFinish
if(replaceFinish)
sphinx.replaceOnClose = true // sometime we don't want to call default callback
exec(`"${sphinxPath}" --config "${config}" --stopwait`)
}

Expand All @@ -203,4 +215,8 @@ module.exports = (callback, dataDirectory, onClose) => {
})

return sphinx

}

return start(callback)
}

0 comments on commit 8836607

Please sign in to comment.