0cf685740fd70f9d69b9ca768fe26fc179590343
[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 bool Database_LevelDB::saveBlock(MapBlock *block)
62 {
63         DSTACK(__FUNCTION_NAME);
64
65         v3s16 p3d = block->getPos();
66
67         /*
68                 Dummy blocks are not written
69         */
70         if(block->isDummy())
71         {
72                 errorstream << "WARNING: saveBlock: Not writing dummy block "
73                                 << PP(p3d) << std::endl;
74                 return true;
75         }
76
77         // Format used for writing
78         u8 version = SER_FMT_VER_HIGHEST_WRITE;
79
80         /*
81                 [0] u8 serialization version
82                 [1] data
83         */
84         std::ostringstream o(std::ios_base::binary);
85         o.write((char*)&version, 1);
86         // Write basic data
87         block->serialize(o, version, true);
88         // Write block to database
89         std::string tmp = o.str();
90
91         leveldb::Status status = m_database->Put(leveldb::WriteOptions(),
92                         i64tos(getBlockAsInteger(p3d)), tmp);
93         if (!status.ok()) {
94                 errorstream << "WARNING: saveBlock: LevelDB error saving block "
95                         << PP(p3d) << ": " << status.ToString() << std::endl;
96                 return false;
97         }
98
99         // We just wrote it to the disk so clear modified flag
100         block->resetModified();
101         return true;
102 }
103
104 MapBlock* Database_LevelDB::loadBlock(v3s16 blockpos)
105 {
106         v2s16 p2d(blockpos.X, blockpos.Z);
107
108         std::string datastr;
109         leveldb::Status status = m_database->Get(leveldb::ReadOptions(),
110                 i64tos(getBlockAsInteger(blockpos)), &datastr);
111         if (datastr.length() == 0 && status.ok()) {
112                 errorstream << "Blank block data in database (datastr.length() == 0) ("
113                         << blockpos.X << "," << blockpos.Y << "," << blockpos.Z << ")" << std::endl;
114
115                 if (g_settings->getBool("ignore_world_load_errors")) {
116                         errorstream << "Ignoring block load error. Duck and cover! "
117                                 << "(ignore_world_load_errors)" << std::endl;
118                 } else {
119                         throw SerializationError("Blank block data in database");
120                 }
121                 return NULL;
122         } 
123         if (status.ok()) {
124                 /*
125                         Make sure sector is loaded
126                 */
127                 MapSector *sector = srvmap->createSector(p2d);
128
129                 try {
130                         std::istringstream is(datastr, std::ios_base::binary);
131                         u8 version = SER_FMT_VER_INVALID;
132                         is.read((char *)&version, 1);
133
134                         if (is.fail())
135                                 throw SerializationError("ServerMap::loadBlock(): Failed"
136                                         " to read MapBlock version");
137
138                         MapBlock *block = NULL;
139                         bool created_new = false;
140                         block = sector->getBlockNoCreateNoEx(blockpos.Y);
141                         if (block == NULL)
142                         {
143                                 block = sector->createBlankBlockNoInsert(blockpos.Y);
144                                 created_new = true;
145                         }
146
147                         // Read basic data
148                         block->deSerialize(is, version, true);
149
150                         // If it's a new block, insert it to the map
151                         if (created_new)
152                                 sector->insertBlock(block);
153
154                         /*
155                                 Save blocks loaded in old format in new format
156                         */
157                         //if(version < SER_FMT_VER_HIGHEST || save_after_load)
158                         // Only save if asked to; no need to update version
159                         //if(save_after_load)
160                         //      saveBlock(block);
161                         // We just loaded it from, so it's up-to-date.
162                         block->resetModified();
163                 }
164                 catch (SerializationError &e)
165                 {
166                         errorstream << "Invalid block data in database"
167                                 << " (" << blockpos.X << "," << blockpos.Y << "," << blockpos.Z
168                                 << ") (SerializationError): " << e.what() << std::endl;
169                         // TODO: Block should be marked as invalid in memory so that it is
170                         // not touched but the game can run
171
172                         if (g_settings->getBool("ignore_world_load_errors")) {
173                                 errorstream << "Ignoring block load error. Duck and cover! "
174                                         << "(ignore_world_load_errors)" << std::endl;
175                         } else {
176                                 throw SerializationError("Invalid block data in database");
177                                 //assert(0);
178                         }
179                 }
180
181                 return srvmap->getBlockNoCreateNoEx(blockpos);  // should not be using this here
182         }
183         return NULL;
184 }
185
186 void Database_LevelDB::listAllLoadableBlocks(std::list<v3s16> &dst)
187 {
188         leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions());
189         for (it->SeekToFirst(); it->Valid(); it->Next()) {
190                 dst.push_back(getIntegerAsBlock(stoi64(it->key().ToString())));
191         }
192         ENSURE_STATUS_OK(it->status());  // Check for any errors found during the scan
193         delete it;
194 }
195
196 Database_LevelDB::~Database_LevelDB()
197 {
198         delete m_database;
199 }
200 #endif