2019-10-22 17:59:56 +03:00
|
|
|
#include "sqldbcache.h"
|
|
|
|
#include "quasarapp.h"
|
2019-10-26 20:53:47 +03:00
|
|
|
#include "sqldbwriter.h"
|
2019-10-23 20:32:13 +03:00
|
|
|
|
2019-10-22 17:59:56 +03:00
|
|
|
#include <clientprotocol.h>
|
2019-10-27 12:35:23 +03:00
|
|
|
#include <dbobject.h>
|
2019-10-22 17:59:56 +03:00
|
|
|
|
|
|
|
#include <QDateTime>
|
2019-10-26 20:53:47 +03:00
|
|
|
#include <QtConcurrent/QtConcurrent>
|
2019-10-23 20:32:13 +03:00
|
|
|
|
2019-10-22 17:59:56 +03:00
|
|
|
|
2019-10-26 20:53:47 +03:00
|
|
|
namespace ClientProtocol {
|
2019-10-22 17:59:56 +03:00
|
|
|
|
|
|
|
void SqlDBCache::globalUpdateDataBasePrivate(qint64 currentTime) {
|
2019-10-28 21:44:41 +03:00
|
|
|
|
|
|
|
QMutexLocker lock(&_saveLaterMutex);
|
|
|
|
|
|
|
|
for (auto listIt = _needToSaveCache.begin(); listIt != _needToSaveCache.end(); ++listIt ) {
|
|
|
|
|
|
|
|
auto list = listIt.value();
|
|
|
|
for (int id: list) {
|
|
|
|
|
|
|
|
auto saveObject = _cache.value(listIt.key()).value(id);
|
|
|
|
|
|
|
|
if (!saveObject.isNull() && !_writer.isNull() && _writer->isValid()) {
|
|
|
|
|
|
|
|
if (!saveObject->isValid()) {
|
|
|
|
deleteFromCache(listIt.key(), id);
|
|
|
|
|
|
|
|
QuasarAppUtils::Params::verboseLog("writeUpdateItemIntoDB failed when"
|
|
|
|
" db object is not valid! id=" + QString::number(id),
|
|
|
|
QuasarAppUtils::VerboseLvl::Error);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_writer->saveObject(saveObject)) {
|
|
|
|
QuasarAppUtils::Params::verboseLog("writeUpdateItemIntoDB failed when"
|
|
|
|
" work globalUpdateDataRelease!!! id=" +
|
|
|
|
QString::number(id),
|
|
|
|
QuasarAppUtils::VerboseLvl::Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
QuasarAppUtils::Params::verboseLog("writeUpdateItemIntoDB failed when"
|
|
|
|
" db writer is npt inited! ",
|
|
|
|
QuasarAppUtils::VerboseLvl::Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastUpdateTime = currentTime;
|
2019-10-22 17:59:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SqlDBCache::globalUpdateDataBase(SqlDBCasheWriteMode mode) {
|
|
|
|
qint64 currentTime = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
|
|
|
|
if (currentTime - lastUpdateTime > updateInterval ||
|
|
|
|
static_cast<bool>(mode & SqlDBCasheWriteMode::Force)) {
|
|
|
|
|
|
|
|
if (static_cast<bool>(mode & SqlDBCasheWriteMode::On_New_Thread)) {
|
|
|
|
|
|
|
|
QtConcurrent::run([currentTime, this](){
|
|
|
|
globalUpdateDataBasePrivate(currentTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
globalUpdateDataBasePrivate(currentTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SqlDBCache::SqlDBCache(qint64 updateInterval) {
|
|
|
|
lastUpdateTime = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
this->updateInterval = updateInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
SqlDBCache::~SqlDBCache() {
|
|
|
|
globalUpdateDataBase(SqlDBCasheWriteMode::Force);
|
|
|
|
}
|
|
|
|
|
2019-10-28 21:44:41 +03:00
|
|
|
QWeakPointer<SqlDBWriter> SqlDBCache::writer() const {
|
2019-10-23 20:32:13 +03:00
|
|
|
return _writer;
|
|
|
|
}
|
|
|
|
|
2019-10-28 21:44:41 +03:00
|
|
|
void SqlDBCache::setWriter(QWeakPointer<SqlDBWriter> writer) {
|
2019-10-23 20:32:13 +03:00
|
|
|
_writer = writer;
|
|
|
|
}
|
|
|
|
|
2019-10-28 21:44:41 +03:00
|
|
|
bool SqlDBCache::getObject(const QString &table, int id, QWeakPointer<DBObject> *result) {
|
2019-10-27 12:08:16 +03:00
|
|
|
|
2019-10-28 21:44:41 +03:00
|
|
|
if (!result) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result->toStrongRef();
|
|
|
|
|
|
|
|
if (ptr.isNull())
|
2019-10-27 12:35:23 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
auto& tableObj = _cache[table];
|
|
|
|
|
2019-10-28 21:44:41 +03:00
|
|
|
if (!tableObj.contains(id) && !_writer.isNull() && _writer->isValid()) {
|
2019-10-28 18:16:35 +03:00
|
|
|
return _writer->getObject(table, id, result);
|
2019-10-27 12:35:23 +03:00
|
|
|
}
|
|
|
|
|
2019-10-28 21:44:41 +03:00
|
|
|
auto &sptr = tableObj[id];
|
2019-10-28 18:16:35 +03:00
|
|
|
|
2019-10-28 21:44:41 +03:00
|
|
|
if (!sptr->isValid()) {
|
2019-10-28 18:16:35 +03:00
|
|
|
deleteFromCache(table, id);
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-27 12:35:23 +03:00
|
|
|
|
2019-10-28 21:44:41 +03:00
|
|
|
*result = sptr;
|
2019-10-27 12:35:23 +03:00
|
|
|
return true;
|
2019-10-27 12:08:16 +03:00
|
|
|
}
|
|
|
|
|
2019-10-28 21:44:41 +03:00
|
|
|
bool SqlDBCache::saveObject(QWeakPointer<DBObject> saveObject) {
|
|
|
|
|
|
|
|
auto ptr = saveObject.toStrongRef();
|
|
|
|
|
|
|
|
if (!(ptr.isNull() && ptr->isValid())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_cache[ptr->getTableStruct().name][ptr->getId()] = saveObject;
|
|
|
|
|
|
|
|
if (getMode() == SqlDBCasheWriteMode::Force) {
|
|
|
|
if (!_writer.isNull() && _writer->isValid()) {
|
|
|
|
return _writer->saveObject(saveObject);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_needToSaveCache[ptr->getTableStruct().name].push_back(ptr->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
2019-10-27 12:08:16 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SqlDBCache::deleteObject(const QString &table, int id) {
|
2019-10-28 18:16:35 +03:00
|
|
|
deleteFromCache(table, id);
|
|
|
|
|
|
|
|
if (_writer && _writer->isValid()) {
|
|
|
|
return _writer->deleteObject(table, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2019-10-27 12:08:16 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-26 20:53:47 +03:00
|
|
|
bool SqlDBCache::init(const QString &initDbParams) {
|
2019-10-22 17:59:56 +03:00
|
|
|
|
2019-10-28 21:44:41 +03:00
|
|
|
if (_writer.isNull()) {
|
2019-10-22 17:59:56 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-28 21:55:48 +03:00
|
|
|
return _writer->initDb(initDbParams);
|
2019-10-22 17:59:56 +03:00
|
|
|
}
|
2019-10-23 20:32:13 +03:00
|
|
|
|
2019-10-28 18:16:35 +03:00
|
|
|
void SqlDBCache::deleteFromCache(const QString &table, int id) {
|
|
|
|
auto& tableObj = _cache[table];
|
|
|
|
tableObj.remove(id);
|
|
|
|
|
|
|
|
if (tableObj.isEmpty()) {
|
|
|
|
_cache.remove(table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 21:44:41 +03:00
|
|
|
SqlDBCasheWriteMode SqlDBCache::getMode() const {
|
|
|
|
return _mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SqlDBCache::setMode(const SqlDBCasheWriteMode &mode) {
|
|
|
|
_mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 SqlDBCache::getUpdateInterval() const {
|
|
|
|
return updateInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SqlDBCache::setUpdateInterval(const qint64 &value) {
|
|
|
|
updateInterval = value;
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:32:13 +03:00
|
|
|
}
|