]> git.lizzy.rs Git - minetest.git/blob - src/database-sqlite3.cpp
Mgv7: Decrease cliff steepness
[minetest.git] / src / database-sqlite3.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 /*
21 SQLite format specification:
22         blocks:
23                 (PK) INT id
24                 BLOB data
25 */
26
27
28 #include "database-sqlite3.h"
29
30 #include "log.h"
31 #include "filesys.h"
32 #include "exceptions.h"
33 #include "settings.h"
34 #include "porting.h"
35 #include "util/string.h"
36
37 #include <cassert>
38
39 // When to print messages when the database is being held locked by another process
40 // Note: I've seen occasional delays of over 250ms while running minetestmapper.
41 #define BUSY_INFO_TRESHOLD      100     // Print first informational message after 100ms.
42 #define BUSY_WARNING_TRESHOLD   250     // Print warning message after 250ms. Lag is increased.
43 #define BUSY_ERROR_TRESHOLD     1000    // Print error message after 1000ms. Significant lag.
44 #define BUSY_FATAL_TRESHOLD     3000    // Allow SQLITE_BUSY to be returned, which will cause a minetest crash.
45 #define BUSY_ERROR_INTERVAL     10000   // Safety net: report again every 10 seconds
46
47
48 #define SQLRES(s, r, m) \
49         if ((s) != (r)) { \
50                 throw FileNotGoodException(std::string(m) + ": " +\
51                                 sqlite3_errmsg(m_database)); \
52         }
53 #define SQLOK(s, m) SQLRES(s, SQLITE_OK, m)
54
55 #define PREPARE_STATEMENT(name, query) \
56         SQLOK(sqlite3_prepare_v2(m_database, query, -1, &m_stmt_##name, NULL),\
57                 "Failed to prepare query '" query "'")
58
59 #define FINALIZE_STATEMENT(statement) \
60         SQLOK(sqlite3_finalize(statement), "Failed to finalize " #statement)
61
62 int Database_SQLite3::busyHandler(void *data, int count)
63 {
64         s64 &first_time = reinterpret_cast<s64 *>(data)[0];
65         s64 &prev_time = reinterpret_cast<s64 *>(data)[1];
66         s64 cur_time = getTimeMs();
67
68         if (count == 0) {
69                 first_time = cur_time;
70                 prev_time = first_time;
71         } else {
72                 while (cur_time < prev_time)
73                         cur_time += s64(1)<<32;
74         }
75
76         if (cur_time - first_time < BUSY_INFO_TRESHOLD) {
77                 ; // do nothing
78         } else if (cur_time - first_time >= BUSY_INFO_TRESHOLD &&
79                         prev_time - first_time < BUSY_INFO_TRESHOLD) {
80                 infostream << "SQLite3 database has been locked for "
81                         << cur_time - first_time << " ms." << std::endl;
82         } else if (cur_time - first_time >= BUSY_WARNING_TRESHOLD &&
83                         prev_time - first_time < BUSY_WARNING_TRESHOLD) {
84                 warningstream << "SQLite3 database has been locked for "
85                         << cur_time - first_time << " ms." << std::endl;
86         } else if (cur_time - first_time >= BUSY_ERROR_TRESHOLD &&
87                         prev_time - first_time < BUSY_ERROR_TRESHOLD) {
88                 errorstream << "SQLite3 database has been locked for "
89                         << cur_time - first_time << " ms; this causes lag." << std::endl;
90         } else if (cur_time - first_time >= BUSY_FATAL_TRESHOLD &&
91                         prev_time - first_time < BUSY_FATAL_TRESHOLD) {
92                 errorstream << "SQLite3 database has been locked for "
93                         << cur_time - first_time << " ms - giving up!" << std::endl;
94         } else if ((cur_time - first_time) / BUSY_ERROR_INTERVAL !=
95                         (prev_time - first_time) / BUSY_ERROR_INTERVAL) {
96                 // Safety net: keep reporting every BUSY_ERROR_INTERVAL
97                 errorstream << "SQLite3 database has been locked for "
98                         << (cur_time - first_time) / 1000 << " seconds!" << std::endl;
99         }
100
101         prev_time = cur_time;
102
103         // Make sqlite transaction fail if delay exceeds BUSY_FATAL_TRESHOLD
104         return cur_time - first_time < BUSY_FATAL_TRESHOLD;
105 }
106
107
108 Database_SQLite3::Database_SQLite3(const std::string &savedir) :
109         m_initialized(false),
110         m_savedir(savedir),
111         m_database(NULL),
112         m_stmt_read(NULL),
113         m_stmt_write(NULL),
114         m_stmt_list(NULL),
115         m_stmt_delete(NULL),
116         m_stmt_begin(NULL),
117         m_stmt_end(NULL)
118 {
119 }
120
121 void Database_SQLite3::beginSave() {
122         verifyDatabase();
123         SQLRES(sqlite3_step(m_stmt_begin), SQLITE_DONE,
124                 "Failed to start SQLite3 transaction");
125         sqlite3_reset(m_stmt_begin);
126 }
127
128 void Database_SQLite3::endSave() {
129         verifyDatabase();
130         SQLRES(sqlite3_step(m_stmt_end), SQLITE_DONE,
131                 "Failed to commit SQLite3 transaction");
132         sqlite3_reset(m_stmt_end);
133 }
134
135 void Database_SQLite3::openDatabase()
136 {
137         if (m_database) return;
138
139         std::string dbp = m_savedir + DIR_DELIM + "map.sqlite";
140
141         // Open the database connection
142
143         if (!fs::CreateAllDirs(m_savedir)) {
144                 infostream << "Database_SQLite3: Failed to create directory \""
145                         << m_savedir << "\"" << std::endl;
146                 throw FileNotGoodException("Failed to create database "
147                                 "save directory");
148         }
149
150         bool needs_create = !fs::PathExists(dbp);
151
152         SQLOK(sqlite3_open_v2(dbp.c_str(), &m_database,
153                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL),
154                 std::string("Failed to open SQLite3 database file ") + dbp);
155
156         SQLOK(sqlite3_busy_handler(m_database, Database_SQLite3::busyHandler,
157                 m_busy_handler_data), "Failed to set SQLite3 busy handler");
158
159         if (needs_create) {
160                 createDatabase();
161         }
162
163         std::string query_str = std::string("PRAGMA synchronous = ")
164                          + itos(g_settings->getU16("sqlite_synchronous"));
165         SQLOK(sqlite3_exec(m_database, query_str.c_str(), NULL, NULL, NULL),
166                 "Failed to modify sqlite3 synchronous mode");
167 }
168
169 void Database_SQLite3::verifyDatabase()
170 {
171         if (m_initialized) return;
172
173         openDatabase();
174
175         PREPARE_STATEMENT(begin, "BEGIN");
176         PREPARE_STATEMENT(end, "COMMIT");
177         PREPARE_STATEMENT(read, "SELECT `data` FROM `blocks` WHERE `pos` = ? LIMIT 1");
178 #ifdef __ANDROID__
179         PREPARE_STATEMENT(write,  "INSERT INTO `blocks` (`pos`, `data`) VALUES (?, ?)");
180 #else
181         PREPARE_STATEMENT(write, "REPLACE INTO `blocks` (`pos`, `data`) VALUES (?, ?)");
182 #endif
183         PREPARE_STATEMENT(delete, "DELETE FROM `blocks` WHERE `pos` = ?");
184         PREPARE_STATEMENT(list, "SELECT `pos` FROM `blocks`");
185
186         m_initialized = true;
187
188         verbosestream << "ServerMap: SQLite3 database opened." << std::endl;
189 }
190
191 inline void Database_SQLite3::bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index)
192 {
193         SQLOK(sqlite3_bind_int64(stmt, index, getBlockAsInteger(pos)),
194                 "Internal error: failed to bind query at " __FILE__ ":" TOSTRING(__LINE__));
195 }
196
197 bool Database_SQLite3::deleteBlock(const v3s16 &pos)
198 {
199         verifyDatabase();
200
201         bindPos(m_stmt_delete, pos);
202
203         bool good = sqlite3_step(m_stmt_delete) == SQLITE_DONE;
204         sqlite3_reset(m_stmt_delete);
205
206         if (!good) {
207                 warningstream << "deleteBlock: Block failed to delete "
208                         << PP(pos) << ": " << sqlite3_errmsg(m_database) << std::endl;
209         }
210         return good;
211 }
212
213 bool Database_SQLite3::saveBlock(const v3s16 &pos, const std::string &data)
214 {
215         verifyDatabase();
216
217 #ifdef __ANDROID__
218         /**
219          * Note: For some unknown reason SQLite3 fails to REPLACE blocks on Android,
220          * deleting them and then inserting works.
221          */
222         bindPos(m_stmt_read, pos);
223
224         if (sqlite3_step(m_stmt_read) == SQLITE_ROW) {
225                 deleteBlock(pos);
226         }
227         sqlite3_reset(m_stmt_read);
228 #endif
229
230         bindPos(m_stmt_write, pos);
231         SQLOK(sqlite3_bind_blob(m_stmt_write, 2, data.data(), data.size(), NULL),
232                 "Internal error: failed to bind query at " __FILE__ ":" TOSTRING(__LINE__));
233
234         SQLRES(sqlite3_step(m_stmt_write), SQLITE_DONE, "Failed to save block")
235         sqlite3_reset(m_stmt_write);
236
237         return true;
238 }
239
240 std::string Database_SQLite3::loadBlock(const v3s16 &pos)
241 {
242         verifyDatabase();
243
244         bindPos(m_stmt_read, pos);
245
246         if (sqlite3_step(m_stmt_read) != SQLITE_ROW) {
247                 sqlite3_reset(m_stmt_read);
248                 return "";
249         }
250         const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0);
251         size_t len = sqlite3_column_bytes(m_stmt_read, 0);
252
253         std::string s;
254         if (data)
255                 s = std::string(data, len);
256
257         sqlite3_step(m_stmt_read);
258         // We should never get more than 1 row, so ok to reset
259         sqlite3_reset(m_stmt_read);
260
261         return s;
262 }
263
264 void Database_SQLite3::createDatabase()
265 {
266         assert(m_database); // Pre-condition
267         SQLOK(sqlite3_exec(m_database,
268                 "CREATE TABLE IF NOT EXISTS `blocks` (\n"
269                 "       `pos` INT PRIMARY KEY,\n"
270                 "       `data` BLOB\n"
271                 ");\n",
272                 NULL, NULL, NULL),
273                 "Failed to create database table");
274 }
275
276 void Database_SQLite3::listAllLoadableBlocks(std::vector<v3s16> &dst)
277 {
278         verifyDatabase();
279
280         while (sqlite3_step(m_stmt_list) == SQLITE_ROW) {
281                 dst.push_back(getIntegerAsBlock(sqlite3_column_int64(m_stmt_list, 0)));
282         }
283         sqlite3_reset(m_stmt_list);
284 }
285
286 Database_SQLite3::~Database_SQLite3()
287 {
288         FINALIZE_STATEMENT(m_stmt_read)
289         FINALIZE_STATEMENT(m_stmt_write)
290         FINALIZE_STATEMENT(m_stmt_list)
291         FINALIZE_STATEMENT(m_stmt_begin)
292         FINALIZE_STATEMENT(m_stmt_end)
293         FINALIZE_STATEMENT(m_stmt_delete)
294
295         SQLOK(sqlite3_close(m_database), "Failed to close database");
296 }
297