Skip to content

Commit

Permalink
Merge pull request #1429 from oxarbitrage/es_objects_templates
Browse files Browse the repository at this point in the history
Add an adaptor to es_objects and template function to reduce code
  • Loading branch information
oxarbitrage authored Dec 19, 2018
2 parents e5978ca + 1e8ccab commit 5c7fd22
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 273 deletions.
195 changes: 22 additions & 173 deletions libraries/plugins/es_objects/es_objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
#include <graphene/chain/proposal_object.hpp>
#include <graphene/chain/balance_object.hpp>
#include <graphene/chain/market_object.hpp>
#include <graphene/chain/asset_object.hpp>
#include <graphene/chain/account_object.hpp>

#include <graphene/utilities/elasticsearch.hpp>


namespace graphene { namespace es_objects {

namespace detail
Expand Down Expand Up @@ -72,12 +73,8 @@ class es_objects_plugin_impl
fc::time_point_sec block_time;

private:
void prepare_proposal(const proposal_object& proposal_object);
void prepare_account(const account_object& account_object);
void prepare_asset(const asset_object& asset_object);
void prepare_balance(const account_balance_object& account_balance_object);
void prepare_limit(const limit_order_object& limit_object);
void prepare_bitasset(const asset_bitasset_data_object& bitasset_object);
template<typename T>
void prepareTemplate(T blockchain_object, string index_name);
};

bool es_objects_plugin_impl::index_database( const vector<object_id_type>& ids, std::string action)
Expand All @@ -102,7 +99,7 @@ bool es_objects_plugin_impl::index_database( const vector<object_id_type>& ids,
if(action == "delete")
remove_from_database(p->id, "proposal");
else
prepare_proposal(*p);
prepareTemplate<proposal_object>(*p, "proposal");
}
}
else if(value.is<account_object>() && _es_objects_accounts) {
Expand All @@ -112,7 +109,7 @@ bool es_objects_plugin_impl::index_database( const vector<object_id_type>& ids,
if(action == "delete")
remove_from_database(a->id, "account");
else
prepare_account(*a);
prepareTemplate<account_object>(*a, "account");
}
}
else if(value.is<asset_object>() && _es_objects_assets) {
Expand All @@ -122,7 +119,7 @@ bool es_objects_plugin_impl::index_database( const vector<object_id_type>& ids,
if(action == "delete")
remove_from_database(a->id, "asset");
else
prepare_asset(*a);
prepareTemplate<asset_object>(*a, "asset");
}
}
else if(value.is<account_balance_object>() && _es_objects_balances) {
Expand All @@ -132,7 +129,7 @@ bool es_objects_plugin_impl::index_database( const vector<object_id_type>& ids,
if(action == "delete")
remove_from_database(b->id, "balance");
else
prepare_balance(*b);
prepareTemplate<account_balance_object>(*b, "balance");
}
}
else if(value.is<limit_order_object>() && _es_objects_limit_orders) {
Expand All @@ -142,7 +139,7 @@ bool es_objects_plugin_impl::index_database( const vector<object_id_type>& ids,
if(action == "delete")
remove_from_database(l->id, "limitorder");
else
prepare_limit(*l);
prepareTemplate<limit_order_object>(*l, "limitorder");
}
}
else if(value.is<asset_bitasset_data_object>() && _es_objects_asset_bitasset) {
Expand All @@ -152,7 +149,7 @@ bool es_objects_plugin_impl::index_database( const vector<object_id_type>& ids,
if(action == "delete")
remove_from_database(ba->id, "bitasset");
else
prepare_bitasset(*ba);
prepareTemplate<asset_bitasset_data_object>(*ba, "bitasset");
}
}
}
Expand Down Expand Up @@ -190,181 +187,33 @@ void es_objects_plugin_impl::remove_from_database( object_id_type id, std::strin
}
}

void es_objects_plugin_impl::prepare_proposal(const proposal_object& proposal_object)
{
proposal_struct prop;
prop.object_id = proposal_object.id;
prop.block_time = block_time;
prop.block_number = block_number;
prop.expiration_time = proposal_object.expiration_time;
prop.review_period_time = proposal_object.review_period_time;
prop.proposed_transaction = fc::json::to_string(proposal_object.proposed_transaction);
prop.required_owner_approvals = fc::json::to_string(proposal_object.required_owner_approvals);
prop.available_owner_approvals = fc::json::to_string(proposal_object.available_owner_approvals);
prop.required_active_approvals = fc::json::to_string(proposal_object.required_active_approvals);
prop.available_key_approvals = fc::json::to_string(proposal_object.available_key_approvals);
prop.proposer = proposal_object.proposer;

std::string data = fc::json::to_string(prop);

fc::mutable_variant_object bulk_header;
bulk_header["_index"] = _es_objects_index_prefix + "proposal";
bulk_header["_type"] = "data";
if(_es_objects_keep_only_current)
{
bulk_header["_id"] = string(prop.object_id);
}

prepare = graphene::utilities::createBulk(bulk_header, std::move(data));
std::move(prepare.begin(), prepare.end(), std::back_inserter(bulk));
prepare.clear();
}

