From 604cb2966c08b075591b9c1d63c7a45fa2e4e0d9 Mon Sep 17 00:00:00 2001 From: abitmore Date: Sat, 23 Jul 2022 20:53:25 +0000 Subject: [PATCH] Simplify code for LP-related database APIs --- libraries/app/database_api.cpp | 125 ++++------------------------ libraries/app/database_api_impl.hxx | 39 ++------- 2 files changed, 26 insertions(+), 138 deletions(-) diff --git a/libraries/app/database_api.cpp b/libraries/app/database_api.cpp index dce24a718c..d6c59a17fa 100644 --- a/libraries/app/database_api.cpp +++ b/libraries/app/database_api.cpp @@ -1720,68 +1720,24 @@ vector database_api::list_liquidity_pools( const optional& start_id, const optional& with_statistics )const { - return my->list_liquidity_pools( + return my->get_liquidity_pools_by_asset_x( limit, start_id, with_statistics ); } -vector database_api_impl::list_liquidity_pools( - const optional& olimit, - const optional& ostart_id, - const optional& with_statistics )const -{ - uint32_t limit = olimit.valid() ? *olimit : application_options::get_default().api_limit_get_liquidity_pools; - - FC_ASSERT( _app_options, "Internal error" ); - const auto configured_limit = _app_options->api_limit_get_liquidity_pools; - FC_ASSERT( limit <= configured_limit, - "limit can not be greater than ${configured_limit}", - ("configured_limit", configured_limit) ); - - bool with_stats = ( with_statistics.valid() && *with_statistics ); - - vector results; - - liquidity_pool_id_type start_id = ostart_id.valid() ? *ostart_id : liquidity_pool_id_type(); - - const auto& idx = _db.get_index_type().indices().get(); - auto lower_itr = idx.lower_bound( start_id ); - auto upper_itr = idx.end(); - - results.reserve( limit ); - for ( ; lower_itr != upper_itr && results.size() < limit; ++lower_itr ) - { - results.emplace_back( extend_liquidity_pool( *lower_itr, with_stats ) ); - } - - return results; -} - vector database_api::get_liquidity_pools_by_asset_a( const std::string& asset_symbol_or_id, const optional& limit, const optional& start_id, const optional& with_statistics )const { - return my->get_liquidity_pools_by_asset_a( - asset_symbol_or_id, - limit, - start_id, - with_statistics ); -} - -vector database_api_impl::get_liquidity_pools_by_asset_a( - const std::string& asset_symbol_or_id, - const optional& limit, - const optional& start_id, - const optional& with_statistics )const -{ - return get_liquidity_pools_by_asset_x( - asset_symbol_or_id, + asset_id_type asset_id = my->get_asset_from_string(asset_symbol_or_id)->id; + return my->get_liquidity_pools_by_asset_x( limit, start_id, - with_statistics ); + with_statistics, + asset_id ); } vector database_api::get_liquidity_pools_by_asset_b( @@ -1790,24 +1746,12 @@ vector database_api::get_liquidity_pools_by_asse const optional& start_id, const optional& with_statistics )const { - return my->get_liquidity_pools_by_asset_b( - asset_symbol_or_id, - limit, - start_id, - with_statistics ); -} - -vector database_api_impl::get_liquidity_pools_by_asset_b( - const std::string& asset_symbol_or_id, - const optional& limit, - const optional& start_id, - const optional& with_statistics )const -{ - return get_liquidity_pools_by_asset_x( - asset_symbol_or_id, + asset_id_type asset_id = my->get_asset_from_string(asset_symbol_or_id)->id; + return my->get_liquidity_pools_by_asset_x( limit, start_id, - with_statistics ); + with_statistics, + asset_id ); } vector database_api::get_liquidity_pools_by_one_asset( @@ -1869,51 +1813,16 @@ vector database_api::get_liquidity_pools_by_both const optional& start_id, const optional& with_statistics )const { - return my->get_liquidity_pools_by_both_assets( - asset_symbol_or_id_a, - asset_symbol_or_id_b, - limit, - start_id, - with_statistics ); -} - -vector database_api_impl::get_liquidity_pools_by_both_assets( - const std::string& asset_symbol_or_id_a, - const std::string& asset_symbol_or_id_b, - const optional& olimit, - const optional& ostart_id, - const optional& with_statistics )const -{ - uint32_t limit = olimit.valid() ? *olimit : application_options::get_default().api_limit_get_liquidity_pools; - - FC_ASSERT( _app_options, "Internal error" ); - const auto configured_limit = _app_options->api_limit_get_liquidity_pools; - FC_ASSERT( limit <= configured_limit, - "limit can not be greater than ${configured_limit}", - ("configured_limit", configured_limit) ); - - bool with_stats = ( with_statistics.valid() && *with_statistics ); - - vector results; - - asset_id_type asset_id_a = get_asset_from_string(asset_symbol_or_id_a)->id; - asset_id_type asset_id_b = get_asset_from_string(asset_symbol_or_id_b)->id; + asset_id_type asset_id_a = my->get_asset_from_string(asset_symbol_or_id_a)->id; + asset_id_type asset_id_b = my->get_asset_from_string(asset_symbol_or_id_b)->id; if( asset_id_a > asset_id_b ) std::swap( asset_id_a, asset_id_b ); - - liquidity_pool_id_type start_id = ostart_id.valid() ? *ostart_id : liquidity_pool_id_type(); - - const auto& idx = _db.get_index_type().indices().get(); - auto lower_itr = idx.lower_bound( std::make_tuple( asset_id_a, asset_id_b, start_id ) ); - auto upper_itr = idx.upper_bound( std::make_tuple( asset_id_a, asset_id_b ) ); - - results.reserve( limit ); - for ( ; lower_itr != upper_itr && results.size() < limit; ++lower_itr ) - { - results.emplace_back( extend_liquidity_pool( *lower_itr, with_stats ) ); - } - - return results; + return my->get_liquidity_pools_by_asset_x( + limit, + start_id, + with_statistics, + asset_id_a, + asset_id_b ); } vector> database_api::get_liquidity_pools( diff --git a/libraries/app/database_api_impl.hxx b/libraries/app/database_api_impl.hxx index 06ddc05ffb..ed119b7107 100644 --- a/libraries/app/database_api_impl.hxx +++ b/libraries/app/database_api_impl.hxx @@ -136,31 +136,11 @@ class database_api_impl : public std::enable_shared_from_this uint32_t limit )const; // Liquidity pools - vector list_liquidity_pools( - const optional& limit, - const optional& start_id, - const optional& with_statistics )const; - vector get_liquidity_pools_by_asset_a( - const std::string& asset_symbol_or_id, - const optional& limit, - const optional& start_id, - const optional& with_statistics )const; - vector get_liquidity_pools_by_asset_b( - const std::string& asset_symbol_or_id, - const optional& limit, - const optional& start_id, - const optional& with_statistics )const; vector get_liquidity_pools_by_one_asset( const std::string& asset_symbol_or_id, const optional& limit, const optional& start_id, const optional& with_statistics )const; - vector get_liquidity_pools_by_both_assets( - const std::string& asset_symbol_or_id_a, - const std::string& asset_symbol_or_id_b, - const optional& limit, - const optional& start_id, - const optional& with_statistics )const; vector> get_liquidity_pools( const vector& ids, const optional& subscribe, @@ -291,12 +271,12 @@ class database_api_impl : public std::enable_shared_from_this } // template function to reduce duplicate code - template + template vector get_liquidity_pools_by_asset_x( - std::string asset_symbol_or_id, - optional olimit, - optional ostart_id, - optional with_statistics )const + const optional& olimit, + const optional& ostart_id, + const optional& with_statistics, + X... x )const { uint32_t limit = olimit.valid() ? *olimit : application_options::get_default().api_limit_get_liquidity_pools; @@ -310,13 +290,12 @@ class database_api_impl : public std::enable_shared_from_this vector results; - const asset_id_type asset_id = get_asset_from_string(asset_symbol_or_id)->id; - liquidity_pool_id_type start_id = ostart_id.valid() ? *ostart_id : liquidity_pool_id_type(); - const auto& idx = _db.get_index_type().indices().get(); - auto lower_itr = idx.lower_bound( std::make_tuple( asset_id, start_id ) ); - auto upper_itr = idx.upper_bound( asset_id ); + const auto& idx = _db.get_index_type().indices().get(); + + auto lower_itr = idx.lower_bound( make_tuple_if_multiple( x..., start_id ) ); + auto upper_itr = call_end_or_upper_bound( idx, x... ); results.reserve( limit ); for ( ; lower_itr != upper_itr && results.size() < limit; ++lower_itr )