Use db transactions when updating the progress from iOS

Timeouts were a problem before.
This commit is contained in:
Luis Ángel San Martín 2019-08-24 13:31:52 +02:00
parent 8bb43a7cf3
commit 9056fffb66
3 changed files with 132 additions and 4 deletions

View File

@ -773,6 +773,10 @@ void DBHelper::updateFromRemoteClientWithHash(const ComicInfo &comicInfo)
ComicInfo info = loadComicInfo(comicInfo.hash, db);
if (!info.existOnDb) {
continue;
}
if (comicInfo.currentPage > 0) {
info.currentPage = comicInfo.currentPage;
@ -795,6 +799,121 @@ void DBHelper::updateFromRemoteClientWithHash(const ComicInfo &comicInfo)
}
}
void DBHelper::updateFromRemoteClient(const QMap<qulonglong, QList<ComicInfo>> &comics)
{
foreach (qulonglong libraryId, comics.keys()) {
QString libraryPath = DBHelper::getLibraries().getPath(libraryId);
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
db.transaction();
QSqlQuery updateComicInfo(db);
updateComicInfo.prepare("UPDATE comic_info SET "
"read = :read, "
"currentPage = :currentPage, "
"hasBeenOpened = :hasBeenOpened, "
"lastTimeOpened = :lastTimeOpened, "
"rating = :rating"
" WHERE id = :id ");
foreach (ComicInfo comicInfo, comics[libraryId]) {
ComicDB comic = DBHelper::loadComic(comicInfo.id, db);
if (comic.info.hash == comicInfo.hash) {
if (comicInfo.currentPage > 0) {
comic.info.currentPage = comicInfo.currentPage;
if (comic.info.currentPage == comic.info.numPages)
comic.info.read = true;
comic.info.hasBeenOpened = true;
if (comic.info.lastTimeOpened.toULongLong() < comicInfo.lastTimeOpened.toULongLong())
comic.info.lastTimeOpened = comicInfo.lastTimeOpened;
}
if (comicInfo.rating > 0)
comic.info.rating = comicInfo.rating;
updateComicInfo.bindValue(":read", comic.info.read ? 1 : 0);
updateComicInfo.bindValue(":currentPage", comic.info.currentPage);
updateComicInfo.bindValue(":hasBeenOpened", comic.info.hasBeenOpened ? 1 : 0);
updateComicInfo.bindValue(":lastTimeOpened", QDateTime::currentMSecsSinceEpoch() / 1000);
updateComicInfo.bindValue(":id", comic.info.id);
updateComicInfo.bindValue(":rating", comic.info.rating);
updateComicInfo.exec();
updateComicInfo.clear();
}
}
db.commit();
db.close();
QSqlDatabase::removeDatabase(db.connectionName());
}
}
void DBHelper::updateFromRemoteClientWithHash(const QList<ComicInfo> &comics)
{
YACReaderLibraries libraries = DBHelper::getLibraries();
QStringList names = libraries.getNames();
foreach (QString name, names) {
QString libraryPath = DBHelper::getLibraries().getPath(libraries.getId(name));
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
db.transaction();
QSqlQuery updateComicInfo(db);
updateComicInfo.prepare("UPDATE comic_info SET "
"read = :read, "
"currentPage = :currentPage, "
"hasBeenOpened = :hasBeenOpened, "
"lastTimeOpened = :lastTimeOpened, "
"rating = :rating"
" WHERE id = :id ");
foreach (ComicInfo comicInfo, comics) {
ComicInfo info = loadComicInfo(comicInfo.hash, db);
if (!info.existOnDb) {
continue;
}
if (comicInfo.currentPage > 0) {
info.currentPage = comicInfo.currentPage;
if (info.currentPage == info.numPages)
info.read = true;
info.hasBeenOpened = true;
if (info.lastTimeOpened.toULongLong() < comicInfo.lastTimeOpened.toULongLong())
info.lastTimeOpened = comicInfo.lastTimeOpened;
}
if (comicInfo.rating > 0) {
info.rating = comicInfo.rating;
}
updateComicInfo.bindValue(":read", info.read ? 1 : 0);
updateComicInfo.bindValue(":currentPage", info.currentPage);
updateComicInfo.bindValue(":hasBeenOpened", info.hasBeenOpened ? 1 : 0);
updateComicInfo.bindValue(":lastTimeOpened", QDateTime::currentMSecsSinceEpoch() / 1000);
updateComicInfo.bindValue(":id", info.id);
updateComicInfo.bindValue(":rating", info.rating);
updateComicInfo.exec();
;
}
db.close();
QSqlDatabase::removeDatabase(db.connectionName());
}
}
void DBHelper::renameLabel(qulonglong id, const QString &name, QSqlDatabase &db)
{
QSqlQuery renameLabelQuery(db);

View File

@ -69,9 +69,11 @@ public:
static void updateChildrenInfo(QSqlDatabase &db);
static void updateProgress(qulonglong libraryId, const ComicInfo &comicInfo);
static void setComicAsReading(qulonglong libraryId, const ComicInfo &comicInfo);
static void updateReadingRemoteProgress(const ComicInfo &comicInfo, QSqlDatabase &db);
static void updateFromRemoteClient(qulonglong libraryId, const ComicInfo &comicInfo);
static void updateFromRemoteClientWithHash(const ComicInfo &comicInfo);
static void updateReadingRemoteProgress(const ComicInfo &comicInfo, QSqlDatabase &db);
static void updateFromRemoteClient(const QMap<qulonglong, QList<ComicInfo>> &comics);
static void updateFromRemoteClientWithHash(const QList<ComicInfo> &comics);
static void renameLabel(qulonglong id, const QString &name, QSqlDatabase &db);
static void renameList(qulonglong id, const QString &name, QSqlDatabase &db);
static void reasignOrderToSublists(QList<qulonglong> ids, QSqlDatabase &db);

View File

@ -27,6 +27,8 @@ void SyncControllerV2::service(HttpRequest &request, HttpResponse &response)
int currentRating;
unsigned long long lastTimeOpened;
QString hash;
QMap<qulonglong, QList<ComicInfo>> comics;
QList<ComicInfo> comicsWithNoLibrary;
foreach (QString comicInfo, data) {
QList<QString> comicInfoProgress = comicInfo.split("\t");
@ -47,8 +49,10 @@ void SyncControllerV2::service(HttpRequest &request, HttpResponse &response)
lastTimeOpened = comicInfoProgress.at(5).toULong();
info.lastTimeOpened = lastTimeOpened;
DBHelper::updateFromRemoteClient(libraryId, info);
if (!comics.contains(libraryId)) {
comics[libraryId] = QList<ComicInfo>();
}
comics[libraryId].push_back(info);
} else {
hash = comicInfoProgress.at(2);
currentPage = comicInfoProgress.at(3).toInt();
@ -63,10 +67,13 @@ void SyncControllerV2::service(HttpRequest &request, HttpResponse &response)
lastTimeOpened = comicInfoProgress.at(5).toULong();
info.lastTimeOpened = lastTimeOpened;
DBHelper::updateFromRemoteClientWithHash(info);
comicsWithNoLibrary.push_back(info);
}
}
}
DBHelper::updateFromRemoteClient(comics);
DBHelper::updateFromRemoteClientWithHash(comicsWithNoLibrary);
} else {
response.setStatus(412, "No comic info received");
response.write("", true);