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

Make get_kv_table_rows work without --lower and --upper and --index #9962

Merged
merged 4 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2222,14 +2222,16 @@ read_only::get_table_rows_result read_only::get_kv_table_rows_context( const rea
///////////////////////////////////////////////////////////
bool has_lower_bound = p.lower_bound.has_value() && !p.lower_bound.value().empty();
bool has_upper_bound = p.upper_bound.has_value() && !p.upper_bound.value().empty();
unbounded = !has_lower_bound || !has_upper_bound;
// reverse mode has upper_bound value, non-reverse and point query has lower bound value
EOS_ASSERT((p.reverse && has_upper_bound) || (!p.reverse && has_lower_bound) || (has_lower_bound && point_query), chain::contract_table_query_exception, "Unknown/Invalid range: ${t} ${i}", ("t", p.table)("i", p.index_name));

const bool has_default_lower_bound = !has_lower_bound && !p.reverse;
const bool has_default_upper_bound = !has_upper_bound && p.reverse;

unbounded = (!has_lower_bound && !has_default_lower_bound) || (!has_upper_bound && !has_default_upper_bound);

vector<char> lb_key;
const string &lower_bound = *p.lower_bound;
vector<char> lv;

if( has_lower_bound ) {
convert_key(index_type, p.encode_type, lower_bound, lv);
lb_key.resize(prefix.size() + lv.size());
Expand All @@ -2253,7 +2255,7 @@ read_only::get_table_rows_result read_only::get_kv_table_rows_context( const rea

std::unique_ptr<kv_iterator> lb_itr;
std::unique_ptr<kv_iterator> ub_itr;

if( has_lower_bound ) {
lb_itr = kv_context.kv_it_create(p.code.to_uint64_t(), prefix.data(), prefix.size());
}
Expand All @@ -2279,6 +2281,15 @@ read_only::get_table_rows_result read_only::get_kv_table_rows_context( const rea
status_lb = lb_itr->kv_it_key(0, lb_key.data(), lb_key_size, lb_key_actual_size);
EOS_ASSERT(lb_key_size == lb_key_actual_size, chain::contract_table_query_exception, "Invalid lower bound iterator in ${t} ${i}", ("t", p.table)("i", p.index_name));
}
}else if(has_default_lower_bound) {
//Set the first entry as default lower bound
lb_itr = kv_context.kv_it_create(p.code.to_uint64_t(), prefix.data(), prefix.size());
lb_itr->kv_it_move_to_end();
status_lb = lb_itr->kv_it_next(&lb_key_size, &lb_value_size);
EOS_ASSERT(status_lb != chain::kv_it_stat::iterator_erased, chain::contract_table_query_exception, "Set default lower bound: invalid lower bound iterator in ${t} ${i}", ("t", p.table)("i", p.index_name));
lb_key.resize(lb_key_size);
status_lb = lb_itr->kv_it_key(0, lb_key.data(), lb_key_size, lb_key_actual_size);
EOS_ASSERT(lb_key_size == lb_key_actual_size, chain::contract_table_query_exception, "Set default lower bound: invalid lower bound iterator in ${t} ${i}", ("t", p.table)("i", p.index_name));
}

// Find upper bound iterator
Expand All @@ -2294,6 +2305,15 @@ read_only::get_table_rows_result read_only::get_kv_table_rows_context( const rea
ub_key.resize(ub_key_size);
status_ub = ub_itr->kv_it_key(0, ub_key.data(), ub_key_size, ub_key_actual_size);
EOS_ASSERT(ub_key_size == ub_key_actual_size, chain::contract_table_query_exception, "Invalid upper bound iterator in ${t} ${i}", ("t", p.table)("i", p.index_name));
}else if(has_default_upper_bound) {
//Set the last entry as default upper bound
ub_itr = kv_context.kv_it_create(p.code.to_uint64_t(), prefix.data(), prefix.size());
ub_itr->kv_it_move_to_end();
status_ub = ub_itr->kv_it_prev(&ub_key_size, &ub_value_size);
EOS_ASSERT(status_ub != chain::kv_it_stat::iterator_erased, chain::contract_table_query_exception, "Set default upper bound: invalid upper bound iterator in ${t} ${i}", ("t", p.table)("i", p.index_name));
ub_key.resize(ub_key_size);
status_ub = ub_itr->kv_it_key(0, ub_key.data(), ub_key_size, ub_key_actual_size);
EOS_ASSERT(ub_key_size == ub_key_actual_size, chain::contract_table_query_exception, "Set default upper bound: invalid upper bound iterator in ${t} ${i}", ("t", p.table)("i", p.index_name));
}

kv_it_stat status;
Expand Down Expand Up @@ -2398,11 +2418,11 @@ read_only::get_table_rows_result read_only::get_kv_table_rows_context( const rea
auto next_key_bytes = string(&row_key.data()[prefix_size()], row_key.size() - prefix_size());

boost::algorithm::hex(next_key_bytes.begin(), next_key_bytes.end(), std::back_inserter(result.next_key_bytes));
}
}
} // end of for
};