void es_objects_plugin_impl::prepare_account(const account_object& account_object)
{
account_struct acct;
acct.object_id = account_object.id;
acct.block_time = block_time;
acct.block_number = block_number;
acct.membership_expiration_date = account_object.membership_expiration_date;
acct.registrar = account_object.registrar;
acct.referrer = account_object.referrer;
acct.lifetime_referrer = account_object.lifetime_referrer;
acct.network_fee_percentage = account_object.network_fee_percentage;
acct.lifetime_referrer_fee_percentage = account_object.lifetime_referrer_fee_percentage;
acct.referrer_rewards_percentage = account_object.referrer_rewards_percentage;
acct.name = account_object.name;
acct.owner_account_auths = fc::json::to_string(account_object.owner.account_auths);
acct.owner_key_auths = fc::json::to_string(account_object.owner.key_auths);
acct.owner_address_auths = fc::json::to_string(account_object.owner.address_auths);
acct.active_account_auths = fc::json::to_string(account_object.active.account_auths);
acct.active_key_auths = fc::json::to_string(account_object.active.key_auths);
acct.active_address_auths = fc::json::to_string(account_object.active.address_auths);
acct.voting_account = account_object.options.voting_account;
acct.votes = fc::json::to_string(account_object.options.votes);

std::string data = fc::json::to_string(acct);

fc::mutable_variant_object bulk_header;
bulk_header["_index"] = _es_objects_index_prefix + "account";
bulk_header["_type"] = "data";
if(_es_objects_keep_only_current)
{
bulk_header["_id"] = string(acct.object_id);
}

prepare = graphene::utilities::createBulk(bulk_header, std::move(data));
std::move(prepare.begin(), prepare.end(), std::back_inserter(bulk));
prepare.clear();
}

void es_objects_plugin_impl::prepare_asset(const asset_object& asset_object)
{
asset_struct asset;
asset.object_id = asset_object.id;
asset.block_time = block_time;
asset.block_number = block_number;
asset.symbol = asset_object.symbol;
asset.issuer = asset_object.issuer;
asset.is_market_issued = asset_object.is_market_issued();
asset.dynamic_asset_data_id = asset_object.dynamic_asset_data_id;
asset.bitasset_data_id = asset_object.bitasset_data_id;

std::string data = fc::json::to_string(asset);

fc::mutable_variant_object bulk_header;
bulk_header["_index"] = _es_objects_index_prefix + "asset";
bulk_header["_type"] = "data";
if(_es_objects_keep_only_current)
{
bulk_header["_id"] = string(asset.object_id);
}

prepare = graphene::utilities::createBulk(bulk_header, std::move(data));
std::move(prepare.begin(), prepare.end(), std::back_inserter(bulk));
prepare.clear();
}

void es_objects_plugin_impl::prepare_balance(const account_balance_object& account_balance_object)
template<typename T>
void es_objects_plugin_impl::prepareTemplate(T blockchain_object, string index_name)
{
balance_struct balance;
balance.object_id = account_balance_object.id;
balance.block_time = block_time;
balance.block_number = block_number;
balance.owner = account_balance_object.owner;
balance.asset_type = account_balance_object.asset_type;
balance.balance = account_balance_object.balance;
balance.maintenance_flag = account_balance_object.maintenance_flag;

std::string data = fc::json::to_string(balance);

fc::mutable_variant_object bulk_header;
bulk_header["_index"] = _es_objects_index_prefix + "balance";
bulk_header["_index"] = _es_objects_index_prefix + index_name;
bulk_header["_type"] = "data";
if(_es_objects_keep_only_current)
{
bulk_header["_id"] = string(balance.object_id);
bulk_header["_id"] = string(blockchain_object.id);
}

prepare = graphene::utilities::createBulk(bulk_header, std::move(data));
std::move(prepare.begin(), prepare.end(), std::back_inserter(bulk));
prepare.clear();
}
adaptor_struct adaptor;
fc::variant blockchain_object_variant;
fc::to_variant( blockchain_object, blockchain_object_variant, GRAPHENE_NET_MAX_NESTED_OBJECTS );
fc::mutable_variant_object o = adaptor.adapt(blockchain_object_variant.get_object());

void es_objects_plugin_impl::prepare_limit(const limit_order_object& limit_object)
{
limit_order_struct limit;
limit.object_id = limit_object.id;
limit.block_time = block_time;
limit.block_number = block_number;
limit.expiration = limit_object.expiration;
limit.seller = limit_object.seller;
limit.for_sale = limit_object.for_sale;
limit.sell_price = limit_object.sell_price;
limit.deferred_fee = limit_object.deferred_fee;

std::string data = fc::json::to_string(limit);
o["object_id"] = string(blockchain_object.id);
o["block_time"] = block_time;
o["block_number"] = block_number;

fc::mutable_variant_object bulk_header;
bulk_header["_index"] = _es_objects_index_prefix + "limitorder";
bulk_header["_type"] = "data";
if(_es_objects_keep_only_current)
{
bulk_header["_id"] = string(limit.object_id);
}
string data = fc::json::to_string(o, fc::json::legacy_generator);

prepare = graphene::utilities::createBulk(bulk_header, std::move(data));
std::move(prepare.begin(), prepare.end(), std::back_inserter(bulk));
prepare.clear();
}

void es_objects_plugin_impl::prepare_bitasset(const asset_bitasset_data_object& bitasset_object)
{
if(!bitasset_object.is_prediction_market) {

bitasset_struct bitasset;
bitasset.object_id = bitasset_object.id;
bitasset.block_time = block_time;
bitasset.block_number = block_number;
bitasset.current_feed = fc::json::to_string(bitasset_object.current_feed);
bitasset.current_feed_publication_time = bitasset_object.current_feed_publication_time;

std::string data = fc::json::to_string(bitasset);

fc::mutable_variant_object bulk_header;
bulk_header["_index"] = _es_objects_index_prefix + "bitasset";
bulk_header["_type"] = "data";
if(_es_objects_keep_only_current)
{
bulk_header["_id"] = string(bitasset.object_id);
}

prepare = graphene::utilities::createBulk(bulk_header, std::move(data));
std::move(prepare.begin(), prepare.end(), std::back_inserter(bulk));
prepare.clear();
}
}

es_objects_plugin_impl::~es_objects_plugin_impl()
{
return;
Expand Down
Loading

0 comments on commit 5c7fd22

Please sign in to comment.