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

Commit

Permalink
Vote collection in mongo_db. #421
Browse files Browse the repository at this point in the history
  • Loading branch information
afalaleev committed Apr 22, 2018
1 parent f34efa6 commit b533809
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 47 deletions.
38 changes: 27 additions & 11 deletions plugins/mongo_db/mongo_db_operations.cpp
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
75 changes: 39 additions & 36 deletions plugins/mongo_db/mongo_db_writer.cpp
Expand Up @@ -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];
// }
// }
}
}}}

0 comments on commit b533809

Please sign in to comment.