mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Clean up database API and save the local map on an interval
This commit is contained in:
parent
c7454d4732
commit
708337dfc2
16 changed files with 391 additions and 519 deletions
|
@ -22,57 +22,54 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#if USE_LEVELDB
|
||||
|
||||
#include "database-leveldb.h"
|
||||
#include "leveldb/db.h"
|
||||
|
||||
#include "map.h"
|
||||
#include "mapsector.h"
|
||||
#include "mapblock.h"
|
||||
#include "serialization.h"
|
||||
#include "main.h"
|
||||
#include "settings.h"
|
||||
#include "log.h"
|
||||
#include "filesys.h"
|
||||
#include "exceptions.h"
|
||||
#include "util/string.h"
|
||||
|
||||
#include "leveldb/db.h"
|
||||
|
||||
|
||||
#define ENSURE_STATUS_OK(s) \
|
||||
if (!(s).ok()) { \
|
||||
throw FileNotGoodException(std::string("LevelDB error: ") + (s).ToString()); \
|
||||
throw FileNotGoodException(std::string("LevelDB error: ") + \
|
||||
(s).ToString()); \
|
||||
}
|
||||
|
||||
Database_LevelDB::Database_LevelDB(ServerMap *map, std::string savedir)
|
||||
|
||||
Database_LevelDB::Database_LevelDB(const std::string &savedir)
|
||||
{
|
||||
leveldb::Options options;
|
||||
options.create_if_missing = true;
|
||||
leveldb::Status status = leveldb::DB::Open(options, savedir + DIR_DELIM + "map.db", &m_database);
|
||||
leveldb::Status status = leveldb::DB::Open(options,
|
||||
savedir + DIR_DELIM + "map.db", &m_database);
|
||||
ENSURE_STATUS_OK(status);
|
||||
srvmap = map;
|
||||
}
|
||||
|
||||
int Database_LevelDB::Initialized(void)
|
||||
Database_LevelDB::~Database_LevelDB()
|
||||
{
|
||||
return 1;
|
||||
delete m_database;
|
||||
}
|
||||
|
||||
void Database_LevelDB::beginSave() {}
|
||||
void Database_LevelDB::endSave() {}
|
||||
|
||||
bool Database_LevelDB::saveBlock(v3s16 blockpos, std::string &data)
|
||||
bool Database_LevelDB::saveBlock(const v3s16 &pos, const std::string &data)
|
||||
{
|
||||
leveldb::Status status = m_database->Put(leveldb::WriteOptions(),
|
||||
i64tos(getBlockAsInteger(blockpos)), data);
|
||||
i64tos(getBlockAsInteger(pos)), data);
|
||||
if (!status.ok()) {
|
||||
errorstream << "WARNING: saveBlock: LevelDB error saving block "
|
||||
<< PP(blockpos) << ": " << status.ToString() << std::endl;
|
||||
<< PP(pos) << ": " << status.ToString() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string Database_LevelDB::loadBlock(v3s16 blockpos)
|
||||
std::string Database_LevelDB::loadBlock(const v3s16 &pos)
|
||||
{
|
||||
std::string datastr;
|
||||
leveldb::Status status = m_database->Get(leveldb::ReadOptions(),
|
||||
i64tos(getBlockAsInteger(blockpos)), &datastr);
|
||||
i64tos(getBlockAsInteger(pos)), &datastr);
|
||||
|
||||
if(status.ok())
|
||||
return datastr;
|
||||
|
@ -80,13 +77,13 @@ std::string Database_LevelDB::loadBlock(v3s16 blockpos)
|
|||
return "";
|
||||
}
|
||||
|
||||
bool Database_LevelDB::deleteBlock(v3s16 blockpos)
|
||||
bool Database_LevelDB::deleteBlock(const v3s16 &pos)
|
||||
{
|
||||
leveldb::Status status = m_database->Delete(leveldb::WriteOptions(),
|
||||
i64tos(getBlockAsInteger(blockpos)));
|
||||
i64tos(getBlockAsInteger(pos)));
|
||||
if (!status.ok()) {
|
||||
errorstream << "WARNING: deleteBlock: LevelDB error deleting block "
|
||||
<< PP(blockpos) << ": " << status.ToString() << std::endl;
|
||||
<< PP(pos) << ": " << status.ToString() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -103,8 +100,5 @@ void Database_LevelDB::listAllLoadableBlocks(std::vector<v3s16> &dst)
|
|||
delete it;
|
||||
}
|
||||
|
||||
Database_LevelDB::~Database_LevelDB()
|
||||
{
|
||||
delete m_database;
|
||||
}
|
||||
#endif
|
||||
#endif // USE_LEVELDB
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue