Commit 969880e0 authored by Camilo Higuita's avatar Camilo Higuita

initial work on non blocking db on large insertions

parent 8a06845a
...@@ -203,7 +203,8 @@ SOURCES += main.cpp \ ...@@ -203,7 +203,8 @@ SOURCES += main.cpp \
taglib/tagunion.cpp \ taglib/tagunion.cpp \
babe.cpp \ babe.cpp \
settings/BabeSettings.cpp \ settings/BabeSettings.cpp \
java/notificationclient.cpp java/notificationclient.cpp \
db/conthread.cpp
...@@ -361,6 +362,7 @@ HEADERS += \ ...@@ -361,6 +362,7 @@ HEADERS += \
taglib/taglib_config.h \ taglib/taglib_config.h \
babe.h \ babe.h \
settings/BabeSettings.h \ settings/BabeSettings.h \
java/notificationclient.h java/notificationclient.h \
db/conthread.h
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <QColor> #include <QColor>
#include <QIcon> #include <QIcon>
#include "db/collectionDB.h" #include "db/collectionDB.h"
#include "db/conthread.h"
#include "settings/BabeSettings.h" #include "settings/BabeSettings.h"
#include "pulpo/pulpo.h" #include "pulpo/pulpo.h"
#include <QApplication> #include <QApplication>
...@@ -28,9 +29,10 @@ Babe::Babe(QObject *parent) : CollectionDB(parent) ...@@ -28,9 +29,10 @@ Babe::Babe(QObject *parent) : CollectionDB(parent)
{ {
qDebug()<<"CONSTRUCTING ABE INTERFACE"; qDebug()<<"CONSTRUCTING ABE INTERFACE";
this->set = new BabeSettings(this); this->settings = new BabeSettings(this);
this->thread = new ConThread;
connect(set, &BabeSettings::refreshTables, [this](QVariantMap tables) connect(settings, &BabeSettings::refreshTables, [this](QVariantMap tables)
{ {
emit this->refreshTables(tables); emit this->refreshTables(tables);
}); });
...@@ -52,7 +54,7 @@ Babe::Babe(QObject *parent) : CollectionDB(parent) ...@@ -52,7 +54,7 @@ Babe::Babe(QObject *parent) : CollectionDB(parent)
Babe::~Babe() Babe::~Babe()
{ {
delete this->thread;
} }
QVariantList Babe::get(const QString &queryTxt) QVariantList Babe::get(const QString &queryTxt)
...@@ -65,10 +67,32 @@ QVariantList Babe::getList(const QStringList &urls) ...@@ -65,10 +67,32 @@ QVariantList Babe::getList(const QStringList &urls)
return Babe::transformData(getDBData(urls)); return Babe::transformData(getDBData(urls));
} }
void Babe::set(const QString &table, const QVariantList &wheres)
{
this->thread->start(table, wheres);
}
void Babe::trackPlaylist(const QStringList &urls, const QString &playlist)
{
QVariantList data;
for(auto url : urls)
{
QVariantMap map {{KEYMAP[KEY::PLAYLIST],playlist},
{KEYMAP[KEY::URL],url},
{KEYMAP[KEY::ADD_DATE],QDateTime::currentDateTime()}};
data << map;
}
qDebug()<<"now to send data to thread";
this->thread->start(BAE::TABLEMAP[TABLE::TRACKS_PLAYLISTS], data);
}
void Babe::trackLyrics(const QString &url) void Babe::trackLyrics(const QString &url)
{ {
auto track = getDBData(QString("SELECT * FROM %1 WHERE %2 = \"%3\"").arg(TABLEMAP[TABLE::TRACKS], auto track = getDBData(QString("SELECT * FROM %1 WHERE %2 = \"%3\"").arg(TABLEMAP[TABLE::TRACKS],
KEYMAP[KEY::URL], url)); KEYMAP[KEY::URL], url));
if(track.isEmpty()) return; if(track.isEmpty()) return;
...@@ -78,7 +102,7 @@ void Babe::trackLyrics(const QString &url) ...@@ -78,7 +102,7 @@ void Babe::trackLyrics(const QString &url)
bool Babe::trackBabe(const QString &path) bool Babe::trackBabe(const QString &path)
{ {
auto babe = getDBData(QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(KEYMAP[KEY::BABE], auto babe = getDBData(QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(KEYMAP[KEY::BABE],
TABLEMAP[TABLE::TRACKS], TABLEMAP[TABLE::TRACKS],
KEYMAP[KEY::URL],path)); KEYMAP[KEY::URL],path));
if(!babe.isEmpty()) if(!babe.isEmpty())
...@@ -90,7 +114,7 @@ bool Babe::trackBabe(const QString &path) ...@@ -90,7 +114,7 @@ bool Babe::trackBabe(const QString &path)
QString Babe::artistArt(const QString &artist) QString Babe::artistArt(const QString &artist)
{ {
auto artwork = getDBData(QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(KEYMAP[KEY::ARTWORK], auto artwork = getDBData(QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(KEYMAP[KEY::ARTWORK],
TABLEMAP[TABLE::ARTISTS], TABLEMAP[TABLE::ARTISTS],
KEYMAP[KEY::ARTIST],artist)); KEYMAP[KEY::ARTIST],artist));
if(!artwork.isEmpty()) if(!artwork.isEmpty())
...@@ -103,7 +127,7 @@ QString Babe::artistArt(const QString &artist) ...@@ -103,7 +127,7 @@ QString Babe::artistArt(const QString &artist)
QString Babe::artistWiki(const QString &artist) QString Babe::artistWiki(const QString &artist)
{ {
auto wiki = getDBData(QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(KEYMAP[KEY::WIKI], auto wiki = getDBData(QString("SELECT %1 FROM %2 WHERE %3 = \"%4\"").arg(KEYMAP[KEY::WIKI],
TABLEMAP[TABLE::ARTISTS], TABLEMAP[TABLE::ARTISTS],
KEYMAP[KEY::ARTIST],artist)); KEYMAP[KEY::ARTIST],artist));
if(!wiki.isEmpty()) if(!wiki.isEmpty())
...@@ -168,10 +192,10 @@ QString Babe::albumWiki(const QString &album, const QString &artist) ...@@ -168,10 +192,10 @@ QString Babe::albumWiki(const QString &album, const QString &artist)
bool Babe::babeTrack(const QString &path, const bool &value) bool Babe::babeTrack(const QString &path, const bool &value)
{ {
if(update(TABLEMAP[TABLE::TRACKS], if(update(TABLEMAP[TABLE::TRACKS],
KEYMAP[KEY::BABE], KEYMAP[KEY::BABE],
value ? 1 : 0, value ? 1 : 0,
KEYMAP[KEY::URL], KEYMAP[KEY::URL],
path)) return true; path)) return true;
return false; return false;
} }
...@@ -201,12 +225,12 @@ void Babe::notifySong(const QString &url) ...@@ -201,12 +225,12 @@ void Babe::notifySong(const QString &url)
void Babe::scanDir(const QString &url) void Babe::scanDir(const QString &url)
{ {
emit this->set->collectionPathChanged({url}); emit this->settings->collectionPathChanged({url});
} }
void Babe::brainz(const bool &on) void Babe::brainz(const bool &on)
{ {
this->set->checkCollectionBrainz(on); this->settings->checkCollectionBrainz(on);
} }
bool Babe::brainzState() bool Babe::brainzState()
......
...@@ -13,7 +13,7 @@ class Notify; ...@@ -13,7 +13,7 @@ class Notify;
class CollectionDB; class CollectionDB;
class Pulpo; class Pulpo;
class BabeSettings; class BabeSettings;
class ConThread;
using namespace BAE; using namespace BAE;
class Babe : public CollectionDB class Babe : public CollectionDB
...@@ -36,6 +36,9 @@ public: ...@@ -36,6 +36,9 @@ public:
Q_INVOKABLE QVariantList get(const QString &queryTxt); Q_INVOKABLE QVariantList get(const QString &queryTxt);
Q_INVOKABLE QVariantList getList(const QStringList &urls); Q_INVOKABLE QVariantList getList(const QStringList &urls);
Q_INVOKABLE void set(const QString &table, const QVariantList &wheres);
Q_INVOKABLE void trackPlaylist(const QStringList &urls, const QString &playlist);
Q_INVOKABLE void trackLyrics(const QString &url); Q_INVOKABLE void trackLyrics(const QString &url);
Q_INVOKABLE bool trackBabe(const QString &path); Q_INVOKABLE bool trackBabe(const QString &path);
Q_INVOKABLE QString artistArt(const QString &artist); Q_INVOKABLE QString artistArt(const QString &artist);
...@@ -105,7 +108,9 @@ public: ...@@ -105,7 +108,9 @@ public:
Q_INVOKABLE QVariantList searchFor(const QStringList &queries); Q_INVOKABLE QVariantList searchFor(const QStringList &queries);
private: private:
BabeSettings *set; BabeSettings *settings;
ConThread *thread;
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID)) #if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
Notify *nof; Notify *nof;
#endif #endif
...@@ -121,7 +126,7 @@ signals: ...@@ -121,7 +126,7 @@ signals:
void skipTrack(); void skipTrack();
void babeIt(); void babeIt();
friend class CollectionDB; friend class CollectionDB;
}; };
......
...@@ -25,6 +25,8 @@ var GET = { ...@@ -25,6 +25,8 @@ var GET = {
} }
var POST = {} var INSERT = {
trackPlaylist_ : "insert into tracks_playlists () ",
}
var UPDATE = {} var UPDATE = {}
...@@ -210,6 +210,9 @@ void CollectionDB::openDB(const QString &name) ...@@ -210,6 +210,9 @@ void CollectionDB::openDB(const QString &name)
{ {
if(!this->m_db.open()) if(!this->m_db.open())
qDebug()<<"ERROR OPENING DB"<<this->m_db.lastError().text()<<m_db.connectionName(); qDebug()<<"ERROR OPENING DB"<<this->m_db.lastError().text()<<m_db.connectionName();
}else
{
this->execQuery("PRAGMA journal_mode=WAL");
} }
} }
...@@ -492,18 +495,6 @@ bool CollectionDB::addPlaylist(const QString &title) ...@@ -492,18 +495,6 @@ bool CollectionDB::addPlaylist(const QString &title)
return false; return false;
} }
bool CollectionDB::trackPlaylist(const QString &url, const QString &playlist)
{
QVariantMap map {{KEYMAP[KEY::PLAYLIST],playlist},
{KEYMAP[KEY::URL],url},
{KEYMAP[KEY::ADD_DATE],QDateTime::currentDateTime()}};
if(insert(TABLEMAP[TABLE::TRACKS_PLAYLISTS],map))
return true;
return false;
}
bool CollectionDB::addFolder(const QString &url) bool CollectionDB::addFolder(const QString &url)
{ {
QVariantMap map {{KEYMAP[KEY::URL],url}, QVariantMap map {{KEYMAP[KEY::URL],url},
......
...@@ -65,7 +65,6 @@ public: ...@@ -65,7 +65,6 @@ public:
bool tagsAlbum(const BAE::DB &track, const QString &value, const QString &context = ""); bool tagsAlbum(const BAE::DB &track, const QString &value, const QString &context = "");
Q_INVOKABLE bool addPlaylist(const QString &title); Q_INVOKABLE bool addPlaylist(const QString &title);
Q_INVOKABLE bool trackPlaylist(const QString &url, const QString &playlist);
bool addFolder(const QString &url); bool addFolder(const QString &url);
...@@ -108,6 +107,8 @@ public: ...@@ -108,6 +107,8 @@ public:
private: private:
QString name; QString name;
QSqlDatabase m_db; QSqlDatabase m_db;
/*basic actions*/ /*basic actions*/
......
#include "conthread.h"
#include <QMap>
#include <QList>
ConThread::ConThread() : CollectionDB(nullptr)
{
this->moveToThread(&t);
this->t.start();
}
ConThread::~ConThread()
{
this->stop();
}
void ConThread::start(QString table, QVariantList wheres)
{
if(!this->go) this->go = true;
this->queue.append({{QString ("TABLE"), table}, {QString ("WHERES"), wheres}});
qDebug()<<"NOW ON THREAD<<"<< queue;
if(this->queue.size() > 1) return;
while(!this->queue.isEmpty())
{
if(this->go)
{
qDebug()<<"RUNNIGN QUERY ON CONTHREAD"<<this->queue.first()["TABLE"].toString();
QMetaObject::invokeMethod(this, "set", Q_ARG(QString, this->queue.first()["TABLE"].toString()), Q_ARG(QVariantList, this->queue.first()["WHERES"].toList()));
qDebug()<<"FINISHED SET ON QUEUE CONTHREAD"<< this->queue.first()["TABLE"].toString();
this->queue.removeFirst();
qDebug()<<this->queue.size();
}else return;
}
qDebug()<<"FINISHED SET ON CONTHREAD TOTALLY";
}
void ConThread::stop()
{
this->go = false;
this->t.quit();
this->t.wait();
}
void ConThread::pause()
{
this->go = false;
}
bool ConThread::isRunning()
{
return this->go;
}
void ConThread::setInterval()
{
}
void ConThread::get(QString query)
{
auto data = getDBDataQML(query);
emit this->dataReady(data);
}
void ConThread::set(QString tableName, QVariantList wheres)
{
for(auto variant : wheres)
{
this->insert(tableName, QVariantMap(variant.toMap()));
this->t.msleep(this->interval);
}
}
#ifndef CONTHREAD_H
#define CONTHREAD_H
#include <QObject>
#include <QThread>
#include "collectionDB.h"
class ConThread : public CollectionDB
{
Q_OBJECT
public:
explicit ConThread();
~ConThread();
void start(QString table, QVariantList wheres);
void stop();
void pause();
bool isRunning();
void setInterval();
void get(QString query);
private:
QThread t;
uint interval = 500;
bool go = false;
QList<QMap<QString, QVariant>> queue;
signals:
void finished();
void dataReady(QVariantList);
public slots:
void set(QString tableName, QVariantList wheres);
};
#endif // CONTHREAD_H
...@@ -231,7 +231,7 @@ Kirigami.ApplicationWindow ...@@ -231,7 +231,7 @@ Kirigami.ApplicationWindow
header: BabeBar header: BabeBar
{ {
id: mainToolbar id: mainToolbar
height: headerHeight height: toolBarHeight
visible: true visible: true
currentIndex: currentView currentIndex: currentView
bgColor: isMobile && pageStack.currentIndex === 0 && !pageStack.wideMode ? babeColor : babeAltColor bgColor: isMobile && pageStack.currentIndex === 0 && !pageStack.wideMode ? babeColor : babeAltColor
...@@ -433,15 +433,15 @@ Kirigami.ApplicationWindow ...@@ -433,15 +433,15 @@ Kirigami.ApplicationWindow
anchors.fill: parent anchors.fill: parent
width: parent.width width: parent.width
height: parent.height height: parent.height
Rectangle
{
visible: (!pageStack.wideMode && pageStack.currentIndex !== 0) || !mainPlaylist.cover.visible
Item
{
Layout.fillHeight: true
height: headerHeight height: headerHeight
width: headerHeight width: headerHeight
Image Image
{ {
visible: (!pageStack.wideMode && pageStack.currentIndex !== 0) || !mainPlaylist.cover.visible
height: headerHeight height: headerHeight
width: headerHeight width: headerHeight
...@@ -470,63 +470,87 @@ Kirigami.ApplicationWindow ...@@ -470,63 +470,87 @@ Kirigami.ApplicationWindow
} }
} }
Item Item
{ {
Layout.fillHeight: true
Layout.fillWidth: true Layout.fillWidth: true
} Layout.alignment: Qt.AlignCenter
BabeButton RowLayout
{
id: babeBtnIcon
iconName: "love" //"love-amarok"
iconColor: currentBabe ? babeColor : defaultColor
onClicked:
{ {
var value = mainPlaylist.list.contextMenu.babeIt(currentTrackIndex) anchors.centerIn: parent
// iconColor = value ? babeColor : foregroundColor anchors.fill: parent
currentTrack.babe = value ? "1" : "0"
currentBabe = value
}
}
BabeButton Item
{ {
id: previousBtn Layout.fillWidth: true
iconName: "media-skip-backward" }
onClicked: Player.previousTrack()
onPressAndHold: Player.playAt(prevTrackIndex)
}
BabeButton BabeButton
{ {
id: playIcon id: babeBtnIcon
iconName: "media-playback-start" iconName: "love" //"love-amarok"
onClicked: iconColor: currentBabe ? babeColor : defaultColor
{ onClicked:
if(player.isPaused()) Player.resumeTrack() {
else Player.pauseTrack() var value = mainPlaylist.list.contextMenu.babeIt(currentTrackIndex)
} // iconColor = value ? babeColor : foregroundColor
} currentTrack.babe = value ? "1" : "0"
currentBabe = value
}
}
BabeButton BabeButton
{ {
id: nextBtn id: previousBtn
iconName: "media-skip-forward" iconName: "media-skip-backward"
onClicked: Player.nextTrack() onClicked: Player.previousTrack()
onPressAndHold: Player.playAt(Player.shuffle()) onPressAndHold: Player.playAt(prevTrackIndex)
} }
BabeButton
{
id: playIcon
iconName: "media-playback-start"
onClicked:
{
if(player.isPaused()) Player.resumeTrack()
else Player.pauseTrack()
}
}
BabeButton
{
id: nextBtn
iconName: "media-skip-forward"
onClicked: Player.nextTrack()
onPressAndHold: Player.playAt(Player.shuffle())
}
BabeButton
{
id: shuffleBtn
iconName: shuffle ? "media-playlist-shuffle" : "media-playlist-repeat"
onClicked: shuffle = !shuffle
}
Item
{
Layout.fillWidth: true
}
}
BabeButton
{
id: shuffleBtn
iconName: shuffle ? "media-playlist-shuffle" : "media-playlist-repeat"
onClicked: shuffle = !shuffle
} }
Item Item
{ {
Layout.fillWidth: true Layout.fillHeight: true
height: headerHeight
width: headerHeight
} }
} }
} }
......
...@@ -184,7 +184,7 @@ void YouTube::processFinished_totally(const int &state,const DB &info,const QPro ...@@ -184,7 +184,7 @@ void YouTube::processFinished_totally(const int &state,const DB &info,const QPro
CollectionDB con(nullptr); CollectionDB con(nullptr);
con.addTrack(trackMap); con.addTrack(trackMap);
con.trackPlaylist(file, track[KEY::PLAYLIST]); // con.trackPlaylist(file, track[KEY::PLAYLIST]);
if(this->ids.isEmpty()) emit this->done(); if(this->ids.isEmpty()) emit this->done();
} }
......
...@@ -81,7 +81,6 @@ BabeSettings::BabeSettings(QObject *parent) : QObject(parent) ...@@ -81,7 +81,6 @@ BabeSettings::BabeSettings(QObject *parent) : QObject(parent)
emit this->refreshTables({{BAE::TABLEMAP[type], true}}); emit this->refreshTables({{BAE::TABLEMAP[type], true}});
}); });
// connect(this->fileLoader, &FileLoader::trackReady, [this]() // connect(this->fileLoader, &FileLoader::trackReady, [this]()
// { // {
// this->ui->progressBar->setValue(this->ui->progressBar->value()+1); // this->ui->progressBar->setValue(this->ui->progressBar->value()+1);
...@@ -91,14 +90,16 @@ BabeSettings::BabeSettings(QObject *parent) : QObject(parent) ...@@ -91,14 +90,16 @@ BabeSettings::BabeSettings(QObject *parent) : QObject(parent)
{ {
if(size > 0) if(size > 0)
{ {
this->collectionWatcher(); // this->collectionWatcher();
emit refreshTables({{BAE::TABLEMAP[TABLE::TRACKS], true}, // emit refreshTables({{BAE::TABLEMAP[TABLE::TRACKS], true},
{BAE::TABLEMAP[TABLE::ALBUMS], true}, // {BAE::TABLEMAP[TABLE::ALBUMS], true},
{BAE::TABLEMAP[TABLE::ARTISTS], true}, // {BAE::TABLEMAP[TABLE::ARTISTS], true},
{BAE::TABLEMAP[TABLE::PLAYLISTS], true}}); // {BAE::TABLEMAP[TABLE::PLAYLISTS], true}});
// this->startBrainz(true, 1500);
this->startBrainz(true, 1500); qDebug()<<"Finished inserting into DB";
}else }else
{ {
this->dirs.clear(); this->dirs.clear();
......
...@@ -235,8 +235,10 @@ function addToPlaylist(urls, playlist) ...@@ -235,8 +235,10 @@ function addToPlaylist(urls, playlist)
{ {
if(urls.length > 0) if(urls.length > 0)
{ {
for(var i in urls) bae.trackPlaylist(urls, playlist)
bae.trackPlaylist(urls[i], playlist) // for(var i in urls)
// bae.trackPlaylist(urls[i], playlist)
if(!isMobile) if(!isMobile)
bae.notify(playlist, urls.length + " tracks added to the playlist:\n"+urls.join("\n")) bae.notify(playlist, urls.length + " tracks added to the playlist:\n"+urls.join("\n"))
......
...@@ -41,7 +41,6 @@ void Brain::pause() ...@@ -41,7 +41,6 @@ void Brain::pause()
this->go = false; this->go = false;
} }
bool Brain::isRunning() const bool Brain::isRunning() const
{ {
return this->go; return this->go;
......
...@@ -49,7 +49,7 @@ ListView ...@@ -49,7 +49,7 @@ ListView
addDisplaced: Transition addDisplaced: Transition
{ {
NumberAnimation { properties: "x,y"; duration: 1000 } NumberAnimation { properties: "x,y"; duration: 100 }
} }
function clearTable() function clearTable()
......
...@@ -53,20 +53,32 @@ ToolBar ...@@ -53,20 +53,32 @@ ToolBar
RowLayout RowLayout
{ {
anchors.fill: parent anchors.fill: parent
BabeButton BabeButton
{ {
iconName: "love" id: settingsView
iconColor: accent && currentIndex === viewsIndex.babeit ? accentColor : textColor iconName: /*"headphones"*/ /*"media-optical-audio"*/ "application-menu"
iconColor: settingsDrawer.visible ? babeColor : textColor/*(pageStack.wideMode || pageStack.currentIndex === 0 ) && !isMobile ? accentColor : textColor*/
onClicked: babeViewClicked() onClicked: settingsViewClicked()/*playlistViewClicked()*/
hoverEnabled: !isMobile hoverEnabled: !isMobile
ToolTip.delay: 1000 ToolTip.delay: 1000
ToolTip.timeout: 5000 ToolTip.timeout: 5000
ToolTip.visible: hovered && !isMobile