Skip to content

Commit

Permalink
perf(torrents): optimization files saving
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jul 18, 2018
1 parent 437e695 commit a84f8c7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 46 deletions.
36 changes: 27 additions & 9 deletions src/background/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,35 @@ const expand = (sphinx) => {
sphinx.insertValues = (table, values, callback) => new Promise((resolve) => {
let names = '';
let data = '';
for(const val in values)
const parseValues = (values) => {
let valuesData = ''
names = ''
for(const val in values)
{
if(values[val] === null)
continue;

names += '`' + val + '`,';
valuesData += sphinx.escape(values[val]) + ',';
}
names = names.slice(0, -1)
valuesData = valuesData.slice(0, -1)
return valuesData
}
if(Array.isArray(values))
{
if(values[val] === null)
continue;

names += '`' + val + '`,';
data += sphinx.escape(values[val]) + ',';
for(const value of values)
{
data += `(${parseValues(value)}),`
}
data = data.slice(0, -1)
}
else
{
data = `(${parseValues(values)})`
}
names = names.slice(0, -1)
data = data.slice(0, -1)
let query = `INSERT INTO ${table}(${names}) VALUES(${data})`;

let query = `INSERT INTO ${table}(${names}) VALUES ${data}`;
queryCall(query, (...responce) => {
if(callback)
callback(...responce)
Expand Down
46 changes: 9 additions & 37 deletions src/background/spider.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,31 +303,6 @@ app.get('*', function(req, res)
}
};

// обновление статистики
/*
setInterval(() => {
let stats = {};
sphinx.query('SELECT COUNT(*) as tornum FROM `torrents`', function (error, rows, fields) {
stats.torrents = rows[0].tornum;
sphinx.query('SELECT COUNT(*) as filesnum, SUM(`size`) as filesizes FROM `files`', function (error, rows, fields) {
stats.files = rows[0].filesnum;
stats.size = rows[0].filesizes;
send('newStatistic', stats);
sphinx.query('DELETE FROM `statistic`', function (err, result) {
if(!result) {
console.error(err);
}
sphinx.query('INSERT INTO `statistic` SET ?', stats, function(err, result) {
if(!result) {
console.error(err);
}
});
})
});
});
}, 10 * 60 * 1000)
*/

const updateTorrentTrackers = (hash) => {
let maxSeeders = 0, maxLeechers = 0, maxCompleted = 0;
mysqlSingle.query('UPDATE torrents SET trackersChecked = ? WHERE hash = ?', [Math.floor(Date.now() / 1000), hash], (err, result) => {
Expand Down Expand Up @@ -535,7 +510,6 @@ setInterval(() => {
});
})

let filesToAdd = filesList.length;
mysqlSingle.query('SELECT count(*) as files_count FROM files WHERE hash = ?', [torrent.hash], function(err, rows) {
if(!rows)
return
Expand All @@ -552,17 +526,15 @@ setInterval(() => {
filesList.forEach((file) => {
file.id = filesId++;
file.pathIndex = file.path;
mysqlSingle.insertValues('files', file, function(err, result) {
if(!result) {
console.log(file);
console.error(err);
return
}
if(--filesToAdd === 0) {
if(!silent)
send('filesReady', torrent.hash);
}
});
});

mysqlSingle.insertValues('files', filesList, function(err, result) {
if(!result) {
console.error(err);
return
}
if(!silent)
send('filesReady', torrent.hash);
});
})
}
Expand Down

0 comments on commit a84f8c7

Please sign in to comment.