Use unique_ptrs for leveldb db and iterators

This commit is contained in:
Desour 2023-03-03 14:53:37 +01:00 committed by DS
parent 8445c5fe60
commit d0bcdff5ce
2 changed files with 19 additions and 30 deletions

@ -45,14 +45,11 @@ Database_LevelDB::Database_LevelDB(const std::string &savedir)
{ {
leveldb::Options options; leveldb::Options options;
options.create_if_missing = true; options.create_if_missing = true;
leveldb::DB *db;
leveldb::Status status = leveldb::DB::Open(options, leveldb::Status status = leveldb::DB::Open(options,
savedir + DIR_DELIM + "map.db", &m_database); savedir + DIR_DELIM + "map.db", &db);
ENSURE_STATUS_OK(status); ENSURE_STATUS_OK(status);
} m_database.reset(db);
Database_LevelDB::~Database_LevelDB()
{
delete m_database;
} }
bool Database_LevelDB::saveBlock(const v3s16 &pos, const std::string &data) bool Database_LevelDB::saveBlock(const v3s16 &pos, const std::string &data)
@ -92,26 +89,22 @@ bool Database_LevelDB::deleteBlock(const v3s16 &pos)
void Database_LevelDB::listAllLoadableBlocks(std::vector<v3s16> &dst) void Database_LevelDB::listAllLoadableBlocks(std::vector<v3s16> &dst)
{ {
leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions()); std::unique_ptr<leveldb::Iterator> it(m_database->NewIterator(leveldb::ReadOptions()));
for (it->SeekToFirst(); it->Valid(); it->Next()) { for (it->SeekToFirst(); it->Valid(); it->Next()) {
dst.push_back(getIntegerAsBlock(stoi64(it->key().ToString()))); dst.push_back(getIntegerAsBlock(stoi64(it->key().ToString())));
} }
ENSURE_STATUS_OK(it->status()); // Check for any errors found during the scan ENSURE_STATUS_OK(it->status()); // Check for any errors found during the scan
delete it;
} }
PlayerDatabaseLevelDB::PlayerDatabaseLevelDB(const std::string &savedir) PlayerDatabaseLevelDB::PlayerDatabaseLevelDB(const std::string &savedir)
{ {
leveldb::Options options; leveldb::Options options;
options.create_if_missing = true; options.create_if_missing = true;
leveldb::DB *db;
leveldb::Status status = leveldb::DB::Open(options, leveldb::Status status = leveldb::DB::Open(options,
savedir + DIR_DELIM + "players.db", &m_database); savedir + DIR_DELIM + "players.db", &db);
ENSURE_STATUS_OK(status); ENSURE_STATUS_OK(status);
} m_database.reset(db);
PlayerDatabaseLevelDB::~PlayerDatabaseLevelDB()
{
delete m_database;
} }
void PlayerDatabaseLevelDB::savePlayer(RemotePlayer *player) void PlayerDatabaseLevelDB::savePlayer(RemotePlayer *player)
@ -202,26 +195,22 @@ bool PlayerDatabaseLevelDB::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
void PlayerDatabaseLevelDB::listPlayers(std::vector<std::string> &res) void PlayerDatabaseLevelDB::listPlayers(std::vector<std::string> &res)
{ {
leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions()); std::unique_ptr<leveldb::Iterator> it(m_database->NewIterator(leveldb::ReadOptions()));
res.clear(); res.clear();
for (it->SeekToFirst(); it->Valid(); it->Next()) { for (it->SeekToFirst(); it->Valid(); it->Next()) {
res.push_back(it->key().ToString()); res.push_back(it->key().ToString());
} }
delete it;
} }
AuthDatabaseLevelDB::AuthDatabaseLevelDB(const std::string &savedir) AuthDatabaseLevelDB::AuthDatabaseLevelDB(const std::string &savedir)
{ {
leveldb::Options options; leveldb::Options options;
options.create_if_missing = true; options.create_if_missing = true;
leveldb::DB *db;
leveldb::Status status = leveldb::DB::Open(options, leveldb::Status status = leveldb::DB::Open(options,
savedir + DIR_DELIM + "auth.db", &m_database); savedir + DIR_DELIM + "auth.db", &db);
ENSURE_STATUS_OK(status); ENSURE_STATUS_OK(status);
} m_database.reset(db);
AuthDatabaseLevelDB::~AuthDatabaseLevelDB()
{
delete m_database;
} }
bool AuthDatabaseLevelDB::getAuth(const std::string &name, AuthEntry &res) bool AuthDatabaseLevelDB::getAuth(const std::string &name, AuthEntry &res)
@ -293,12 +282,11 @@ bool AuthDatabaseLevelDB::deleteAuth(const std::string &name)
void AuthDatabaseLevelDB::listNames(std::vector<std::string> &res) void AuthDatabaseLevelDB::listNames(std::vector<std::string> &res)
{ {
leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions()); std::unique_ptr<leveldb::Iterator> it(m_database->NewIterator(leveldb::ReadOptions()));
res.clear(); res.clear();
for (it->SeekToFirst(); it->Valid(); it->Next()) { for (it->SeekToFirst(); it->Valid(); it->Next()) {
res.emplace_back(it->key().ToString()); res.emplace_back(it->key().ToString());
} }
delete it;
} }
void AuthDatabaseLevelDB::reload() void AuthDatabaseLevelDB::reload()

