Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3958 from EOSIO/gh#3948-get-table-rows-bounds
Browse files Browse the repository at this point in the history
Better get_table_rows upper/lower bounds support
  • Loading branch information
arhag authored Jun 8, 2018
2 parents bb72c7a + 5036477 commit a034551
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
31 changes: 31 additions & 0 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <eosio/chain/wast_to_wasm.hpp>

#include <boost/signals2/connection.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>

#include <fc/io/json.hpp>
#include <fc/variant.hpp>
Expand Down Expand Up @@ -600,6 +602,35 @@ read_only::get_info_results read_only::get_info(const read_only::get_info_params
};
}

template<>
uint64_t convert_to_type(const string& str, const string& desc) {
uint64_t value = 0;
try {
name s(str);
value = s.value;
} catch( ... ) {
try {
auto trimmed_str = str;
boost::trim(trimmed_str);
value = boost::lexical_cast<uint64_t>(trimmed_str.c_str(), trimmed_str.size());
} catch( ... ) {
try {
auto symb = eosio::chain::symbol::from_string(str);
value = symb.value();
} catch( ... ) {
try {
value = ( eosio::chain::string_to_symbol( 0, str.c_str() ) >> 8 );
} catch( ... ) {
FC_ASSERT( false, "Could not convert ${desc} string '${str}' to any of the following: "
"uint64_t, valid name, or valid symbol (with or without the precision)",
("desc", desc)("str", str));
}
}
}
}
return value;
}

abi_def get_abi( const controller& db, const name& account ) {
const auto &d = db.db();
const account_object *code_accnt = d.find<account_object, by_name>(account);
Expand Down
43 changes: 15 additions & 28 deletions plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#include <eosio/chain/plugin_interface.hpp>

#include <boost/container/flat_set.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>

#include <fc/static_variant.hpp>

Expand Down Expand Up @@ -50,6 +48,16 @@ struct permission {
template<typename>
struct resolver_factory;

template<typename Type>
Type convert_to_type(const string& str, const string& desc) {
try {
return fc::variant(str).as<Type>();
} FC_RETHROW_EXCEPTIONS(warn, "Could not convert ${desc} string '${str}' to key type.", ("desc", desc)("str",str) )
}

template<>
uint64_t convert_to_type(const string& str, const string& desc);

class read_only {
const controller& db;

Expand Down Expand Up @@ -273,28 +281,7 @@ class read_only {
read_only::get_table_rows_result result;
const auto& d = db.db();

uint64_t scope = 0;
try {
name s(p.scope);
scope = s.value;
} catch( ... ) {
try {
auto trimmed_scope_str = p.scope;
boost::trim(trimmed_scope_str);
scope = boost::lexical_cast<uint64_t>(trimmed_scope_str.c_str(), trimmed_scope_str.size());
} catch( ... ) {
try {
auto symb = eosio::chain::symbol::from_string(p.scope);
scope = symb.value();
} catch( ... ) {
try {
scope = ( eosio::chain::string_to_symbol( 0, p.scope.c_str() ) >> 8 );
} catch( ... ) {
FC_ASSERT( false, "could not convert scope string to any of the following: uint64_t, valid name, or valid symbol (with or without the precision)" );
}
}
}
}
uint64_t scope = convert_to_type<uint64_t>(p.scope, "scope");

abi_serializer abis;
abis.set_abi(abi);
Expand All @@ -306,12 +293,12 @@ class read_only {
auto upper = idx.lower_bound(boost::make_tuple(next_tid));

if (p.lower_bound.size()) {
lower = idx.lower_bound(boost::make_tuple(t_id->id, fc::variant(
p.lower_bound).as<typename IndexType::value_type::key_type>()));
auto lv = convert_to_type<typename IndexType::value_type::key_type>(p.lower_bound, "lower_bound");
lower = idx.lower_bound(boost::make_tuple(t_id->id, lv));
}
if (p.upper_bound.size()) {
upper = idx.lower_bound(boost::make_tuple(t_id->id, fc::variant(
p.upper_bound).as<typename IndexType::value_type::key_type>()));
auto uv = convert_to_type<typename IndexType::value_type::key_type>(p.upper_bound, "upper_bound");
upper = idx.lower_bound(boost::make_tuple(t_id->id, uv));
}

vector<char> data;
Expand Down
1 change: 1 addition & 0 deletions programs/cleos/httpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <ostream>
#include <string>
#include <regex>
#include <boost/algorithm/string.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <fc/variant.hpp>
Expand Down

0 comments on commit a034551

Please sign in to comment.