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.
28 #include "util/serialize.h"
29 #include "util/string.h"
30 #include "util/numeric.h"
31 #include "inventorymanager.h" // deserializing InventoryLocations
35 #define POINTS_PER_NODE (16.0)
37 #define SQLRES(f, good) \
39 throw FileNotGoodException(std::string("RollbackManager: " \
40 "SQLite3 error (" __FILE__ ":" TOSTRING(__LINE__) \
41 "): ") + sqlite3_errmsg(db)); \
43 #define SQLOK(f) SQLRES(f, SQLITE_OK)
46 class ItemStackRow : public ItemStack {
48 ItemStackRow & operator = (const ItemStack & other)
50 *static_cast<ItemStack *>(this) = other;
62 std::string location, list;
68 int oldParam1, oldParam2;
71 int newParam1, newParam2;
84 RollbackManager::RollbackManager(const std::string & world_path,
85 IGameDef * gamedef_) :
87 current_actor_is_guess(false)
89 verbosestream << "RollbackManager::RollbackManager(" << world_path
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";
98 if (fs::PathExists(txt_filename) && (fs::PathExists(migrating_flag) ||
99 !fs::PathExists(database_path))) {
100 std::ofstream of(migrating_flag.c_str());
102 migrate(txt_filename);
103 fs::DeleteSingleFileOrEmptyDirectory(migrating_flag);
108 RollbackManager::~RollbackManager()
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));
122 SQLOK(sqlite3_close(db));
126 void RollbackManager::registerNewActor(const int id, const std::string &name)
128 Entity actor = {id, name};
129 knownActors.push_back(actor);
133 void RollbackManager::registerNewNode(const int id, const std::string &name)
135 Entity node = {id, name};
136 knownNodes.push_back(node);
140 int RollbackManager::getActorId(const std::string &name)
142 for (std::vector<Entity>::const_iterator iter = knownActors.begin();
143 iter != knownActors.end(); ++iter) {
144 if (iter->name == name) {
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));
153 int id = sqlite3_last_insert_rowid(db);
154 registerNewActor(id, name);
160 int RollbackManager::getNodeId(const std::string &name)
162 for (std::vector<Entity>::const_iterator iter = knownNodes.begin();
163 iter != knownNodes.end(); ++iter) {
164 if (iter->name == name) {
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));
173 int id = sqlite3_last_insert_rowid(db);
174 registerNewNode(id, name);
180 const char * RollbackManager::getActorName(const int id)
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();
193 const char * RollbackManager::getNodeName(const int id)
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();
206 bool RollbackManager::createTables()
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"
213 "CREATE TABLE IF NOT EXISTS `node` (\n"
214 " `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
215 " `name` TEXT NOT NULL\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"
223 " `index` INTEGER,\n"
225 " `stackNode` INTEGER,\n"
226 " `stackQuantity` INTEGER,\n"
227 " `nodeMeta` INTEGER,\n"
231 " `oldNode` INTEGER,\n"
232 " `oldParam1` INTEGER,\n"
233 " `oldParam2` INTEGER,\n"
235 " `newNode` INTEGER,\n"
236 " `newParam1` INTEGER,\n"
237 " `newParam2` INTEGER,\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"
245 "CREATE INDEX IF NOT EXISTS `actionActor` ON `action`(`actor`);\n"
246 "CREATE INDEX IF NOT EXISTS `actionTimestamp` ON `action`(`timestamp`);\n",
248 verbosestream << "SQL Rollback: SQLite3 database structure was created" << std::endl;
254 void RollbackManager::initDatabase()
256 verbosestream << "RollbackManager: Database connection setup" << std::endl;
258 bool needsCreate = !fs::PathExists(database_path);
259 SQLOK(sqlite3_open_v2(database_path.c_str(), &db,
260 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL));
266 SQLOK(sqlite3_prepare_v2(db,
267 "INSERT INTO `action` (\n"
268 " `actor`, `timestamp`, `type`,\n"
269 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,\n"
271 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
272 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
276 " ?, ?, ?, ?, ?, ?,\n"
282 -1, &stmt_insert, NULL));
284 SQLOK(sqlite3_prepare_v2(db,
285 "REPLACE INTO `action` (\n"
286 " `actor`, `timestamp`, `type`,\n"
287 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,\n"
289 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
290 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
291 " `guessedActor`, `id`\n"
294 " ?, ?, ?, ?, ?, ?,\n"
300 -1, &stmt_replace, NULL));
302 SQLOK(sqlite3_prepare_v2(db,
304 " `actor`, `timestamp`, `type`,\n"
305 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
307 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
308 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
311 " WHERE `timestamp` >= ?\n"
312 " ORDER BY `timestamp` DESC, `id` DESC",
313 -1, &stmt_select, NULL));
315 SQLOK(sqlite3_prepare_v2(db,
317 " `actor`, `timestamp`, `type`,\n"
318 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
320 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
321 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
324 "WHERE `timestamp` >= ?\n"
325 " AND `x` IS NOT NULL\n"
326 " AND `y` IS NOT NULL\n"
327 " AND `z` IS NOT NULL\n"
328 " AND `x` BETWEEN ? AND ?\n"
329 " AND `y` BETWEEN ? AND ?\n"
330 " AND `z` BETWEEN ? AND ?\n"
331 "ORDER BY `timestamp` DESC, `id` DESC\n"
333 -1, &stmt_select_range, NULL));
335 SQLOK(sqlite3_prepare_v2(db,
337 " `actor`, `timestamp`, `type`,\n"
338 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
340 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
341 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
344 "WHERE `timestamp` >= ?\n"
346 "ORDER BY `timestamp` DESC, `id` DESC\n",
347 -1, &stmt_select_withActor, NULL));
349 SQLOK(sqlite3_prepare_v2(db, "SELECT `id`, `name` FROM `actor`",
350 -1, &stmt_knownActor_select, NULL));
352 SQLOK(sqlite3_prepare_v2(db, "INSERT INTO `actor` (`name`) VALUES (?)",
353 -1, &stmt_knownActor_insert, NULL));
355 SQLOK(sqlite3_prepare_v2(db, "SELECT `id`, `name` FROM `node`",
356 -1, &stmt_knownNode_select, NULL));
358 SQLOK(sqlite3_prepare_v2(db, "INSERT INTO `node` (`name`) VALUES (?)",
359 -1, &stmt_knownNode_insert, NULL));
361 verbosestream << "SQL prepared statements setup correctly" << std::endl;
363 while (sqlite3_step(stmt_knownActor_select) == SQLITE_ROW) {
365 sqlite3_column_int(stmt_knownActor_select, 0),
366 reinterpret_cast<const char *>(sqlite3_column_text(stmt_knownActor_select, 1))
369 SQLOK(sqlite3_reset(stmt_knownActor_select));
371 while (sqlite3_step(stmt_knownNode_select) == SQLITE_ROW) {
373 sqlite3_column_int(stmt_knownNode_select, 0),
374 reinterpret_cast<const char *>(sqlite3_column_text(stmt_knownNode_select, 1))
377 SQLOK(sqlite3_reset(stmt_knownNode_select));
381 bool RollbackManager::registerRow(const ActionRow & row)
383 sqlite3_stmt * stmt_do = (row.id) ? stmt_replace : stmt_insert;
385 bool nodeMeta = false;
387 SQLOK(sqlite3_bind_int (stmt_do, 1, row.actor));
388 SQLOK(sqlite3_bind_int64(stmt_do, 2, row.timestamp));
389 SQLOK(sqlite3_bind_int (stmt_do, 3, row.type));
391 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
392 const std::string & loc = row.location;
393 nodeMeta = (loc.substr(0, 9) == "nodemeta:");
395 SQLOK(sqlite3_bind_text(stmt_do, 4, row.list.c_str(), row.list.size(), NULL));
396 SQLOK(sqlite3_bind_int (stmt_do, 5, row.index));
397 SQLOK(sqlite3_bind_int (stmt_do, 6, row.add));
398 SQLOK(sqlite3_bind_int (stmt_do, 7, row.stack.id));
399 SQLOK(sqlite3_bind_int (stmt_do, 8, row.stack.count));
400 SQLOK(sqlite3_bind_int (stmt_do, 9, (int) nodeMeta));
403 std::string::size_type p1, p2;
404 p1 = loc.find(':') + 1;
406 std::string x = loc.substr(p1, p2 - p1);
408 p2 = loc.find(',', p1);
409 std::string y = loc.substr(p1, p2 - p1);
410 std::string z = loc.substr(p2 + 1);
411 SQLOK(sqlite3_bind_int(stmt_do, 10, atoi(x.c_str())));
412 SQLOK(sqlite3_bind_int(stmt_do, 11, atoi(y.c_str())));
413 SQLOK(sqlite3_bind_int(stmt_do, 12, atoi(z.c_str())));
416 SQLOK(sqlite3_bind_null(stmt_do, 4));
417 SQLOK(sqlite3_bind_null(stmt_do, 5));
418 SQLOK(sqlite3_bind_null(stmt_do, 6));
419 SQLOK(sqlite3_bind_null(stmt_do, 7));
420 SQLOK(sqlite3_bind_null(stmt_do, 8));
421 SQLOK(sqlite3_bind_null(stmt_do, 9));
424 if (row.type == RollbackAction::TYPE_SET_NODE) {
425 SQLOK(sqlite3_bind_int (stmt_do, 10, row.x));
426 SQLOK(sqlite3_bind_int (stmt_do, 11, row.y));
427 SQLOK(sqlite3_bind_int (stmt_do, 12, row.z));
428 SQLOK(sqlite3_bind_int (stmt_do, 13, row.oldNode));
429 SQLOK(sqlite3_bind_int (stmt_do, 14, row.oldParam1));
430 SQLOK(sqlite3_bind_int (stmt_do, 15, row.oldParam2));
431 SQLOK(sqlite3_bind_text(stmt_do, 16, row.oldMeta.c_str(), row.oldMeta.size(), NULL));
432 SQLOK(sqlite3_bind_int (stmt_do, 17, row.newNode));
433 SQLOK(sqlite3_bind_int (stmt_do, 18, row.newParam1));
434 SQLOK(sqlite3_bind_int (stmt_do, 19, row.newParam2));
435 SQLOK(sqlite3_bind_text(stmt_do, 20, row.newMeta.c_str(), row.newMeta.size(), NULL));
436 SQLOK(sqlite3_bind_int (stmt_do, 21, row.guessed ? 1 : 0));
439 SQLOK(sqlite3_bind_null(stmt_do, 10));
440 SQLOK(sqlite3_bind_null(stmt_do, 11));
441 SQLOK(sqlite3_bind_null(stmt_do, 12));
443 SQLOK(sqlite3_bind_null(stmt_do, 13));
444 SQLOK(sqlite3_bind_null(stmt_do, 14));
445 SQLOK(sqlite3_bind_null(stmt_do, 15));
446 SQLOK(sqlite3_bind_null(stmt_do, 16));
447 SQLOK(sqlite3_bind_null(stmt_do, 17));
448 SQLOK(sqlite3_bind_null(stmt_do, 18));
449 SQLOK(sqlite3_bind_null(stmt_do, 19));
450 SQLOK(sqlite3_bind_null(stmt_do, 20));
451 SQLOK(sqlite3_bind_null(stmt_do, 21));
455 SQLOK(sqlite3_bind_int(stmt_do, 22, row.id));
458 int written = sqlite3_step(stmt_do);
460 SQLOK(sqlite3_reset(stmt_do));
462 return written == SQLITE_DONE;
466 const std::list<ActionRow> RollbackManager::actionRowsFromSelect(sqlite3_stmt* stmt)
468 std::list<ActionRow> rows;
469 const unsigned char * text;
472 while (sqlite3_step(stmt) == SQLITE_ROW) {
475 row.actor = sqlite3_column_int (stmt, 0);
476 row.timestamp = sqlite3_column_int64(stmt, 1);
477 row.type = sqlite3_column_int (stmt, 2);
479 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
480 text = sqlite3_column_text (stmt, 3);
481 size = sqlite3_column_bytes(stmt, 3);
482 row.list = std::string(reinterpret_cast<const char*>(text), size);
483 row.index = sqlite3_column_int(stmt, 4);
484 row.add = sqlite3_column_int(stmt, 5);
485 row.stack.id = sqlite3_column_int(stmt, 6);
486 row.stack.count = sqlite3_column_int(stmt, 7);
487 row.nodeMeta = sqlite3_column_int(stmt, 8);
490 if (row.type == RollbackAction::TYPE_SET_NODE || row.nodeMeta) {
491 row.x = sqlite3_column_int(stmt, 9);
492 row.y = sqlite3_column_int(stmt, 10);
493 row.z = sqlite3_column_int(stmt, 11);
496 if (row.type == RollbackAction::TYPE_SET_NODE) {
497 row.oldNode = sqlite3_column_int(stmt, 12);
498 row.oldParam1 = sqlite3_column_int(stmt, 13);
499 row.oldParam2 = sqlite3_column_int(stmt, 14);
500 text = sqlite3_column_text (stmt, 15);
501 size = sqlite3_column_bytes(stmt, 15);
502 row.oldMeta = std::string(reinterpret_cast<const char*>(text), size);
503 row.newNode = sqlite3_column_int(stmt, 16);
504 row.newParam1 = sqlite3_column_int(stmt, 17);
505 row.newParam2 = sqlite3_column_int(stmt, 18);
506 text = sqlite3_column_text(stmt, 19);
507 size = sqlite3_column_bytes(stmt, 19);
508 row.newMeta = std::string(reinterpret_cast<const char*>(text), size);
509 row.guessed = sqlite3_column_int(stmt, 20);
513 row.location = "nodemeta:";
514 row.location += itos(row.x);
516 row.location += itos(row.y);
518 row.location += itos(row.z);
520 row.location = getActorName(row.actor);
526 SQLOK(sqlite3_reset(stmt));
532 ActionRow RollbackManager::actionRowFromRollbackAction(const RollbackAction & action)
537 row.actor = getActorId(action.actor);
538 row.timestamp = action.unix_time;
539 row.type = action.type;
541 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
542 row.location = action.inventory_location;
543 row.list = action.inventory_list;
544 row.index = action.inventory_index;
545 row.add = action.inventory_add;
546 row.stack = action.inventory_stack;
547 row.stack.id = getNodeId(row.stack.name);
552 row.oldNode = getNodeId(action.n_old.name);
553 row.oldParam1 = action.n_old.param1;
554 row.oldParam2 = action.n_old.param2;
555 row.oldMeta = action.n_old.meta;
556 row.newNode = getNodeId(action.n_new.name);
557 row.newParam1 = action.n_new.param1;
558 row.newParam2 = action.n_new.param2;
559 row.newMeta = action.n_new.meta;
560 row.guessed = action.actor_is_guess;
567 const std::list<RollbackAction> RollbackManager::rollbackActionsFromActionRows(
568 const std::list<ActionRow> & rows)
570 std::list<RollbackAction> actions;
572 for (std::list<ActionRow>::const_iterator it = rows.begin();
573 it != rows.end(); ++it) {
574 RollbackAction action;
575 action.actor = (it->actor) ? getActorName(it->actor) : "";
576 action.unix_time = it->timestamp;
577 action.type = static_cast<RollbackAction::Type>(it->type);
579 switch (action.type) {
580 case RollbackAction::TYPE_MODIFY_INVENTORY_STACK:
581 action.inventory_location = it->location.c_str();
582 action.inventory_list = it->list;
583 action.inventory_index = it->index;
584 action.inventory_add = it->add;
585 action.inventory_stack = it->stack;
586 if (action.inventory_stack.name.empty()) {
587 action.inventory_stack.name = getNodeName(it->stack.id);
591 case RollbackAction::TYPE_SET_NODE:
592 action.p = v3s16(it->x, it->y, it->z);
593 action.n_old.name = getNodeName(it->oldNode);
594 action.n_old.param1 = it->oldParam1;
595 action.n_old.param2 = it->oldParam2;
596 action.n_old.meta = it->oldMeta;
597 action.n_new.name = getNodeName(it->newNode);
598 action.n_new.param1 = it->newParam1;
599 action.n_new.param2 = it->newParam2;
600 action.n_new.meta = it->newMeta;
608 actions.push_back(action);
615 const std::list<ActionRow> RollbackManager::getRowsSince(time_t firstTime, const std::string & actor)
617 sqlite3_stmt *stmt_stmt = actor.empty() ? stmt_select : stmt_select_withActor;
618 sqlite3_bind_int64(stmt_stmt, 1, firstTime);
620 if (!actor.empty()) {
621 sqlite3_bind_int(stmt_stmt, 2, getActorId(actor));
624 const std::list<ActionRow> & rows = actionRowsFromSelect(stmt_stmt);
625 sqlite3_reset(stmt_stmt);
631 const std::list<ActionRow> RollbackManager::getRowsSince_range(
632 time_t start_time, v3s16 p, int range, int limit)
635 sqlite3_bind_int64(stmt_select_range, 1, start_time);
636 sqlite3_bind_int (stmt_select_range, 2, static_cast<int>(p.X - range));
637 sqlite3_bind_int (stmt_select_range, 3, static_cast<int>(p.X + range));
638 sqlite3_bind_int (stmt_select_range, 4, static_cast<int>(p.Y - range));
639 sqlite3_bind_int (stmt_select_range, 5, static_cast<int>(p.Y + range));
640 sqlite3_bind_int (stmt_select_range, 6, static_cast<int>(p.Z - range));
641 sqlite3_bind_int (stmt_select_range, 7, static_cast<int>(p.Z + range));
642 sqlite3_bind_int (stmt_select_range, 8, limit);
644 const std::list<ActionRow> & rows = actionRowsFromSelect(stmt_select_range);
645 sqlite3_reset(stmt_select_range);
651 const std::list<RollbackAction> RollbackManager::getActionsSince_range(
652 time_t start_time, v3s16 p, int range, int limit)
654 return rollbackActionsFromActionRows(getRowsSince_range(start_time, p, range, limit));
658 const std::list<RollbackAction> RollbackManager::getActionsSince(
659 time_t start_time, const std::string & actor)
661 return rollbackActionsFromActionRows(getRowsSince(start_time, actor));
665 void RollbackManager::migrate(const std::string & file_path)
667 std::cout << "Migrating from rollback.txt to rollback.sqlite." << std::endl;
669 std::ifstream fh(file_path.c_str(), std::ios::in | std::ios::ate);
671 throw FileNotGoodException("Unable to open rollback.txt");
674 std::streampos file_size = fh.tellg();
676 if (file_size > 10) {
677 errorstream << "Empty rollback log." << std::endl;
686 time_t start = time(0);
688 sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);
695 std::getline(fh, bit, ' ');
697 if (!atoi(bit.c_str())) {
698 std::getline(fh, bit);
701 row.timestamp = atoi(bit.c_str());
704 row.actor = getActorId(deSerializeJsonString(fh));
706 // Get the action type
707 std::getline(fh, bit, '[');
708 std::getline(fh, bit, ' ');
710 if (bit == "modify_inventory_stack") {
711 row.type = RollbackAction::TYPE_MODIFY_INVENTORY_STACK;
712 row.location = trim(deSerializeJsonString(fh));
713 std::getline(fh, bit, ' ');
714 row.list = trim(deSerializeJsonString(fh));
715 std::getline(fh, bit, ' ');
716 std::getline(fh, bit, ' ');
717 row.index = atoi(trim(bit).c_str());
718 std::getline(fh, bit, ' ');
719 row.add = (int)(trim(bit) == "add");
720 row.stack.deSerialize(deSerializeJsonString(fh));
721 row.stack.id = getNodeId(row.stack.name);
722 std::getline(fh, bit);
723 } else if (bit == "set_node") {
724 row.type = RollbackAction::TYPE_SET_NODE;
725 std::getline(fh, bit, '(');
726 std::getline(fh, bit, ',');
727 row.x = atoi(trim(bit).c_str());
728 std::getline(fh, bit, ',');
729 row.y = atoi(trim(bit).c_str());
730 std::getline(fh, bit, ')');
731 row.z = atoi(trim(bit).c_str());
732 std::getline(fh, bit, ' ');
733 row.oldNode = getNodeId(trim(deSerializeJsonString(fh)));
734 std::getline(fh, bit, ' ');
735 std::getline(fh, bit, ' ');
736 row.oldParam1 = atoi(trim(bit).c_str());
737 std::getline(fh, bit, ' ');
738 row.oldParam2 = atoi(trim(bit).c_str());
739 row.oldMeta = trim(deSerializeJsonString(fh));
740 std::getline(fh, bit, ' ');
741 row.newNode = getNodeId(trim(deSerializeJsonString(fh)));
742 std::getline(fh, bit, ' ');
743 std::getline(fh, bit, ' ');
744 row.newParam1 = atoi(trim(bit).c_str());
745 std::getline(fh, bit, ' ');
746 row.newParam2 = atoi(trim(bit).c_str());
747 row.newMeta = trim(deSerializeJsonString(fh));
748 std::getline(fh, bit, ' ');
749 std::getline(fh, bit, ' ');
750 std::getline(fh, bit);
751 row.guessed = (int)(trim(bit) == "actor_is_guess");
753 errorstream << "Unrecognized rollback action type \""
754 << bit << "\"!" << std::endl;
761 if (time(0) - t >= 1) {
762 sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
765 << " Done: " << static_cast<int>((static_cast<float>(fh.tellg()) / static_cast<float>(file_size)) * 100) << "%"
766 << " Speed: " << i / (t - start) << "/second \r" << std::flush;
767 sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);
772 << " Done: 100% " << std::endl
773 << "Now you can delete the old rollback.txt file." << std::endl;
777 // Get nearness factor for subject's action for this action
778 // Return value: 0 = impossible, >0 = factor
779 float RollbackManager::getSuspectNearness(bool is_guess, v3s16 suspect_p,
780 time_t suspect_t, v3s16 action_p, time_t action_t)
782 // Suspect cannot cause things in the past
783 if (action_t < suspect_t) {
784 return 0; // 0 = cannot be
788 // Distance (1 node = -x points)
789 f -= POINTS_PER_NODE * intToFloat(suspect_p, 1).getDistanceFrom(intToFloat(action_p, 1));
790 // Time (1 second = -x points)
791 f -= 1 * (action_t - suspect_t);
792 // If is a guess, halve the points
804 void RollbackManager::reportAction(const RollbackAction &action_)
806 // Ignore if not important
807 if (!action_.isImportant(gamedef)) {
811 RollbackAction action = action_;
812 action.unix_time = time(0);
815 action.actor = current_actor;
816 action.actor_is_guess = current_actor_is_guess;
818 if (action.actor.empty()) { // If actor is not known, find out suspect or cancel
820 if (!action.getPosition(&p)) {
824 action.actor = getSuspect(p, 83, 1);
825 if (action.actor.empty()) {
829 action.actor_is_guess = true;
835 std::string RollbackManager::getActor()
837 return current_actor;
840 bool RollbackManager::isActorGuess()
842 return current_actor_is_guess;
845 void RollbackManager::setActor(const std::string & actor, bool is_guess)
847 current_actor = actor;
848 current_actor_is_guess = is_guess;
851 std::string RollbackManager::getSuspect(v3s16 p, float nearness_shortcut,
854 if (current_actor != "") {
855 return current_actor;
857 int cur_time = time(0);
858 time_t first_time = cur_time - (100 - min_nearness);
859 RollbackAction likely_suspect;
860 float likely_suspect_nearness = 0;
861 for (std::list<RollbackAction>::const_reverse_iterator
862 i = action_latest_buffer.rbegin();
863 i != action_latest_buffer.rend(); ++i) {
864 if (i->unix_time < first_time) {
867 if (i->actor == "") {
870 // Find position of suspect or continue
872 if (!i->getPosition(&suspect_p)) {
875 float f = getSuspectNearness(i->actor_is_guess, suspect_p,
876 i->unix_time, p, cur_time);
877 if (f >= min_nearness && f > likely_suspect_nearness) {
878 likely_suspect_nearness = f;
880 if (likely_suspect_nearness >= nearness_shortcut) {
885 // No likely suspect was found
886 if (likely_suspect_nearness == 0) {
889 // Likely suspect was found
890 return likely_suspect.actor;
894 void RollbackManager::flush()
896 sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);
898 std::list<RollbackAction>::const_iterator iter;
900 for (iter = action_todisk_buffer.begin();
901 iter != action_todisk_buffer.end();
903 if (iter->actor == "") {
907 registerRow(actionRowFromRollbackAction(*iter));
910 sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
911 action_todisk_buffer.clear();
915 void RollbackManager::addAction(const RollbackAction & action)
917 action_todisk_buffer.push_back(action);
918 action_latest_buffer.push_back(action);
920 // Flush to disk sometimes
921 if (action_todisk_buffer.size() >= 500) {
926 std::list<RollbackAction> RollbackManager::getEntriesSince(time_t first_time)
929 return getActionsSince(first_time);
932 std::list<RollbackAction> RollbackManager::getNodeActors(v3s16 pos, int range,
933 time_t seconds, int limit)
936 time_t cur_time = time(0);
937 time_t first_time = cur_time - seconds;
939 return getActionsSince_range(first_time, pos, range, limit);
942 std::list<RollbackAction> RollbackManager::getRevertActions(
943 const std::string &actor_filter,
946 time_t cur_time = time(0);
947 time_t first_time = cur_time - seconds;
951 return getActionsSince(first_time, actor_filter);