1
0
forked from blue/lmdbal
lmdbal/table.hpp
2022-09-05 23:25:39 +03:00

54 lines
1.6 KiB
C++

// 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"
template<typename K, typename V>
void Core::DataBase::Table<K, V>::addRecord(const K& key, const V& value) {
if (!db->opened) {
throw Closed("addRecord", db->name);
}
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
ds << value;
MDB_val lmdbKey, lmdbData;
lmdbKey << key;
lmdbData.mv_size = ba.size();
lmdbData.mv_data = (uint8_t*)ba.data();
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) {
throw Exist(db->name, std::to_string(key));
} else {
throw Unknown(db->name, mdb_strerror(rc));
}
} else {
mdb_txn_commit(txn);
}
}
#endif //CORE_TABLE_HPP