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 `actionIndex` ON `action`(`x`,`y`,`z`,`timestamp`,`actor`);\n",
247 verbosestream << "SQL Rollback: SQLite3 database structure was created" << std::endl;
253 void RollbackManager::initDatabase()
255 verbosestream << "RollbackManager: Database connection setup" << std::endl;
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));
265 SQLOK(sqlite3_prepare_v2(db,
266 "INSERT INTO `action` (\n"
267 " `actor`, `timestamp`, `type`,\n"
268 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,\n"
270 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
271 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
275 " ?, ?, ?, ?, ?, ?,\n"
281 -1, &stmt_insert, NULL));
283 SQLOK(sqlite3_prepare_v2(db,
284 "REPLACE INTO `action` (\n"
285 " `actor`, `timestamp`, `type`,\n"
286 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,\n"
288 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
289 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
290 " `guessedActor`, `id`\n"
293 " ?, ?, ?, ?, ?, ?,\n"
299 -1, &stmt_replace, NULL));
301 SQLOK(sqlite3_prepare_v2(db,
303 " `actor`, `timestamp`, `type`,\n"
304 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
306 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
307 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
310 " WHERE `timestamp` >= ?\n"
311 " ORDER BY `timestamp` DESC, `id` DESC",
312 -1, &stmt_select, NULL));
314 SQLOK(sqlite3_prepare_v2(db,
316 " `actor`, `timestamp`, `type`,\n"
317 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
319 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
320 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\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"
332 -1, &stmt_select_range, NULL));
334 SQLOK(sqlite3_prepare_v2(db,
336 " `actor`, `timestamp`, `type`,\n"
337 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,\n"
339 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,\n"
340 " `newNode`, `newParam1`, `newParam2`, `newMeta`,\n"
343 "WHERE `timestamp` >= ?\n"
345 "ORDER BY `timestamp` DESC, `id` DESC\n",
346 -1, &stmt_select_withActor, NULL));
348 SQLOK(sqlite3_prepare_v2(db, "SELECT `id`, `name` FROM `actor`",
349 -1, &stmt_knownActor_select, NULL));
351 SQLOK(sqlite3_prepare_v2(db, "INSERT INTO `actor` (`name`) VALUES (?)",
352 -1, &stmt_knownActor_insert, NULL));
354 SQLOK(sqlite3_prepare_v2(db, "SELECT `id`, `name` FROM `node`",
355 -1, &stmt_knownNode_select, NULL));
357 SQLOK(sqlite3_prepare_v2(db, "INSERT INTO `node` (`name`) VALUES (?)",
358 -1, &stmt_knownNode_insert, NULL));
360 verbosestream << "SQL prepared statements setup correctly" << std::endl;
362 while (sqlite3_step(stmt_knownActor_select) == SQLITE_ROW) {
364 sqlite3_column_int(stmt_knownActor_select, 0),
365 reinterpret_cast<const char *>(sqlite3_column_text(stmt_knownActor_select, 1))
368 SQLOK(sqlite3_reset(stmt_knownActor_select));
370 while (sqlite3_step(stmt_knownNode_select) == SQLITE_ROW) {
372 sqlite3_column_int(stmt_knownNode_select, 0),
373 reinterpret_cast<const char *>(sqlite3_column_text(stmt_knownNode_select, 1))
376 SQLOK(sqlite3_reset(stmt_knownNode_select));
380 bool RollbackManager::registerRow(const ActionRow & row)
382 sqlite3_stmt * stmt_do = (row.id) ? stmt_replace : stmt_insert;
384 bool nodeMeta = false;
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));
390 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
391 const std::string & loc = row.location;
392 nodeMeta = (loc.substr(0, 9) == "nodemeta:");
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));
402 std::string::size_type p1, p2;
403 p1 = loc.find(':') + 1;
405 std::string x = loc.substr(p1, p2 - p1);
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())));
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));
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));
438 SQLOK(sqlite3_bind_null(stmt_do, 10));
439 SQLOK(sqlite3_bind_null(stmt_do, 11));
440 SQLOK(sqlite3_bind_null(stmt_do, 12));
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));
454 SQLOK(sqlite3_bind_int(stmt_do, 22, row.id));
457 int written = sqlite3_step(stmt_do);
459 SQLOK(sqlite3_reset(stmt_do));
461 return written == SQLITE_DONE;
465 const std::list<ActionRow> RollbackManager::actionRowsFromSelect(sqlite3_stmt* stmt)
467 std::list<ActionRow> rows;
468 const unsigned char * text;
471 while (sqlite3_step(stmt) == SQLITE_ROW) {
474 row.actor = sqlite3_column_int (stmt, 0);
475 row.timestamp = sqlite3_column_int64(stmt, 1);
476 row.type = sqlite3_column_int (stmt, 2);
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);
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);
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);
512 row.location = "nodemeta:";
513 row.location += itos(row.x);
515 row.location += itos(row.y);
517 row.location += itos(row.z);
519 row.location = getActorName(row.actor);
525 SQLOK(sqlite3_reset(stmt));
531 ActionRow RollbackManager::actionRowFromRollbackAction(const RollbackAction & action)
536 row.actor = getActorId(action.actor);
537 row.timestamp = action.unix_time;
538 row.type = action.type;
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);
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;
566 const std::list<RollbackAction> RollbackManager::rollbackActionsFromActionRows(
567 const std::list<ActionRow> & rows)
569 std::list<RollbackAction> actions;
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);
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);
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;
607 actions.push_back(action);
614 const std::list<ActionRow> RollbackManager::getRowsSince(time_t firstTime, const std::string & actor)
616 sqlite3_stmt *stmt_stmt = actor.empty() ? stmt_select : stmt_select_withActor;
617 sqlite3_bind_int64(stmt_stmt, 1, firstTime);
619 if (!actor.empty()) {
620 sqlite3_bind_int(stmt_stmt, 2, getActorId(actor));
623 const std::list<ActionRow> & rows = actionRowsFromSelect(stmt_stmt);
624 sqlite3_reset(stmt_stmt);
630 const std::list<ActionRow> RollbackManager::getRowsSince_range(
631 time_t start_time, v3s16 p, int range, int limit)
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);
643 const std::list<ActionRow> & rows = actionRowsFromSelect(stmt_select_range);
644 sqlite3_reset(stmt_select_range);
650 const std::list<RollbackAction> RollbackManager::getActionsSince_range(
651 time_t start_time, v3s16 p, int range, int limit)
653 return rollbackActionsFromActionRows(getRowsSince_range(start_time, p, range, limit));
657 const std::list<RollbackAction> RollbackManager::getActionsSince(
658 time_t start_time, const std::string & actor)
660 return rollbackActionsFromActionRows(getRowsSince(start_time, actor));
664 void RollbackManager::migrate(const std::string & file_path)
666 std::cout << "Migrating from rollback.txt to rollback.sqlite." << std::endl;
668 std::ifstream fh(file_path.c_str(), std::ios::in | std::ios::ate);
670 throw FileNotGoodException("Unable to open rollback.txt");
673 std::streampos file_size = fh.tellg();
675 if (file_size > 10) {
676 errorstream << "Empty rollback log." << std::endl;
685 time_t start = time(0);
687 sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);
694 std::getline(fh, bit, ' ');
696 if (!atoi(bit.c_str())) {
697 std::getline(fh, bit);
700 row.timestamp = atoi(bit.c_str());
703 row.actor = getActorId(deSerializeJsonString(fh));
705 // Get the action type
706 std::getline(fh, bit, '[');
707 std::getline(fh, bit, ' ');
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");
752 errorstream << "Unrecognized rollback action type \""
753 << bit << "\"!" << std::endl;
760 if (time(0) - t >= 1) {
761 sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
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);
771 << " Done: 100% " << std::endl
772 << "Now you can delete the old rollback.txt file." << std::endl;
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)
781 // Suspect cannot cause things in the past
782 if (action_t < suspect_t) {
783 return 0; // 0 = cannot be
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
803 void RollbackManager::reportAction(const RollbackAction &action_)
805 // Ignore if not important
806 if (!action_.isImportant(gamedef)) {
810 RollbackAction action = action_;
811 action.unix_time = time(0);
814 action.actor = current_actor;
815 action.actor_is_guess = current_actor_is_guess;
817 if (action.actor.empty()) { // If actor is not known, find out suspect or cancel
819 if (!action.getPosition(&p)) {
823 action.actor = getSuspect(p, 83, 1);
824 if (action.actor.empty()) {
828 action.actor_is_guess = true;
834 std::string RollbackManager::getActor()
836 return current_actor;
839 bool RollbackManager::isActorGuess()
841 return current_actor_is_guess;
844 void RollbackManager::setActor(const std::string & actor, bool is_guess)
846 current_actor = actor;
847 current_actor_is_guess = is_guess;
850 std::string RollbackManager::getSuspect(v3s16 p, float nearness_shortcut,
853 if (current_actor != "") {
854 return current_actor;
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) {
866 if (i->actor == "") {
869 // Find position of suspect or continue
871 if (!i->getPosition(&suspect_p)) {
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;
879 if (likely_suspect_nearness >= nearness_shortcut) {
884 // No likely suspect was found
885 if (likely_suspect_nearness == 0) {
888 // Likely suspect was found
889 return likely_suspect.actor;
893 void RollbackManager::flush()
895 sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);
897 std::list<RollbackAction>::const_iterator iter;
899 for (iter = action_todisk_buffer.begin();
900 iter != action_todisk_buffer.end();
902 if (iter->actor == "") {
906 registerRow(actionRowFromRollbackAction(*iter));
909 sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
910 action_todisk_buffer.clear();
914 void RollbackManager::addAction(const RollbackAction & action)
916 action_todisk_buffer.push_back(action);
917 action_latest_buffer.push_back(action);
919 // Flush to disk sometimes
920 if (action_todisk_buffer.size() >= 500) {
925 std::list<RollbackAction> RollbackManager::getEntriesSince(time_t first_time)
928 return getActionsSince(first_time);
931 std::list<RollbackAction> RollbackManager::getNodeActors(v3s16 pos, int range,
932 time_t seconds, int limit)
935 time_t cur_time = time(0);
936 time_t first_time = cur_time - seconds;
938 return getActionsSince_range(first_time, pos, range, limit);
941 std::list<RollbackAction> RollbackManager::getRevertActions(
942 const std::string &actor_filter,
945 time_t cur_time = time(0);
946 time_t first_time = cur_time - seconds;
950 return getActionsSince(first_time, actor_filter);