lmdbal/table.cpp

70 lines
1.8 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/>.
#include "table.h"
2022-09-14 22:18:31 +00:00
DataBase::_Table::_Table(const std::string& p_name, DataBase* parent):
2022-09-05 20:25:39 +00:00
dbi(),
2022-09-09 17:15:40 +00:00
db(parent),
name(p_name)
2022-09-05 20:25:39 +00:00
{
}
2022-09-14 22:18:31 +00:00
DataBase::_Table::~_Table() {}
void DataBase::_Table::drop()
{
if (!db->opened) {
throw Closed("drop", db->name, name);
}
MDB_txn *txn;
int rc = mdb_txn_begin(db->environment, NULL, 0, &txn);
if (rc) {
throw Unknown(db->name, mdb_strerror(rc), name);
}
rc = drop(txn);
if (rc) {
throw Unknown(db->name, mdb_strerror(rc), name);
}
mdb_txn_commit(txn);
}
int DataBase::_Table::drop(MDB_txn* transaction)
{
return mdb_drop(transaction, dbi, 0);
}
uint32_t DataBase::_Table::count() const
{
if (!db->opened) {
throw Closed("count", db->name, name);
}
MDB_txn *txn;
MDB_stat stat;
int rc = mdb_txn_begin(db->environment, NULL, 0, &txn);
rc = mdb_stat(txn, dbi, &stat);
if (rc) {
throw Unknown(db->name, mdb_strerror(rc), name);
}
uint32_t amount = stat.ms_entries;
mdb_txn_abort(txn);
return amount;
}