forked from blue/squawk
BUILD FAILS! some ideas of storage and cache
This commit is contained in:
parent
87973b3b67
commit
820dc845ea
10 changed files with 198 additions and 89 deletions
|
@ -22,9 +22,10 @@
|
|||
#include <QDir>
|
||||
|
||||
#include "storage.h"
|
||||
#include <cstring>
|
||||
|
||||
template <class T>
|
||||
Core::Storage<T>::Storage(const QString& p_name):
|
||||
template <class K, class V>
|
||||
Core::Storage<K, V>::Storage(const QString& p_name):
|
||||
name(p_name),
|
||||
opened(false),
|
||||
environment(),
|
||||
|
@ -32,14 +33,14 @@ Core::Storage<T>::Storage(const QString& p_name):
|
|||
{
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Core::Storage<T>::~Storage()
|
||||
template <class K, class V>
|
||||
Core::Storage<K, V>::~Storage()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Core::Storage<T>::open()
|
||||
template <class K, class V>
|
||||
void Core::Storage<K, V>::open()
|
||||
{
|
||||
if (!opened) {
|
||||
mdb_env_create(&environment);
|
||||
|
@ -66,8 +67,8 @@ void Core::Storage<T>::open()
|
|||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Core::Storage<T>::close()
|
||||
template <class K, class V>
|
||||
void Core::Storage<K, V>::close()
|
||||
{
|
||||
if (opened) {
|
||||
mdb_dbi_close(environment, base);
|
||||
|
@ -76,20 +77,19 @@ void Core::Storage<T>::close()
|
|||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Core::Storage<T>::addRecord(const QString& key, const T& value)
|
||||
template <class K, class V>
|
||||
void Core::Storage<K, V>::addRecord(const K& key, const V& value)
|
||||
{
|
||||
if (!opened) {
|
||||
throw Archive::Closed("addRecord", name.toStdString());
|
||||
}
|
||||
QByteArray ba;
|
||||
QDataStream ds(&ba, QIODevice::WriteOnly);
|
||||
const std::string& id = key.toStdString();
|
||||
ds << value;
|
||||
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
lmdbKey.mv_size = id.size();
|
||||
lmdbKey.mv_data = (char*)id.c_str();
|
||||
lmdbKey << key;
|
||||
|
||||
lmdbData.mv_size = ba.size();
|
||||
lmdbData.mv_data = (uint8_t*)ba.data();
|
||||
MDB_txn *txn;
|
||||
|
@ -99,7 +99,7 @@ void Core::Storage<T>::addRecord(const QString& key, const T& value)
|
|||
if (rc != 0) {
|
||||
mdb_txn_abort(txn);
|
||||
if (rc == MDB_KEYEXIST) {
|
||||
throw Archive::Exist(name.toStdString(), id);
|
||||
throw Archive::Exist(name.toStdString(), std::to_string(key));
|
||||
} else {
|
||||
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
|
||||
}
|
||||
|
@ -108,8 +108,8 @@ void Core::Storage<T>::addRecord(const QString& key, const T& value)
|
|||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Core::Storage<T>::changeRecord(const QString& key, const T& value)
|
||||
template <class K, class V>
|
||||
void Core::Storage<K, V>::changeRecord(const K& key, const V& value)
|
||||
{
|
||||
if (!opened) {
|
||||
throw Archive::Closed("changeRecord", name.toStdString());
|
||||
|
@ -117,12 +117,10 @@ void Core::Storage<T>::changeRecord(const QString& key, const T& value)
|
|||
|
||||
QByteArray ba;
|
||||
QDataStream ds(&ba, QIODevice::WriteOnly);
|
||||
const std::string& id = key.toStdString();
|
||||
ds << value;
|
||||
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
lmdbKey.mv_size = id.size();
|
||||
lmdbKey.mv_data = (char*)id.c_str();
|
||||
lmdbKey << key;
|
||||
lmdbData.mv_size = ba.size();
|
||||
lmdbData.mv_data = (uint8_t*)ba.data();
|
||||
MDB_txn *txn;
|
||||
|
@ -139,17 +137,15 @@ void Core::Storage<T>::changeRecord(const QString& key, const T& value)
|
|||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T Core::Storage<T>::getRecord(const QString& key) const
|
||||
template <class K, class V>
|
||||
V Core::Storage<K, V>::getRecord(const K& key) const
|
||||
{
|
||||
if (!opened) {
|
||||
throw Archive::Closed("addElement", name.toStdString());
|
||||
}
|
||||
const std::string& id = key.toStdString();
|
||||
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
lmdbKey.mv_size = id.size();
|
||||
lmdbKey.mv_data = (char*)id.c_str();
|
||||
lmdbKey << key;
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc;
|
||||
|
@ -158,14 +154,14 @@ T Core::Storage<T>::getRecord(const QString& key) const
|
|||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
if (rc == MDB_NOTFOUND) {
|
||||
throw Archive::NotFound(id, name.toStdString());
|
||||
throw Archive::NotFound(std::to_string(key), name.toStdString());
|
||||
} else {
|
||||
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
|
||||
}
|
||||
} else {
|
||||
QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);
|
||||
QDataStream ds(&ba, QIODevice::ReadOnly);
|
||||
T value;
|
||||
V value;
|
||||
ds >> value;
|
||||
mdb_txn_abort(txn);
|
||||
|
||||
|
@ -173,17 +169,15 @@ T Core::Storage<T>::getRecord(const QString& key) const
|
|||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Core::Storage<T>::removeRecord(const QString& key)
|
||||
template <class K, class V>
|
||||
void Core::Storage<K, V>::removeRecord(const K& key)
|
||||
{
|
||||
if (!opened) {
|
||||
throw Archive::Closed("addElement", name.toStdString());
|
||||
}
|
||||
const std::string& id = key.toStdString();
|
||||
|
||||
MDB_val lmdbKey;
|
||||
lmdbKey.mv_size = id.size();
|
||||
lmdbKey.mv_data = (char*)id.c_str();
|
||||
lmdbKey << key;
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc;
|
||||
|
@ -192,7 +186,7 @@ void Core::Storage<T>::removeRecord(const QString& key)
|
|||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
if (rc == MDB_NOTFOUND) {
|
||||
throw Archive::NotFound(id, name.toStdString());
|
||||
throw Archive::NotFound(std::to_string(key), name.toStdString());
|
||||
} else {
|
||||
throw Archive::Unknown(name.toStdString(), mdb_strerror(rc));
|
||||
}
|
||||
|
@ -201,8 +195,32 @@ void Core::Storage<T>::removeRecord(const QString& key)
|
|||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
QString Core::Storage<T>::getName() const {
|
||||
template <class K, class V>
|
||||
QString Core::Storage<K, V>::getName() const {
|
||||
return name;}
|
||||
|
||||
MDB_val& operator << (MDB_val& data, const QString& value) {
|
||||
QByteArray ba = value.toUtf8();
|
||||
data.mv_size = ba.size();
|
||||
data.mv_data = ba.data();
|
||||
return data;
|
||||
}
|
||||
MDB_val& operator >> (MDB_val& data, QString& value) {
|
||||
value = QString::fromUtf8((const char*)data.mv_data, data.mv_size);
|
||||
return data;
|
||||
}
|
||||
|
||||
MDB_val& operator << (MDB_val& data, uint32_t& value) {
|
||||
data.mv_size = 4;
|
||||
data.mv_data = &value;
|
||||
return data;
|
||||
}
|
||||
MDB_val& operator >> (MDB_val& data, uint32_t& value) {
|
||||
std::memcpy(&value, data.mv_data, data.mv_size);
|
||||
return data;
|
||||
}
|
||||
|
||||
std::string std::to_string(const QString& str) {
|
||||
return str.toStdString();
|
||||
}
|
||||
#endif //CORE_STORAGE_HPP
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue