Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Commit

Permalink
Minor bugfix #421
Browse files Browse the repository at this point in the history
  • Loading branch information
vpavliv committed Mar 15, 2018
1 parent 3d8b261 commit f405af6
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions plugins/mongo_db/mongo_db_writer.cpp
Expand Up @@ -43,20 +43,14 @@ namespace mongo_db {

void mongo_db_writer::on_block(const signed_block& block) {
try {

ilog("MongoDB on_block: pushing block number ${p}", ("p", block.block_num()));
_blocks[block.block_num()] = block;

// Update last irreversible block number
last_irreversible_block_num = _db.last_non_undoable_block_num();
if (last_irreversible_block_num >= _blocks.begin()->first) {
// Having irreversible blocks. Writing em into Mongo.
try {
write_blocks();
}
catch (...) {
ilog("Exception in MongoDB on_block while writing blocks");
}
write_blocks();
}

++processed_blocks;
Expand All @@ -67,26 +61,32 @@ namespace mongo_db {
catch (...) {
ilog("Unknown exception in MongoDB on_block");
}

}

void mongo_db_writer::write_blocks() {
try {
mongocxx::options::bulk_write bulk_opts;
bulk_opts.ordered(false);
mongocxx::bulk_write _bulk{bulk_opts};

// Write all the blocks that has num less then last irreversible block
while (!_blocks.empty() && _blocks.begin()->first <= last_irreversible_block_num) {
auto head_iter = _blocks.begin();
write_block(head_iter->second, _bulk);
_blocks.erase(head_iter);
}

mongocxx::options::bulk_write bulk_opts;
bulk_opts.ordered(false);
mongocxx::bulk_write _bulk{bulk_opts};
auto blocks_table = mongo_conn[db_name][blocks]; // Blocks

// Write all the blocks that has num less then last irreversible block
while (!_blocks.empty() && _blocks.begin()->first <= last_irreversible_block_num) {
auto head_iter = _blocks.begin();
write_block(head_iter->second, _bulk);
_blocks.erase(head_iter);
if (!blocks_table.bulk_write(_bulk)) {
ilog("Failed to write blocks to Mongo DB");
}
}

auto blocks_table = mongo_conn[db_name][blocks]; // Blocks

if (!blocks_table.bulk_write(_bulk)) {
ilog("Failed to write blocks to Mongo DB");
catch (mongocxx::exception & ex) {
ilog("Exception in MongoDB write_blocks: ${p}", ("p", ex.what()));
}
catch (...) {
ilog("Unknown exception in MongoDB write_blocks");
}
}

Expand Down

0 comments on commit f405af6

Please sign in to comment.