From ef100f12a1346f7bfd2a05dc606cb22e97179c02 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Fri, 12 Feb 2016 18:39:03 -0500 Subject: Fix rollback.txt migration Broken by b1965ac20922e3722392114bd63a22b403dcbe98. This also prepares the begin and commit statements only once. --- src/rollback.cpp | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'src/rollback.cpp') diff --git a/src/rollback.cpp b/src/rollback.cpp index 2367c3a21..ac4324cab 100644 --- a/src/rollback.cpp +++ b/src/rollback.cpp @@ -93,10 +93,10 @@ RollbackManager::RollbackManager(const std::string & world_path, std::string migrating_flag = txt_filename + ".migrating"; database_path = world_path + DIR_DELIM "rollback.sqlite"; - initDatabase(); + bool created = initDatabase(); - if (fs::PathExists(txt_filename) && (fs::PathExists(migrating_flag) || - !fs::PathExists(database_path))) { + if (fs::PathExists(txt_filename) && (created || + fs::PathExists(migrating_flag))) { std::ofstream of(migrating_flag.c_str()); of.close(); migrate(txt_filename); @@ -250,15 +250,15 @@ bool RollbackManager::createTables() } -void RollbackManager::initDatabase() +bool RollbackManager::initDatabase() { verbosestream << "RollbackManager: Database connection setup" << std::endl; - bool needsCreate = !fs::PathExists(database_path); + bool needs_create = !fs::PathExists(database_path); SQLOK(sqlite3_open_v2(database_path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)); - if (needsCreate) { + if (needs_create) { createTables(); } @@ -374,6 +374,8 @@ void RollbackManager::initDatabase() ); } SQLOK(sqlite3_reset(stmt_knownNode_select)); + + return needs_create; } @@ -672,23 +674,27 @@ void RollbackManager::migrate(const std::string & file_path) std::streampos file_size = fh.tellg(); - if (file_size > 10) { + if (file_size < 10) { errorstream << "Empty rollback log." << std::endl; return; } fh.seekg(0); + sqlite3_stmt *stmt_begin; + sqlite3_stmt *stmt_commit; + SQLOK(sqlite3_prepare_v2(db, "BEGIN", -1, &stmt_begin, NULL)); + SQLOK(sqlite3_prepare_v2(db, "COMMIT", -1, &stmt_commit, NULL)); + std::string bit; int i = 0; - int id = 1; time_t start = time(0); time_t t = start; - sqlite3_exec(db, "BEGIN", NULL, NULL, NULL); + SQLRES(sqlite3_step(stmt_begin), SQLITE_DONE); + sqlite3_reset(stmt_begin); do { ActionRow row; - - row.id = id; + row.id = 0; // Get the timestamp std::getline(fh, bit, ' '); @@ -758,17 +764,24 @@ void RollbackManager::migrate(const std::string & file_path) ++i; if (time(0) - t >= 1) { - sqlite3_exec(db, "COMMIT", NULL, NULL, NULL); + SQLRES(sqlite3_step(stmt_commit), SQLITE_DONE); + sqlite3_reset(stmt_commit); t = time(0); std::cout << " Done: " << static_cast((static_cast(fh.tellg()) / static_cast(file_size)) * 100) << "%" << " Speed: " << i / (t - start) << "/second \r" << std::flush; - sqlite3_exec(db, "BEGIN", NULL, NULL, NULL); + SQLRES(sqlite3_step(stmt_begin), SQLITE_DONE); + sqlite3_reset(stmt_begin); } } while (fh.good()); + SQLRES(sqlite3_step(stmt_commit), SQLITE_DONE); + sqlite3_reset(stmt_commit); + + SQLOK(sqlite3_finalize(stmt_begin)); + SQLOK(sqlite3_finalize(stmt_commit)); std::cout - << " Done: 100% " << std::endl + << " Done: 100% " << std::endl << "Now you can delete the old rollback.txt file." << std::endl; } -- cgit v1.2.3 From 9db78dc4c4130c1ef08696e5d381a157cb66e656 Mon Sep 17 00:00:00 2001 From: est31 Date: Fri, 8 Jul 2016 07:26:00 +0200 Subject: Fix -Wterminate warnings in rollback.cpp as well Fix #4137 for rollback.cpp as well. --- src/rollback.cpp | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'src/rollback.cpp') diff --git a/src/rollback.cpp b/src/rollback.cpp index ac4324cab..4d34decf3 100644 --- a/src/rollback.cpp +++ b/src/rollback.cpp @@ -42,6 +42,14 @@ with this program; if not, write to the Free Software Foundation, Inc., } #define SQLOK(f) SQLRES(f, SQLITE_OK) +#define SQLOK_ERRSTREAM(s, m) \ + if ((s) != SQLITE_OK) { \ + errorstream << "RollbackManager: " << (m) << ": " \ + << sqlite3_errmsg(db) << std::endl; \ + } + +#define FINALIZE_STATEMENT(statement) \ + SQLOK_ERRSTREAM(sqlite3_finalize(statement), "Failed to finalize " #statement) class ItemStackRow : public ItemStack { public: @@ -109,17 +117,17 @@ RollbackManager::~RollbackManager() { flush(); - SQLOK(sqlite3_finalize(stmt_insert)); - SQLOK(sqlite3_finalize(stmt_replace)); - SQLOK(sqlite3_finalize(stmt_select)); - SQLOK(sqlite3_finalize(stmt_select_range)); - SQLOK(sqlite3_finalize(stmt_select_withActor)); - SQLOK(sqlite3_finalize(stmt_knownActor_select)); - SQLOK(sqlite3_finalize(stmt_knownActor_insert)); - SQLOK(sqlite3_finalize(stmt_knownNode_select)); - SQLOK(sqlite3_finalize(stmt_knownNode_insert)); - - SQLOK(sqlite3_close(db)); + FINALIZE_STATEMENT(stmt_insert); + FINALIZE_STATEMENT(stmt_replace); + FINALIZE_STATEMENT(stmt_select); + FINALIZE_STATEMENT(stmt_select_range); + FINALIZE_STATEMENT(stmt_select_withActor); + FINALIZE_STATEMENT(stmt_knownActor_select); + FINALIZE_STATEMENT(stmt_knownActor_insert); + FINALIZE_STATEMENT(stmt_knownNode_select); + FINALIZE_STATEMENT(stmt_knownNode_insert); + + SQLOK_ERRSTREAM(sqlite3_close(db), "Could not close db"); } -- cgit v1.2.3