diff --git a/src/background/background.js b/src/background/background.js index 6b5dee3..44386fe 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -10,6 +10,7 @@ import { app, Menu, ipcMain, Tray, dialog } from "electron"; import createWindow from "./helpers/window"; import { autoUpdater } from 'electron-updater' import appPath from './electronAppPath' +import dbPatcher from './dbPatcher' import { devMenuTemplate } from "./menu/dev_menu_template"; import { editMenuTemplate } from "./menu/edit_menu_template"; @@ -144,14 +145,13 @@ const writeSphinxConfig = (path, dbPath) => { rt_attr_bigint = size } - index statistic + index version { type = rt - path = ${dbPath}/database/statistic + path = ${dbPath}/database/version - rt_attr_bigint = size - rt_attr_bigint = files - rt_attr_uint = torrents + rt_attr_uint = version + rt_field = versionIndex } searchd @@ -279,12 +279,14 @@ let tray = undefined app.on("ready", () => { startSphinx(() => { - setApplicationMenu(); + + mainWindow = createWindow("main", { + width: 1000, + height: 600 + }); - mainWindow = createWindow("main", { - width: 1000, - height: 600 - }); + dbPatcher(() => { + setApplicationMenu(); mainWindow.loadURL( url.format({ @@ -363,6 +365,7 @@ app.on("ready", () => { callback.apply(null, arg) }) }, app.getPath("userData"), app.getVersion(), env.name) + }, mainWindow) }) }); diff --git a/src/background/dbPatcher.js b/src/background/dbPatcher.js new file mode 100644 index 0000000..41d75b2 --- /dev/null +++ b/src/background/dbPatcher.js @@ -0,0 +1,148 @@ +import config from './config' +import mysql from 'mysql' +import forBigTable from './forBigTable' +import { BrowserWindow } from "electron"; +import url from 'url' +import path from 'path' + +module.exports = (callback, mainWindow) => { + const sphinx = mysql.createConnection({ + host : config.sphinx.host, + port : config.sphinx.port + }); + + const query = (sql) => new Promise((resolve, reject) => { + sphinx.query(sql, (err, res) => { + if(err) + reject(err) + else + resolve(res) + }) + }) + + const insertValues = (table, values, callback) => new Promise((resolve) => { + let names = ''; + let data = ''; + for(const val in values) + { + if(values[val] === null) + continue; + + names += '`' + val + '`,'; + data += sphinx.escape(values[val]) + ','; + } + names = names.slice(0, -1) + data = data.slice(0, -1) + let query = `INSERT INTO ${table}(${names}) VALUES(${data})`; + sphinx.query(query, (...responce) => { + if(callback) + callback(...responce) + resolve(...responce) + }) + }) + + let patchWindow; + const openPatchWindow = () => { + if(patchWindow) + return + + if(mainWindow) + mainWindow.hide() + + patchWindow = new BrowserWindow({width: 600, height: 200, closable: false}) + + patchWindow.setMenu(null) + } + + const patch = async (version) => { + console.log('db version', version) + switch(version) + { + case 1: + { + console.log('patch db to version 2') + openPatchWindow() + let i = 1 + + if(patchWindow) + { + patchWindow.loadURL("data:text/html;charset=utf-8," + encodeURI(` + Database patching... + + + `)) + } + + const torrents = (await query("SELECT COUNT(*) AS c FROM torrents"))[0].c + const files = (await query("SELECT COUNT(*) AS c FROM files"))[0].c + + await forBigTable(sphinx, 'torrents', async (torrent) => { + console.log('update index', torrent.id, torrent.name) + if(patchWindow) + patchWindow.webContents.send('reindex', {field: torrent.name, index: i++, all: torrents, torrent: true}) + + torrent.nameIndex = torrent.name + await query(`DELETE FROM torrents WHERE id = ${torrent.id}`) + await insertValues('torrents', torrent) + }) + i = 0 + await forBigTable(sphinx, 'files', async (file) => { + console.log('update index', file.id, file.path) + if(patchWindow) + patchWindow.webContents.send('reindex', {field: file.path, index: i++, all: files}) + + file.pathIndex = file.path + await query(`DELETE FROM files WHERE id = ${file.id}`) + await insertValues('files', file) + }) + await query(`UPDATE version SET version = 2 WHERE id = 1`) + } + } + console.log('db patch done') + sphinx.destroy() + if(patchWindow) + { + patchWindow.destroy() + if(mainWindow) + mainWindow.show() + } + callback() + } + + sphinx.connect((mysqlError) => { + if(mysqlError) + { + console.log('error on sphinx connecting on db patching', mysqlError) + return + } + + sphinx.query('select * from version', (err, version) => { + if(err) + { + console.log('error on version get on db patch') + return + } + + if(!version || !version[0] || !version[0].version) + { + sphinx.query('insert into version(id, version) values(1, 1)', (err) => { + if(err) + { + console.log('cant set first version') + return + } + patch(1) + }) + } + else + { + patch(version[0].version) + } + }) + }) +} \ No newline at end of file diff --git a/src/background/forBigTable.js b/src/background/forBigTable.js index 754cb86..f8e2ae2 100644 --- a/src/background/forBigTable.js +++ b/src/background/forBigTable.js @@ -10,8 +10,9 @@ export default (sphinx, table, callback, doneCallback, max = 1000) => new Promis done(true) return } - torrents.forEach(callback) - checker(torrents[torrents.length - 1].id) + Promise.all(torrents.map(callback)).then(() => { + checker(torrents[torrents.length - 1].id) + }) }); } checker()