2013-09-10 15:49:43 +02: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 23:18:44 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#if USE_LEVELDB
|
|
|
|
|
2013-09-10 15:49:43 +02:00
|
|
|
#include "database-leveldb.h"
|
|
|
|
|
2012-10-22 23:18:44 +02:00
|
|
|
#include "log.h"
|
2014-09-12 00:22:05 +02:00
|
|
|
#include "filesys.h"
|
2014-11-16 21:31:57 +01:00
|
|
|
#include "exceptions.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
|
|
|
|
#include "leveldb/db.h"
|
|
|
|
|
2012-10-22 23:18:44 +02:00
|
|
|
|
2014-06-25 07:25:33 +02:00
|
|
|
#define ENSURE_STATUS_OK(s) \
|
2014-07-07 07:20:25 +02:00
|
|
|
if (!(s).ok()) { \
|
2014-11-16 21:31:57 +01:00
|
|
|
throw FileNotGoodException(std::string("LevelDB error: ") + \
|
|
|
|
(s).ToString()); \
|
2014-06-25 07:25:33 +02:00
|
|
|
}
|
|
|
|
|
2014-11-16 21:31:57 +01:00
|
|
|
|
|
|
|
Database_LevelDB::Database_LevelDB(const std::string &savedir)
|
2012-10-22 23:18:44 +02:00
|
|
|
{
|
|
|
|
leveldb::Options options;
|
|
|
|
options.create_if_missing = true;
|
2014-11-16 21:31:57 +01:00
|
|
|
leveldb::Status status = leveldb::DB::Open(options,
|
|
|
|
savedir + DIR_DELIM + "map.db", &m_database);
|
2014-06-25 07:25:33 +02:00
|
|
|
ENSURE_STATUS_OK(status);
|
2012-10-22 23:18:44 +02:00
|
|
|
}
|
|
|
|
|
2014-11-16 21:31:57 +01:00
|
|
|
Database_LevelDB::~Database_LevelDB()
|
2012-10-22 23:18:44 +02:00
|
|
|
{
|
2014-11-16 21:31:57 +01:00
|
|
|
delete m_database;
|
2012-10-22 23:18:44 +02:00
|
|
|
}
|
|
|
|
|
2014-11-16 21:31:57 +01:00
|
|
|
bool Database_LevelDB::saveBlock(const v3s16 &pos, const std::string &data)
|
2012-10-22 23:18:44 +02:00
|
|
|
{
|
2014-07-07 07:20:25 +02:00
|
|
|
leveldb::Status status = m_database->Put(leveldb::WriteOptions(),
|
2014-11-16 21:31:57 +01:00
|
|
|
i64tos(getBlockAsInteger(pos)), data);
|
2014-07-07 07:20:25 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
errorstream << "WARNING: saveBlock: LevelDB error saving block "
|
2014-11-16 21:31:57 +01:00
|
|
|
<< PP(pos) << ": " << status.ToString() << std::endl;
|
2014-07-07 07:20:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-22 23:18:44 +02:00
|
|
|
|
2014-07-07 07:20:25 +02:00
|
|
|
return true;
|
2012-10-22 23:18:44 +02:00
|
|
|
}
|
|
|
|
|
2014-11-16 21:31:57 +01:00
|
|
|
std::string Database_LevelDB::loadBlock(const v3s16 &pos)
|
2012-10-22 23:18:44 +02:00
|
|
|
{
|
|
|
|
std::string datastr;
|
2014-06-25 07:25:33 +02:00
|
|
|
leveldb::Status status = m_database->Get(leveldb::ReadOptions(),
|
2014-11-16 21:31:57 +01:00
|
|
|
i64tos(getBlockAsInteger(pos)), &datastr);
|
2014-07-08 20:04:37 +02:00
|
|
|
|
|
|
|
if(status.ok())
|
|
|
|
return datastr;
|
|
|
|
else
|
|
|
|
return "";
|
2012-10-22 23:18:44 +02:00
|
|
|
}
|
|
|
|
|
2014-11-16 21:31:57 +01:00
|
|
|
bool Database_LevelDB::deleteBlock(const v3s16 &pos)
|
2015-01-15 22:20:05 +01:00
|
|
|
{
|
|
|
|
leveldb::Status status = m_database->Delete(leveldb::WriteOptions(),
|
2014-11-16 21:31:57 +01:00
|
|
|
i64tos(getBlockAsInteger(pos)));
|
2015-01-15 22:20:05 +01:00
|
|
|
if (!status.ok()) {
|
|
|
|
errorstream << "WARNING: deleteBlock: LevelDB error deleting block "
|
2014-11-16 21:31:57 +01:00
|
|
|
<< PP(pos) << ": " << status.ToString() << std::endl;
|
2015-01-15 22:20:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-17 14:30:32 +01:00
|
|
|
void Database_LevelDB::listAllLoadableBlocks(std::vector<v3s16> &dst)
|
2012-10-22 23:18:44 +02: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 07:25:33 +02:00
|
|
|
ENSURE_STATUS_OK(it->status()); // Check for any errors found during the scan
|
2012-10-22 23:18:44 +02:00
|
|
|
delete it;
|
|
|
|
}
|
|
|
|
|
2014-11-16 21:31:57 +01:00
|
|
|
#endif // USE_LEVELDB
|
|
|
|
|