if( p.reverse ) {
if (p.reverse){
walk_table_row_range( ub_itr, lb_itr, true);
} else {
walk_table_row_range( lb_itr, ub_itr, false );
Expand Down
124 changes: 113 additions & 11 deletions tests/get_kv_table_nodeos_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,6 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_nodeos_test, TESTER ) try {
chk_result(8, 2);
chk_result(9, 1);

p.lower_bound = "bobc";
p.upper_bound = "";
BOOST_CHECK_THROW(plugin.read_only::get_kv_table_rows(p), chain::contract_table_query_exception);

p.reverse = false;
p.lower_bound = "";
p.upper_bound = "bobz";
BOOST_CHECK_THROW(plugin.read_only::get_kv_table_rows(p), chain::contract_table_query_exception);

p.reverse = true;
p.lower_bound = "bobj";
p.upper_bound = "bobz";
Expand Down Expand Up @@ -338,8 +329,6 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_nodeos_test, TESTER ) try {
result = plugin.read_only::get_kv_table_rows(p);
BOOST_REQUIRE_EQUAL(10u, result.rows.size());



p.index_value = "";
p.encode_type = "dec";
p.lower_bound = "0";
Expand Down Expand Up @@ -1119,6 +1108,119 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_nodeos_test, TESTER ) try {
chk_result(0, 4);
chk_result(1, 5);

// test no --lower, --upper, and --index with different --limit
p.reverse = false;
p.index_name = "foo"_n;
p.encode_type = "dec";
p.index_value = "";
p.lower_bound = "";
p.upper_bound = "";
p.limit = 9;
result = plugin.read_only::get_kv_table_rows(p);
BOOST_REQUIRE_EQUAL(9u, result.rows.size());
BOOST_REQUIRE_EQUAL(result.more, true);
BOOST_REQUIRE_EQUAL(result.next_key, std::to_string(10));
BOOST_REQUIRE_EQUAL(result.next_key_bytes, "000000000000000A");
chk_result(0, 1);
chk_result(1, 2);
chk_result(2, 3);
chk_result(3, 4);
chk_result(4, 5);
chk_result(5, 6);
chk_result(6, 7);
chk_result(7, 8);
chk_result(8, 9);

p.limit = 20;
result = plugin.read_only::get_kv_table_rows(p);
BOOST_REQUIRE_EQUAL(10u, result.rows.size());
BOOST_REQUIRE_EQUAL(result.more, false);
BOOST_REQUIRE_EQUAL(result.next_key, "");
BOOST_REQUIRE_EQUAL(result.next_key_bytes, "");
chk_result(0, 1);
chk_result(1, 2);
chk_result(2, 3);
chk_result(3, 4);
chk_result(4, 5);
chk_result(5, 6);
chk_result(6, 7);
chk_result(7, 8);
chk_result(8, 9);
chk_result(9, 10);

p.reverse = true;
p.limit = 9;
result = plugin.read_only::get_kv_table_rows(p);
BOOST_REQUIRE_EQUAL(9u, result.rows.size());
BOOST_REQUIRE_EQUAL(result.more, true);
BOOST_REQUIRE_EQUAL(result.next_key, std::to_string(1));
BOOST_REQUIRE_EQUAL(result.next_key_bytes, "0000000000000001");
chk_result(0, 10);
chk_result(1, 9);
chk_result(2, 8);
chk_result(3, 7);
chk_result(4, 6);
chk_result(5, 5);
chk_result(6, 4);
chk_result(7, 3);
chk_result(8, 2);

p.limit = 20;
result = plugin.read_only::get_kv_table_rows(p);
BOOST_REQUIRE_EQUAL(10u, result.rows.size());
BOOST_REQUIRE_EQUAL(result.more, false);
BOOST_REQUIRE_EQUAL(result.next_key, "");
BOOST_REQUIRE_EQUAL(result.next_key_bytes, "");
chk_result(0, 10);
chk_result(1, 9);
chk_result(2, 8);
chk_result(3, 7);
chk_result(4, 6);
chk_result(5, 5);
chk_result(6, 4);
chk_result(7, 3);
chk_result(8, 2);
chk_result(9, 1);

nickjjzhao marked this conversation as resolved.
Show resolved Hide resolved
// test default lower bound
p.reverse = false;
p.lower_bound = "";
p.upper_bound = "9";
result = plugin.read_only::get_kv_table_rows(p);
BOOST_REQUIRE_EQUAL(9u, result.rows.size());
BOOST_REQUIRE_EQUAL(result.more, false);
BOOST_REQUIRE_EQUAL(result.next_key, "");
BOOST_REQUIRE_EQUAL(result.next_key_bytes, "");
chk_result(0, 1);
chk_result(1, 2);
chk_result(2, 3);
chk_result(3, 4);
chk_result(4, 5);
chk_result(5, 6);
chk_result(6, 7);
chk_result(7, 8);
chk_result(8, 9);

// test default upper bound
p.reverse = true;
p.lower_bound = "2";
p.upper_bound = "";
result = plugin.read_only::get_kv_table_rows(p);
BOOST_REQUIRE_EQUAL(9u, result.rows.size());
BOOST_REQUIRE_EQUAL(result.more, false);
BOOST_REQUIRE_EQUAL(result.next_key, "");
BOOST_REQUIRE_EQUAL(result.next_key_bytes, "");
chk_result(0, 10);
chk_result(1, 9);
chk_result(2, 8);
chk_result(3, 7);
chk_result(4, 6);
chk_result(5, 5);
chk_result(6, 4);
chk_result(7, 3);
chk_result(8, 2);


}
FC_LOG_AND_RETHROW()

Expand Down