#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 DatabaseException(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;
+ warningstream << "saveBlock: LevelDB error saving block "
+ << PP(pos) << ": " << status.ToString() << std::endl;
return false;
}
return true;
}
-std::string Database_LevelDB::loadBlock(v3s16 blockpos)
+void Database_LevelDB::loadBlock(const v3s16 &pos, std::string *block)
{
std::string datastr;
leveldb::Status status = m_database->Get(leveldb::ReadOptions(),
- i64tos(getBlockAsInteger(blockpos)), &datastr);
+ i64tos(getBlockAsInteger(pos)), &datastr);
- if(status.ok())
- return datastr;
- else
- return "";
+ *block = (status.ok()) ? datastr : "";
}
-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;
+ warningstream << "deleteBlock: LevelDB error deleting block "
+ << PP(pos) << ": " << status.ToString() << std::endl;
return false;
}
return true;
}
-void Database_LevelDB::listAllLoadableBlocks(std::list<v3s16> &dst)
+void Database_LevelDB::listAllLoadableBlocks(std::vector<v3s16> &dst)
{
leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next()) {
delete it;
}
-Database_LevelDB::~Database_LevelDB()
-{
- delete m_database;
-}
-#endif
+#endif // USE_LEVELDB
+