diff options
author | ShadowNinja <shadowninja@minetest.net> | 2014-11-16 15:31:57 -0500 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2015-03-06 00:20:45 -0500 |
commit | 708337dfc2b3871dc6de983e781e4a4a60a1881d (patch) | |
tree | 97352d676d9d76b036d8b559ae4d3c6fe83bbf12 /src/database-leveldb.cpp | |
parent | c7454d4732dee0f7364ccb8e07002df1a037b94d (diff) | |
download | minetest-708337dfc2b3871dc6de983e781e4a4a60a1881d.tar.gz minetest-708337dfc2b3871dc6de983e781e4a4a60a1881d.tar.bz2 minetest-708337dfc2b3871dc6de983e781e4a4a60a1881d.zip |
Clean up database API and save the local map on an interval
Diffstat (limited to 'src/database-leveldb.cpp')
-rw-r--r-- | src/database-leveldb.cpp | 52 |
1 files changed, 23 insertions, 29 deletions
diff --git a/src/database-leveldb.cpp b/src/database-leveldb.cpp index b35e612b6..e895354a4 100644 --- a/src/database-leveldb.cpp +++ b/src/database-leveldb.cpp @@ -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 + |