lmdbal/src/storage.cpp

124 lines
3.5 KiB
C++
Raw Normal View History

/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 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/>.
*/
2023-03-20 15:37:13 +00:00
#include "storage.h"
2023-03-21 11:05:54 +00:00
LMDBAL::iStorage::iStorage(const std::string& p_name, Base* parent):
2023-03-20 15:37:13 +00:00
dbi(),
db(parent),
name(p_name)
{}
2023-03-21 11:05:54 +00:00
LMDBAL::iStorage::~iStorage() {}
2023-03-20 15:37:13 +00:00
2023-03-21 11:05:54 +00:00
void LMDBAL::iStorage::drop() {
2023-03-20 15:37:13 +00:00
ensureOpened(dropMethodName);
MDB_txn *txn;
int rc = mdb_txn_begin(db->environment, NULL, 0, &txn);
if (rc) {
mdb_txn_abort(txn);
throw Unknown(db->name, mdb_strerror(rc), name);
}
rc = drop(txn);
if (rc) {
mdb_txn_abort(txn);
throw Unknown(db->name, mdb_strerror(rc), name);
}
mdb_txn_commit(txn);
}
2023-03-21 11:05:54 +00:00
int LMDBAL::iStorage::drop(MDB_txn* transaction) {
2023-03-20 15:37:13 +00:00
return mdb_drop(transaction, dbi, 0);
}
2023-03-21 11:05:54 +00:00
const std::string & LMDBAL::iStorage::dbName() const {
2023-03-20 15:37:13 +00:00
return db->name;}
2023-03-21 11:05:54 +00:00
bool LMDBAL::iStorage::isDBOpened() const {
2023-03-20 15:37:13 +00:00
return db->opened;}
2023-03-21 11:05:54 +00:00
void LMDBAL::iStorage::ensureOpened(const std::string& methodName) const {
2023-03-20 15:37:13 +00:00
if (!db->opened)
throw Closed(methodName, db->name, name);
}
2023-03-21 11:05:54 +00:00
uint32_t LMDBAL::iStorage::count() const {
2023-03-20 15:37:13 +00:00
ensureOpened(countMethodName);
MDB_txn *txn;
MDB_stat stat;
int rc = mdb_txn_begin(db->environment, NULL, MDB_RDONLY, &txn);
if (rc) {
mdb_txn_abort(txn);
throw Unknown(db->name, mdb_strerror(rc), name);
}
rc = mdb_stat(txn, dbi, &stat);
if (rc) {
mdb_txn_abort(txn);
throw Unknown(db->name, mdb_strerror(rc), name);
}
uint32_t amount = stat.ms_entries;
mdb_txn_abort(txn);
return amount;
}
2023-03-21 11:05:54 +00:00
void LMDBAL::iStorage::throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const {
2023-03-20 15:37:13 +00:00
abortTransaction(txn);
if (rc == MDB_KEYEXIST)
throwDuplicate(key);
else
throwUnknown(rc);
}
2023-03-21 11:05:54 +00:00
void LMDBAL::iStorage::throwNotFoundOrUnknown(int rc, LMDBAL::TransactionID txn, const std::string& key) const {
2023-03-20 15:37:13 +00:00
abortTransaction(txn);
if (rc == MDB_NOTFOUND)
throwNotFound(key);
else
throwUnknown(rc);
}
2023-03-21 11:05:54 +00:00
void LMDBAL::iStorage::throwUnknown(int rc, LMDBAL::TransactionID txn) const {
2023-03-20 15:37:13 +00:00
abortTransaction(txn);
throwUnknown(rc);
}
2023-03-21 11:05:54 +00:00
void LMDBAL::iStorage::throwUnknown(int rc) const {
2023-03-20 15:37:13 +00:00
throw Unknown(db->name, mdb_strerror(rc), name);}
2023-03-21 11:05:54 +00:00
void LMDBAL::iStorage::throwDuplicate(const std::string& key) const {
2023-03-20 15:37:13 +00:00
throw Exist(key, db->name, name);}
2023-03-21 11:05:54 +00:00
void LMDBAL::iStorage::throwNotFound(const std::string& key) const {
2023-03-20 15:37:13 +00:00
throw NotFound(key, db->name, name);}
2023-03-21 11:05:54 +00:00
LMDBAL::TransactionID LMDBAL::iStorage::beginReadOnlyTransaction() const {
2023-03-20 15:37:13 +00:00
return db->beginReadOnlyTransaction(name);}
2023-03-21 11:05:54 +00:00
LMDBAL::TransactionID LMDBAL::iStorage::beginTransaction() const {
2023-03-20 15:37:13 +00:00
return db->beginTransaction(name);}
2023-03-21 11:05:54 +00:00
void LMDBAL::iStorage::abortTransaction(LMDBAL::TransactionID id) const {
2023-03-20 15:37:13 +00:00
db->abortTransaction(id);}
2023-03-21 11:05:54 +00:00
void LMDBAL::iStorage::commitTransaction(LMDBAL::TransactionID id) const {
2023-03-20 15:37:13 +00:00
db->commitTransaction(id);}