database-sqlite3.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. /*
  17. SQLite format specification:
  18. - Initially only replaces sectors/ and sectors2/
  19. If map.sqlite does not exist in the save dir
  20. or the block was not found in the database
  21. the map will try to load from sectors folder.
  22. In either case, map.sqlite will be created
  23. and all future saves will save there.
  24. Structure of map.sqlite:
  25. Tables:
  26. blocks
  27. (PK) INT pos
  28. BLOB data
  29. */
  30. #include "database-sqlite3.h"
  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. Database_SQLite3::Database_SQLite3(ServerMap *map, std::string savedir)
  39. {
  40. m_database = NULL;
  41. m_database_read = NULL;
  42. m_database_write = NULL;
  43. m_database_list = NULL;
  44. m_savedir = savedir;
  45. srvmap = map;
  46. }
  47. int Database_SQLite3::Initialized(void)
  48. {
  49. return m_database ? 1 : 0;
  50. }
  51. void Database_SQLite3::beginSave() {
  52. verifyDatabase();
  53. if(sqlite3_exec(m_database, "BEGIN;", NULL, NULL, NULL) != SQLITE_OK)
  54. errorstream<<"WARNING: beginSave() failed, saving might be slow.";
  55. }
  56. void Database_SQLite3::endSave() {
  57. verifyDatabase();
  58. if(sqlite3_exec(m_database, "COMMIT;", NULL, NULL, NULL) != SQLITE_OK)
  59. errorstream<<"WARNING: endSave() failed, map might not have saved.";
  60. }
  61. void Database_SQLite3::createDirs(std::string path)
  62. {
  63. if(fs::CreateAllDirs(path) == false)
  64. {
  65. infostream<<DTIME<<"Database_SQLite3: Failed to create directory "
  66. <<"\""<<path<<"\""<<std::endl;
  67. throw BaseException("Database_SQLite3 failed to create directory");
  68. }
  69. }
  70. void Database_SQLite3::verifyDatabase() {
  71. if(m_database)
  72. return;
  73. std::string dbp = m_savedir + DIR_DELIM "map.sqlite";
  74. bool needs_create = false;
  75. int d;
  76. // Open the database connection
  77. createDirs(m_savedir); // ?
  78. if(!fs::PathExists(dbp))
  79. needs_create = true;
  80. d = sqlite3_open_v2(dbp.c_str(), &m_database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
  81. if(d != SQLITE_OK) {
  82. errorstream<<"SQLite3 database failed to open: "<<sqlite3_errmsg(m_database)<<std::endl;
  83. throw FileNotGoodException("Cannot open database file");
  84. }
  85. if(needs_create)
  86. createDatabase();
  87. std::string querystr = std::string("PRAGMA synchronous = ")
  88. + itos(g_settings->getU16("sqlite_synchronous"));
  89. d = sqlite3_exec(m_database, querystr.c_str(), NULL, NULL, NULL);
  90. if(d != SQLITE_OK) {
  91. errorstream<<"Database pragma set failed: "
  92. <<sqlite3_errmsg(m_database)<<std::endl;
  93. throw FileNotGoodException("Cannot set pragma");
  94. }
  95. d = sqlite3_prepare(m_database, "SELECT `data` FROM `blocks` WHERE `pos`=? LIMIT 1", -1, &m_database_read, NULL);
  96. if(d != SQLITE_OK) {
  97. errorstream<<"SQLite3 read statment failed to prepare: "<<sqlite3_errmsg(m_database)<<std::endl;
  98. throw FileNotGoodException("Cannot prepare read statement");
  99. }
  100. #ifdef __ANDROID__
  101. d = sqlite3_prepare(m_database, "INSERT INTO `blocks` VALUES(?, ?);", -1, &m_database_write, NULL);
  102. #else
  103. d = sqlite3_prepare(m_database, "REPLACE INTO `blocks` VALUES(?, ?);", -1, &m_database_write, NULL);
  104. #endif
  105. if(d != SQLITE_OK) {
  106. errorstream<<"SQLite3 write statment failed to prepare: "<<sqlite3_errmsg(m_database)<<std::endl;
  107. throw FileNotGoodException("Cannot prepare write statement");
  108. }
  109. #ifdef __ANDROID__
  110. d = sqlite3_prepare(m_database, "DELETE FROM `blocks` WHERE `pos`=?;", -1, &m_database_delete, NULL);
  111. if(d != SQLITE_OK) {
  112. infostream<<"WARNING: SQLite3 database delete statment failed to prepare: "<<sqlite3_errmsg(m_database)<<std::endl;
  113. throw FileNotGoodException("Cannot prepare delete statement");
  114. }
  115. #endif
  116. d = sqlite3_prepare(m_database, "SELECT `pos` FROM `blocks`", -1, &m_database_list, NULL);
  117. if(d != SQLITE_OK) {
  118. infostream<<"SQLite3 list statment failed to prepare: "<<sqlite3_errmsg(m_database)<<std::endl;
  119. throw FileNotGoodException("Cannot prepare read statement");
  120. }
  121. infostream<<"ServerMap: SQLite3 database opened"<<std::endl;
  122. }
  123. bool Database_SQLite3::saveBlock(v3s16 blockpos, std::string &data)
  124. {
  125. verifyDatabase();
  126. #ifdef __ANDROID__
  127. /**
  128. * Note: For some unknown reason sqlite3 fails to REPLACE blocks on android,
  129. * deleting them and inserting first works.
  130. */
  131. if (sqlite3_bind_int64(m_database_read, 1, getBlockAsInteger(blockpos)) != SQLITE_OK) {
  132. infostream << "WARNING: Could not bind block position for load: "
  133. << sqlite3_errmsg(m_database)<<std::endl;
  134. }
  135. if (sqlite3_step(m_database_read) == SQLITE_ROW) {
  136. if (sqlite3_bind_int64(m_database_delete, 1, getBlockAsInteger(blockpos)) != SQLITE_OK) {
  137. infostream << "WARNING: Could not bind block position for delete: "
  138. << sqlite3_errmsg(m_database)<<std::endl;
  139. }
  140. if (sqlite3_step(m_database_delete) != SQLITE_DONE) {
  141. errorstream << "WARNING: saveBlock: Block failed to delete "
  142. << PP(blockpos) << ": " << sqlite3_errmsg(m_database) << std::endl;
  143. return false;
  144. }
  145. sqlite3_reset(m_database_delete);
  146. }
  147. sqlite3_reset(m_database_read);
  148. #endif
  149. if (sqlite3_bind_int64(m_database_write, 1, getBlockAsInteger(blockpos)) != SQLITE_OK) {
  150. errorstream << "WARNING: saveBlock: Block position failed to bind: "
  151. << PP(blockpos) << ": " << sqlite3_errmsg(m_database) << std::endl;
  152. sqlite3_reset(m_database_write);
  153. return false;
  154. }
  155. if (sqlite3_bind_blob(m_database_write, 2, (void *) data.c_str(), data.size(), NULL) != SQLITE_OK) {
  156. errorstream << "WARNING: saveBlock: Block data failed to bind: "
  157. << PP(blockpos) << ": " << sqlite3_errmsg(m_database) << std::endl;
  158. sqlite3_reset(m_database_write);
  159. return false;
  160. }
  161. if (sqlite3_step(m_database_write) != SQLITE_DONE) {
  162. errorstream << "WARNING: saveBlock: Block failed to save "
  163. << PP(blockpos) << ": " << sqlite3_errmsg(m_database) << std::endl;
  164. sqlite3_reset(m_database_write);
  165. return false;
  166. }
  167. sqlite3_reset(m_database_write);
  168. return true;
  169. }
  170. std::string Database_SQLite3::loadBlock(v3s16 blockpos)
  171. {
  172. verifyDatabase();
  173. if (sqlite3_bind_int64(m_database_read, 1, getBlockAsInteger(blockpos)) != SQLITE_OK) {
  174. errorstream << "Could not bind block position for load: "
  175. << sqlite3_errmsg(m_database)<<std::endl;
  176. }
  177. if (sqlite3_step(m_database_read) == SQLITE_ROW) {
  178. const char *data = (const char *) sqlite3_column_blob(m_database_read, 0);
  179. size_t len = sqlite3_column_bytes(m_database_read, 0);
  180. std::string s = "";
  181. if(data)
  182. s = std::string(data, len);
  183. sqlite3_step(m_database_read);
  184. // We should never get more than 1 row, so ok to reset
  185. sqlite3_reset(m_database_read);
  186. return s;
  187. }
  188. sqlite3_reset(m_database_read);
  189. return "";
  190. }
  191. void Database_SQLite3::createDatabase()
  192. {
  193. int e;
  194. assert(m_database);
  195. e = sqlite3_exec(m_database,
  196. "CREATE TABLE IF NOT EXISTS `blocks` ("
  197. "`pos` INT NOT NULL PRIMARY KEY,"
  198. "`data` BLOB"
  199. ");"
  200. , NULL, NULL, NULL);
  201. if(e != SQLITE_OK)
  202. throw FileNotGoodException("Could not create sqlite3 database structure");
  203. else
  204. infostream<<"ServerMap: SQLite3 database structure was created";
  205. }
  206. void Database_SQLite3::listAllLoadableBlocks(std::list<v3s16> &dst)
  207. {
  208. verifyDatabase();
  209. while(sqlite3_step(m_database_list) == SQLITE_ROW)
  210. {
  211. sqlite3_int64 block_i = sqlite3_column_int64(m_database_list, 0);
  212. v3s16 p = getIntegerAsBlock(block_i);
  213. //dstream<<"block_i="<<block_i<<" p="<<PP(p)<<std::endl;
  214. dst.push_back(p);
  215. }
  216. }
  217. #define FINALIZE_STATEMENT(statement) \
  218. if ( statement ) \
  219. rc = sqlite3_finalize(statement); \
  220. if ( rc != SQLITE_OK ) \
  221. errorstream << "Database_SQLite3::~Database_SQLite3():" \
  222. << "Failed to finalize: " << #statement << ": rc=" << rc << std::endl;
  223. Database_SQLite3::~Database_SQLite3()
  224. {
  225. int rc = SQLITE_OK;
  226. FINALIZE_STATEMENT(m_database_read)
  227. FINALIZE_STATEMENT(m_database_write)
  228. FINALIZE_STATEMENT(m_database_list)
  229. if(m_database)
  230. rc = sqlite3_close(m_database);
  231. if (rc != SQLITE_OK) {
  232. errorstream << "Database_SQLite3::~Database_SQLite3(): "
  233. << "Failed to close database: rc=" << rc << std::endl;
  234. }
  235. }