Fix regression in leveldb backend
[oweals/minetest.git] / src / database-leveldb.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "config.h"
21
22 #if USE_LEVELDB
23 /*
24 LevelDB databases
25 */
26
27
28 #include "database-leveldb.h"
29 #include "leveldb/db.h"
30
31 #include "map.h"
32 #include "mapsector.h"
33 #include "mapblock.h"
34 #include "serialization.h"
35 #include "main.h"
36 #include "settings.h"
37 #include "log.h"
38
39 #define ENSURE_STATUS_OK(s) \
40         if (!s.ok()) { \
41                 throw FileNotGoodException(std::string("LevelDB error: ") + s.ToString()); \
42         }
43
44 Database_LevelDB::Database_LevelDB(ServerMap *map, std::string savedir)
45 {
46         leveldb::Options options;
47         options.create_if_missing = true;
48         leveldb::Status status = leveldb::DB::Open(options, savedir + DIR_DELIM + "map.db", &m_database);
49         ENSURE_STATUS_OK(status);
50         srvmap = map;
51 }
52
53 int Database_LevelDB::Initialized(void)
54 {
55         return 1;
56 }
57
58 void Database_LevelDB::beginSave() {}
59 void Database_LevelDB::endSave() {}
60
61 void Database_LevelDB::saveBlock(MapBlock *block)
62 {
63         DSTACK(__FUNCTION_NAME);
64         /*
65                 Dummy blocks are not written
66         */
67         if(block->isDummy())
68         {
69                 return;
70         }
71
72         // Format used for writing
73         u8 version = SER_FMT_VER_HIGHEST_WRITE;
74         // Get destination
75         v3s16 p3d = block->getPos();
76
77         /*
78                 [0] u8 serialization version
79                 [1] data
80         */
81
82         std::ostringstream o(std::ios_base::binary);
83         o.write((char*)&version, 1);
84         // Write basic data
85         block->serialize(o, version, true);
86         // Write block to database
87         std::string tmp = o.str();
88
89         leveldb::Status status = m_database->Put(leveldb::WriteOptions(), i64tos(getBlockAsInteger(p3d)), tmp);
90         ENSURE_STATUS_OK(status);
91
92         // We just wrote it to the disk so clear modified flag
93         block->resetModified();
94 }
95
96 MapBlock* Database_LevelDB::loadBlock(v3s16 blockpos)
97 {
98         v2s16 p2d(blockpos.X, blockpos.Z);
99
100         std::string datastr;
101         leveldb::Status status = m_database->Get(leveldb::ReadOptions(),
102                 i64tos(getBlockAsInteger(blockpos)), &datastr);
103         if (datastr.length() == 0 && status.ok()) {
104                 errorstream << "Blank block data in database (datastr.length() == 0) ("
105                         << blockpos.X << "," << blockpos.Y << "," << blockpos.Z << ")" << std::endl;
106
107                 if (g_settings->getBool("ignore_world_load_errors")) {
108                         errorstream << "Ignoring block load error. Duck and cover! "
109                                 << "(ignore_world_load_errors)" << std::endl;
110                 } else {
111                         throw SerializationError("Blank block data in database");
112                 }
113                 return NULL;
114         } 
115         if (status.ok()) {
116                 /*
117                         Make sure sector is loaded
118                 */
119                 MapSector *sector = srvmap->createSector(p2d);
120
121                 try {
122                         std::istringstream is(datastr, std::ios_base::binary);
123                         u8 version = SER_FMT_VER_INVALID;
124                         is.read((char *)&version, 1);
125
126                         if (is.fail())
127                                 throw SerializationError("ServerMap::loadBlock(): Failed"
128                                         " to read MapBlock version");
129
130                         MapBlock *block = NULL;
131                         bool created_new = false;
132                         block = sector->getBlockNoCreateNoEx(blockpos.Y);
133                         if (block == NULL)
134                         {
135                                 block = sector->createBlankBlockNoInsert(blockpos.Y);
136                                 created_new = true;
137                         }
138
139                         // Read basic data
140                         block->deSerialize(is, version, true);
141
142                         // If it's a new block, insert it to the map
143                         if (created_new)
144                                 sector->insertBlock(block);
145
146                         /*
147                                 Save blocks loaded in old format in new format
148                         */
149                         //if(version < SER_FMT_VER_HIGHEST || save_after_load)
150                         // Only save if asked to; no need to update version
151                         //if(save_after_load)
152                         //      saveBlock(block);
153                         // We just loaded it from, so it's up-to-date.
154                         block->resetModified();
155                 }
156                 catch (SerializationError &e)
157                 {
158                         errorstream << "Invalid block data in database"
159                                 << " (" << blockpos.X << "," << blockpos.Y << "," << blockpos.Z
160                                 << ") (SerializationError): " << e.what() << std::endl;
161                         // TODO: Block should be marked as invalid in memory so that it is
162                         // not touched but the game can run
163
164                         if (g_settings->getBool("ignore_world_load_errors")) {
165                                 errorstream << "Ignoring block load error. Duck and cover! "
166                                         << "(ignore_world_load_errors)" << std::endl;
167                         } else {
168                                 throw SerializationError("Invalid block data in database");
169                                 //assert(0);
170                         }
171                 }
172
173                 return srvmap->getBlockNoCreateNoEx(blockpos);  // should not be using this here
174         }
175         return NULL;
176 }
177
178 void Database_LevelDB::listAllLoadableBlocks(std::list<v3s16> &dst)
179 {
180         leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions());
181         for (it->SeekToFirst(); it->Valid(); it->Next()) {
182                 dst.push_back(getIntegerAsBlock(stoi64(it->key().ToString())));
183         }
184         ENSURE_STATUS_OK(it->status());  // Check for any errors found during the scan
185         delete it;
186 }
187
188 Database_LevelDB::~Database_LevelDB()
189 {
190         delete m_database;
191 }
192 #endif