@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#if USE_LEVELDB #if USE_LEVELDB
#include <memory>
#include <string> #include <string>
#include "database.h" #include "database.h"
#include "leveldb/db.h" #include "leveldb/db.h"
@ -31,7 +32,7 @@ class Database_LevelDB : public MapDatabase
{ {
public: public:
Database_LevelDB(const std::string &savedir); Database_LevelDB(const std::string &savedir);
~Database_LevelDB(); ~Database_LevelDB() = default;
bool saveBlock(const v3s16 &pos, const std::string &data); bool saveBlock(const v3s16 &pos, const std::string &data);
void loadBlock(const v3s16 &pos, std::string *block); void loadBlock(const v3s16 &pos, std::string *block);
@ -42,14 +43,14 @@ public:
void endSave() {} void endSave() {}
private: private:
leveldb::DB *m_database; std::unique_ptr<leveldb::DB> m_database;
}; };
class PlayerDatabaseLevelDB : public PlayerDatabase class PlayerDatabaseLevelDB : public PlayerDatabase
{ {
public: public:
PlayerDatabaseLevelDB(const std::string &savedir); PlayerDatabaseLevelDB(const std::string &savedir);
~PlayerDatabaseLevelDB(); ~PlayerDatabaseLevelDB() = default;
void savePlayer(RemotePlayer *player); void savePlayer(RemotePlayer *player);
bool loadPlayer(RemotePlayer *player, PlayerSAO *sao); bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
@ -57,14 +58,14 @@ public:
void listPlayers(std::vector<std::string> &res); void listPlayers(std::vector<std::string> &res);
private: private:
leveldb::DB *m_database; std::unique_ptr<leveldb::DB> m_database;
}; };
class AuthDatabaseLevelDB : public AuthDatabase class AuthDatabaseLevelDB : public AuthDatabase
{ {
public: public:
AuthDatabaseLevelDB(const std::string &savedir); AuthDatabaseLevelDB(const std::string &savedir);
virtual ~AuthDatabaseLevelDB(); virtual ~AuthDatabaseLevelDB() = default;
virtual bool getAuth(const std::string &name, AuthEntry &res); virtual bool getAuth(const std::string &name, AuthEntry &res);
virtual bool saveAuth(const AuthEntry &authEntry); virtual bool saveAuth(const AuthEntry &authEntry);
@ -74,7 +75,7 @@ public:
virtual void reload(); virtual void reload();
private: private:
leveldb::DB *m_database; std::unique_ptr<leveldb::DB> m_database;
}; };
#endif // USE_LEVELDB #endif // USE_LEVELDB