Skip to content

Commit

Permalink
Merge pull request #2613 from bitshares/refactor-api-limits
Browse files Browse the repository at this point in the history
Refactor API code
  • Loading branch information
abitmore authored Jul 25, 2022
2 parents 908609c + 604cb29 commit 296808a
Show file tree
Hide file tree
Showing 20 changed files with 947 additions and 945 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
name: Build and test in macOS
strategy:
matrix:
os: [macos-10.15, macos-11]
os: [macos-11]
runs-on: ${{ matrix.os }}
steps:
- name: Install dependencies
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/build-and-test.ubuntu-debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ jobs:
run: |
pwd
df -h .
free
sudo dd if=/dev/zero of=/swapfile bs=1024 count=4M
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
free
mkdir -p _build
sudo mkdir -p /_build/libraries /_build/programs /_build/tests /mnt/_build
sudo chmod a+rwx /_build/libraries /_build/programs /_build/tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.ubuntu-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
run: |
export CCACHE_DIR="$GITHUB_WORKSPACE/ccache"
mkdir -p "$CCACHE_DIR"
make -j 2 -C _build
make -j 1 -C _build
df -h
- name: Unit-Tests
run: |
Expand Down
229 changes: 131 additions & 98 deletions libraries/app/api.cpp

Large diffs are not rendered by default.

152 changes: 83 additions & 69 deletions libraries/app/application.cpp

Large diffs are not rendered by default.

373 changes: 109 additions & 264 deletions libraries/app/database_api.cpp

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions libraries/app/database_api_helper.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2017 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once

namespace graphene { namespace app {

class database_api_helper
{
public:
database_api_helper( graphene::chain::database& db, const application_options* app_options );
explicit database_api_helper( graphene::app::application& app );

// Member variables
graphene::chain::database& _db;
const application_options* _app_options = nullptr;

// Accounts
const account_object* get_account_from_string( const std::string& name_or_id,
bool throw_if_not_found = true ) const;

// Assets
const asset_object* get_asset_from_string( const std::string& symbol_or_id,
bool throw_if_not_found = true ) const;

/// Template functions for simple list_X and get_X_by_T APIs, to reduce duplicate code
/// @{
template <typename X>
auto make_tuple_if_multiple(X x) const
{ return x; }

template <typename... X>
auto make_tuple_if_multiple(X... x) const
{ return std::make_tuple( x... ); }

template <typename T>
auto call_end_or_upper_bound( const T& t ) const
{ return std::end( t ); }

template <typename T, typename... X>
auto call_end_or_upper_bound( const T& t, X... x ) const
{ return t.upper_bound( make_tuple_if_multiple( x... ) ); }

template <typename OBJ_TYPE, typename OBJ_ID_TYPE, typename INDEX_TYPE, typename T, typename... X >
vector<OBJ_TYPE> get_objects_by_x(
T application_options::* app_opt_member_ptr,
const INDEX_TYPE& idx,
const optional<uint32_t>& olimit,
const optional<OBJ_ID_TYPE>& ostart_id,
X... x ) const
{
uint64_t limit = olimit.valid() ? *olimit : ( application_options::get_default().*app_opt_member_ptr );

FC_ASSERT( _app_options, "Internal error" );
const auto configured_limit = _app_options->*app_opt_member_ptr;
FC_ASSERT( limit <= configured_limit,
"limit can not be greater than ${configured_limit}",
("configured_limit", configured_limit) );

vector<OBJ_TYPE> results;

OBJ_ID_TYPE start_id = ostart_id.valid() ? *ostart_id : OBJ_ID_TYPE();

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 )
{
results.emplace_back( *lower_itr );
}

return results;
}
/// @}

};

} } // graphene::app
Loading

0 comments on commit 296808a

Please sign in to comment.