New object for cached database, also ClientInfo class

This commit is contained in:
Blue 2022-08-20 00:28:59 +03:00
parent d162494ec8
commit 2ae75a4b91
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
10 changed files with 287 additions and 19 deletions

View File

@ -93,6 +93,8 @@ Account::Account(const QString& p_login, const QString& p_server, const QString&
QObject::connect(reconnectTimer, &QTimer::timeout, this, &Account::onReconnectTimer); QObject::connect(reconnectTimer, &QTimer::timeout, this, &Account::onReconnectTimer);
if (name == "Test") { if (name == "Test") {
qDebug() << "Presence capabilities: " << presence.capabilityNode();
QXmppLogger* logger = new QXmppLogger(this); QXmppLogger* logger = new QXmppLogger(this);
logger->setLoggingType(QXmppLogger::SignalLogging); logger->setLoggingType(QXmppLogger::SignalLogging);
client.setLogger(logger); client.setLogger(logger);

View File

@ -31,8 +31,10 @@
#include "shared/enums.h" #include "shared/enums.h"
#include "shared/message.h" #include "shared/message.h"
#include "shared/global.h" #include "shared/global.h"
#include "shared/clientinfo.h"
#include "networkaccess.h" #include "networkaccess.h"
#include "external/simpleCrypt/simplecrypt.h" #include "external/simpleCrypt/simplecrypt.h"
#include <core/storage/cache.h>
#ifdef WITH_KWALLET #ifdef WITH_KWALLET
#include "passwordStorageEngines/kwallet.h" #include "passwordStorageEngines/kwallet.h"
@ -135,6 +137,7 @@ private:
Shared::Availability state; Shared::Availability state;
NetworkAccess network; NetworkAccess network;
bool isInitialized; bool isInitialized;
//Cache<Shared::ClientInfo> clientCache;
#ifdef WITH_KWALLET #ifdef WITH_KWALLET
PSE::KWallet kwallet; PSE::KWallet kwallet;

View File

@ -5,4 +5,6 @@ target_sources(squawk PRIVATE
storage.h storage.h
urlstorage.cpp urlstorage.cpp
urlstorage.h urlstorage.h
cache.cpp
cache.h
) )

69
core/storage/cache.cpp Normal file
View File

@ -0,0 +1,69 @@
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "cache.h"
#include <shared/clientinfo.h>
template class Core::Cache<Shared::ClientInfo>;
template <class T>
Core::Cache<T>::Cache(const QString& name):
storage(name),
cache(new std::map<QString, T> ()) {}
template <class T>
Core::Cache<T>::~Cache() {
close();
delete cache;
}
template <class T>
void Core::Cache<T>::open() {
storage.open();}
template <class T>
void Core::Cache<T>::close() {
storage.close();}
template <class T>
void Core::Cache<T>::addRecord(const QString& key, const T& value) {
storage.addRecord(key, value);
cache->insert(std::make_pair(key, value));
}
template <class T>
T Core::Cache<T>::getRecord(const QString& key) const {
typename std::map<QString, T>::const_iterator itr = cache->find(key);
if (itr == cache->end()) {
T value = storage.getRecord(key);
itr = cache->insert(std::make_pair(key, value)).first;
}
return itr->second;
}
template<typename T>
void Core::Cache<T>::changeRecord(const QString& key, const T& value) {
storage.changeRecord(key, value);
cache->at(key) = value;
}
template<typename T>
void Core::Cache<T>::removeRecord(const QString& key) {
storage.removeRecord(key);
cache->erase(key);
}

50
core/storage/cache.h Normal file
View File

@ -0,0 +1,50 @@
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef CORE_CACHE_H
#define CORE_CACHE_H
#include <map>
#include <QString>
#include <core/storage/storage.h>
namespace Core {
template <class T>
class Cache
{
public:
Cache(const QString& name);
~Cache();
void open();
void close();
void addRecord(const QString& key, const T& value);
void changeRecord(const QString& key, const T& value);
void removeRecord(const QString& key);
T getRecord(const QString& key) const;
private:
Core::Storage<T> storage;
std::map<QString, T>* cache;
};
}
#endif // CORE_CACHE_H

View File

@ -21,7 +21,12 @@
#include "storage.h" #include "storage.h"
Core::Storage::Storage(const QString& p_name): #include <shared/clientinfo.h>
template class Core::Storage<Shared::ClientInfo>;
template <class T>
Core::Storage<T>::Storage(const QString& p_name):
name(p_name), name(p_name),
opened(false), opened(false),
environment(), environment(),
@ -29,12 +34,14 @@ Core::Storage::Storage(const QString& p_name):
{ {
} }
Core::Storage::~Storage() template <class T>
Core::Storage<T>::~Storage()
{ {
close(); close();
} }
void Core::Storage::open() template <class T>
void Core::Storage<T>::open()
{ {
if (!opened) { if (!opened) {
mdb_env_create(&environment); mdb_env_create(&environment);
@ -61,7 +68,8 @@ void Core::Storage::open()
} }
} }
void Core::Storage::close() template <class T>
void Core::Storage<T>::close()
{ {
if (opened) { if (opened) {
mdb_dbi_close(environment, base); mdb_dbi_close(environment, base);
@ -70,19 +78,22 @@ void Core::Storage::close()
} }
} }
void Core::Storage::addRecord(const QString& key, const QString& value) template <class T>
void Core::Storage<T>::addRecord(const QString& key, const T& value)
{ {
if (!opened) { if (!opened) {
throw Archive::Closed("addRecord", name.toStdString()); throw Archive::Closed("addRecord", name.toStdString());
} }
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
const std::string& id = key.toStdString(); const std::string& id = key.toStdString();
const std::string& val = value.toStdString(); ds << value;
MDB_val lmdbKey, lmdbData; MDB_val lmdbKey, lmdbData;
lmdbKey.mv_size = id.size(); lmdbKey.mv_size = id.size();
lmdbKey.mv_data = (char*)id.c_str(); lmdbKey.mv_data = (char*)id.c_str();
lmdbData.mv_size = val.size(); lmdbData.mv_size = ba.size();
lmdbData.mv_data = (char*)val.c_str(); lmdbData.mv_data = (uint8_t*)ba.data();
MDB_txn *txn; MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn); mdb_txn_begin(environment, NULL, 0, &txn);
int rc; int rc;
@ -99,19 +110,23 @@ void Core::Storage::addRecord(const QString& key, const QString& value)
} }
} }
void Core::Storage::changeRecord(const QString& key, const QString& value) template <class T>
void Core::Storage<T>::changeRecord(const QString& key, const T& value)
{ {
if (!opened) { if (!opened) {
throw Archive::Closed("changeRecord", name.toStdString()); throw Archive::Closed("changeRecord", name.toStdString());
} }
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
const std::string& id = key.toStdString(); const std::string& id = key.toStdString();
const std::string& val = value.toStdString(); ds << value;
MDB_val lmdbKey, lmdbData; MDB_val lmdbKey, lmdbData;
lmdbKey.mv_size = id.size(); lmdbKey.mv_size = id.size();
lmdbKey.mv_data = (char*)id.c_str(); lmdbKey.mv_data = (char*)id.c_str();
lmdbData.mv_size = val.size(); lmdbData.mv_size = ba.size();
lmdbData.mv_data = (char*)val.c_str(); lmdbData.mv_data = (uint8_t*)ba.data();
MDB_txn *txn; MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn); mdb_txn_begin(environment, NULL, 0, &txn);
int rc; int rc;
@ -126,7 +141,8 @@ void Core::Storage::changeRecord(const QString& key, const QString& value)
} }
} }
QString Core::Storage::getRecord(const QString& key) const template <class T>
T Core::Storage<T>::getRecord(const QString& key) const
{ {
if (!opened) { if (!opened) {
throw Archive::Closed("addElement", name.toStdString()); throw Archive::Closed("addElement", name.toStdString());
@ -149,14 +165,18 @@ QString Core::Storage::getRecord(const QString& key) const
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc)); throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
} }
} else { } else {
std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size); QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);
QString value(sId.c_str()); QDataStream ds(&ba, QIODevice::ReadOnly);
T value;
ds >> value;
mdb_txn_abort(txn); mdb_txn_abort(txn);
return value; return value;
} }
} }
void Core::Storage::removeRecord(const QString& key) template <class T>
void Core::Storage<T>::removeRecord(const QString& key)
{ {
if (!opened) { if (!opened) {
throw Archive::Closed("addElement", name.toStdString()); throw Archive::Closed("addElement", name.toStdString());

View File

@ -29,6 +29,7 @@ namespace Core {
/** /**
* @todo write docs * @todo write docs
*/ */
template <class T>
class Storage class Storage
{ {
public: public:
@ -38,10 +39,10 @@ public:
void open(); void open();
void close(); void close();
void addRecord(const QString& key, const QString& value); void addRecord(const QString& key, const T& value);
void changeRecord(const QString& key, const QString& value); void changeRecord(const QString& key, const T& value);
void removeRecord(const QString& key); void removeRecord(const QString& key);
QString getRecord(const QString& key) const; T getRecord(const QString& key) const;
private: private:

View File

@ -18,4 +18,6 @@ target_sources(squawk PRIVATE
vcard.h vcard.h
pathcheck.cpp pathcheck.cpp
pathcheck.h pathcheck.h
clientinfo.h
clientinfo.cpp
) )

69
shared/clientinfo.cpp Normal file
View File

@ -0,0 +1,69 @@
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "clientinfo.h"
Shared::ClientInfo::ClientInfo():
name(),
category(),
type(),
capabilitiesNode(),
capabilitiesVerification(),
capabilitiesHash(),
capabilitiesExtensions() {}
QDataStream & Shared::ClientInfo::operator >> (QDataStream& stream) const {
stream << name;
stream << category;
stream << type;
stream << capabilitiesNode;
stream << capabilitiesVerification;
stream << capabilitiesHash;
stream << (quint8)capabilitiesExtensions.size();
for (const QString& ext : capabilitiesExtensions) {
stream << ext;
}
return stream;
}
QDataStream & Shared::ClientInfo::operator << (QDataStream& stream) {
stream >> name;
stream >> category;
stream >> type;
stream >> capabilitiesNode;
stream >> capabilitiesVerification;
stream >> capabilitiesHash;
quint8 size;
stream >> size;
for (quint8 i = 0; i < size; ++i) {
QString ext;
stream >> ext;
capabilitiesExtensions.insert(ext);
}
return stream;
}
QDataStream& operator<< (QDataStream& stream, const Shared::ClientInfo& image) {
image >> stream;
return stream;
}
QDataStream& operator>> (QDataStream& stream, Shared::ClientInfo& image) {
image << stream;
return stream;
}

50
shared/clientinfo.h Normal file
View File

@ -0,0 +1,50 @@
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef SHARED_CLIENTINFO_H
#define SHARED_CLIENTINFO_H
#include <set>
#include <QDataStream>
#include <QString>
namespace Shared {
class ClientInfo
{
public:
ClientInfo();
QDataStream& operator << (QDataStream& stream);
QDataStream& operator >> (QDataStream& stream) const;
public:
QString name;
QString category;
QString type;
QString capabilitiesNode;
QString capabilitiesVerification;
QString capabilitiesHash;
std::set<QString> capabilitiesExtensions;
};
}
QDataStream& operator<< (QDataStream& stream, const Shared::ClientInfo& image);
QDataStream& operator>> (QDataStream& stream, Shared::ClientInfo& image);
#endif // SHARED_CLIENTINFO_H