Commit 7ea3b201 authored by Martin Straka's avatar Martin Straka

Code refactoring

parent de0ee1c9
...@@ -342,5 +342,5 @@ bool DbWrapper::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, ...@@ -342,5 +342,5 @@ bool DbWrapper::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
const QByteArray &zfoData) const QByteArray &zfoData)
{ {
return globZfoDbPtr->insertZfoToDb(msgId, isTestAccount, zfoSize, return globZfoDbPtr->insertZfoToDb(msgId, isTestAccount, zfoSize,
zfoData.toBase64()); zfoData.toBase64(), (globSet.zfoDbSizeMBs * 1000000));
} }
...@@ -286,7 +286,6 @@ public: ...@@ -286,7 +286,6 @@ public:
* @param[out] fileList List of files structure. * @param[out] fileList List of files structure.
* @param[out] txt Error description if something failed. * @param[out] txt Error description if something failed.
* @param[out] zfoData ZFO file content. * @param[out] zfoData ZFO file content.
*
* @return true if success. * @return true if success.
*/ */
static static
......
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QObject>
#include <QSqlDatabase> #include <QSqlDatabase>
#include <QSqlError> #include <QSqlError>
#include <QSqlQuery> #include <QSqlQuery>
...@@ -35,7 +34,6 @@ ...@@ -35,7 +34,6 @@
#include "src/sqlite/db_tables.h" #include "src/sqlite/db_tables.h"
#include "src/sqlite/dbs.h" #include "src/sqlite/dbs.h"
#include "src/sqlite/zfo_db.h" #include "src/sqlite/zfo_db.h"
#include "src/settings.h"
ZfoDb::ZfoDb(const QString &connectionName) ZfoDb::ZfoDb(const QString &connectionName)
: SQLiteDb(connectionName) : SQLiteDb(connectionName)
...@@ -65,7 +63,7 @@ bool ZfoDb::openDb(const QString &fileName, bool storeToDisk) ...@@ -65,7 +63,7 @@ bool ZfoDb::openDb(const QString &fileName, bool storeToDisk)
QString queryStr = "SELECT count(*) FROM zfo_size_cnt"; QString queryStr = "SELECT count(*) FROM zfo_size_cnt";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
goto fail; goto fail;
} }
...@@ -75,7 +73,7 @@ bool ZfoDb::openDb(const QString &fileName, bool storeToDisk) ...@@ -75,7 +73,7 @@ bool ZfoDb::openDb(const QString &fileName, bool storeToDisk)
queryStr = "INSERT INTO zfo_size_cnt (id,totalSize) " queryStr = "INSERT INTO zfo_size_cnt (id,totalSize) "
"VALUES (0,0)"; "VALUES (0,0)";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
goto fail; goto fail;
} }
...@@ -83,7 +81,7 @@ bool ZfoDb::openDb(const QString &fileName, bool storeToDisk) ...@@ -83,7 +81,7 @@ bool ZfoDb::openDb(const QString &fileName, bool storeToDisk)
} }
return true; return true;
} else { } else {
qDebug() << qCritical() <<
"Cannot execute SQL query and/or read SQL data: %s.", "Cannot execute SQL query and/or read SQL data: %s.",
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
} }
...@@ -251,21 +249,21 @@ void ZfoDb::deleteZfo(qint64 msgId, bool isTestAccount, int zfoSize) ...@@ -251,21 +249,21 @@ void ZfoDb::deleteZfo(qint64 msgId, bool isTestAccount, int zfoSize)
QString queryStr = "DELETE FROM message_zfos WHERE " QString queryStr = "DELETE FROM message_zfos WHERE "
"dmID = :msgId AND testEnv = :isTestAccount"; "dmID = :msgId AND testEnv = :isTestAccount";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
return; return;
} }
query.bindValue(":msgId", msgId); query.bindValue(":msgId", msgId);
query.bindValue(":isTestAccount", isTestAccount); query.bindValue(":isTestAccount", isTestAccount);
if (!query.exec()) { if (!query.exec()) {
qDebug() << "Cannot execute SQL query:" << qCritical() << "Cannot execute SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
} }
queryStr = "UPDATE zfo_size_cnt SET totalSize = totalSize - :zfoSize"; queryStr = "UPDATE zfo_size_cnt SET totalSize = totalSize - :zfoSize";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
return; return;
} }
...@@ -273,7 +271,7 @@ void ZfoDb::deleteZfo(qint64 msgId, bool isTestAccount, int zfoSize) ...@@ -273,7 +271,7 @@ void ZfoDb::deleteZfo(qint64 msgId, bool isTestAccount, int zfoSize)
query.bindValue(":zfoSize", zfoSize); query.bindValue(":zfoSize", zfoSize);
if (!query.exec()) { if (!query.exec()) {
qDebug() << "Cannot execute SQL query:" << qCritical() << "Cannot execute SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
} }
} }
...@@ -285,7 +283,7 @@ QByteArray ZfoDb::getZfoContentFromDb(qint64 msgId, bool isTestAccount) ...@@ -285,7 +283,7 @@ QByteArray ZfoDb::getZfoContentFromDb(qint64 msgId, bool isTestAccount)
QString queryStr = "SELECT data FROM message_zfos WHERE " QString queryStr = "SELECT data FROM message_zfos WHERE "
"dmID = :msgId AND testEnv = :isTestAccount"; "dmID = :msgId AND testEnv = :isTestAccount";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
goto fail; goto fail;
} }
...@@ -307,7 +305,7 @@ int ZfoDb::getZfoSizeFromDb(qint64 msgId, bool isTestAccount) ...@@ -307,7 +305,7 @@ int ZfoDb::getZfoSizeFromDb(qint64 msgId, bool isTestAccount)
QString queryStr = "SELECT size FROM message_zfos WHERE " QString queryStr = "SELECT size FROM message_zfos WHERE "
"dmID = :msgId AND testEnv = :isTestAccount"; "dmID = :msgId AND testEnv = :isTestAccount";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
goto fail; goto fail;
} }
...@@ -323,7 +321,7 @@ fail: ...@@ -323,7 +321,7 @@ fail:
} }
bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
const QByteArray &zfoData) const QByteArray &zfoData, unsigned int sizeLimit)
{ {
QSqlQuery query(m_db); QSqlQuery query(m_db);
qint64 lastAccessTime = -1; qint64 lastAccessTime = -1;
...@@ -331,7 +329,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, ...@@ -331,7 +329,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
QString queryStr = "SELECT lastAccessTime FROM message_zfos WHERE " QString queryStr = "SELECT lastAccessTime FROM message_zfos WHERE "
"dmID = :msgId AND testEnv = :isTestAccount"; "dmID = :msgId AND testEnv = :isTestAccount";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
return false; return false;
} }
...@@ -343,7 +341,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, ...@@ -343,7 +341,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
lastAccessTime = query.value(0).toLongLong(); lastAccessTime = query.value(0).toLongLong();
} }
} else { } else {
qDebug() << "Cannot execute SQL query:" << qCritical() << "Cannot execute SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
return false; return false;
} }
...@@ -355,7 +353,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, ...@@ -355,7 +353,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
"size = :zfoSize, data = :zfoData " "size = :zfoSize, data = :zfoData "
"WHERE dmID = :msgId AND testEnv = :isTestAccount"; "WHERE dmID = :msgId AND testEnv = :isTestAccount";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
return false; return false;
} }
...@@ -365,7 +363,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, ...@@ -365,7 +363,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
query.bindValue(":zfoSize", zfoSize); query.bindValue(":zfoSize", zfoSize);
query.bindValue(":zfoData", zfoData); query.bindValue(":zfoData", zfoData);
if (!query.exec()) { if (!query.exec()) {
qDebug() << "Cannot execute SQL query:" << qCritical() << "Cannot execute SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
return false; return false;
} }
...@@ -374,10 +372,8 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, ...@@ -374,10 +372,8 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
/* ZFO is not in the databse = insert and release db size if needed */ /* ZFO is not in the databse = insert and release db size if needed */
// compute database size with new zfo
uint maximalSize = (globSet.zfoDbSizeMBs * 1000000) - zfoSize;
// if database is exceeded, release some oldest zfo files (based on size) // if database is exceeded, release some oldest zfo files (based on size)
if (isDbSizeExceeded(maximalSize)) { if (isDbSizeExceeded(sizeLimit - zfoSize)) {
releaseDb(zfoSize); releaseDb(zfoSize);
} }
...@@ -386,7 +382,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, ...@@ -386,7 +382,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
":msgId, :isTestAccount, :downloadTime, :zfoSize, :zfoData)"; ":msgId, :isTestAccount, :downloadTime, :zfoSize, :zfoData)";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
return false; return false;
} }
...@@ -398,7 +394,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, ...@@ -398,7 +394,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
query.bindValue(":zfoData", zfoData); query.bindValue(":zfoData", zfoData);
if (!query.exec()) { if (!query.exec()) {
qDebug() << "Cannot execute SQL query:" << qCritical() << "Cannot execute SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
return false; return false;
} }
...@@ -407,7 +403,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, ...@@ -407,7 +403,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
queryStr = "UPDATE zfo_size_cnt SET totalSize = totalSize + :zfoSize"; queryStr = "UPDATE zfo_size_cnt SET totalSize = totalSize + :zfoSize";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
return false; return false;
} }
...@@ -415,7 +411,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, ...@@ -415,7 +411,7 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
query.bindValue(":zfoSize", zfoSize); query.bindValue(":zfoSize", zfoSize);
if (!query.exec()) { if (!query.exec()) {
qDebug() << "Cannot execute SQL query:" << qCritical() << "Cannot execute SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
return false; return false;
} }
...@@ -423,13 +419,13 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, ...@@ -423,13 +419,13 @@ bool ZfoDb::insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
return true; return true;
} }
bool ZfoDb::isDbSizeExceeded(uint currentLimit) bool ZfoDb::isDbSizeExceeded(unsigned int currentLimit)
{ {
QSqlQuery query(m_db); QSqlQuery query(m_db);
QString queryStr = "SELECT totalSize FROM zfo_size_cnt WHERE id = :id"; QString queryStr = "SELECT totalSize FROM zfo_size_cnt WHERE id = :id";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
return false; return false;
} }
...@@ -438,7 +434,7 @@ bool ZfoDb::isDbSizeExceeded(uint currentLimit) ...@@ -438,7 +434,7 @@ bool ZfoDb::isDbSizeExceeded(uint currentLimit)
query.first() && query.isValid()) { query.first() && query.isValid()) {
return (currentLimit < query.value(0).toUInt()); return (currentLimit < query.value(0).toUInt());
} else { } else {
qDebug() << "Cannot execute SQL query:" << qCritical() << "Cannot execute SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
} }
...@@ -454,7 +450,7 @@ void ZfoDb::updateZfoLastAccessTime(qint64 msgId, bool isTestAccount) ...@@ -454,7 +450,7 @@ void ZfoDb::updateZfoLastAccessTime(qint64 msgId, bool isTestAccount)
"WHERE dmID = :msgId AND testEnv = :isTestAccount"; "WHERE dmID = :msgId AND testEnv = :isTestAccount";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
return; return;
} }
...@@ -464,20 +460,20 @@ void ZfoDb::updateZfoLastAccessTime(qint64 msgId, bool isTestAccount) ...@@ -464,20 +460,20 @@ void ZfoDb::updateZfoLastAccessTime(qint64 msgId, bool isTestAccount)
query.bindValue(":newTime", QDateTime::currentSecsSinceEpoch()); query.bindValue(":newTime", QDateTime::currentSecsSinceEpoch());
if (!query.exec()) { if (!query.exec()) {
qDebug() << "Cannot execute SQL query:" << qCritical() << "Cannot execute SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
} }
} }
bool ZfoDb::releaseDb(uint releaseSpace) bool ZfoDb::releaseDb(unsigned int releaseSpace)
{ {
QSqlQuery query(m_db); QSqlQuery query(m_db);
uint releasedBytes = 0; unsigned int releasedBytes = 0;
QString queryStr = "SELECT dmId, testEnv, size, lastAccessTime " QString queryStr = "SELECT dmId, testEnv, size, lastAccessTime "
"FROM message_zfos ORDER BY lastAccessTime ASC"; "FROM message_zfos ORDER BY lastAccessTime ASC";
if (!query.prepare(queryStr)) { if (!query.prepare(queryStr)) {
qDebug() << "Cannot prepare SQL query:" << qCritical() << "Cannot prepare SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
goto fail; goto fail;
} }
...@@ -497,7 +493,7 @@ bool ZfoDb::releaseDb(uint releaseSpace) ...@@ -497,7 +493,7 @@ bool ZfoDb::releaseDb(uint releaseSpace)
query.next(); query.next();
} }
} else { } else {
qDebug() << "Cannot execute SQL query:" << qCritical() << "Cannot execute SQL query:" <<
query.lastError().text().toUtf8().constData(); query.lastError().text().toUtf8().constData();
goto fail; goto fail;
} }
......
...@@ -127,10 +127,11 @@ public: ...@@ -127,10 +127,11 @@ public:
* @param[in] isTestAccount True if account is ISDS testing. * @param[in] isTestAccount True if account is ISDS testing.
* @param[in] zfoSize Real size of zfo. * @param[in] zfoSize Real size of zfo.
* @param[in] zfoBase64Data Zfo data in base64. * @param[in] zfoBase64Data Zfo data in base64.
* @param[in] sizeLimit Zfo database size limit in bytes.
* @return true if success. * @return true if success.
*/ */
bool insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize, bool insertZfoToDb(qint64 msgId, bool isTestAccount, int zfoSize,
const QByteArray &zfoBase64Data); const QByteArray &zfoBase64Data, unsigned int sizeLimit);
/*! /*!
* @brief Test if is zfo database size exceeded. * @brief Test if is zfo database size exceeded.
...@@ -138,7 +139,7 @@ public: ...@@ -138,7 +139,7 @@ public:
* @param[in] currentLimit Current database size limit. * @param[in] currentLimit Current database size limit.
* @return True if db size is exceeded. * @return True if db size is exceeded.
*/ */
bool isDbSizeExceeded(uint currentLimit); bool isDbSizeExceeded(unsigned int currentLimit);
/*! /*!
* @brief Release db - delete oldest zfo files and vacuum db. * @brief Release db - delete oldest zfo files and vacuum db.
...@@ -146,7 +147,7 @@ public: ...@@ -146,7 +147,7 @@ public:
* @param[in] releaseSpace How many bytes should be released. * @param[in] releaseSpace How many bytes should be released.
* @return True true if success. * @return True true if success.
*/ */
bool releaseDb(uint releaseSpace); bool releaseDb(unsigned int releaseSpace);
/*! /*!
* @brief Update Zfo last access time. * @brief Update Zfo last access time.
......
...@@ -33,7 +33,7 @@ int Zfo::getZfoSizeFromDb(qint64 msgId, bool isTestAccount) ...@@ -33,7 +33,7 @@ int Zfo::getZfoSizeFromDb(qint64 msgId, bool isTestAccount)
return globZfoDbPtr->getZfoSizeFromDb(msgId, isTestAccount); return globZfoDbPtr->getZfoSizeFromDb(msgId, isTestAccount);
} }
void Zfo::reduceZfoDbSize(uint releaseSpaceInMB) void Zfo::reduceZfoDbSize(unsigned int releaseSpaceInMB)
{ {
// convert MBs from QML to bytes for database resize operation // convert MBs from QML to bytes for database resize operation
globZfoDbPtr->releaseDb(releaseSpaceInMB * 1000000); globZfoDbPtr->releaseDb(releaseSpaceInMB * 1000000);
......
...@@ -48,7 +48,7 @@ public: ...@@ -48,7 +48,7 @@ public:
* @param[in] isTestAccount True if account is in the ISDS testing environment. * @param[in] isTestAccount True if account is in the ISDS testing environment.
* @return Size of zfo file in bytes. * @return Size of zfo file in bytes.
*/ */
Q_INVOKABLE Q_INVOKABLE static
int getZfoSizeFromDb(qint64 msgId, bool isTestAccount); int getZfoSizeFromDb(qint64 msgId, bool isTestAccount);
/*! /*!
...@@ -56,8 +56,8 @@ public: ...@@ -56,8 +56,8 @@ public:
* *
* @param[in] releaseSpaceInMB How many mega bytes should be released. * @param[in] releaseSpaceInMB How many mega bytes should be released.
*/ */
Q_INVOKABLE Q_INVOKABLE static
void reduceZfoDbSize(uint releaseSpaceInMB); void reduceZfoDbSize(unsigned int releaseSpaceInMB);
}; };
#endif // ZFO_H #endif // ZFO_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment