2013-09-10 09:49:43 -04:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2012-10-22 17:18:44 -04:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#if USE_LEVELDB
|
|
|
|
|
2013-09-10 09:49:43 -04:00
|
|
|
#include "database-leveldb.h"
|
|
|
|
#include "leveldb/db.h"
|
|
|
|
|
2012-10-22 17:18:44 -04:00
|
|
|
#include "map.h"
|
|
|
|
#include "mapsector.h"
|
|
|
|
#include "mapblock.h"
|
2013-09-10 09:49:43 -04:00
|
|
|
#include "serialization.h"
|
2012-10-22 17:18:44 -04:00
|
|
|
#include "main.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "log.h"
|
2014-09-11 18:22:05 -04:00
|
|
|
#include "filesys.h"
|
2012-10-22 17:18:44 -04:00
|
|
|
|
2014-06-25 01:25:33 -04:00
|
|
|
#define ENSURE_STATUS_OK(s) \
|
2014-07-07 01:20:25 -04:00
|
|
|
if (!(s).ok()) { \
|
|
|
|
throw FileNotGoodException(std::string("LevelDB error: ") + (s).ToString()); \
|
2014-06-25 01:25:33 -04:00
|
|
|
}
|
|
|
|
|
2012-10-22 17:18:44 -04:00
|
|
|
Database_LevelDB::Database_LevelDB(ServerMap *map, 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);
|
2014-06-25 01:25:33 -04:00
|
|
|
ENSURE_STATUS_OK(status);
|
2012-10-22 17:18:44 -04:00
|
|
|
srvmap = map;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Database_LevelDB::Initialized(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database_LevelDB::beginSave() {}
|
|
|
|
void Database_LevelDB::endSave() {}
|
|
|
|
|
2014-07-08 14:04:37 -04:00
|
|
|
bool Database_LevelDB::saveBlock(v3s16 blockpos, std::string &data)
|
2012-10-22 17:18:44 -04:00
|
|
|
{
|
2014-07-07 01:20:25 -04:00
|
|
|
leveldb::Status status = m_database->Put(leveldb::WriteOptions(),
|
2014-07-08 14:04:37 -04:00
|
|
|
i64tos(getBlockAsInteger(blockpos)), data);
|
2014-07-07 01:20:25 -04:00
|
|
|
if (!status.ok()) {
|
|
|
|
errorstream << "WARNING: saveBlock: LevelDB error saving block "
|
2014-07-08 14:04:37 -04:00
|
|
|
<< PP(blockpos) << ": " << status.ToString() << std::endl;
|
2014-07-07 01:20:25 -04:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-22 17:18:44 -04:00
|
|
|
|
2014-07-07 01:20:25 -04:00
|
|
|
return true;
|
2012-10-22 17:18:44 -04:00
|
|
|
}
|
|
|
|
|
2014-07-08 14:04:37 -04:00
|
|
|
std::string Database_LevelDB::loadBlock(v3s16 blockpos)
|
2012-10-22 17:18:44 -04:00
|
|
|
{
|
|
|
|
std::string datastr;
|
2014-06-25 01:25:33 -04:00
|
|
|
leveldb::Status status = m_database->Get(leveldb::ReadOptions(),
|
2013-11-17 11:26:34 -05:00
|
|
|
i64tos(getBlockAsInteger(blockpos)), &datastr);
|
2014-07-08 14:04:37 -04:00
|
|
|
|
|
|
|
if(status.ok())
|
|
|
|
return datastr;
|
|
|
|
else
|
|
|
|
return "";
|
2012-10-22 17:18:44 -04:00
|
|
|
}
|
|
|
|
|
2013-09-09 12:44:50 -04:00
|
|
|
void Database_LevelDB::listAllLoadableBlocks(std::list<v3s16> &dst)
|
2012-10-22 17:18:44 -04:00
|
|
|
{
|
|
|
|
leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions());
|
|
|
|
for (it->SeekToFirst(); it->Valid(); it->Next()) {
|
|
|
|
dst.push_back(getIntegerAsBlock(stoi64(it->key().ToString())));
|
|
|
|
}
|
2014-06-25 01:25:33 -04:00
|
|
|
ENSURE_STATUS_OK(it->status()); // Check for any errors found during the scan
|
2012-10-22 17:18:44 -04:00
|
|
|
delete it;
|
|
|
|
}
|
|
|
|
|
|
|
|
Database_LevelDB::~Database_LevelDB()
|
|
|
|
{
|
|
|
|
delete m_database;
|
|
|
|
}
|
|
|
|
#endif
|