3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "database-leveldb.h"
29 #include "leveldb/db.h"
32 #include "mapsector.h"
34 #include "serialization.h"
39 Database_LevelDB::Database_LevelDB(ServerMap *map, std::string savedir)
41 leveldb::Options options;
42 options.create_if_missing = true;
43 leveldb::Status status = leveldb::DB::Open(options, savedir + DIR_DELIM + "map.db", &m_database);
48 int Database_LevelDB::Initialized(void)
53 void Database_LevelDB::beginSave() {}
54 void Database_LevelDB::endSave() {}
56 void Database_LevelDB::saveBlock(MapBlock *block)
58 DSTACK(__FUNCTION_NAME);
60 Dummy blocks are not written
67 // Format used for writing
68 u8 version = SER_FMT_VER_HIGHEST_WRITE;
70 v3s16 p3d = block->getPos();
73 [0] u8 serialization version
77 std::ostringstream o(std::ios_base::binary);
78 o.write((char*)&version, 1);
80 block->serialize(o, version, true);
81 // Write block to database
82 std::string tmp = o.str();
84 m_database->Put(leveldb::WriteOptions(), i64tos(getBlockAsInteger(p3d)), tmp);
86 // We just wrote it to the disk so clear modified flag
87 block->resetModified();
90 MapBlock* Database_LevelDB::loadBlock(v3s16 blockpos)
92 v2s16 p2d(blockpos.X, blockpos.Z);
95 leveldb::Status s = m_database->Get(leveldb::ReadOptions(), i64tos(getBlockAsInteger(blockpos)), &datastr);
99 Make sure sector is loaded
101 MapSector *sector = srvmap->createSector(p2d);
104 std::istringstream is(datastr, std::ios_base::binary);
105 u8 version = SER_FMT_VER_INVALID;
106 is.read((char*)&version, 1);
109 throw SerializationError("ServerMap::loadBlock(): Failed"
110 " to read MapBlock version");
112 MapBlock *block = NULL;
113 bool created_new = false;
114 block = sector->getBlockNoCreateNoEx(blockpos.Y);
117 block = sector->createBlankBlockNoInsert(blockpos.Y);
121 block->deSerialize(is, version, true);
122 // If it's a new block, insert it to the map
124 sector->insertBlock(block);
126 Save blocks loaded in old format in new format
129 //if(version < SER_FMT_VER_HIGHEST || save_after_load)
130 // Only save if asked to; no need to update version
131 //if(save_after_load)
133 // We just loaded it from, so it's up-to-date.
134 block->resetModified();
137 catch(SerializationError &e)
139 errorstream<<"Invalid block data in database"
140 <<" ("<<blockpos.X<<","<<blockpos.Y<<","<<blockpos.Z<<")"
141 <<" (SerializationError): "<<e.what()<<std::endl;
142 // TODO: Block should be marked as invalid in memory so that it is
143 // not touched but the game can run
145 if(g_settings->getBool("ignore_world_load_errors")){
146 errorstream<<"Ignoring block load error. Duck and cover! "
147 <<"(ignore_world_load_errors)"<<std::endl;
149 throw SerializationError("Invalid block data in database");
154 return srvmap->getBlockNoCreateNoEx(blockpos); // should not be using this here
159 void Database_LevelDB::listAllLoadableBlocks(std::list<v3s16> &dst)
161 leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions());
162 for (it->SeekToFirst(); it->Valid(); it->Next()) {
163 dst.push_back(getIntegerAsBlock(stoi64(it->key().ToString())));
165 assert(it->status().ok()); // Check for any errors found during the scan
169 Database_LevelDB::~Database_LevelDB()