Skip to content

Commit

Permalink
Scan optimizations (#419)
Browse files Browse the repository at this point in the history
* dbcontroller.php: add iface for transactions

* controller: scan & reset library in transaction

* lib: reduce prepare() calls in loops

* Revert "lib: reduce prepare() calls in loops"

This reverts commit cb15546.
It did not provide any measurable performance benefits.

* use EventSource for scan updates

* scannercontroller.php: refactor scanForAudios()

* enable rollback on exception

* scannercontroller.php: fix incorrect variable name

* remove unnecessary variables and arguments

* fix harmless typos

* docs and consistency fixes

* catch Exception from global namespace
  • Loading branch information
Rello committed Aug 5, 2019
2 parents d9d99b5 + fc4d9c4 commit 742760e
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 223 deletions.
3 changes: 1 addition & 2 deletions appinfo/routes.php
Expand Up @@ -24,8 +24,7 @@
['name' => 'playlist#removePlaylist', 'url' => '/removeplaylist', 'verb' => 'POST'],
['name' => 'playlist#removeTrackFromPlaylist', 'url' => '/removetrackfromplaylist', 'verb' => 'POST'],
['name' => 'scanner#getImportTpl', 'url' => '/getimporttpl', 'verb' => 'GET'],
['name' => 'scanner#getProgress', 'url' => '/getprogress', 'verb' => 'POST'],
['name' => 'scanner#scanForAudios', 'url' => '/scanforaudiofiles', 'verb' => 'POST'],
['name' => 'scanner#scanForAudios', 'url' => '/scanforaudiofiles', 'verb' => 'GET'],
['name' => 'scanner#checkNewTracks', 'url' => '/checknewtracks', 'verb' => 'POST'],
['name' => 'music#getAudioStream', 'url' => '/getaudiostream', 'verb' => 'GET'],
['name' => 'music#getPublicAudioStream', 'url' => '/getpublicaudiostream', 'verb' => 'GET'],
Expand Down
64 changes: 26 additions & 38 deletions js/settings/settings.js
Expand Up @@ -110,32 +110,21 @@ OCA.Audioplayer.Settings = {
processScan: function () {
$('#audios_import_form').css('display', 'none');
$('#audios_import_process').css('display', 'block');

OCA.Audioplayer.Settings.startScan();
window.setTimeout(function () {
OCA.Audioplayer.Settings.updateScanProgress();
}, 1500);
},

startScan: function () {
var jqXHR = $.post(OC.generateUrl('apps/audioplayer/scanforaudiofiles'),
{}, function (data) {
if (data.status === 'success') {
$('#audios_import_process').css('display', 'none');
$('#audios_import_done').css('display', 'block');
$('#audios_import_done_message').html(data.message);
OCA.Audioplayer.Core.init();
} else {
$('#audios_import_progressbar').progressbar('option', 'value', 100);
$('#audios_import_done_message').html(data.message);
}
});
var scanUrl = OC.generateUrl('apps/audioplayer/scanforaudiofiles');
var source = new OC.EventSource(scanUrl);
source.listen('progress', OCA.Audioplayer.Settings.updateScanProgress);
source.listen('done', OCA.Audioplayer.Settings.scanDone);
source.listen('error', OCA.Audioplayer.Settings.scanError);
},

stopScan: function () {
OCA.Audioplayer.Settings.percentage = 0;
$.ajax({
type: 'POST',
type: 'GET',
url: OC.generateUrl('apps/audioplayer/scanforaudiofiles'),
data: {
'scanstop': true
Expand All @@ -145,27 +134,26 @@ OCA.Audioplayer.Settings = {
});
},

updateScanProgress: function () {
$.post(OC.generateUrl('apps/audioplayer/getprogress'),
{}, function (data) {
if (data.status === 'success') {
OCA.Audioplayer.Settings.percentage = parseInt(data.percent);
$('#audios_import_progressbar').progressbar('option', 'value', parseInt(data.percent));
$('#audios_import_process_progress').text(data.prog);
$('#audios_import_process_message').text(data.msg);
if (data.percent < 100) {
window.setTimeout(function () {
OCA.Audioplayer.Settings.updateScanProgress();
}, 1500);
} else {
$('#audios_import_process').css('display', 'none');
$('#audios_import_done').css('display', 'block');
}
} else {
//alert("getprogress error");
}
});
return 0;
updateScanProgress: function (message) {
var data = JSON.parse(message);
OCA.Audioplayer.Settings.percentage = data.filesProcessed / data.filesTotal * 100;
$('#audios_import_progressbar').progressbar('option', 'value', OCA.Audioplayer.Settings.percentage);
$('#audios_import_process_progress').text(`${data.filesProcessed}/${data.filesTotal}`);
$('#audios_import_process_message').text(data.currentFile);
},

scanDone: function (message) {
var data = JSON.parse(message);
$('#audios_import_process').css('display', 'none');
$('#audios_import_done').css('display', 'block');
$('#audios_import_done_message').html(data.message);
OCA.Audioplayer.Core.init();
},

scanError: function (message) {
var data = JSON.parse(message);
$('#audios_import_progressbar').progressbar('option', 'value', 100);
$('#audios_import_done_message').text(data.message);
},
};

Expand Down
19 changes: 19 additions & 0 deletions lib/Controller/DbController.php
Expand Up @@ -155,6 +155,7 @@ public function resetMediaLibrary($userId = null, $output = null, $hook = null)
$this->occ_job = false;
}

$this->db->beginTransaction();
$stmt = $this->db->prepare('DELETE FROM `*PREFIX*audioplayer_tracks` WHERE `user_id` = ?');
$stmt->execute(array($this->userId));

Expand All @@ -168,6 +169,7 @@ public function resetMediaLibrary($userId = null, $output = null, $hook = null)
$stmt->execute(array($this->userId));

$stmt = $this->db->prepare('SELECT `id` FROM `*PREFIX*audioplayer_playlists` WHERE `user_id` = ?');

$stmt->execute(array($this->userId));
$results = $stmt->fetchAll();
if (!is_null($results)) {
Expand All @@ -186,6 +188,8 @@ public function resetMediaLibrary($userId = null, $output = null, $hook = null)
$stmt = $this->db->prepare('DELETE FROM `*PREFIX*audioplayer_streams` WHERE `user_id` = ?');
$stmt->execute(array($this->userId));

$this->db->commit();

$result = [
'status' => 'success',
'msg' => 'all good'
Expand Down Expand Up @@ -375,6 +379,21 @@ public function writeArtistToDB($userId, $sArtist)
}
}

public function beginTransaction()
{
$this->db->beginTransaction();
}

public function commit()
{
$this->db->commit();
}

public function rollBack()
{
$this->db->rollBack();
}

/**
* Add genre to db if not exist
* @param int $userId
Expand Down

0 comments on commit 742760e

Please sign in to comment.