forked from Mirrorlandia_minetest/minetest
Use unique_ptrs for leveldb db and iterators
This commit is contained in:
parent
8445c5fe60
commit
d0bcdff5ce
@ -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<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()) {
|
||||
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<std::string> &res)
|
||||
{
|
||||
leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions());
|
||||
std::unique_ptr<leveldb::Iterator> 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<std::string> &res)
|
||||
{
|
||||
leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions());
|
||||
std::unique_ptr<leveldb::Iterator> 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()
|
||||
|
@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
|
||||
#if USE_LEVELDB
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#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<leveldb::DB> 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<std::string> &res);
|
||||
|
||||
private:
|
||||
leveldb::DB *m_database;
|
||||
std::unique_ptr<leveldb::DB> 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<leveldb::DB> m_database;
|
||||
};
|
||||
|
||||
#endif // USE_LEVELDB
|
||||
|
Loading…
Reference in New Issue
Block a user