forked from blue/lmdbal
122 lines
3.7 KiB
C++
122 lines
3.7 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/>.
|
||
|
|
||
|
#include "storage.h"
|
||
|
|
||
|
LMDBDataBase::StorageBase::StorageBase(const std::string& p_name, DataBase* parent):
|
||
|
dbi(),
|
||
|
db(parent),
|
||
|
name(p_name)
|
||
|
{}
|
||
|
|
||
|
LMDBDataBase::StorageBase::~StorageBase() {}
|
||
|
|
||
|
void LMDBDataBase::StorageBase::drop() {
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
int LMDBDataBase::StorageBase::drop(MDB_txn* transaction) {
|
||
|
return mdb_drop(transaction, dbi, 0);
|
||
|
}
|
||
|
|
||
|
const std::string & LMDBDataBase::StorageBase::dbName() const {
|
||
|
return db->name;}
|
||
|
|
||
|
bool LMDBDataBase::StorageBase::isDBOpened() const {
|
||
|
return db->opened;}
|
||
|
|
||
|
void LMDBDataBase::StorageBase::ensureOpened(const std::string& methodName) const {
|
||
|
if (!db->opened)
|
||
|
throw Closed(methodName, db->name, name);
|
||
|
}
|
||
|
|
||
|
uint32_t LMDBDataBase::StorageBase::count() const {
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
void LMDBDataBase::StorageBase::throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const {
|
||
|
abortTransaction(txn);
|
||
|
if (rc == MDB_KEYEXIST)
|
||
|
throwDuplicate(key);
|
||
|
else
|
||
|
throwUnknown(rc);
|
||
|
}
|
||
|
|
||
|
void LMDBDataBase::StorageBase::throwNotFoundOrUnknown(int rc, LMDBDataBase::TransactionID txn, const std::string& key) const {
|
||
|
abortTransaction(txn);
|
||
|
if (rc == MDB_NOTFOUND)
|
||
|
throwNotFound(key);
|
||
|
else
|
||
|
throwUnknown(rc);
|
||
|
}
|
||
|
|
||
|
void LMDBDataBase::StorageBase::throwUnknown(int rc, LMDBDataBase::TransactionID txn) const {
|
||
|
abortTransaction(txn);
|
||
|
throwUnknown(rc);
|
||
|
}
|
||
|
|
||
|
void LMDBDataBase::StorageBase::throwUnknown(int rc) const {
|
||
|
throw Unknown(db->name, mdb_strerror(rc), name);}
|
||
|
|
||
|
void LMDBDataBase::StorageBase::throwDuplicate(const std::string& key) const {
|
||
|
throw Exist(key, db->name, name);}
|
||
|
|
||
|
void LMDBDataBase::StorageBase::throwNotFound(const std::string& key) const {
|
||
|
throw NotFound(key, db->name, name);}
|
||
|
|
||
|
LMDBDataBase::TransactionID LMDBDataBase::StorageBase::beginReadOnlyTransaction() const {
|
||
|
return db->beginReadOnlyTransaction(name);}
|
||
|
|
||
|
LMDBDataBase::TransactionID LMDBDataBase::StorageBase::beginTransaction() const {
|
||
|
return db->beginTransaction(name);}
|
||
|
|
||
|
void LMDBDataBase::StorageBase::abortTransaction(LMDBDataBase::TransactionID id) const {
|
||
|
db->abortTransaction(id);}
|
||
|
|
||
|
void LMDBDataBase::StorageBase::commitTransaction(LMDBDataBase::TransactionID id) const {
|
||
|
db->commitTransaction(id);}
|