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