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 #6264 from taokayan/get_table_reverse
Browse files Browse the repository at this point in the history
support reverse iteration & show RAM payer in get table (#6216 #6271)
  • Loading branch information
heifner authored Nov 19, 2018
2 parents f6df10a + fd1c8c4 commit be05dad
Show file tree
Hide file tree
Showing 4 changed files with 427 additions and 70 deletions.
36 changes: 18 additions & 18 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1219,29 +1219,29 @@ read_only::get_table_by_scope_result read_only::get_table_by_scope( const read_o
if( upper_bound_lookup_tuple < lower_bound_lookup_tuple )
return result;

auto lower = idx.lower_bound( lower_bound_lookup_tuple );
auto upper = idx.upper_bound( upper_bound_lookup_tuple );
auto walk_table_range = [&]( auto itr, auto end_itr ) {
auto cur_time = fc::time_point::now();
auto end_time = cur_time + fc::microseconds(1000 * 10); /// 10ms max time
for( unsigned int count = 0; cur_time <= end_time && count < p.limit && itr != end_itr; ++itr, cur_time = fc::time_point::now() ) {
if( p.table && itr->table != p.table ) continue;

auto end = fc::time_point::now() + fc::microseconds(1000 * 10); /// 10ms max time
result.rows.push_back( {itr->code, itr->scope, itr->table, itr->payer, itr->count} );

unsigned int count = 0;
auto itr = lower;
for (; itr != upper; ++itr) {
if (p.table && itr->table != p.table) {
if (fc::time_point::now() > end) {
break;
}
continue;
++count;
}
result.rows.push_back({itr->code, itr->scope, itr->table, itr->payer, itr->count});
if (++count == p.limit || fc::time_point::now() > end) {
++itr;
break;
if( itr != end_itr ) {
result.more = string(itr->scope);
}
};

auto lower = idx.lower_bound( lower_bound_lookup_tuple );
auto upper = idx.upper_bound( upper_bound_lookup_tuple );
if( p.reverse && *p.reverse ) {
walk_table_range( boost::make_reverse_iterator(upper), boost::make_reverse_iterator(lower) );
} else {
walk_table_range( lower, upper );
}
if (itr != upper) {
result.more = (string)itr->scope;
}

return result;
}

Expand Down
117 changes: 67 additions & 50 deletions plugins/chain_plugin/include/eosio/chain_plugin/chain_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ class read_only {
string key_type; // type of key specified by index_position
string index_position; // 1 - primary (first), 2 - secondary index (in order defined by multi_index), 3 - third index, etc
string encode_type{"dec"}; //dec, hex , default=dec
optional<bool> reverse;
optional<bool> show_payer; // show RAM pyer
};

struct get_table_rows_result {
Expand All @@ -287,6 +289,7 @@ class read_only {
string lower_bound; // lower bound of scope, optional
string upper_bound; // upper bound of scope, optional
uint32_t limit = 10;
optional<bool> reverse;
};
struct get_table_by_scope_result_row {
name code;
Expand Down Expand Up @@ -438,33 +441,41 @@ class read_only {
if( upper_bound_lookup_tuple < lower_bound_lookup_tuple )
return result;

auto lower = secidx.lower_bound( lower_bound_lookup_tuple );
auto upper = secidx.upper_bound( upper_bound_lookup_tuple );

vector<char> data;

auto end = fc::time_point::now() + fc::microseconds(1000 * 10); /// 10ms max time

unsigned int count = 0;
auto itr = lower;
for (; itr != upper; ++itr) {

const auto* itr2 = d.find<chain::key_value_object, chain::by_scope_primary>(boost::make_tuple(t_id->id, itr->primary_key));
if (itr2 == nullptr) continue;
copy_inline_row(*itr2, data);

if (p.json) {
result.rows.emplace_back( abis.binary_to_variant( abis.get_table_type(p.table), data, abi_serializer_max_time, shorten_abi_errors ) );
} else {
result.rows.emplace_back(fc::variant(data));
auto walk_table_row_range = [&]( auto itr, auto end_itr ) {
auto cur_time = fc::time_point::now();
auto end_time = cur_time + fc::microseconds(1000 * 10); /// 10ms max time
vector<char> data;
for( unsigned int count = 0; cur_time <= end_time && count < p.limit && itr != end_itr; ++itr, cur_time = fc::time_point::now() ) {
const auto* itr2 = d.find<chain::key_value_object, chain::by_scope_primary>( boost::make_tuple(t_id->id, itr->primary_key) );
if( itr2 == nullptr ) continue;
copy_inline_row(*itr2, data);

fc::variant data_var;
if( p.json ) {
data_var = abis.binary_to_variant( abis.get_table_type(p.table), data, abi_serializer_max_time, shorten_abi_errors );
} else {
data_var = fc::variant( data );
}

if( p.show_payer && *p.show_payer ) {
result.rows.emplace_back( fc::mutable_variant_object("data", std::move(data_var))("payer", itr->payer) );
} else {
result.rows.emplace_back( std::move(data_var) );
}

++count;
}

if (++count == p.limit || fc::time_point::now() > end) {
break;
if( itr != end_itr ) {
result.more = true;
}
}
if (itr != upper) {
result.more = true;
};

auto lower = secidx.lower_bound( lower_bound_lookup_tuple );
auto upper = secidx.upper_bound( upper_bound_lookup_tuple );
if( p.reverse && *p.reverse ) {
walk_table_row_range( boost::make_reverse_iterator(upper), boost::make_reverse_iterator(lower) );
} else {
walk_table_row_range( lower, upper );
}
}
return result;
Expand Down Expand Up @@ -508,31 +519,37 @@ class read_only {
if( upper_bound_lookup_tuple < lower_bound_lookup_tuple )
return result;

auto lower = idx.lower_bound( lower_bound_lookup_tuple );
auto upper = idx.upper_bound( upper_bound_lookup_tuple );

vector<char> data;

auto end = fc::time_point::now() + fc::microseconds(1000 * 10); /// 10ms max time

unsigned int count = 0;
auto itr = lower;
for (; itr != upper; ++itr) {
copy_inline_row(*itr, data);

if (p.json) {
result.rows.emplace_back( abis.binary_to_variant( abis.get_table_type(p.table), data, abi_serializer_max_time, shorten_abi_errors ) );
} else {
result.rows.emplace_back(fc::variant(data));
auto walk_table_row_range = [&]( auto itr, auto end_itr ) {
auto cur_time = fc::time_point::now();
auto end_time = cur_time + fc::microseconds(1000 * 10); /// 10ms max time
vector<char> data;
for( unsigned int count = 0; cur_time <= end_time && count < p.limit && itr != end_itr; ++count, ++itr, cur_time = fc::time_point::now() ) {
copy_inline_row(*itr, data);

fc::variant data_var;
if( p.json ) {
data_var = abis.binary_to_variant( abis.get_table_type(p.table), data, abi_serializer_max_time, shorten_abi_errors );
} else {
data_var = fc::variant( data );
}

if( p.show_payer && *p.show_payer ) {
result.rows.emplace_back( fc::mutable_variant_object("data", std::move(data_var))("payer", itr->payer) );
} else {
result.rows.emplace_back( std::move(data_var) );
}
}

if (++count == p.limit || fc::time_point::now() > end) {
++itr;
break;
if( itr != end_itr ) {
result.more = true;
}
}
if (itr != upper) {
result.more = true;
};

auto lower = idx.lower_bound( lower_bound_lookup_tuple );
auto upper = idx.upper_bound( upper_bound_lookup_tuple );
if( p.reverse && *p.reverse ) {
walk_table_row_range( boost::make_reverse_iterator(upper), boost::make_reverse_iterator(lower) );
} else {
walk_table_row_range( lower, upper );
}
}
return result;
Expand Down Expand Up @@ -693,10 +710,10 @@ FC_REFLECT(eosio::chain_apis::read_only::get_block_header_state_params, (block_n

FC_REFLECT( eosio::chain_apis::read_write::push_transaction_results, (transaction_id)(processed) )

FC_REFLECT( eosio::chain_apis::read_only::get_table_rows_params, (json)(code)(scope)(table)(table_key)(lower_bound)(upper_bound)(limit)(key_type)(index_position)(encode_type) )
FC_REFLECT( eosio::chain_apis::read_only::get_table_rows_params, (json)(code)(scope)(table)(table_key)(lower_bound)(upper_bound)(limit)(key_type)(index_position)(encode_type)(reverse)(show_payer) )
FC_REFLECT( eosio::chain_apis::read_only::get_table_rows_result, (rows)(more) );

FC_REFLECT( eosio::chain_apis::read_only::get_table_by_scope_params, (code)(table)(lower_bound)(upper_bound)(limit) )
FC_REFLECT( eosio::chain_apis::read_only::get_table_by_scope_params, (code)(table)(lower_bound)(upper_bound)(limit)(reverse) )
FC_REFLECT( eosio::chain_apis::read_only::get_table_by_scope_result_row, (code)(scope)(table)(payer)(count));
FC_REFLECT( eosio::chain_apis::read_only::get_table_by_scope_result, (rows)(more) );

Expand Down
12 changes: 10 additions & 2 deletions programs/cleos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ struct approve_producer_subcommand {
);
auto res = result.as<eosio::chain_apis::read_only::get_table_rows_result>();
// Condition in if statement below can simply be res.rows.empty() when cleos no longer needs to support nodeos versions older than 1.5.0
// Although since this subcommand will actually change the voter's vote, it is probably better to just keep this check to protect
// Although since this subcommand will actually change the voter's vote, it is probably better to just keep this check to protect
// against future potential chain_plugin bugs.
if( res.rows.empty() || res.rows[0].get_object()["owner"].as_string() != name(voter).to_string() ) {
std::cerr << "Voter info not found for account " << voter << std::endl;
Expand Down Expand Up @@ -2133,6 +2133,8 @@ int main( int argc, char** argv ) {
bool binary = false;
uint32_t limit = 10;
string index_position;
bool reverse = false;
bool show_payer = false;
auto getTable = get->add_subcommand( "table", localized("Retrieve the contents of a database table"), false);
getTable->add_option( "account", code, localized("The account who owns the table") )->required();
getTable->add_option( "scope", scope, localized("The scope within the contract in which the table is found") )->required();
Expand All @@ -2150,7 +2152,9 @@ int main( int argc, char** argv ) {
"\t\t\t\tSpecial type 'name' indicates an account name."));
getTable->add_option( "--encode-type", encode_type,
localized("The encoding type of key_type (i64 , i128 , float64, float128) only support decimal encoding e.g. 'dec'"
"i256 - supports both 'dec' and 'hex', ripemd160 and sha256 is 'hex' only\n"));
"i256 - supports both 'dec' and 'hex', ripemd160 and sha256 is 'hex' only"));
getTable->add_flag("-r,--reverse", reverse, localized("Iterate in reverse order"));
getTable->add_flag("--show-payer", show_payer, localized("show RAM payer"));


getTable->set_callback([&] {
Expand All @@ -2165,6 +2169,8 @@ int main( int argc, char** argv ) {
("key_type",key_type)
("index_position", index_position)
("encode_type", encode_type)
("reverse", reverse)
("show_payer", show_payer)
);

std::cout << fc::json::to_pretty_string(result)
Expand All @@ -2177,12 +2183,14 @@ int main( int argc, char** argv ) {
getScope->add_option( "-l,--limit", limit, localized("The maximum number of rows to return") );
getScope->add_option( "-L,--lower", lower, localized("lower bound of scope") );
getScope->add_option( "-U,--upper", upper, localized("upper bound of scope") );
getScope->add_flag("-r,--reverse", reverse, localized("Iterate in reverse order"));
getScope->set_callback([&] {
auto result = call(get_table_by_scope_func, fc::mutable_variant_object("code",code)
("table",table)
("lower_bound",lower)
("upper_bound",upper)
("limit",limit)
("reverse", reverse)
);
std::cout << fc::json::to_pretty_string(result)
<< std::endl;
Expand Down
Loading

0 comments on commit be05dad

Please sign in to comment.