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.
21 SQLite format specification:
28 #include "database-sqlite3.h"
32 #include "exceptions.h"
35 #include "util/string.h"
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
48 #define SQLRES(s, r) \
50 throw FileNotGoodException(std::string(\
51 "SQLite3 database error (" \
52 __FILE__ ":" TOSTRING(__LINE__) \
54 sqlite3_errmsg(m_database)); \
56 #define SQLOK(s) SQLRES(s, SQLITE_OK)
58 #define PREPARE_STATEMENT(name, query) \
59 SQLOK(sqlite3_prepare_v2(m_database, query, -1, &m_stmt_##name, NULL))
61 #define FINALIZE_STATEMENT(statement) \
62 if (sqlite3_finalize(statement) != SQLITE_OK) { \
63 throw FileNotGoodException(std::string( \
64 "SQLite3: Failed to finalize " #statement ": ") + \
65 sqlite3_errmsg(m_database)); \
68 int Database_SQLite3::busyHandler(void *data, int count)
70 s64 &first_time = reinterpret_cast<s64 *>(data)[0];
71 s64 &prev_time = reinterpret_cast<s64 *>(data)[1];
72 s64 cur_time = getTimeMs();
75 first_time = cur_time;
76 prev_time = first_time;
78 while (cur_time < prev_time)
79 cur_time += s64(1)<<32;
82 if (cur_time - first_time < BUSY_INFO_TRESHOLD) {
84 } else if (cur_time - first_time >= BUSY_INFO_TRESHOLD &&
85 prev_time - first_time < BUSY_INFO_TRESHOLD) {
86 infostream << "SQLite3 database has been locked for "
87 << cur_time - first_time << " ms." << std::endl;
88 } else if (cur_time - first_time >= BUSY_WARNING_TRESHOLD &&
89 prev_time - first_time < BUSY_WARNING_TRESHOLD) {
90 warningstream << "Sqlite3 database has been locked for "
91 << cur_time - first_time << " ms." << std::endl;
92 } else if (cur_time - first_time >= BUSY_ERROR_TRESHOLD &&
93 prev_time - first_time < BUSY_ERROR_TRESHOLD) {
94 errorstream << "SQLite3 database has been locked for "
95 << cur_time - first_time << " ms; this causes lag." << std::endl;
96 } else if (cur_time - first_time >= BUSY_FATAL_TRESHOLD &&
97 prev_time - first_time < BUSY_FATAL_TRESHOLD) {
98 errorstream << "Sqlite3 database has been locked for "
99 << cur_time - first_time << " ms - giving up!" << std::endl;
100 } else if ((cur_time - first_time) / BUSY_ERROR_INTERVAL !=
101 (prev_time - first_time) / BUSY_ERROR_INTERVAL) {
102 // Safety net: keep reporting every BUSY_ERROR_INTERVAL
103 errorstream << "SQLite3 database has been locked for "
104 << (cur_time - first_time) / 1000 << " seconds!" << std::endl;
107 prev_time = cur_time;
109 // Make sqlite transaction fail if delay exceeds BUSY_FATAL_TRESHOLD
110 return cur_time - first_time < BUSY_FATAL_TRESHOLD;
114 Database_SQLite3::Database_SQLite3(const std::string &savedir) :
115 m_initialized(false),
127 void Database_SQLite3::beginSave() {
129 SQLRES(sqlite3_step(m_stmt_begin), SQLITE_DONE);
130 sqlite3_reset(m_stmt_begin);
133 void Database_SQLite3::endSave() {
135 SQLRES(sqlite3_step(m_stmt_end), SQLITE_DONE);
136 sqlite3_reset(m_stmt_end);
139 void Database_SQLite3::openDatabase()
141 if (m_database) return;
143 std::string dbp = m_savedir + DIR_DELIM + "map.sqlite";
145 // Open the database connection
147 if (!fs::CreateAllDirs(m_savedir)) {
148 infostream << "Database_SQLite3: Failed to create directory \""
149 << m_savedir << "\"" << std::endl;
150 throw FileNotGoodException("Failed to create database "
154 bool needs_create = !fs::PathExists(dbp);
156 if (sqlite3_open_v2(dbp.c_str(), &m_database,
157 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
158 NULL) != SQLITE_OK) {
159 errorstream << "SQLite3 database failed to open: "
160 << sqlite3_errmsg(m_database) << std::endl;
161 throw FileNotGoodException("Cannot open database file");
164 if (sqlite3_busy_handler(m_database, sqlite3BusyHandler, busy_handler_data) != SQLITE_OK) {
165 errorstream << "SQLite3 database failed to set busy handler: "
166 << sqlite3_errmsg(m_database) << std::endl;
167 throw FileNotGoodException("Failed to set busy handler for sqlite connection");
174 std::string query_str = std::string("PRAGMA synchronous = ")
175 + itos(g_settings->getU16("sqlite_synchronous"));
176 SQLOK(sqlite3_exec(m_database, query_str.c_str(), NULL, NULL, NULL));
179 void Database_SQLite3::verifyDatabase()
181 if (m_initialized) return;
185 PREPARE_STATEMENT(begin, "BEGIN");
186 PREPARE_STATEMENT(end, "COMMIT");
187 PREPARE_STATEMENT(read, "SELECT `data` FROM `blocks` WHERE `pos` = ? LIMIT 1");
189 PREPARE_STATEMENT(write, "INSERT INTO `blocks` (`pos`, `data`) VALUES (?, ?)");
191 PREPARE_STATEMENT(write, "REPLACE INTO `blocks` (`pos`, `data`) VALUES (?, ?)");
193 PREPARE_STATEMENT(delete, "DELETE FROM `blocks` WHERE `pos` = ?");
194 PREPARE_STATEMENT(list, "SELECT `pos` FROM `blocks`");
196 m_initialized = true;
198 verbosestream << "ServerMap: SQLite3 database opened." << std::endl;
201 inline void Database_SQLite3::bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index)
203 SQLOK(sqlite3_bind_int64(stmt, index, getBlockAsInteger(pos)));
206 bool Database_SQLite3::deleteBlock(const v3s16 &pos)
210 bindPos(m_stmt_delete, pos);
212 bool good = sqlite3_step(m_stmt_delete) == SQLITE_DONE;
213 sqlite3_reset(m_stmt_delete);
216 warningstream << "deleteBlock: Block failed to delete "
217 << PP(pos) << ": " << sqlite3_errmsg(m_database) << std::endl;
222 bool Database_SQLite3::saveBlock(const v3s16 &pos, const std::string &data)
228 * Note: For some unknown reason SQLite3 fails to REPLACE blocks on Android,
229 * deleting them and then inserting works.
231 bindPos(m_stmt_read, pos);
233 if (sqlite3_step(m_stmt_read) == SQLITE_ROW) {
236 sqlite3_reset(m_stmt_read);
239 bindPos(m_stmt_write, pos);
240 SQLOK(sqlite3_bind_blob(m_stmt_write, 2, data.data(), data.size(), NULL));
242 SQLRES(sqlite3_step(m_stmt_write), SQLITE_DONE)
243 sqlite3_reset(m_stmt_write);
248 std::string Database_SQLite3::loadBlock(const v3s16 &pos)
252 bindPos(m_stmt_read, pos);
254 if (sqlite3_step(m_stmt_read) != SQLITE_ROW) {
255 sqlite3_reset(m_stmt_read);
258 const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0);
259 size_t len = sqlite3_column_bytes(m_stmt_read, 0);
263 s = std::string(data, len);
265 sqlite3_step(m_stmt_read);
266 // We should never get more than 1 row, so ok to reset
267 sqlite3_reset(m_stmt_read);
272 void Database_SQLite3::createDatabase()
274 assert(m_database); // Pre-condition
275 SQLOK(sqlite3_exec(m_database,
276 "CREATE TABLE IF NOT EXISTS `blocks` (\n"
277 " `pos` INT PRIMARY KEY,\n"
283 void Database_SQLite3::listAllLoadableBlocks(std::vector<v3s16> &dst)
287 while (sqlite3_step(m_stmt_list) == SQLITE_ROW) {
288 dst.push_back(getIntegerAsBlock(sqlite3_column_int64(m_stmt_list, 0)));
290 sqlite3_reset(m_stmt_list);
293 Database_SQLite3::~Database_SQLite3()
295 FINALIZE_STATEMENT(m_stmt_read)
296 FINALIZE_STATEMENT(m_stmt_write)
297 FINALIZE_STATEMENT(m_stmt_list)
298 FINALIZE_STATEMENT(m_stmt_begin)
299 FINALIZE_STATEMENT(m_stmt_end)
300 FINALIZE_STATEMENT(m_stmt_delete)
302 if (sqlite3_close(m_database) != SQLITE_OK) {
303 errorstream << "Database_SQLite3::~Database_SQLite3(): "
304 << "Failed to close database: "
305 << sqlite3_errmsg(m_database) << std::endl;