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 PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
37 #define POINTS_PER_NODE (16.0)
39 static std::string dbp;
40 static sqlite3* dbh = NULL;
41 static sqlite3_stmt* dbs_insert = NULL;
42 static sqlite3_stmt* dbs_replace = NULL;
43 static sqlite3_stmt* dbs_select = NULL;
44 static sqlite3_stmt* dbs_select_range = NULL;
45 static sqlite3_stmt* dbs_select_withActor = NULL;
46 static sqlite3_stmt* dbs_knownActor_select = NULL;
47 static sqlite3_stmt* dbs_knownActor_insert = NULL;
48 static sqlite3_stmt* dbs_knownNode_select = NULL;
49 static sqlite3_stmt* dbs_knownNode_insert = NULL;
60 std::string location, list;
66 int oldParam1, oldParam2;
69 int newParam1, newParam2;
79 typedef std::vector<Entity> Entities;
84 void registerNewActor(int id, std::string name)
91 KnownActors.push_back(newActor);
93 //std::cout << "New actor registered: " << id << " | " << name << std::endl;
96 void registerNewNode(int id, std::string name)
103 KnownNodes.push_back(newNode);
105 //std::cout << "New node registered: " << id << " | " << name << std::endl;
108 int getActorId(std::string name)
110 Entities::const_iterator iter;
112 for (iter = KnownActors.begin(); iter != KnownActors.end(); ++iter)
113 if (iter->name == name) {
117 sqlite3_reset(dbs_knownActor_insert);
118 sqlite3_bind_text(dbs_knownActor_insert, 1, name.c_str(), -1, NULL);
119 sqlite3_step(dbs_knownActor_insert);
121 int id = sqlite3_last_insert_rowid(dbh);
123 //std::cout << "Actor ID insert returns " << insert << std::endl;
125 registerNewActor(id, name);
130 int getNodeId(std::string name)
132 Entities::const_iterator iter;
134 for (iter = KnownNodes.begin(); iter != KnownNodes.end(); ++iter)
135 if (iter->name == name) {
139 sqlite3_reset(dbs_knownNode_insert);
140 sqlite3_bind_text(dbs_knownNode_insert, 1, name.c_str(), -1, NULL);
141 sqlite3_step(dbs_knownNode_insert);
143 int id = sqlite3_last_insert_rowid(dbh);
145 registerNewNode(id, name);
150 const char * getActorName(int id)
152 Entities::const_iterator iter;
154 //std::cout << "getActorName of id " << id << std::endl;
156 for (iter = KnownActors.begin(); iter != KnownActors.end(); ++iter)
157 if (iter->id == id) {
158 return iter->name.c_str();
164 const char * getNodeName(int id)
166 Entities::const_iterator iter;
168 //std::cout << "getNodeName of id " << id << std::endl;
170 for (iter = KnownNodes.begin(); iter != KnownNodes.end(); ++iter)
171 if (iter->id == id) {
172 return iter->name.c_str();
178 Stack getStackFromString(std::string text)
182 size_t off = text.find_last_of(" ");
184 stack.node = getNodeId(text.substr(0, off));
185 stack.quantity = atoi(text.substr(off + 1).c_str());
190 std::string getStringFromStack(Stack stack)
194 text.append(getNodeName(stack.node));
196 text.append(itos(stack.quantity));
201 bool SQL_createDatabase(void)
203 infostream << "CreateDB:" << dbp << std::endl;
205 int dbs = sqlite3_exec(dbh,
206 "CREATE TABLE IF NOT EXISTS `actor` ("
207 "`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
208 "`name` TEXT NOT NULL);"
209 "CREATE TABLE IF NOT EXISTS `node` ("
210 "`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
211 "`name` TEXT NOT NULL);"
212 "CREATE TABLE IF NOT EXISTS `action` ("
213 "`id` INTEGER PRIMARY KEY AUTOINCREMENT,"
214 "`actor` INTEGER NOT NULL,"
215 "`timestamp` TIMESTAMP NOT NULL,"
216 "`type` INTEGER NOT NULL,"
220 "`stackNode` INTEGER,"
221 "`stackQuantity` INTEGER,"
222 "`nodeMeta` INTEGER,"
227 "`oldParam1` INTEGER,"
228 "`oldParam2` INTEGER,"
231 "`newParam1` INTEGER,"
232 "`newParam2` INTEGER,"
234 "`guessedActor` INTEGER,"
235 "FOREIGN KEY (`actor`) REFERENCES `actor`(`id`),"
236 "FOREIGN KEY (`oldNode`) REFERENCES `node`(`id`),"
237 "FOREIGN KEY (`newNode`) REFERENCES `node`(`id`));"
238 "CREATE INDEX IF NOT EXISTS `actionActor` ON `action`(`actor`);"
239 "CREATE INDEX IF NOT EXISTS `actionTimestamp` ON `action`(`timestamp`);",
241 if (dbs == SQLITE_ABORT) {
242 throw FileNotGoodException("Could not create sqlite3 database structure");
243 } else if (dbs != 0) {
244 throw FileNotGoodException("SQL Rollback: Exec statement to create table structure returned a non-zero value");
246 infostream << "SQL Rollback: SQLite3 database structure was created" << std::endl;
251 void SQL_databaseCheck(void)
257 infostream << "Database connection setup" << std::endl;
259 bool needsCreate = !fs::PathExists(dbp);
260 int dbo = sqlite3_open_v2(dbp.c_str(), &dbh, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
263 if (dbo != SQLITE_OK) {
264 infostream << "SQLROLLBACK: SQLite3 database failed to open: "
265 << sqlite3_errmsg(dbh) << std::endl;
266 throw FileNotGoodException("Cannot open database file");
270 SQL_createDatabase();
275 dbr = sqlite3_prepare_v2(dbh,
276 "INSERT INTO `action` ("
277 " `actor`, `timestamp`, `type`,"
278 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,"
280 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,"
281 " `newNode`, `newParam1`, `newParam2`, `newMeta`,"
291 -1, &dbs_insert, NULL);
293 if (dbr != SQLITE_OK) {
294 throw FileNotGoodException(sqlite3_errmsg(dbh));
297 dbr = sqlite3_prepare_v2(dbh,
298 "REPLACE INTO `action` ("
299 " `actor`, `timestamp`, `type`,"
300 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodeMeta`,"
302 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,"
303 " `newNode`, `newParam1`, `newParam2`, `newMeta`,"
304 " `guessedActor`, `id`"
313 -1, &dbs_replace, NULL);
315 if (dbr != SQLITE_OK) {
316 throw FileNotGoodException(sqlite3_errmsg(dbh));
319 dbr = sqlite3_prepare_v2(dbh,
321 " `actor`, `timestamp`, `type`,"
322 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,"
324 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,"
325 " `newNode`, `newParam1`, `newParam2`, `newMeta`,"
328 " WHERE `timestamp` >= ?"
329 " ORDER BY `timestamp` DESC, `id` DESC",
330 -1, &dbs_select, NULL);
331 if (dbr != SQLITE_OK) {
332 throw FileNotGoodException(itos(dbr).c_str());
335 dbr = sqlite3_prepare_v2(dbh,
337 " `actor`, `timestamp`, `type`,"
338 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,"
340 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,"
341 " `newNode`, `newParam1`, `newParam2`, `newMeta`,"
344 " WHERE `timestamp` >= ?"
345 " AND `x` IS NOT NULL"
346 " AND `y` IS NOT NULL"
347 " AND `z` IS NOT NULL"
348 " AND ABS(`x` - ?) <= ?"
349 " AND ABS(`y` - ?) <= ?"
350 " AND ABS(`z` - ?) <= ?"
351 " ORDER BY `timestamp` DESC, `id` DESC"
353 -1, &dbs_select_range, NULL);
354 if (dbr != SQLITE_OK) {
355 throw FileNotGoodException(itos(dbr).c_str());
358 dbr = sqlite3_prepare_v2(dbh,
360 " `actor`, `timestamp`, `type`,"
361 " `list`, `index`, `add`, `stackNode`, `stackQuantity`, `nodemeta`,"
363 " `oldNode`, `oldParam1`, `oldParam2`, `oldMeta`,"
364 " `newNode`, `newParam1`, `newParam2`, `newMeta`,"
367 " WHERE `timestamp` >= ?"
369 " ORDER BY `timestamp` DESC, `id` DESC",
370 -1, &dbs_select_withActor, NULL);
371 if (dbr != SQLITE_OK) {
372 throw FileNotGoodException(itos(dbr).c_str());
375 dbr = sqlite3_prepare_v2(dbh, "SELECT `id`, `name` FROM `actor`", -1,
376 &dbs_knownActor_select, NULL);
377 if (dbr != SQLITE_OK) {
378 throw FileNotGoodException(itos(dbr).c_str());
381 dbr = sqlite3_prepare_v2(dbh, "INSERT INTO `actor` (`name`) VALUES (?)", -1,
382 &dbs_knownActor_insert, NULL);
383 if (dbr != SQLITE_OK) {
384 throw FileNotGoodException(itos(dbr).c_str());
387 dbr = sqlite3_prepare_v2(dbh, "SELECT `id`, `name` FROM `node`", -1,
388 &dbs_knownNode_select, NULL);
389 if (dbr != SQLITE_OK) {
390 throw FileNotGoodException(itos(dbr).c_str());
393 dbr = sqlite3_prepare_v2(dbh, "INSERT INTO `node` (`name`) VALUES (?)", -1,
394 &dbs_knownNode_insert, NULL);
395 if (dbr != SQLITE_OK) {
396 throw FileNotGoodException(itos(dbr).c_str());
399 infostream << "SQL prepared statements setup correctly" << std::endl;
403 sqlite3_reset(dbs_knownActor_select);
404 while (SQLITE_ROW == (select = sqlite3_step(dbs_knownActor_select)))
406 sqlite3_column_int(dbs_knownActor_select, 0),
407 reinterpret_cast<const char *>(sqlite3_column_text(dbs_knownActor_select, 1))
410 sqlite3_reset(dbs_knownNode_select);
411 while (SQLITE_ROW == (select = sqlite3_step(dbs_knownNode_select)))
413 sqlite3_column_int(dbs_knownNode_select, 0),
414 reinterpret_cast<const char *>(sqlite3_column_text(dbs_knownNode_select, 1))
419 bool SQL_registerRow(ActionRow row)
423 sqlite3_stmt * dbs_do = (row.id) ? dbs_replace : dbs_insert;
427 << (row.id? "Replacing": "Inserting")
428 << " ActionRow" << std::endl;
430 sqlite3_reset(dbs_do);
432 int bind [22], ii = 0;
433 bool nodeMeta = false;
435 bind[ii++] = sqlite3_bind_int(dbs_do, 1, row.actor);
436 bind[ii++] = sqlite3_bind_int64(dbs_do, 2, row.timestamp);
437 bind[ii++] = sqlite3_bind_int(dbs_do, 3, row.type);
439 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
440 std::string loc = row.location;
441 std::string locType = loc.substr(0, loc.find(":"));
442 nodeMeta = (locType == "nodemeta");
444 bind[ii++] = sqlite3_bind_text(dbs_do, 4, row.list.c_str(), row.list.size(), NULL);
445 bind[ii++] = sqlite3_bind_int(dbs_do, 5, row.index);
446 bind[ii++] = sqlite3_bind_int(dbs_do, 6, row.add);
447 bind[ii++] = sqlite3_bind_int(dbs_do, 7, row.stack.node);
448 bind[ii++] = sqlite3_bind_int(dbs_do, 8, row.stack.quantity);
449 bind[ii++] = sqlite3_bind_int(dbs_do, 9, (int) nodeMeta);
454 l = loc.find(':') + 1;
456 x = loc.substr(l, r - l);
458 r = loc.find(',', l);
459 y = loc.substr(l, r - l);
460 z = loc.substr(r + 1);
461 bind[ii++] = sqlite3_bind_int(dbs_do, 10, atoi(x.c_str()));
462 bind[ii++] = sqlite3_bind_int(dbs_do, 11, atoi(y.c_str()));
463 bind[ii++] = sqlite3_bind_int(dbs_do, 12, atoi(z.c_str()));
466 bind[ii++] = sqlite3_bind_null(dbs_do, 4);
467 bind[ii++] = sqlite3_bind_null(dbs_do, 5);
468 bind[ii++] = sqlite3_bind_null(dbs_do, 6);
469 bind[ii++] = sqlite3_bind_null(dbs_do, 7);
470 bind[ii++] = sqlite3_bind_null(dbs_do, 8);
471 bind[ii++] = sqlite3_bind_null(dbs_do, 9);
474 if (row.type == RollbackAction::TYPE_SET_NODE) {
475 bind[ii++] = sqlite3_bind_int(dbs_do, 10, row.x);
476 bind[ii++] = sqlite3_bind_int(dbs_do, 11, row.y);
477 bind[ii++] = sqlite3_bind_int(dbs_do, 12, row.z);
478 bind[ii++] = sqlite3_bind_int(dbs_do, 13, row.oldNode);
479 bind[ii++] = sqlite3_bind_int(dbs_do, 14, row.oldParam1);
480 bind[ii++] = sqlite3_bind_int(dbs_do, 15, row.oldParam2);
481 bind[ii++] = sqlite3_bind_text(dbs_do, 16, row.oldMeta.c_str(), row.oldMeta.size(), NULL);
482 bind[ii++] = sqlite3_bind_int(dbs_do, 17, row.newNode);
483 bind[ii++] = sqlite3_bind_int(dbs_do, 18, row.newParam1);
484 bind[ii++] = sqlite3_bind_int(dbs_do, 19, row.newParam2);
485 bind[ii++] = sqlite3_bind_text(dbs_do, 20, row.newMeta.c_str(), row.newMeta.size(), NULL);
486 bind[ii++] = sqlite3_bind_int(dbs_do, 21, row.guessed ? 1 : 0);
489 bind[ii++] = sqlite3_bind_null(dbs_do, 10);
490 bind[ii++] = sqlite3_bind_null(dbs_do, 11);
491 bind[ii++] = sqlite3_bind_null(dbs_do, 12);
493 bind[ii++] = sqlite3_bind_null(dbs_do, 13);
494 bind[ii++] = sqlite3_bind_null(dbs_do, 14);
495 bind[ii++] = sqlite3_bind_null(dbs_do, 15);
496 bind[ii++] = sqlite3_bind_null(dbs_do, 16);
497 bind[ii++] = sqlite3_bind_null(dbs_do, 17);
498 bind[ii++] = sqlite3_bind_null(dbs_do, 18);
499 bind[ii++] = sqlite3_bind_null(dbs_do, 19);
500 bind[ii++] = sqlite3_bind_null(dbs_do, 20);
501 bind[ii++] = sqlite3_bind_null(dbs_do, 21);
505 bind[ii++] = sqlite3_bind_int(dbs_do, 22, row.id);
508 for (ii = 0; ii < 20; ++ii)
509 if (bind[ii] != SQLITE_OK)
511 << "WARNING: failed to bind param " << ii + 1
512 << " when inserting an entry in table setnode" << std::endl;
515 std::cout << "========DB-WRITTEN==========" << std::endl;
516 std::cout << "id: " << row.id << std::endl;
517 std::cout << "actor: " << row.actor << std::endl;
518 std::cout << "time: " << row.timestamp << std::endl;
519 std::cout << "type: " << row.type << std::endl;
520 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK)
522 std::cout << "Location: " << row.location << std::endl;
523 std::cout << "List: " << row.list << std::endl;
524 std::cout << "Index: " << row.index << std::endl;
525 std::cout << "Add: " << row.add << std::endl;
526 std::cout << "Stack: " << row.stack << std::endl;
528 if (row.type == RollbackAction::TYPE_SET_NODE)
530 std::cout << "x: " << row.x << std::endl;
531 std::cout << "y: " << row.y << std::endl;
532 std::cout << "z: " << row.z << std::endl;
533 std::cout << "oldNode: " << row.oldNode << std::endl;
534 std::cout << "oldParam1: " << row.oldParam1 << std::endl;
535 std::cout << "oldParam2: " << row.oldParam2 << std::endl;
536 std::cout << "oldMeta: " << row.oldMeta << std::endl;
537 std::cout << "newNode: " << row.newNode << std::endl;
538 std::cout << "newParam1: " << row.newParam1 << std::endl;
539 std::cout << "newParam2: " << row.newParam2 << std::endl;
540 std::cout << "newMeta: " << row.newMeta << std::endl;
541 std::cout << "DESERIALIZE" << row.newMeta.c_str() << std::endl;
542 std::cout << "guessed: " << row.guessed << std::endl;
546 int written = sqlite3_step(dbs_do);
548 return written == SQLITE_DONE;
550 //if (written != SQLITE_DONE)
551 // std::cout << "WARNING: rollback action not written: " << sqlite3_errmsg(dbh) << std::endl;
552 //else std::cout << "Action correctly inserted via SQL" << std::endl;
554 std::list<ActionRow> actionRowsFromSelect(sqlite3_stmt* stmt)
556 std::list<ActionRow> rows;
557 const unsigned char * text;
560 while (SQLITE_ROW == sqlite3_step(stmt)) {
563 row.actor = sqlite3_column_int(stmt, 0);
564 row.timestamp = sqlite3_column_int64(stmt, 1);
565 row.type = sqlite3_column_int(stmt, 2);
567 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
568 text = sqlite3_column_text(stmt, 3);
569 size = sqlite3_column_bytes(stmt, 3);
570 row.list = std::string(reinterpret_cast<const char*>(text), size);
571 row.index = sqlite3_column_int(stmt, 4);
572 row.add = sqlite3_column_int(stmt, 5);
573 row.stack.node = sqlite3_column_int(stmt, 6);
574 row.stack.quantity = sqlite3_column_int(stmt, 7);
575 row.nodeMeta = sqlite3_column_int(stmt, 8);
578 if (row.type == RollbackAction::TYPE_SET_NODE || row.nodeMeta) {
579 row.x = sqlite3_column_int(stmt, 9);
580 row.y = sqlite3_column_int(stmt, 10);
581 row.z = sqlite3_column_int(stmt, 11);
584 if (row.type == RollbackAction::TYPE_SET_NODE) {
585 row.oldNode = sqlite3_column_int(stmt, 12);
586 row.oldParam1 = sqlite3_column_int(stmt, 13);
587 row.oldParam2 = sqlite3_column_int(stmt, 14);
588 text = sqlite3_column_text(stmt, 15);
589 size = sqlite3_column_bytes(stmt, 15);
590 row.oldMeta = std::string(reinterpret_cast<const char*>(text), size);
591 row.newNode = sqlite3_column_int(stmt, 16);
592 row.newParam1 = sqlite3_column_int(stmt, 17);
593 row.newParam2 = sqlite3_column_int(stmt, 18);
594 text = sqlite3_column_text(stmt, 19);
595 size = sqlite3_column_bytes(stmt, 19);
596 row.newMeta = std::string(reinterpret_cast<const char*>(text), size);
597 row.guessed = sqlite3_column_int(stmt, 20);
600 row.location = row.nodeMeta ? "nodemeta:" : getActorName(row.actor);
603 row.location.append(itos(row.x));
604 row.location.append(",");
605 row.location.append(itos(row.y));
606 row.location.append(",");
607 row.location.append(itos(row.z));
611 std::cout << "=======SELECTED==========" << "\n";
612 std::cout << "Actor: " << row.actor << "\n";
613 std::cout << "Timestamp: " << row.timestamp << "\n";
615 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK)
617 std::cout << "list: " << row.list << "\n";
618 std::cout << "index: " << row.index << "\n";
619 std::cout << "add: " << row.add << "\n";
620 std::cout << "stackNode: " << row.stack.node << "\n";
621 std::cout << "stackQuantity: " << row.stack.quantity << "\n";
624 std::cout << "X: " << row.x << "\n";
625 std::cout << "Y: " << row.y << "\n";
626 std::cout << "Z: " << row.z << "\n";
628 std::cout << "Location: " << row.location << "\n";
632 std::cout << "X: " << row.x << "\n";
633 std::cout << "Y: " << row.y << "\n";
634 std::cout << "Z: " << row.z << "\n";
635 std::cout << "oldNode: " << row.oldNode << "\n";
636 std::cout << "oldParam1: " << row.oldParam1 << "\n";
637 std::cout << "oldParam2: " << row.oldParam2 << "\n";
638 std::cout << "oldMeta: " << row.oldMeta << "\n";
639 std::cout << "newNode: " << row.newNode << "\n";
640 std::cout << "newParam1: " << row.newParam1 << "\n";
641 std::cout << "newParam2: " << row.newParam2 << "\n";
642 std::cout << "newMeta: " << row.newMeta << "\n";
643 std::cout << "guessed: " << row.guessed << "\n";
652 ActionRow actionRowFromRollbackAction(RollbackAction action)
657 row.actor = getActorId(action.actor);
658 row.timestamp = action.unix_time;
659 row.type = action.type;
661 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
662 row.location = action.inventory_location;
663 row.list = action.inventory_list;
664 row.index = action.inventory_index;
665 row.add = action.inventory_add;
666 row.stack = getStackFromString(action.inventory_stack);
671 row.oldNode = getNodeId(action.n_old.name);
672 row.oldParam1 = action.n_old.param1;
673 row.oldParam2 = action.n_old.param2;
674 row.oldMeta = action.n_old.meta;
675 row.newNode = getNodeId(action.n_new.name);
676 row.newParam1 = action.n_new.param1;
677 row.newParam2 = action.n_new.param2;
678 row.newMeta = action.n_new.meta;
679 row.guessed = action.actor_is_guess;
684 std::list<RollbackAction> rollbackActionsFromActionRows(std::list<ActionRow> rows)
686 std::list<RollbackAction> actions;
687 std::list<ActionRow>::const_iterator it;
689 for (it = rows.begin(); it != rows.end(); ++it) {
690 RollbackAction action;
691 action.actor = (it->actor) ? getActorName(it->actor) : "";
692 action.unix_time = it->timestamp;
693 action.type = static_cast<RollbackAction::Type>(it->type);
695 switch (action.type) {
696 case RollbackAction::TYPE_MODIFY_INVENTORY_STACK:
698 action.inventory_location = it->location.c_str();
699 action.inventory_list = it->list;
700 action.inventory_index = it->index;
701 action.inventory_add = it->add;
702 action.inventory_stack = getStringFromStack(it->stack);
705 case RollbackAction::TYPE_SET_NODE:
707 action.p = v3s16(it->x, it->y, it->z);
708 action.n_old.name = getNodeName(it->oldNode);
709 action.n_old.param1 = it->oldParam1;
710 action.n_old.param2 = it->oldParam2;
711 action.n_old.meta = it->oldMeta;
712 action.n_new.name = getNodeName(it->newNode);
713 action.n_new.param1 = it->newParam1;
714 action.n_new.param2 = it->newParam2;
715 action.n_new.meta = it->newMeta;
724 actions.push_back(action);
730 std::list<ActionRow> SQL_getRowsSince(time_t firstTime, std::string actor = "")
732 sqlite3_stmt *dbs_stmt = (!actor.length()) ? dbs_select : dbs_select_withActor;
733 sqlite3_reset(dbs_stmt);
734 sqlite3_bind_int64(dbs_stmt, 1, firstTime);
736 if (actor.length()) {
737 sqlite3_bind_int(dbs_stmt, 2, getActorId(actor));
740 return actionRowsFromSelect(dbs_stmt);
743 std::list<ActionRow> SQL_getRowsSince_range(time_t firstTime, v3s16 p, int range,
746 sqlite3_stmt *stmt = dbs_select_range;
749 sqlite3_bind_int64(stmt, 1, firstTime);
750 sqlite3_bind_int(stmt, 2, (int) p.X);
751 sqlite3_bind_int(stmt, 3, range);
752 sqlite3_bind_int(stmt, 4, (int) p.Y);
753 sqlite3_bind_int(stmt, 5, range);
754 sqlite3_bind_int(stmt, 6, (int) p.Z);
755 sqlite3_bind_int(stmt, 7, range);
756 sqlite3_bind_int(stmt, 8, limit);
758 return actionRowsFromSelect(stmt);
761 std::list<RollbackAction> SQL_getActionsSince_range(time_t firstTime, v3s16 p, int range,
764 std::list<ActionRow> rows = SQL_getRowsSince_range(firstTime, p, range, limit);
766 return rollbackActionsFromActionRows(rows);
769 std::list<RollbackAction> SQL_getActionsSince(time_t firstTime, std::string actor = "")
771 std::list<ActionRow> rows = SQL_getRowsSince(firstTime, actor);
772 return rollbackActionsFromActionRows(rows);
775 void TXT_migrate(std::string filepath)
777 std::cout << "Migrating from rollback.txt to rollback.sqlite" << std::endl;
780 std::ifstream fh(filepath.c_str(), std::ios::in | std::ios::ate);
785 std::streampos filesize = fh.tellg();
800 std::getline(fh, bit, ' ');
802 if (!atoi(trim(bit).c_str())) {
803 std::getline(fh, bit);
806 row.timestamp = atoi(bit.c_str());
809 row.actor = getActorId(trim(deSerializeJsonString(fh)));
811 // Get the action type
812 std::getline(fh, bit, '[');
813 std::getline(fh, bit, ' ');
815 if (bit == "modify_inventory_stack") {
816 row.type = RollbackAction::TYPE_MODIFY_INVENTORY_STACK;
819 if (bit == "set_node") {
820 row.type = RollbackAction::TYPE_SET_NODE;
823 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK) {
824 row.location = trim(deSerializeJsonString(fh));
825 std::getline(fh, bit, ' ');
826 row.list = trim(deSerializeJsonString(fh));
827 std::getline(fh, bit, ' ');
828 std::getline(fh, bit, ' ');
829 row.index = atoi(trim(bit).c_str());
830 std::getline(fh, bit, ' ');
831 row.add = (int)(trim(bit) == "add");
832 row.stack = getStackFromString(trim(deSerializeJsonString(fh)));
833 std::getline(fh, bit);
834 } else if (row.type == RollbackAction::TYPE_SET_NODE) {
835 std::getline(fh, bit, '(');
836 std::getline(fh, bit, ',');
837 row.x = atoi(trim(bit).c_str());
838 std::getline(fh, bit, ',');
839 row.y = atoi(trim(bit).c_str());
840 std::getline(fh, bit, ')');
841 row.z = atoi(trim(bit).c_str());
842 std::getline(fh, bit, ' ');
843 row.oldNode = getNodeId(trim(deSerializeJsonString(fh)));
844 std::getline(fh, bit, ' ');
845 std::getline(fh, bit, ' ');
846 row.oldParam1 = atoi(trim(bit).c_str());
847 std::getline(fh, bit, ' ');
848 row.oldParam2 = atoi(trim(bit).c_str());
849 row.oldMeta = trim(deSerializeJsonString(fh));
850 std::getline(fh, bit, ' ');
851 row.newNode = getNodeId(trim(deSerializeJsonString(fh)));
852 std::getline(fh, bit, ' ');
853 std::getline(fh, bit, ' ');
854 row.newParam1 = atoi(trim(bit).c_str());
855 std::getline(fh, bit, ' ');
856 row.newParam2 = atoi(trim(bit).c_str());
857 row.newMeta = trim(deSerializeJsonString(fh));
858 std::getline(fh, bit, ' ');
859 std::getline(fh, bit, ' ');
860 std::getline(fh, bit);
861 row.guessed = (int)(trim(bit) == "actor_is_guess");
865 std::cout << "==========READ===========" << std::endl;
866 std::cout << "time: " << row.timestamp << std::endl;
867 std::cout << "actor: " << row.actor << std::endl;
868 std::cout << "type: " << row.type << std::endl;
869 if (row.type == RollbackAction::TYPE_MODIFY_INVENTORY_STACK)
871 std::cout << "Location: " << row.location << std::endl;
872 std::cout << "List: " << row.list << std::endl;
873 std::cout << "Index: " << row.index << std::endl;
874 std::cout << "Add: " << row.add << std::endl;
875 std::cout << "Stack: " << row.stack << std::endl;
877 if (row.type == RollbackAction::TYPE_SET_NODE)
879 std::cout << "x: " << row.x << std::endl;
880 std::cout << "y: " << row.y << std::endl;
881 std::cout << "z: " << row.z << std::endl;
882 std::cout << "oldNode: " << row.oldNode << std::endl;
883 std::cout << "oldParam1: " << row.oldParam1 << std::endl;
884 std::cout << "oldParam2: " << row.oldParam2 << std::endl;
885 std::cout << "oldMeta: " << row.oldMeta << std::endl;
886 std::cout << "newNode: " << row.newNode << std::endl;
887 std::cout << "newParam1: " << row.newParam1 << std::endl;
888 std::cout << "newParam2: " << row.newParam2 << std::endl;
889 std::cout << "newMeta: " << row.newMeta << std::endl;
890 std::cout << "guessed: " << row.guessed << std::endl;
896 sqlite3_exec(dbh, "BEGIN", NULL, NULL, NULL);
899 SQL_registerRow(row);
903 sqlite3_exec(dbh, "COMMIT", NULL, NULL, NULL);
906 << " Done: " << (int)(((float) fh.tellg() / (float) filesize) * 100) << "%"
907 << " Speed: " << i / t << "/second \r" << std::flush;
912 } while (!fh.eof() && fh.good());
914 errorstream << "Empty rollback log" << std::endl;
918 << " Done: 100% " << std::endl
919 << " Now you can delete the old rollback.txt file." << std::endl;
922 // Get nearness factor for subject's action for this action
923 // Return value: 0 = impossible, >0 = factor
924 static float getSuspectNearness(bool is_guess, v3s16 suspect_p, time_t suspect_t,
925 v3s16 action_p, time_t action_t)
927 // Suspect cannot cause things in the past
928 if (action_t < suspect_t) {
929 return 0; // 0 = cannot be
933 // Distance (1 node = -x points)
934 f -= POINTS_PER_NODE * intToFloat(suspect_p, 1).getDistanceFrom(intToFloat(action_p, 1));
935 // Time (1 second = -x points)
936 f -= 1 * (action_t - suspect_t);
937 // If is a guess, halve the points
947 class RollbackManager: public IRollbackManager
950 // IRollbackManager interface
951 void reportAction(const RollbackAction &action_) {
952 // Ignore if not important
953 if (!action_.isImportant(m_gamedef)) {
957 RollbackAction action = action_;
958 action.unix_time = time(0);
961 action.actor = m_current_actor;
962 action.actor_is_guess = m_current_actor_is_guess;
964 if (action.actor.empty()) { // If actor is not known, find out suspect or cancel
966 if (!action.getPosition(&p)) {
970 action.actor = getSuspect(p, 83, 1);
971 if (action.actor.empty()) {
975 action.actor_is_guess = true;
979 << "RollbackManager::reportAction():"
980 << " time=" << action.unix_time
981 << " actor=\"" << action.actor << "\""
982 << (action.actor_is_guess ? " (guess)" : "")
983 << " action=" << action.toString()
988 std::string getActor() {
989 return m_current_actor;
992 bool isActorGuess() {
993 return m_current_actor_is_guess;
996 void setActor(const std::string &actor, bool is_guess) {
997 m_current_actor = actor;
998 m_current_actor_is_guess = is_guess;
1001 std::string getSuspect(v3s16 p, float nearness_shortcut, float min_nearness) {
1002 if (m_current_actor != "") {
1003 return m_current_actor;
1005 int cur_time = time(0);
1006 time_t first_time = cur_time - (100 - min_nearness);
1007 RollbackAction likely_suspect;
1008 float likely_suspect_nearness = 0;
1009 for (std::list<RollbackAction>::const_reverse_iterator
1010 i = m_action_latest_buffer.rbegin();
1011 i != m_action_latest_buffer.rend(); i++) {
1012 if (i->unix_time < first_time) {
1015 if (i->actor == "") {
1018 // Find position of suspect or continue
1020 if (!i->getPosition(&suspect_p)) {
1023 float f = getSuspectNearness(i->actor_is_guess, suspect_p,
1024 i->unix_time, p, cur_time);
1025 if (f >= min_nearness && f > likely_suspect_nearness) {
1026 likely_suspect_nearness = f;
1027 likely_suspect = *i;
1028 if (likely_suspect_nearness >= nearness_shortcut) {
1033 // No likely suspect was found
1034 if (likely_suspect_nearness == 0) {
1037 // Likely suspect was found
1038 return likely_suspect.actor;
1042 infostream << "RollbackManager::flush()" << std::endl;
1044 sqlite3_exec(dbh, "BEGIN", NULL, NULL, NULL);
1046 std::list<RollbackAction>::const_iterator iter;
1048 for (iter = m_action_todisk_buffer.begin();
1049 iter != m_action_todisk_buffer.end();
1051 if (iter->actor == "") {
1055 SQL_registerRow(actionRowFromRollbackAction(*iter));
1058 sqlite3_exec(dbh, "COMMIT", NULL, NULL, NULL);
1059 m_action_todisk_buffer.clear();
1062 RollbackManager(const std::string &filepath, IGameDef *gamedef):
1063 m_filepath(filepath),
1065 m_current_actor_is_guess(false) {
1067 << "RollbackManager::RollbackManager(" << filepath << ")"
1070 // Operate correctly in case of still being given rollback.txt as filepath
1071 std::string directory = filepath.substr(0, filepath.rfind(DIR_DELIM) + 1);
1072 std::string filenameOld = filepath.substr(filepath.rfind(DIR_DELIM) + 1);
1073 std::string filenameNew = (filenameOld == "rollback.txt") ? "rollback.sqlite" :
1075 std::string filenameTXT = directory + "rollback.txt";
1076 std::string migratingFlag = filepath + ".migrating";
1078 infostream << "Directory: " << directory << std::endl;
1079 infostream << "CheckFor: " << filenameTXT << std::endl;
1080 infostream << "FileOld: " << filenameOld << std::endl;
1081 infostream << "FileNew: " << filenameNew << std::endl;
1083 dbp = directory + filenameNew;
1085 if ((fs::PathExists(filenameTXT) && fs::PathExists(migratingFlag)) ||
1086 (fs::PathExists(filenameTXT) && !fs::PathExists(dbp))) {
1087 std::ofstream of(migratingFlag.c_str());
1088 TXT_migrate(filenameTXT);
1089 fs::DeleteSingleFileOrEmptyDirectory(migratingFlag);
1092 SQL_databaseCheck();
1094 #define FINALIZE_STATEMENT(statement) \
1096 rc = sqlite3_finalize(statement); \
1098 if ( rc != SQLITE_OK ) \
1099 errorstream << "RollbackManager::~RollbackManager():" \
1100 << "Failed to finalize: " << #statement << ": rc=" << rc << std::endl;
1102 ~RollbackManager() {
1103 infostream << "RollbackManager::~RollbackManager()" << std::endl;
1108 FINALIZE_STATEMENT(dbs_insert)
1109 FINALIZE_STATEMENT(dbs_replace)
1110 FINALIZE_STATEMENT(dbs_select)
1111 FINALIZE_STATEMENT(dbs_select_range)
1112 FINALIZE_STATEMENT(dbs_select_withActor)
1113 FINALIZE_STATEMENT(dbs_knownActor_select)
1114 FINALIZE_STATEMENT(dbs_knownActor_insert)
1115 FINALIZE_STATEMENT(dbs_knownNode_select)
1116 FINALIZE_STATEMENT(dbs_knownNode_insert)
1119 rc = sqlite3_close(dbh);
1123 if (rc != SQLITE_OK) {
1124 errorstream << "RollbackManager::~RollbackManager(): "
1125 << "Failed to close database: rc=" << rc << std::endl;
1129 void addAction(const RollbackAction &action) {
1130 m_action_todisk_buffer.push_back(action);
1131 m_action_latest_buffer.push_back(action);
1133 // Flush to disk sometimes
1134 if (m_action_todisk_buffer.size() >= 500) {
1139 std::list<RollbackAction> getEntriesSince(time_t first_time) {
1141 << "RollbackManager::getEntriesSince(" << first_time << ")"
1146 std::list<RollbackAction> result = SQL_getActionsSince(first_time);
1151 std::list<RollbackAction> getNodeActors(v3s16 pos, int range, time_t seconds, int limit) {
1152 time_t cur_time = time(0);
1153 time_t first_time = cur_time - seconds;
1155 return SQL_getActionsSince_range(first_time, pos, range, limit);
1158 std::list<RollbackAction> getRevertActions(const std::string &actor_filter,
1161 << "RollbackManager::getRevertActions(" << actor_filter
1162 << ", " << seconds << ")"
1166 time_t cur_time = time(0);
1167 time_t first_time = cur_time - seconds;
1171 std::list<RollbackAction> result = SQL_getActionsSince(first_time, actor_filter);
1176 std::string m_filepath;
1177 IGameDef *m_gamedef;
1178 std::string m_current_actor;
1179 bool m_current_actor_is_guess;
1180 std::list<RollbackAction> m_action_todisk_buffer;
1181 std::list<RollbackAction> m_action_latest_buffer;
1184 IRollbackManager *createRollbackManager(const std::string &filepath, IGameDef *gamedef)
1186 return new RollbackManager(filepath, gamedef);