Improve rollback database indexing
[oweals/minetest.git] / src / rollback.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 "rollback.h"
21 #include <fstream>
22 #include <list>
23 #include <sstream>
24 #include "log.h"
25 #include "mapnode.h"
26 #include "gamedef.h"
27 #include "nodedef.h"
28 #include "util/serialize.h"
29 #include "util/string.h"
30 #include "util/numeric.h"
31 #include "inventorymanager.h" // deserializing InventoryLocations
32 #include "sqlite3.h"
33 #include "filesys.h"
34
35 #define POINTS_PER_NODE (16.0)
36
37 #define SQLRES(f, good) \
38         if ((f) != (good)) {\
39                 throw FileNotGoodException(std::string("RollbackManager: " \
40                         "SQLite3 error (" __FILE__ ":" TOSTRING(__LINE__) \
41                         "): ") + sqlite3_errmsg(db)); \
42         }
43 #define SQLOK(f) SQLRES(f, SQLITE_OK)
44
45
46 class ItemStackRow : public ItemStack {
47 public:
48         ItemStackRow & operator = (const ItemStack & other)
49         {
50                 *static_cast<ItemStack *>(this) = other;
51                 return *this;
52         }
53
54         int id;
55 };
56
57 struct ActionRow {
58         int          id;
59         int          actor;
60         time_t       timestamp;
61         int          type;
62         std::string  location, list;
63         int          index, add;
64         ItemStackRow stack;
65         int          nodeMeta;
66         int          x, y, z;
67         int          oldNode;
68         int          oldParam1, oldParam2;
69         std::string  oldMeta;
70         int          newNode;
71         int          newParam1, newParam2;
72         std::string  newMeta;
73         int          guessed;
74 };
75
76
77 struct Entity {
78         int         id;
79         std::string name;
80 };
81
82
83
84 RollbackManager::RollbackManager(const std::string & world_path,
85                 IGameDef * gamedef_) :
86         gamedef(gamedef_),
87         current_actor_is_guess(false)
88 {
89         verbosestream << "RollbackManager::RollbackManager(" << world_path
90                 << ")" << std::endl;
91
92         std::string txt_filename = world_path + DIR_DELIM "rollback.txt";
93         std::string migrating_flag = txt_filename + ".migrating";
94         database_path = world_path + DIR_DELIM "rollback.sqlite";
95
96         initDatabase();
97
98         if (fs::PathExists(txt_filename) && (fs::PathExists(migrating_flag) ||
99                         !fs::PathExists(database_path))) {
100                 std::ofstream of(migrating_flag.c_str());
101                 of.close();
102                 migrate(txt_filename);
103                 fs::DeleteSingleFileOrEmptyDirectory(migrating_flag);
104         }
105 }
106
107
108 RollbackManager::~RollbackManager()
109 {
110         flush();
111
112         SQLOK(sqlite3_finalize(stmt_insert));
113         SQLOK(sqlite3_finalize(stmt_replace));
114         SQLOK(sqlite3_finalize(stmt_select));
115         SQLOK(sqlite3_finalize(stmt_select_range));
116         SQLOK(sqlite3_finalize(stmt_select_withActor));
117         SQLOK(sqlite3_finalize(stmt_knownActor_select));
118         SQLOK(sqlite3_finalize(stmt_knownActor_insert));
119         SQLOK(sqlite3_finalize(stmt_knownNode_select));
120         SQLOK(sqlite3_finalize(stmt_knownNode_insert));
121
122         SQLOK(sqlite3_close(db));
123 }
124
125
126 void RollbackManager::registerNewActor(const int id, const std::string &name)
127 {
128         Entity actor = {id, name};
129         knownActors.push_back(actor);
130 }
131
132
133 void RollbackManager::registerNewNode(const int id, const std::string &name)
134 {
135         Entity node = {id, name};
136         knownNodes.push_back(node);
137 }
138
139
140 int RollbackManager::getActorId(const std::string &name)
141 {
142         for (std::vector<Entity>::const_iterator iter = knownActors.begin();
143                         iter != knownActors.end(); ++iter) {
144                 if (iter->name == name) {
145                         return iter->id;
146                 }
147         }
148
149         SQLOK(sqlite3_bind_text(stmt_knownActor_insert, 1, name.c_str(), name.size(), NULL));
150         SQLRES(sqlite3_step(stmt_knownActor_insert), SQLITE_DONE);
151         SQLOK(sqlite3_reset(stmt_knownActor_insert));
152
153         int id = sqlite3_last_insert_rowid(db);
154         registerNewActor(id, name);
155
156         return id;
157 }
158
159
160 int RollbackManager::getNodeId(const std::string &name)
161 {
162         for (std::vector<Entity>::const_iterator iter = knownNodes.begin();
163                         iter != knownNodes.end(); ++iter) {
164                 if (iter->name == name) {
165                         return iter->id;
166                 }
167         }
168
169         SQLOK(sqlite3_bind_text(stmt_knownNode_insert, 1, name.c_str(), name.size(), NULL));
170         SQLRES(sqlite3_step(stmt_knownNode_insert), SQLITE_DONE);
171         SQLOK(sqlite3_reset(stmt_knownNode_insert));
172
173         int id = sqlite3_last_insert_rowid(db);
174         registerNewNode(id, name);
175
176         return id;
177 }
178
179
180 const char * RollbackManager::getActorName(const int id)
181 {
182         for (std::vector<Entity>::const_iterator iter = knownActors.begin();
183                         iter != knownActors.end(); ++iter) {
184                 if (iter->id == id) {
185                         return iter->name.c_str();
186                 }
187         }
188
189         return "";
190 }
191
192
193 const char * RollbackManager::getNodeName(const int id)
194 {
195         for (std::vector<Entity>::const_iterator iter = knownNodes.begin();
196                         iter != knownNodes.end(); ++iter) {
197                 if (iter->id == id) {
198                         return iter->name.c_str();
199                 }
200         }
201
202         return "";
203 }
204
205
206 bool RollbackManager::createTables()
207 {
208         SQLOK(sqlite3_exec(db,
209                 "CREATE TABLE IF NOT EXISTS `actor` (\n"
210                 "       `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
211                 "       `name` TEXT NOT NULL\n"
212                 ");\n"
213                 "CREATE TABLE IF NOT EXISTS `node` (\n"
214                 "       `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
215                 "       `name` TEXT NOT NULL\n"
216                 ");\n"
217                 "CREATE TABLE IF NOT EXISTS `action` (\n"
218                 "       `id` INTEGER PRIMARY KEY AUTOINCREMENT,\n"
219                 "       `actor` INTEGER NOT NULL,\n"
220                 "       `timestamp` TIMESTAMP NOT NULL,\n"
221                 "       `type` INTEGER NOT NULL,\n"
222                 "       `list` TEXT,\n"
223                 "       `index` INTEGER,\n"
224                 "       `add` INTEGER,\n"
225                 "       `stackNode` INTEGER,\n"
226                 "       `stackQuantity` INTEGER,\n"
227                 "       `nodeMeta` INTEGER,\n"
228                 "       `x` INT,\n"
229                 "       `y` INT,\n"
230                 "       `z` INT,\n"
231                 "       `oldNode` INTEGER,\n"
232                 "       `oldParam1` INTEGER,\n"
233                 "       `oldParam2` INTEGER,\n"
234                 "       `oldMeta` TEXT,\n"
235                 "       `newNode` INTEGER,\n"
236                 "       `newParam1` INTEGER,\n"
237                 "       `newParam2` INTEGER,\n"
238                 "       `newMeta` TEXT,\n"
239                 "       `guessedActor` INTEGER,\n"
240                 "       FOREIGN KEY (`actor`) REFERENCES `actor`(`id`),\n"
241                 "       FOREIGN KEY (`stackNode`) REFERENCES `node`(`id`),\n"
242                 "       FOREIGN KEY (`oldNode`)   REFERENCES `node`(`id`),\n"
243                 "       FOREIGN KEY (`newNode`)   REFERENCES `node`(`id`)\n"
244                 ");\n"
245                 "CREATE INDEX IF NOT EXISTS `actionIndex` ON `action`(`x`,`y`,`z`,`timestamp`,`actor`);\n",
246                 NULL, NULL, NULL));
247         verbosestream << "SQL Rollback: SQLite3 database structure was created" << std::endl;
248
249         return true;
250 }
251
252
253 void RollbackManager::initDatabase()
254 {
255         verbosestream << "RollbackManager: Database connection setup" << std::endl;
256
257         bool needsCreate = !fs::PathExists(database_path);
258         SQLOK(sqlite3_open_v2(database_path.c_str(), &db,
259                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL));
260
261         if (needsCreate) {
262                 createTables();
263         }
264
265         SQLOK(sqlite3_prepare_v2(db,
266                 "INSERT INTO `action` (\n"
267                 "       `actor`, `timestamp`, `type`,\n"
268                 "       `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,\n"
269                 "       `x`, `y`, `z`,\n"
270                 "       `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
271                 "       `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
272                 "       `guessedActor`\n"
273                 ") VALUES (\n"
274                 "       ?, ?, ?,\n"
275                 "       ?, ?, ?, ?, ?, ?,\n"
276                 "       ?, ?, ?,\n"
277                 "       ?, ?, ?, ?,\n"
278                 "       ?, ?, ?, ?,\n"
279                 "       ?"
280                 ");",
281                 -1, &stmt_insert, NULL));
282
283         SQLOK(sqlite3_prepare_v2(db,
284                 "REPLACE INTO `action` (\n"
285                 "       `actor`, `timestamp`, `type`,\n"
286                 "       `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,\n"
287                 "       `x`, `y`, `z`,\n"
288                 "       `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
289                 "       `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
290                 "       `guessedActor`, `id`\n"
291                 ") VALUES (\n"
292                 "       ?, ?, ?,\n"
293                 "       ?, ?, ?, ?, ?, ?,\n"
294                 "       ?, ?, ?,\n"
295                 "       ?, ?, ?, ?,\n"
296                 "       ?, ?, ?, ?,\n"
297                 "       ?, ?\n"
298                 ");",
299                 -1, &stmt_replace, NULL));
300
301         SQLOK(sqlite3_prepare_v2(db,
302                 "SELECT\n"
303                 "       `actor`, `timestamp`, `type`,\n"
304                 "       `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
305                 "       `x`, `y`, `z`,\n"
306                 "       `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
307                 "       `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
308                 "       `guessedActor`\n"
309                 " FROM `action`\n"
310                 " WHERE `timestamp` >= ?\n"
311                 " ORDER BY `timestamp` DESC, `id` DESC",
312                 -1, &stmt_select, NULL));
313
314         SQLOK(sqlite3_prepare_v2(db,
315                 "SELECT\n"
316                 "       `actor`, `timestamp`, `type`,\n"
317                 "       `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
318                 "       `x`, `y`, `z`,\n"
319                 "       `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
320                 "       `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
321                 "       `guessedActor`\n"
322                 "FROM `action`\n"
323                 "WHERE `timestamp` >= ?\n"
324                 "       AND `x` IS NOT NULL\n"
325                 "       AND `y` IS NOT NULL\n"
326                 "       AND `z` IS NOT NULL\n"
327                 "       AND `x` BETWEEN ? AND ?\n"
328                 "       AND `y` BETWEEN ? AND ?\n"
329                 "       AND `z` BETWEEN ? AND ?\n"
330                 "ORDER BY `timestamp` DESC, `id` DESC\n"
331                 "LIMIT 0,?",
332                 -1, &stmt_select_range, NULL));
333
334         SQLOK(sqlite3_prepare_v2(db,
335                 "SELECT\n"
336                 "       `actor`, `timestamp`, `type`,\n"
337                 "       `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
338                 "       `x`, `y`, `z`,\n"
339                 "       `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
340                 "       `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
341                 "       `guessedActor`\n"
342                 "FROM `action`\n"
343                 "WHERE `timestamp` >= ?\n"
344                 "       AND `actor` = ?\n"
345                 "ORDER BY `timestamp` DESC, `id` DESC\n",
346                 -1, &stmt_select_withActor, NULL));
347
348         SQLOK(sqlite3_prepare_v2(db, "SELECT `id`, `name` FROM `actor`",
349                         -1, &stmt_knownActor_select, NULL));
350
351         SQLOK(sqlite3_prepare_v2(db, "INSERT INTO `actor` (`name`) VALUES (?)",
352                         -1, &stmt_knownActor_insert, NULL));
353
354         SQLOK(sqlite3_prepare_v2(db, "SELECT `id`, `name` FROM `node`",
355                         -1, &stmt_knownNode_select, NULL));
356
357         SQLOK(sqlite3_prepare_v2(db, "INSERT INTO `node` (`name`) VALUES (?)",
358                         -1, &stmt_knownNode_insert, NULL));
359
360         verbosestream << "SQL prepared statements setup correctly" << std::endl;
361
362         while (sqlite3_step(stmt_knownActor_select) == SQLITE_ROW) {
363                 registerNewActor(
364                         sqlite3_column_int(stmt_knownActor_select, 0),
365                         reinterpret_cast<const char *>(sqlite3_column_text(stmt_knownActor_select, 1))
366                 );
367         }
368         SQLOK(sqlite3_reset(stmt_knownActor_select));
369
370         while (sqlite3_step(stmt_knownNode_select) == SQLITE_ROW) {
371                 registerNewNode(
372                         sqlite3_column_int(stmt_knownNode_select, 0),
373                         reinterpret_cast<const char *>(sqlite3_column_text(stmt_knownNode_select, 1))
374                 );
375         }
376         SQLOK(sqlite3_reset(stmt_knownNode_select));
377 }
378
379
380 bool RollbackManager::registerRow(const ActionRow & row)
381 {
382         sqlite3_stmt * stmt_do = (row.id) ? stmt_replace : stmt_insert;
383
384         bool nodeMeta = false;
385
386         SQLOK(sqlite3_bind_int  (stmt_do, 1, row.actor));
387         SQLOK(sqlite3_bind_int64(stmt_do, 2, row.timestamp));
388         SQLOK(sqlite3_bind_int  (stmt_do, 3, row.type));
389
390         if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
391                 const std::string & loc = row.location;
392                 nodeMeta = (loc.substr(0, 9) == "nodemeta:");
393
394                 SQLOK(sqlite3_bind_text(stmt_do, 4, row.list.c_str(), row.list.size(), NULL));
395                 SQLOK(sqlite3_bind_int (stmt_do, 5, row.index));
396                 SQLOK(sqlite3_bind_int (stmt_do, 6, row.add));
397                 SQLOK(sqlite3_bind_int (stmt_do, 7, row.stack.id));
398                 SQLOK(sqlite3_bind_int (stmt_do, 8, row.stack.count));
399                 SQLOK(sqlite3_bind_int (stmt_do, 9, (int) nodeMeta));
400
401                 if (nodeMeta) {
402                         std::string::size_type p1, p2;
403                         p1 = loc.find(':') + 1;
404                         p2 = loc.find(',');
405                         std::string x = loc.substr(p1, p2 - p1);
406                         p1 = p2 + 1;
407                         p2 = loc.find(',', p1);
408                         std::string y = loc.substr(p1, p2 - p1);
409                         std::string z = loc.substr(p2 + 1);
410                         SQLOK(sqlite3_bind_int(stmt_do, 10, atoi(x.c_str())));
411                         SQLOK(sqlite3_bind_int(stmt_do, 11, atoi(y.c_str())));
412                         SQLOK(sqlite3_bind_int(stmt_do, 12, atoi(z.c_str())));
413                 }
414         } else {
415                 SQLOK(sqlite3_bind_null(stmt_do, 4));
416                 SQLOK(sqlite3_bind_null(stmt_do, 5));
417                 SQLOK(sqlite3_bind_null(stmt_do, 6));
418                 SQLOK(sqlite3_bind_null(stmt_do, 7));
419                 SQLOK(sqlite3_bind_null(stmt_do, 8));
420                 SQLOK(sqlite3_bind_null(stmt_do, 9));
421         }
422
423         if (row.type == RollbackAction::TYPE_SET_NODE) {
424                 SQLOK(sqlite3_bind_int (stmt_do, 10, row.x));
425                 SQLOK(sqlite3_bind_int (stmt_do, 11, row.y));
426                 SQLOK(sqlite3_bind_int (stmt_do, 12, row.z));
427                 SQLOK(sqlite3_bind_int (stmt_do, 13, row.oldNode));
428                 SQLOK(sqlite3_bind_int (stmt_do, 14, row.oldParam1));
429                 SQLOK(sqlite3_bind_int (stmt_do, 15, row.oldParam2));
430                 SQLOK(sqlite3_bind_text(stmt_do, 16, row.oldMeta.c_str(), row.oldMeta.size(), NULL));
431                 SQLOK(sqlite3_bind_int (stmt_do, 17, row.newNode));
432                 SQLOK(sqlite3_bind_int (stmt_do, 18, row.newParam1));
433                 SQLOK(sqlite3_bind_int (stmt_do, 19, row.newParam2));
434                 SQLOK(sqlite3_bind_text(stmt_do, 20, row.newMeta.c_str(), row.newMeta.size(), NULL));
435                 SQLOK(sqlite3_bind_int (stmt_do, 21, row.guessed ? 1 : 0));
436         } else {
437                 if (!nodeMeta) {
438                         SQLOK(sqlite3_bind_null(stmt_do, 10));
439                         SQLOK(sqlite3_bind_null(stmt_do, 11));
440                         SQLOK(sqlite3_bind_null(stmt_do, 12));
441                 }
442                 SQLOK(sqlite3_bind_null(stmt_do, 13));
443                 SQLOK(sqlite3_bind_null(stmt_do, 14));
444                 SQLOK(sqlite3_bind_null(stmt_do, 15));
445                 SQLOK(sqlite3_bind_null(stmt_do, 16));
446                 SQLOK(sqlite3_bind_null(stmt_do, 17));
447                 SQLOK(sqlite3_bind_null(stmt_do, 18));
448                 SQLOK(sqlite3_bind_null(stmt_do, 19));
449                 SQLOK(sqlite3_bind_null(stmt_do, 20));
450                 SQLOK(sqlite3_bind_null(stmt_do, 21));
451         }
452
453         if (row.id) {
454                 SQLOK(sqlite3_bind_int(stmt_do, 22, row.id));
455         }
456
457         int written = sqlite3_step(stmt_do);
458
459         SQLOK(sqlite3_reset(stmt_do));
460
461         return written == SQLITE_DONE;
462 }
463
464
465 const std::list<ActionRow> RollbackManager::actionRowsFromSelect(sqlite3_stmt* stmt)
466 {
467         std::list<ActionRow> rows;
468         const unsigned char * text;
469         size_t size;
470
471         while (sqlite3_step(stmt) == SQLITE_ROW) {
472                 ActionRow row;
473
474                 row.actor     = sqlite3_column_int  (stmt, 0);
475                 row.timestamp = sqlite3_column_int64(stmt, 1);
476                 row.type      = sqlite3_column_int  (stmt, 2);
477
478                 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
479                         text = sqlite3_column_text (stmt, 3);
480                         size = sqlite3_column_bytes(stmt, 3);
481                         row.list        = std::string(reinterpret_cast<const char*>(text), size);
482                         row.index       = sqlite3_column_int(stmt, 4);
483                         row.add         = sqlite3_column_int(stmt, 5);
484                         row.stack.id    = sqlite3_column_int(stmt, 6);
485                         row.stack.count = sqlite3_column_int(stmt, 7);
486                         row.nodeMeta    = sqlite3_column_int(stmt, 8);
487                 }
488
489                 if (row.type == RollbackAction::TYPE_SET_NODE || row.nodeMeta) {
490                         row.x = sqlite3_column_int(stmt,  9);
491                         row.y = sqlite3_column_int(stmt, 10);
492                         row.z = sqlite3_column_int(stmt, 11);
493                 }
494
495                 if (row.type == RollbackAction::TYPE_SET_NODE) {
496                         row.oldNode   = sqlite3_column_int(stmt, 12);
497                         row.oldParam1 = sqlite3_column_int(stmt, 13);
498                         row.oldParam2 = sqlite3_column_int(stmt, 14);
499                         text = sqlite3_column_text (stmt, 15);
500                         size = sqlite3_column_bytes(stmt, 15);
501                         row.oldMeta   = std::string(reinterpret_cast<const char*>(text), size);
502                         row.newNode   = sqlite3_column_int(stmt, 16);
503                         row.newParam1 = sqlite3_column_int(stmt, 17);
504                         row.newParam2 = sqlite3_column_int(stmt, 18);
505                         text = sqlite3_column_text(stmt, 19);
506                         size = sqlite3_column_bytes(stmt, 19);
507                         row.newMeta   = std::string(reinterpret_cast<const char*>(text), size);
508                         row.guessed   = sqlite3_column_int(stmt, 20);
509                 }
510
511                 if (row.nodeMeta) {
512                         row.location = "nodemeta:";
513                         row.location += itos(row.x);
514                         row.location += ',';
515                         row.location += itos(row.y);
516                         row.location += ',';
517                         row.location += itos(row.z);
518                 } else {
519                         row.location = getActorName(row.actor);
520                 }
521
522                 rows.push_back(row);
523         }
524
525         SQLOK(sqlite3_reset(stmt));
526
527         return rows;
528 }
529
530
531 ActionRow RollbackManager::actionRowFromRollbackAction(const RollbackAction & action)
532 {
533         ActionRow row;
534
535         row.id        = 0;
536         row.actor     = getActorId(action.actor);
537         row.timestamp = action.unix_time;
538         row.type      = action.type;
539
540         if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
541                 row.location = action.inventory_location;
542                 row.list     = action.inventory_list;
543                 row.index    = action.inventory_index;
544                 row.add      = action.inventory_add;
545                 row.stack    = action.inventory_stack;
546                 row.stack.id = getNodeId(row.stack.name);
547         } else {
548                 row.x         = action.p.X;
549                 row.y         = action.p.Y;
550                 row.z         = action.p.Z;
551                 row.oldNode   = getNodeId(action.n_old.name);
552                 row.oldParam1 = action.n_old.param1;
553                 row.oldParam2 = action.n_old.param2;
554                 row.oldMeta   = action.n_old.meta;
555                 row.newNode   = getNodeId(action.n_new.name);
556                 row.newParam1 = action.n_new.param1;
557                 row.newParam2 = action.n_new.param2;
558                 row.newMeta   = action.n_new.meta;
559                 row.guessed   = action.actor_is_guess;
560         }
561
562         return row;
563 }
564
565
566 const std::list<RollbackAction> RollbackManager::rollbackActionsFromActionRows(
567                 const std::list<ActionRow> & rows)
568 {
569         std::list<RollbackAction> actions;
570
571         for (std::list<ActionRow>::const_iterator it = rows.begin();
572                         it != rows.end(); ++it) {
573                 RollbackAction action;
574                 action.actor     = (it->actor) ? getActorName(it->actor) : "";
575                 action.unix_time = it->timestamp;
576                 action.type      = static_cast<RollbackAction::Type>(it->type);
577
578                 switch (action.type) {
579                 case RollbackAction::TYPE_MODIFY_INVENTORY_STACK:
580                         action.inventory_location = it->location.c_str();
581                         action.inventory_list     = it->list;
582                         action.inventory_index    = it->index;
583                         action.inventory_add      = it->add;
584                         action.inventory_stack    = it->stack;
585                         if (action.inventory_stack.name.empty()) {
586                                 action.inventory_stack.name = getNodeName(it->stack.id);
587                         }
588                         break;
589
590                 case RollbackAction::TYPE_SET_NODE:
591                         action.p            = v3s16(it->x, it->y, it->z);
592                         action.n_old.name   = getNodeName(it->oldNode);
593                         action.n_old.param1 = it->oldParam1;
594                         action.n_old.param2 = it->oldParam2;
595                         action.n_old.meta   = it->oldMeta;
596                         action.n_new.name   = getNodeName(it->newNode);
597                         action.n_new.param1 = it->newParam1;
598                         action.n_new.param2 = it->newParam2;
599                         action.n_new.meta   = it->newMeta;
600                         break;
601
602                 default:
603                         throw ("W.T.F.");
604                         break;
605                 }
606
607                 actions.push_back(action);
608         }
609
610         return actions;
611 }
612
613
614 const std::list<ActionRow> RollbackManager::getRowsSince(time_t firstTime, const std::string & actor)
615 {
616         sqlite3_stmt *stmt_stmt = actor.empty() ? stmt_select : stmt_select_withActor;
617         sqlite3_bind_int64(stmt_stmt, 1, firstTime);
618
619         if (!actor.empty()) {
620                 sqlite3_bind_int(stmt_stmt, 2, getActorId(actor));
621         }
622
623         const std::list<ActionRow> & rows = actionRowsFromSelect(stmt_stmt);
624         sqlite3_reset(stmt_stmt);
625
626         return rows;
627 }
628
629
630 const std::list<ActionRow> RollbackManager::getRowsSince_range(
631                 time_t start_time, v3s16 p, int range, int limit)
632 {
633
634         sqlite3_bind_int64(stmt_select_range, 1, start_time);
635         sqlite3_bind_int  (stmt_select_range, 2, static_cast<int>(p.X - range));
636         sqlite3_bind_int  (stmt_select_range, 3, static_cast<int>(p.X + range));
637         sqlite3_bind_int  (stmt_select_range, 4, static_cast<int>(p.Y - range));
638         sqlite3_bind_int  (stmt_select_range, 5, static_cast<int>(p.Y + range));
639         sqlite3_bind_int  (stmt_select_range, 6, static_cast<int>(p.Z - range));
640         sqlite3_bind_int  (stmt_select_range, 7, static_cast<int>(p.Z + range));
641         sqlite3_bind_int  (stmt_select_range, 8, limit);
642
643         const std::list<ActionRow> & rows = actionRowsFromSelect(stmt_select_range);
644         sqlite3_reset(stmt_select_range);
645
646         return rows;
647 }
648
649
650 const std::list<RollbackAction> RollbackManager::getActionsSince_range(
651                 time_t start_time, v3s16 p, int range, int limit)
652 {
653         return rollbackActionsFromActionRows(getRowsSince_range(start_time, p, range, limit));
654 }
655
656
657 const std::list<RollbackAction> RollbackManager::getActionsSince(
658                 time_t start_time, const std::string & actor)
659 {
660         return rollbackActionsFromActionRows(getRowsSince(start_time, actor));
661 }
662
663
664 void RollbackManager::migrate(const std::string & file_path)
665 {
666         std::cout << "Migrating from rollback.txt to rollback.sqlite." << std::endl;
667
668         std::ifstream fh(file_path.c_str(), std::ios::in | std::ios::ate);
669         if (!fh.good()) {
670                 throw FileNotGoodException("Unable to open rollback.txt");
671         }
672
673         std::streampos file_size = fh.tellg();
674
675         if (file_size > 10) {
676                 errorstream << "Empty rollback log." << std::endl;
677                 return;
678         }
679
680         fh.seekg(0);
681
682         std::string bit;
683         int i = 0;
684         int id = 1;
685         time_t start = time(0);
686         time_t t = start;
687         sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);
688         do {
689                 ActionRow row;
690
691                 row.id = id;
692
693                 // Get the timestamp
694                 std::getline(fh, bit, ' ');
695                 bit = trim(bit);
696                 if (!atoi(bit.c_str())) {
697                         std::getline(fh, bit);
698                         continue;
699                 }
700                 row.timestamp = atoi(bit.c_str());
701
702                 // Get the actor
703                 row.actor = getActorId(deSerializeJsonString(fh));
704
705                 // Get the action type
706                 std::getline(fh, bit, '[');
707                 std::getline(fh, bit, ' ');
708
709                 if (bit == "modify_inventory_stack") {
710                         row.type = RollbackAction::TYPE_MODIFY_INVENTORY_STACK;
711                         row.location = trim(deSerializeJsonString(fh));
712                         std::getline(fh, bit, ' ');
713                         row.list     = trim(deSerializeJsonString(fh));
714                         std::getline(fh, bit, ' ');
715                         std::getline(fh, bit, ' ');
716                         row.index    = atoi(trim(bit).c_str());
717                         std::getline(fh, bit, ' ');
718                         row.add      = (int)(trim(bit) == "add");
719                         row.stack.deSerialize(deSerializeJsonString(fh));
720                         row.stack.id = getNodeId(row.stack.name);
721                         std::getline(fh, bit);
722                 } else if (bit == "set_node") {
723                         row.type = RollbackAction::TYPE_SET_NODE;
724                         std::getline(fh, bit, '(');
725                         std::getline(fh, bit, ',');
726                         row.x       = atoi(trim(bit).c_str());
727                         std::getline(fh, bit, ',');
728                         row.y       = atoi(trim(bit).c_str());
729                         std::getline(fh, bit, ')');
730                         row.z       = atoi(trim(bit).c_str());
731                         std::getline(fh, bit, ' ');
732                         row.oldNode = getNodeId(trim(deSerializeJsonString(fh)));
733                         std::getline(fh, bit, ' ');
734                         std::getline(fh, bit, ' ');
735                         row.oldParam1 = atoi(trim(bit).c_str());
736                         std::getline(fh, bit, ' ');
737                         row.oldParam2 = atoi(trim(bit).c_str());
738                         row.oldMeta   = trim(deSerializeJsonString(fh));
739                         std::getline(fh, bit, ' ');
740                         row.newNode   = getNodeId(trim(deSerializeJsonString(fh)));
741                         std::getline(fh, bit, ' ');
742                         std::getline(fh, bit, ' ');
743                         row.newParam1 = atoi(trim(bit).c_str());
744                         std::getline(fh, bit, ' ');
745                         row.newParam2 = atoi(trim(bit).c_str());
746                         row.newMeta   = trim(deSerializeJsonString(fh));
747                         std::getline(fh, bit, ' ');
748                         std::getline(fh, bit, ' ');
749                         std::getline(fh, bit);
750                         row.guessed = (int)(trim(bit) == "actor_is_guess");
751                 } else {
752                         errorstream << "Unrecognized rollback action type \""
753                                 << bit << "\"!" << std::endl;
754                         continue;
755                 }
756
757                 registerRow(row);
758                 ++i;
759
760                 if (time(0) - t >= 1) {
761                         sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
762                         t = time(0);
763                         std::cout
764                                 << " Done: " << static_cast<int>((static_cast<float>(fh.tellg()) / static_cast<float>(file_size)) * 100) << "%"
765                                 << " Speed: " << i / (t - start) << "/second     \r" << std::flush;
766                         sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);
767                 }
768         } while (fh.good());
769
770         std::cout
771                 << " Done: 100%                                   " << std::endl
772                 << "Now you can delete the old rollback.txt file." << std::endl;
773 }
774
775
776 // Get nearness factor for subject's action for this action
777 // Return value: 0 = impossible, >0 = factor
778 float RollbackManager::getSuspectNearness(bool is_guess, v3s16 suspect_p,
779                 time_t suspect_t, v3s16 action_p, time_t action_t)
780 {
781         // Suspect cannot cause things in the past
782         if (action_t < suspect_t) {
783                 return 0;        // 0 = cannot be
784         }
785         // Start from 100
786         int f = 100;
787         // Distance (1 node = -x points)
788         f -= POINTS_PER_NODE * intToFloat(suspect_p, 1).getDistanceFrom(intToFloat(action_p, 1));
789         // Time (1 second = -x points)
790         f -= 1 * (action_t - suspect_t);
791         // If is a guess, halve the points
792         if (is_guess) {
793                 f *= 0.5;
794         }
795         // Limit to 0
796         if (f < 0) {
797                 f = 0;
798         }
799         return f;
800 }
801
802
803 void RollbackManager::reportAction(const RollbackAction &action_)
804 {
805         // Ignore if not important
806         if (!action_.isImportant(gamedef)) {
807                 return;
808         }
809
810         RollbackAction action = action_;
811         action.unix_time = time(0);
812
813         // Figure out actor
814         action.actor = current_actor;
815         action.actor_is_guess = current_actor_is_guess;
816
817         if (action.actor.empty()) { // If actor is not known, find out suspect or cancel
818                 v3s16 p;
819                 if (!action.getPosition(&p)) {
820                         return;
821                 }
822
823                 action.actor = getSuspect(p, 83, 1);
824                 if (action.actor.empty()) {
825                         return;
826                 }
827
828                 action.actor_is_guess = true;
829         }
830
831         addAction(action);
832 }
833
834 std::string RollbackManager::getActor()
835 {
836         return current_actor;
837 }
838
839 bool RollbackManager::isActorGuess()
840 {
841         return current_actor_is_guess;
842 }
843
844 void RollbackManager::setActor(const std::string & actor, bool is_guess)
845 {
846         current_actor = actor;
847         current_actor_is_guess = is_guess;
848 }
849
850 std::string RollbackManager::getSuspect(v3s16 p, float nearness_shortcut,
851                 float min_nearness)
852 {
853         if (current_actor != "") {
854                 return current_actor;
855         }
856         int cur_time = time(0);
857         time_t first_time = cur_time - (100 - min_nearness);
858         RollbackAction likely_suspect;
859         float likely_suspect_nearness = 0;
860         for (std::list<RollbackAction>::const_reverse_iterator
861              i = action_latest_buffer.rbegin();
862              i != action_latest_buffer.rend(); ++i) {
863                 if (i->unix_time < first_time) {
864                         break;
865                 }
866                 if (i->actor == "") {
867                         continue;
868                 }
869                 // Find position of suspect or continue
870                 v3s16 suspect_p;
871                 if (!i->getPosition(&suspect_p)) {
872                         continue;
873                 }
874                 float f = getSuspectNearness(i->actor_is_guess, suspect_p,
875                                              i->unix_time, p, cur_time);
876                 if (f >= min_nearness && f > likely_suspect_nearness) {
877                         likely_suspect_nearness = f;
878                         likely_suspect = *i;
879                         if (likely_suspect_nearness >= nearness_shortcut) {
880                                 break;
881                         }
882                 }
883         }
884         // No likely suspect was found
885         if (likely_suspect_nearness == 0) {
886                 return "";
887         }
888         // Likely suspect was found
889         return likely_suspect.actor;
890 }
891
892
893 void RollbackManager::flush()
894 {
895         sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);
896
897         std::list<RollbackAction>::const_iterator iter;
898
899         for (iter  = action_todisk_buffer.begin();
900                         iter != action_todisk_buffer.end();
901                         ++iter) {
902                 if (iter->actor == "") {
903                         continue;
904                 }
905
906                 registerRow(actionRowFromRollbackAction(*iter));
907         }
908
909         sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
910         action_todisk_buffer.clear();
911 }
912
913
914 void RollbackManager::addAction(const RollbackAction & action)
915 {
916         action_todisk_buffer.push_back(action);
917         action_latest_buffer.push_back(action);
918
919         // Flush to disk sometimes
920         if (action_todisk_buffer.size() >= 500) {
921                 flush();
922         }
923 }
924
925 std::list<RollbackAction> RollbackManager::getEntriesSince(time_t first_time)
926 {
927         flush();
928         return getActionsSince(first_time);
929 }
930
931 std::list<RollbackAction> RollbackManager::getNodeActors(v3s16 pos, int range,
932                 time_t seconds, int limit)
933 {
934         flush();
935         time_t cur_time = time(0);
936         time_t first_time = cur_time - seconds;
937
938         return getActionsSince_range(first_time, pos, range, limit);
939 }
940
941 std::list<RollbackAction> RollbackManager::getRevertActions(
942                 const std::string &actor_filter,
943                 time_t seconds)
944 {
945         time_t cur_time = time(0);
946         time_t first_time = cur_time - seconds;
947
948         flush();
949
950         return getActionsSince(first_time, actor_filter);
951 }
952