Fix rollback.txt migration

Broken by b1965ac20922e3722392114bd63a22b403dcbe98.
This also prepares the begin and commit statements only once.
This commit is contained in:
ShadowNinja 2016-02-12 18:39:03 -05:00
parent 22f78ea38e
commit ef100f12a1
2 changed files with 28 additions and 15 deletions

@ -93,10 +93,10 @@ RollbackManager::RollbackManager(const std::string & world_path,
std::string migrating_flag = txt_filename + ".migrating"; std::string migrating_flag = txt_filename + ".migrating";
database_path = world_path + DIR_DELIM "rollback.sqlite"; database_path = world_path + DIR_DELIM "rollback.sqlite";
initDatabase(); bool created = initDatabase();
if (fs::PathExists(txt_filename) && (fs::PathExists(migrating_flag) || if (fs::PathExists(txt_filename) && (created ||
!fs::PathExists(database_path))) { fs::PathExists(migrating_flag))) {
std::ofstream of(migrating_flag.c_str()); std::ofstream of(migrating_flag.c_str());
of.close(); of.close();
migrate(txt_filename); migrate(txt_filename);
@ -250,15 +250,15 @@ bool RollbackManager::createTables()
} }
void RollbackManager::initDatabase() bool RollbackManager::initDatabase()
{ {
verbosestream << "RollbackManager: Database connection setup" << std::endl; 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, SQLOK(sqlite3_open_v2(database_path.c_str(), &db,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)); SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL));
if (needsCreate) { if (needs_create) {
createTables(); createTables();
} }
@ -374,6 +374,8 @@ void RollbackManager::initDatabase()
); );
} }
SQLOK(sqlite3_reset(stmt_knownNode_select)); 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(); std::streampos file_size = fh.tellg();
if (file_size > 10) { if (file_size < 10) {
errorstream << "Empty rollback log." << std::endl; errorstream << "Empty rollback log." << std::endl;
return; return;
} }
fh.seekg(0); 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; std::string bit;
int i = 0; int i = 0;
int id = 1;
time_t start = time(0); time_t start = time(0);
time_t t = start; time_t t = start;
sqlite3_exec(db, "BEGIN", NULL, NULL, NULL); SQLRES(sqlite3_step(stmt_begin), SQLITE_DONE);
sqlite3_reset(stmt_begin);
do { do {
ActionRow row; ActionRow row;
row.id = 0;
row.id = id;
// Get the timestamp // Get the timestamp
std::getline(fh, bit, ' '); std::getline(fh, bit, ' ');
@ -758,14 +764,21 @@ void RollbackManager::migrate(const std::string & file_path)
++i; ++i;
if (time(0) - t >= 1) { 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); t = time(0);
std::cout std::cout
<< " Done: " << static_cast<int>((static_cast<float>(fh.tellg()) / static_cast<float>(file_size)) * 100) << "%" << " Done: " << static_cast<int>((static_cast<float>(fh.tellg()) / static_cast<float>(file_size)) * 100) << "%"
<< " Speed: " << i / (t - start) << "/second \r" << std::flush; << " 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()); } 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 std::cout
<< " Done: 100% " << std::endl << " Done: 100% " << std::endl

@ -61,7 +61,7 @@ private:
const char * getActorName(const int id); const char * getActorName(const int id);
const char * getNodeName(const int id); const char * getNodeName(const int id);
bool createTables(); bool createTables();
void initDatabase(); bool initDatabase();
bool registerRow(const ActionRow & row); bool registerRow(const ActionRow & row);
const std::list<ActionRow> actionRowsFromSelect(sqlite3_stmt * stmt); const std::list<ActionRow> actionRowsFromSelect(sqlite3_stmt * stmt);
ActionRow actionRowFromRollbackAction(const RollbackAction & action); ActionRow actionRowFromRollbackAction(const RollbackAction & action);