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