Skip to content

Commit ef100f1

Browse files
committedMay 23, 2016
Fix rollback.txt migration
Broken by b1965ac. This also prepares the begin and commit statements only once.
1 parent 22f78ea commit ef100f1

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed
 

‎src/rollback.cpp

+27-14
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ RollbackManager::RollbackManager(const std::string & world_path,
9393
std::string migrating_flag = txt_filename + ".migrating";
9494
database_path = world_path + DIR_DELIM "rollback.sqlite";
9595

96-
initDatabase();
96+
bool created = initDatabase();
9797

98-
if (fs::PathExists(txt_filename) && (fs::PathExists(migrating_flag) ||
99-
!fs::PathExists(database_path))) {
98+
if (fs::PathExists(txt_filename) && (created ||
99+
fs::PathExists(migrating_flag))) {
100100
std::ofstream of(migrating_flag.c_str());
101101
of.close();
102102
migrate(txt_filename);
@@ -250,15 +250,15 @@ bool RollbackManager::createTables()
250250
}
251251

252252

253-
void RollbackManager::initDatabase()
253+
bool RollbackManager::initDatabase()
254254
{
255255
verbosestream << "RollbackManager: Database connection setup" << std::endl;
256256

257-
bool needsCreate = !fs::PathExists(database_path);
257+
bool needs_create = !fs::PathExists(database_path);
258258
SQLOK(sqlite3_open_v2(database_path.c_str(), &db,
259259
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL));
260260

261-
if (needsCreate) {
261+
if (needs_create) {
262262
createTables();
263263
}
264264

@@ -374,6 +374,8 @@ void RollbackManager::initDatabase()
374374
);
375375
}
376376
SQLOK(sqlite3_reset(stmt_knownNode_select));
377+
378+
return needs_create;
377379
}
378380

379381

@@ -672,23 +674,27 @@ void RollbackManager::migrate(const std::string & file_path)
672674

673675
std::streampos file_size = fh.tellg();
674676

675-
if (file_size > 10) {
677+
if (file_size < 10) {
676678
errorstream << "Empty rollback log." << std::endl;
677679
return;
678680
}
679681

680682
fh.seekg(0);
681683

684+
sqlite3_stmt *stmt_begin;
685+
sqlite3_stmt *stmt_commit;
686+
SQLOK(sqlite3_prepare_v2(db, "BEGIN", -1, &stmt_begin, NULL));
687+
SQLOK(sqlite3_prepare_v2(db, "COMMIT", -1, &stmt_commit, NULL));
688+
682689
std::string bit;
683690
int i = 0;
684-
int id = 1;
685691
time_t start = time(0);
686692
time_t t = start;
687-
sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);
693+
SQLRES(sqlite3_step(stmt_begin), SQLITE_DONE);
694+
sqlite3_reset(stmt_begin);
688695
do {
689696
ActionRow row;
690-
691-
row.id = id;
697+
row.id = 0;
692698

693699
// Get the timestamp
694700
std::getline(fh, bit, ' ');
@@ -758,17 +764,24 @@ void RollbackManager::migrate(const std::string & file_path)
758764
++i;
759765

760766
if (time(0) - t >= 1) {
761-
sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
767+
SQLRES(sqlite3_step(stmt_commit), SQLITE_DONE);
768+
sqlite3_reset(stmt_commit);
762769
t = time(0);
763770
std::cout
764771
<< " Done: " << static_cast<int>((static_cast<float>(fh.tellg()) / static_cast<float>(file_size)) * 100) << "%"
765772
<< " Speed: " << i / (t - start) << "/second \r" << std::flush;
766-
sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);
773+
SQLRES(sqlite3_step(stmt_begin), SQLITE_DONE);
774+
sqlite3_reset(stmt_begin);
767775
}
768776
} while (fh.good());
777+
SQLRES(sqlite3_step(stmt_commit), SQLITE_DONE);
778+
sqlite3_reset(stmt_commit);
779+
780+
SQLOK(sqlite3_finalize(stmt_begin));
781+
SQLOK(sqlite3_finalize(stmt_commit));
769782

770783
std::cout
771-
<< " Done: 100% " << std::endl
784+
<< " Done: 100% " << std::endl
772785
<< "Now you can delete the old rollback.txt file." << std::endl;
773786
}
774787

‎src/rollback.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class RollbackManager: public IRollbackManager
6161
const char * getActorName(const int id);
6262
const char * getNodeName(const int id);
6363
bool createTables();
64-
void initDatabase();
64+
bool initDatabase();
6565
bool registerRow(const ActionRow & row);
6666
const std::list<ActionRow> actionRowsFromSelect(sqlite3_stmt * stmt);
6767
ActionRow actionRowFromRollbackAction(const RollbackAction & action);

0 commit comments

Comments
 (0)
Please sign in to comment.