From bfa600c5594cd05519fff72917f47133ebeeb516 Mon Sep 17 00:00:00 2001 From: elmato Date: Fri, 10 Feb 2017 08:36:07 +0000 Subject: [PATCH] handle new database_object signals, refactor --- libraries/app/database_api.cpp | 166 ++++++++++++++++++++------------- 1 file changed, 99 insertions(+), 67 deletions(-) diff --git a/libraries/app/database_api.cpp b/libraries/app/database_api.cpp index f92bf977..6b52053b 100644 --- a/libraries/app/database_api.cpp +++ b/libraries/app/database_api.cpp @@ -41,6 +41,8 @@ #define GET_REQUIRED_FEES_MAX_RECURSION 4 +typedef std::map< std::pair, std::vector > market_queue_type; + namespace graphene { namespace app { class database_api_impl; @@ -164,9 +166,26 @@ class database_api_impl : public std::enable_shared_from_this return _subscribe_filter.contains( i ); } - void broadcast_updates( const vector& updates ); + template + void enqueue_if_subscribed_to_market(const object* obj, market_queue_type& queue, bool full_object=true) + { + const T* order = dynamic_cast(obj); + FC_ASSERT( order != nullptr); + auto market = order->get_market(); + + auto sub = _market_subscriptions.find( market ); + if( sub != _market_subscriptions.end() ) { + queue[market].emplace_back( full_object ? obj->to_variant() : fc::variant(obj->id) ); + } + } + + void broadcast_updates( const vector& updates ); + void broadcast_market_updates( const market_queue_type& queue); + void check_for_market_objects(const vector& ids); + /** called every time a block is applied to report the objects that were changed */ + void on_objects_new(const vector& ids); void on_objects_changed(const vector& ids); void on_objects_removed(const vector& objs); void on_applied_block(); @@ -176,6 +195,7 @@ class database_api_impl : public std::enable_shared_from_this std::function _pending_trx_callback; std::function _block_applied_callback; + boost::signals2::scoped_connection _new_connection; boost::signals2::scoped_connection _change_connection; boost::signals2::scoped_connection _removed_connection; boost::signals2::scoped_connection _applied_block_connection; @@ -198,6 +218,9 @@ database_api::~database_api() {} database_api_impl::database_api_impl( graphene::chain::database& db ):_db(db) { wlog("creating database api ${x}", ("x",int64_t(this)) ); + _new_connection = _db.new_objects.connect([this](const vector& ids) { + on_objects_new(ids); + }); _change_connection = _db.changed_objects.connect([this](const vector& ids) { on_objects_changed(ids); }); @@ -628,7 +651,7 @@ std::map database_api_impl::get_full_accounts( const [&acnt] (const call_order_object& call) { acnt.call_orders.emplace_back(call); }); - + // get assets issued by user auto asset_range = _db.get_index_type().indices().get().equal_range(account->id); std::for_each(asset_range.first, asset_range.second, @@ -1800,10 +1823,27 @@ vector database_api_impl::get_blinded_balances( const fl void database_api_impl::broadcast_updates( const vector& updates ) { - if( updates.size() ) { + if( updates.size() && _subscribe_callback ) { auto capture_this = shared_from_this(); fc::async([capture_this,updates](){ - capture_this->_subscribe_callback( fc::variant(updates) ); + if(capture_this->_subscribe_callback) + capture_this->_subscribe_callback( fc::variant(updates) ); + }); + } +} + +void database_api_impl::broadcast_market_updates( const market_queue_type& queue) +{ + if( queue.size() ) + { + auto capture_this = shared_from_this(); + fc::async([capture_this, this, queue](){ + for( const auto& item : queue ) + { + auto sub = _market_subscriptions.find(item.first); + if( sub != _market_subscriptions.end() ) + sub->second( fc::variant(item.second ) ); + } }); } } @@ -1813,97 +1853,89 @@ void database_api_impl::on_objects_removed( const vector& objs ) /// we need to ensure the database_api is not deleted for the life of the async operation if( _subscribe_callback ) { - vector updates; - updates.reserve(objs.size()); + vector updates; - for( auto obj : objs ) { + for( auto obj : objs ) + { if ( is_subscribed_to_item(obj->id) ) - updates.emplace_back( obj->id ); + { + updates.push_back( fc::variant(obj->id) ); + } } + broadcast_updates( updates ); } if( _market_subscriptions.size() ) { - map< pair, vector > broadcast_queue; + market_queue_type broadcast_queue; + for( const auto& obj : objs ) { - const limit_order_object* order = dynamic_cast(obj); - if( order ) + if( obj->id.is() ) { - auto sub = _market_subscriptions.find( order->get_market() ); - if( sub != _market_subscriptions.end() ) - broadcast_queue[order->get_market()].emplace_back( order->id ); + enqueue_if_subscribed_to_market( obj, broadcast_queue, false ); + } + else if( obj->id.is() ) + { + enqueue_if_subscribed_to_market( obj, broadcast_queue, false ); } } - if( broadcast_queue.size() ) - { - auto capture_this = shared_from_this(); - fc::async([capture_this,this,broadcast_queue](){ - for( const auto& item : broadcast_queue ) - { - auto sub = _market_subscriptions.find(item.first); - if( sub != _market_subscriptions.end() ) - sub->second( fc::variant(item.second ) ); - } - }); - } + + broadcast_market_updates(broadcast_queue); } } +void database_api_impl::check_for_market_objects(const vector& ids) +{ + if( _market_subscriptions.size() ) + { + market_queue_type broadcast_queue; + + for(auto id : ids) + { + if( id.is() ) + { + enqueue_if_subscribed_to_market( _db.find_object(id), broadcast_queue ); + } + else if( id.is() ) + { + enqueue_if_subscribed_to_market( _db.find_object(id), broadcast_queue ); + } + } + + broadcast_market_updates(broadcast_queue); + } +} + +void database_api_impl::on_objects_new(const vector& ids) +{ + check_for_market_objects(ids); +} + void database_api_impl::on_objects_changed(const vector& ids) { - vector updates; - map< pair, vector > market_broadcast_queue; - - for(auto id : ids) + if( _subscribe_callback ) { - const object* obj = nullptr; - if( is_subscribed_to_item(id) ) + vector updates; + + for(auto id : ids) { - obj = _db.find_object( id ); - if( obj ) + const object* obj = nullptr; + if( is_subscribed_to_item(id) ) { - updates.emplace_back( obj->to_variant() ); - } - else - { - updates.emplace_back(id); // send just the id to indicate removal - } - } - - if( _market_subscriptions.size() ) - { - if( !is_subscribed_to_item(id) ) obj = _db.find_object( id ); - if( obj ) - { - const limit_order_object* order = dynamic_cast(obj); - if( order ) + if( obj ) { - auto sub = _market_subscriptions.find( order->get_market() ); - if( sub != _market_subscriptions.end() ) - market_broadcast_queue[order->get_market()].emplace_back( order->id ); + updates.emplace_back( obj->to_variant() ); } } } + + broadcast_updates(updates); } - auto capture_this = shared_from_this(); - - /// pushing the future back / popping the prior future if it is complete. - /// if a connection hangs then this could get backed up and result in - /// a failure to exit cleanly. - fc::async([capture_this,this,updates,market_broadcast_queue](){ - if( _subscribe_callback && updates.size() ) _subscribe_callback( updates ); - - for( const auto& item : market_broadcast_queue ) - { - auto sub = _market_subscriptions.find(item.first); - if( sub != _market_subscriptions.end() ) - sub->second( fc::variant(item.second ) ); - } - }); + check_for_market_objects(ids); } /** note: this method cannot yield because it is called in the middle of