From b53380977463af282baa26dee9d7d7975863d239 Mon Sep 17 00:00:00 2001 From: Andrew Falaleev Date: Sun, 22 Apr 2018 22:38:53 +0700 Subject: [PATCH] Vote collection in mongo_db. #421 --- plugins/mongo_db/mongo_db_operations.cpp | 38 ++++++++---- plugins/mongo_db/mongo_db_writer.cpp | 75 ++++++++++++------------ 2 files changed, 66 insertions(+), 47 deletions(-) diff --git a/plugins/mongo_db/mongo_db_operations.cpp b/plugins/mongo_db/mongo_db_operations.cpp index 4aed040ca6..9afd744899 100644 --- a/plugins/mongo_db/mongo_db_operations.cpp +++ b/plugins/mongo_db/mongo_db_operations.cpp @@ -130,7 +130,7 @@ namespace mongo_db { try { const comment_object& comment_obj = _db.get_comment(auth, perm); auto key = std::string(comment_obj.author).append("/").append(to_string(comment_obj.permlink)); - const auto hash = fc::sha512::hash(key); + const auto hash = fc::sha256::hash(key); format_value(comment_doc, "author", comment_obj.author); format_value(comment_doc, "permlink", to_string(comment_obj.permlink)); @@ -192,7 +192,7 @@ namespace mongo_db { // last_reply_by format_value(comment_doc, "json_metadata", to_string(comment_obj.json_metadata)); - format_comment_active_votes(comment_doc, comment_obj); + // format_comment_active_votes(comment_doc, comment_obj); // TODO: the following fields depends on follow-plugin operations - custom_json ... // format_value(comment_doc, "author_reputation", get_account_reputation(comment_obj.author)); @@ -273,12 +273,26 @@ namespace mongo_db { log_operation("vote"); - format_comment(body, op.author, op.permlink); - - format_value(body, "voter", op.voter); - format_value(body, "weight", op.weight); - - *data << "vote" << body; + try { + const comment_object& comment_obj = _db.get_comment(op.author, op.permlink); + auto key = std::string(op.author).append("/").append(op.permlink).append("/").append(op.voter); + const auto hash = fc::sha256::hash(key); + + format_value(body, "author", op.author); + format_value(body, "permlink", op.permlink); + format_value(body, "voter", op.voter); + format_value(body, "comment", comment_obj.id._id); + format_value(body, "_id", hash.str()); + format_value(body, "weight", op.weight); + + *data << "vote" << body; + } + catch (fc::exception& ex) { + ilog("MongoDB operations fc::exception. ${e}", ("e", ex.what())); + } + catch (...) { + ilog("Unknown exception during formatting comment."); + } } void operation_writer::operator()(const comment_operation &op) { @@ -586,7 +600,8 @@ namespace mongo_db { format_comment(body, op.author, op.permlink); - *data << "comment_options" << body; + // *data << "comment_options" << body; + *data << "comment_object" << body; } void operation_writer::operator()(const set_withdraw_vesting_route_operation &op) { @@ -1007,9 +1022,10 @@ namespace mongo_db { log_operation("comment_payout_update"); - format_comment(body, op.author, op.permlink); + // format_comment(body, op.author, op.permlink); - *data << "comment_payout_update" << body; + // *data << "comment_payout_update" << body; + *data << "comment_object" << body; } void operation_writer::operator()(const comment_benefactor_reward_operation& op) { diff --git a/plugins/mongo_db/mongo_db_writer.cpp b/plugins/mongo_db/mongo_db_writer.cpp index 1c819784bd..e002cb726b 100644 --- a/plugins/mongo_db/mongo_db_writer.cpp +++ b/plugins/mongo_db/mongo_db_writer.cpp @@ -248,41 +248,44 @@ namespace mongo_db { } mongocxx::collection mongo_db_writer::get_active_collection(const std::string& coll_type) { - - if (active_collections.find(coll_type) == active_collections.end()) { - std::string coll_name = coll_type + std::string("_1"); - mongocxx::collection coll = mongo_database[coll_name]; - active_collections[coll_type] = coll; - return coll; - } - else { - mongocxx::collection coll = active_collections[coll_type]; - std::string coll_name = coll.name().to_string(); - - const int64_t coll_size = coll.count(document{} << bsoncxx::builder::stream::finalize); - - if (coll_size > max_collection_size) { - - std::string new_coll_name; - - std::string::iterator iter = std::find(coll_name.begin(), coll_name.end(), '_'); - if (iter != coll_name.end()) { - // Always should be here - std::string coll_num_str = std::string(iter + 1, coll_name.end()); - std::istringstream ss(coll_num_str); - int coll_num = 0; - ss >> coll_num; - if (coll_num != 0) { - new_coll_name = coll_type + std::string("_") + std::to_string(coll_num + 1); - } - } - mongocxx::collection coll = mongo_database[new_coll_name]; - active_collections[coll_type] = coll; - return coll; - } - else { - return active_collections[coll_type]; - } - } + mongocxx::collection coll = mongo_database[coll_type]; + + return coll; +// +// if (active_collections.find(coll_type) == active_collections.end()) { +// std::string coll_name = coll_type + std::string("_1"); +// mongocxx::collection coll = mongo_database[coll_name]; +// active_collections[coll_type] = coll; +// return coll; +// } +// else { +// mongocxx::collection coll = active_collections[coll_type]; +// std::string coll_name = coll.name().to_string(); +// +// const int64_t coll_size = coll.count(document{} << bsoncxx::builder::stream::finalize); +// +// if (coll_size > max_collection_size) { +// +// std::string new_coll_name; +// +// std::string::iterator iter = std::find(coll_name.begin(), coll_name.end(), '_'); +// if (iter != coll_name.end()) { +// // Always should be here +// std::string coll_num_str = std::string(iter + 1, coll_name.end()); +// std::istringstream ss(coll_num_str); +// int coll_num = 0; +// ss >> coll_num; +// if (coll_num != 0) { +// new_coll_name = coll_type + std::string("_") + std::to_string(coll_num + 1); +// } +// } +// mongocxx::collection coll = mongo_database[new_coll_name]; +// active_collections[coll_type] = coll; +// return coll; +// } +// else { +// return active_collections[coll_type]; +// } +// } } }}}