From d0bcdff5cee888d5c02273508ade987dba601b14 Mon Sep 17 00:00:00 2001 From: Desour Date: Fri, 3 Mar 2023 14:53:37 +0100 Subject: [PATCH] Use unique_ptrs for leveldb db and iterators --- src/database/database-leveldb.cpp | 36 +++++++++++-------------------- src/database/database-leveldb.h | 13 +++++------ 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/src/database/database-leveldb.cpp b/src/database/database-leveldb.cpp index 6e59daab3..80493d5f2 100644 --- a/src/database/database-leveldb.cpp +++ b/src/database/database-leveldb.cpp @@ -45,14 +45,11 @@ Database_LevelDB::Database_LevelDB(const std::string &savedir) { leveldb::Options options; options.create_if_missing = true; + leveldb::DB *db; leveldb::Status status = leveldb::DB::Open(options, - savedir + DIR_DELIM + "map.db", &m_database); + savedir + DIR_DELIM + "map.db", &db); ENSURE_STATUS_OK(status); -} - -Database_LevelDB::~Database_LevelDB() -{ - delete m_database; + m_database.reset(db); } 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 &dst) { - leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions()); + std::unique_ptr it(m_database->NewIterator(leveldb::ReadOptions())); for (it->SeekToFirst(); it->Valid(); it->Next()) { dst.push_back(getIntegerAsBlock(stoi64(it->key().ToString()))); } ENSURE_STATUS_OK(it->status()); // Check for any errors found during the scan - delete it; } PlayerDatabaseLevelDB::PlayerDatabaseLevelDB(const std::string &savedir) { leveldb::Options options; options.create_if_missing = true; + leveldb::DB *db; leveldb::Status status = leveldb::DB::Open(options, - savedir + DIR_DELIM + "players.db", &m_database); + savedir + DIR_DELIM + "players.db", &db); ENSURE_STATUS_OK(status); -} - -PlayerDatabaseLevelDB::~PlayerDatabaseLevelDB() -{ - delete m_database; + m_database.reset(db); } void PlayerDatabaseLevelDB::savePlayer(RemotePlayer *player) @@ -202,26 +195,22 @@ bool PlayerDatabaseLevelDB::loadPlayer(RemotePlayer *player, PlayerSAO *sao) void PlayerDatabaseLevelDB::listPlayers(std::vector &res) { - leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions()); + std::unique_ptr it(m_database->NewIterator(leveldb::ReadOptions())); res.clear(); for (it->SeekToFirst(); it->Valid(); it->Next()) { res.push_back(it->key().ToString()); } - delete it; } AuthDatabaseLevelDB::AuthDatabaseLevelDB(const std::string &savedir) { leveldb::Options options; options.create_if_missing = true; + leveldb::DB *db; leveldb::Status status = leveldb::DB::Open(options, - savedir + DIR_DELIM + "auth.db", &m_database); + savedir + DIR_DELIM + "auth.db", &db); ENSURE_STATUS_OK(status); -} - -AuthDatabaseLevelDB::~AuthDatabaseLevelDB() -{ - delete m_database; + m_database.reset(db); } 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 &res) { - leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions()); + std::unique_ptr it(m_database->NewIterator(leveldb::ReadOptions())); res.clear(); for (it->SeekToFirst(); it->Valid(); it->Next()) { res.emplace_back(it->key().ToString()); } - delete it; } void AuthDatabaseLevelDB::reload() diff --git a/src/database/database-leveldb.h b/src/database/database-leveldb.h index 61def1256..812752a67 100644 --- a/src/database/database-leveldb.h +++ b/src/database/database-leveldb.h @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #if USE_LEVELDB +#include #include #include "database.h" #include "leveldb/db.h" @@ -31,7 +32,7 @@ class Database_LevelDB : public MapDatabase { public: Database_LevelDB(const std::string &savedir); - ~Database_LevelDB(); + ~Database_LevelDB() = default; bool saveBlock(const v3s16 &pos, const std::string &data); void loadBlock(const v3s16 &pos, std::string *block); @@ -42,14 +43,14 @@ public: void endSave() {} private: - leveldb::DB *m_database; + std::unique_ptr m_database; }; class PlayerDatabaseLevelDB : public PlayerDatabase { public: PlayerDatabaseLevelDB(const std::string &savedir); - ~PlayerDatabaseLevelDB(); + ~PlayerDatabaseLevelDB() = default; void savePlayer(RemotePlayer *player); bool loadPlayer(RemotePlayer *player, PlayerSAO *sao); @@ -57,14 +58,14 @@ public: void listPlayers(std::vector &res); private: - leveldb::DB *m_database; + std::unique_ptr m_database; }; class AuthDatabaseLevelDB : public AuthDatabase { public: AuthDatabaseLevelDB(const std::string &savedir); - virtual ~AuthDatabaseLevelDB(); + virtual ~AuthDatabaseLevelDB() = default; virtual bool getAuth(const std::string &name, AuthEntry &res); virtual bool saveAuth(const AuthEntry &authEntry); @@ -74,7 +75,7 @@ public: virtual void reload(); private: - leveldb::DB *m_database; + std::unique_ptr m_database; }; #endif // USE_LEVELDB