lmdbal/table.hpp

163 lines
4.3 KiB
C++
Raw Normal View History

2022-09-05 20:25:39 +00:00
// 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_TABLE_HPP
#define CORE_TABLE_HPP
#include "table.h"
2022-09-09 17:15:40 +00:00
#include "exceptions.h"
2022-09-05 20:25:39 +00:00
2022-09-09 17:15:40 +00:00
template<typename K, typename V>
Core::DataBase::Table<K, V>::Table(const std::string& p_name, Core::DataBase* parent):
_Table(p_name, parent)
{
}
template<typename K, typename V>
Core::DataBase::Table<K, V>::~Table() {
}
2022-09-05 20:25:39 +00:00
template<typename K, typename V>
void Core::DataBase::Table<K, V>::addRecord(const K& key, const V& value) {
if (!db->opened) {
2022-09-09 17:15:40 +00:00
throw Closed("addRecord", db->name, name);
2022-09-05 20:25:39 +00:00
}
MDB_val lmdbKey, lmdbData;
lmdbKey << key;
2022-09-09 17:15:40 +00:00
lmdbData << value;
2022-09-05 20:25:39 +00:00
MDB_txn *txn;
mdb_txn_begin(db->environment, NULL, 0, &txn);
int rc;
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE);
if (rc != 0) {
mdb_txn_abort(txn);
if (rc == MDB_KEYEXIST) {
2022-09-09 17:15:40 +00:00
throw Exist(std::to_string(key), db->name, name);
} else {
throw Unknown(db->name, mdb_strerror(rc), name);
}
} else {
mdb_txn_commit(txn);
}
}
template<typename K, typename V>
void Core::DataBase::Table<K, V>::changeRecord(const K& key, const V& value) {
if (!db->opened) {
throw Closed("changeRecord", db->name, name);
}
MDB_val lmdbKey, lmdbData;
lmdbKey << key;
lmdbData << value;
MDB_txn *txn;
mdb_txn_begin(db->environment, NULL, 0, &txn);
int rc;
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0);
if (rc != 0) {
mdb_txn_abort(txn);
if (rc) {
throw Unknown(db->name, mdb_strerror(rc), name);
}
} else {
mdb_txn_commit(txn);
}
}
template<typename K, typename V>
V Core::DataBase::Table<K, V>::getRecord(const K& key) const {
if (!db->opened) {
throw Closed("getRecord", db->name, name);
}
MDB_val lmdbKey, lmdbData;
lmdbKey << key;
MDB_txn *txn;
int rc;
mdb_txn_begin(db->environment, NULL, MDB_RDONLY, &txn);
rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
if (rc) {
mdb_txn_abort(txn);
if (rc == MDB_NOTFOUND) {
throw NotFound(std::to_string(key), db->name, name);
} else {
throw Unknown(db->name, mdb_strerror(rc), name);
}
} else {
V value;
lmdbData >> value;
mdb_txn_abort(txn);
return value;
}
}
template<typename K, typename V>
void Core::DataBase::Table<K, V>::removeRecord(const K& key) {
if (!db->opened) {
throw Closed("removeRecord", db->name, name);
}
MDB_val lmdbKey;
lmdbKey << key;
MDB_txn *txn;
int rc;
mdb_txn_begin(db->environment, NULL, 0, &txn);
rc = mdb_del(txn, dbi, &lmdbKey, NULL);
if (rc) {
mdb_txn_abort(txn);
if (rc == MDB_NOTFOUND) {
throw NotFound(std::to_string(key), db->name, name);
2022-09-05 20:25:39 +00:00
} else {
2022-09-09 17:15:40 +00:00
throw Unknown(db->name, mdb_strerror(rc), name);
2022-09-05 20:25:39 +00:00
}
} else {
mdb_txn_commit(txn);
}
}
2022-09-09 17:15:40 +00:00
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, const 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();
}
2022-09-05 20:25:39 +00:00
#endif //CORE_TABLE_HPP