BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
database_api_helper.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 
26 namespace graphene { namespace app {
27 
29 {
30 public:
32  explicit database_api_helper( const graphene::app::application& app );
33 
34  // Member variables
37 
38  // Accounts
39  const account_object* get_account_from_string( const std::string& name_or_id,
40  bool throw_if_not_found = true ) const;
41 
42  // Assets
43  const asset_object* get_asset_from_string( const std::string& symbol_or_id,
44  bool throw_if_not_found = true ) const;
45 
48  template <typename X>
49  auto make_tuple_if_multiple(X x) const
50  { return x; }
51 
52  template <typename... X>
53  auto make_tuple_if_multiple(X... x) const
54  { return std::make_tuple( x... ); }
55 
56  template <typename T>
57  auto call_end_or_upper_bound( const T& t ) const
58  { return std::end( t ); }
59 
60  template <typename T, typename... X>
61  auto call_end_or_upper_bound( const T& t, X... x ) const
62  { return t.upper_bound( make_tuple_if_multiple( x... ) ); }
63 
64  template <typename OBJ_TYPE, typename OBJ_ID_TYPE, typename INDEX_TYPE, typename T, typename... X >
65  vector<OBJ_TYPE> get_objects_by_x(
66  T application_options::* app_opt_member_ptr,
67  const INDEX_TYPE& idx,
68  const optional<uint32_t>& olimit,
69  const optional<OBJ_ID_TYPE>& ostart_id,
70  X... x ) const
71  {
72  FC_ASSERT( _app_options, "Internal error" );
73  const auto configured_limit = _app_options->*app_opt_member_ptr;
74  uint64_t limit = olimit.valid() ? *olimit : configured_limit;
75  FC_ASSERT( limit <= configured_limit,
76  "limit can not be greater than ${configured_limit}",
77  ("configured_limit", configured_limit) );
78 
79  vector<OBJ_TYPE> results;
80 
81  OBJ_ID_TYPE start_obj_id = ostart_id.valid() ? *ostart_id : OBJ_ID_TYPE();
82  object_id_type start_id { start_obj_id };
83 
84  auto lower_itr = idx.lower_bound( make_tuple_if_multiple( x..., start_id ) );
85  auto upper_itr = call_end_or_upper_bound( idx, x... );
86 
87  results.reserve( limit );
88  while( lower_itr != upper_itr && results.size() < limit )
89  {
90  results.emplace_back( *lower_itr );
91  ++lower_itr;
92  }
93 
94  return results;
95  }
97 
98 };
99 
100 } } // graphene::app
graphene::app::database_api_helper::get_asset_from_string
const asset_object * get_asset_from_string(const std::string &symbol_or_id, bool throw_if_not_found=true) const
Definition: database_api.cpp:3068
graphene::app::database_api_helper::call_end_or_upper_bound
auto call_end_or_upper_bound(const T &t, X... x) const
Definition: database_api_helper.hxx:61
graphene::chain::database
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
graphene::app::database_api_helper::get_objects_by_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
Definition: database_api_helper.hxx:65
graphene::app::database_api_helper::_app_options
const application_options * _app_options
Definition: database_api_helper.hxx:36
graphene::app::database_api_helper::call_end_or_upper_bound
auto call_end_or_upper_bound(const T &t) const
Definition: database_api_helper.hxx:57
graphene::chain::asset_object
tracks the parameters of an asset
Definition: asset_object.hpp:75
graphene::app::application
Definition: application.hpp:91
graphene::app::database_api_helper::database_api_helper
database_api_helper(graphene::chain::database &db, const application_options *app_options)
Definition: database_api.cpp:59
fc::optional::valid
bool valid() const
Definition: optional.hpp:186
graphene::chain::account_object
This class represents an account on the object graph.
Definition: account_object.hpp:180
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
graphene::db::object_id_type
Definition: object_id.hpp:30
graphene::app::database_api_helper::_db
graphene::chain::database & _db
Definition: database_api_helper.hxx:35
fc::optional
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
graphene::app::database_api_helper::make_tuple_if_multiple
auto make_tuple_if_multiple(X... x) const
Definition: database_api_helper.hxx:53
graphene
Definition: api.cpp:48
graphene::app::database_api_helper::make_tuple_if_multiple
auto make_tuple_if_multiple(X x) const
Definition: database_api_helper.hxx:49
graphene::app::application_options
Definition: application.hpp:38
graphene::app::database_api_helper
Definition: database_api_helper.hxx:28
graphene::app::database_api_helper::get_account_from_string
const account_object * get_account_from_string(const std::string &name_or_id, bool throw_if_not_found=true) const
Definition: database_api.cpp:3042