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 #10019 from EOSIO/huangminghuang/fix-kv-table-query
Browse files Browse the repository at this point in the history
revert changes to empty string as present for lower_bound, upper_bound, or index_value
  • Loading branch information
huangminghuang authored Feb 11, 2021
2 parents d05cd1a + f641b09 commit b1e1ffa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 38 deletions.
14 changes: 6 additions & 8 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2275,25 +2275,23 @@ struct kv_table_rows_context {
abis.set_abi(abi, yield_function);
}

bool point_query() const { return p.index_value.has_value(); }
bool point_query() const { return p.index_value.size(); }

void write_prefix(fixed_buf_stream& strm) const {
strm.write('\1');
to_key(p.table.to_uint64_t(), strm);
to_key(p.index_name.to_uint64_t(), strm);
}

std::vector<char> get_full_key(const std::optional<string>& maybe_key) const {
std::vector<char> get_full_key(string key) const {
// the max possible encoded_key_byte_count occurs when the encoded type is string and when all characters
// in the string is '\0'
const size_t max_encoded_key_byte_count =
maybe_key.has_value() ? std::max(sizeof(uint64_t), 2 * maybe_key->size() + 1) : 0;
const size_t max_encoded_key_byte_count = std::max(sizeof(uint64_t), 2 * key.size() + 1);
std::vector<char> full_key(prefix_size + max_encoded_key_byte_count);
fixed_buf_stream strm(full_key.data(), full_key.size());
write_prefix(strm);
if (maybe_key.has_value()) {
key_helper::write_key(index_type, p.encode_type, *maybe_key, strm);
}
if (key.size())
key_helper::write_key(index_type, p.encode_type, key, strm);
full_key.resize(strm.pos - full_key.data());
return full_key;
}
Expand Down Expand Up @@ -2455,7 +2453,7 @@ read_only::get_table_rows_result read_only::get_kv_table_rows(const read_only::g
kv_table_rows_context context{db, p, abi_serializer_max_time, shorten_abi_errors};

if (context.point_query()) {
EOS_ASSERT(!p.lower_bound && !p.upper_bound, chain::contract_table_query_exception,
EOS_ASSERT(p.lower_bound.empty() && p.upper_bound.empty(), chain::contract_table_query_exception,
"specify both index_value and ranges (i.e. lower_bound/upper_bound) is not allowed");
read_only::get_table_rows_result result;
auto full_key = context.get_full_key(p.index_value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,10 @@ class read_only {
name code; // name of contract
name table; // name of kv table,
name index_name; // name of index index
string encode_type = "bytes"; // "bytes" : binary values in index_value/lower_bound/upper_bound
std::optional<string> index_value; // index value for point query. If this is set, it is processed as a point query
std::optional<string> lower_bound; // lower bound value of index of index_name. If index_value is not set and lower_bound is not set, return from the beginning of range in the prefix
std::optional<string> upper_bound; // upper bound value of index of index_name, If index_value is not set and upper_bound is not set, It is set to the beginning of the next prefix range.
string encode_type; // encoded type for values in index_value/lower_bound/upper_bound
string index_value; // index value for point query. If this is set, it is processed as a point query
string lower_bound; // lower bound value of index of index_name. If index_value is not set and lower_bound is not set, return from the beginning of range in the prefix
string upper_bound; // upper bound value of index of index_name, If index_value is not set and upper_bound is not set, It is set to the beginning of the next prefix range.
uint32_t limit = 10; // max number of rows
bool reverse = false; // if true output rows in reverse order
bool show_payer = false;
Expand Down
30 changes: 15 additions & 15 deletions tests/get_kv_table_addr_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,19 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try {
p.index_name = "accname"_n;
p.index_value = "john";
p.encode_type = "name";
p.lower_bound = {};
p.upper_bound = {};
p.lower_bound = "";
p.upper_bound = "";
p.json = true;
p.reverse = false;
result = plugin.read_only::get_kv_table_rows(p);
BOOST_REQUIRE_EQUAL(1u, result.rows.size());
chk_result(0, 2);

p.index_name = "accname"_n;
p.index_value = {};
p.index_value = "";
p.encode_type = "name";
p.lower_bound = "aaa";
p.upper_bound = {};
p.upper_bound = "";
p.reverse = false;
result = plugin.read_only::get_kv_table_rows(p);
BOOST_REQUIRE_EQUAL(4u, result.rows.size());
Expand All @@ -131,10 +131,10 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try {
chk_result(3, 4);

p.index_name = "accname"_n;
p.index_value = {};
p.index_value = "";
p.encode_type = "name";
p.lower_bound = "john";
p.upper_bound = {};
p.upper_bound = "";
p.reverse = false;
result = plugin.read_only::get_kv_table_rows(p);
BOOST_REQUIRE_EQUAL(3u, result.rows.size());
Expand All @@ -143,7 +143,7 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try {
chk_result(2, 4);

p.index_name = "accname"_n;
p.index_value = {};
p.index_value = "";
p.encode_type = "name";
p.lower_bound = "john";
p.upper_bound = "lois";
Expand All @@ -153,9 +153,9 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try {
chk_result(0, 2);

p.index_name = "accname"_n;
p.index_value = {};
p.index_value = "";
p.encode_type = "name";
p.lower_bound = {};
p.lower_bound = "";
p.upper_bound = "steve";
p.reverse = true;
result = plugin.read_only::get_kv_table_rows(p);
Expand All @@ -166,7 +166,7 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try {
chk_result(3, 1);

p.index_name = "accname"_n;
p.index_value = {};
p.index_value = "";
p.encode_type = "name";
p.lower_bound = "john";
p.upper_bound = "steve";
Expand All @@ -177,16 +177,16 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try {
chk_result(1, 3);

p.index_name = "accname"_n;
p.index_value = {};
p.index_value = "";
p.encode_type = "name";
p.lower_bound = {};
p.lower_bound = "";
p.upper_bound = "aaaa";
p.reverse = true;
result = plugin.read_only::get_kv_table_rows(p);
BOOST_REQUIRE_EQUAL(0u, result.rows.size());

p.index_name = "accname"_n;
p.index_value = {};
p.index_value = "";
p.encode_type = "name";
p.lower_bound = "steve";
p.upper_bound = "john";
Expand All @@ -195,9 +195,9 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_addr_test, TESTER ) try {
BOOST_REQUIRE_EQUAL(0u, result.rows.size());

p.index_name = "accname"_n;
p.index_value = {};
p.index_value = "";
p.encode_type = "name";
p.lower_bound = {};
p.lower_bound = "";
p.upper_bound = "john";
p.reverse = true;
result = plugin.read_only::get_kv_table_rows(p);
Expand Down
11 changes: 0 additions & 11 deletions tests/get_kv_table_nodeos_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_nodeos_test, TESTER ) try {
p.upper_bound = "bobe";
BOOST_CHECK_THROW(plugin.read_only::get_kv_table_rows(p), contract_table_query_exception);

p.index_value = "";
p.lower_bound = "bobb";
p.upper_bound = "bobe";
BOOST_CHECK_THROW(plugin.read_only::get_kv_table_rows(p), contract_table_query_exception);

p.index_value = {};
p.lower_bound = "bobe";
p.upper_bound = "bobb";
Expand All @@ -169,12 +164,6 @@ BOOST_FIXTURE_TEST_CASE( get_kv_table_nodeos_test, TESTER ) try {
chk_result(8, 9);
chk_result(9, 10);

p.show_payer = false;
p.lower_bound = "aaaa";
p.upper_bound = "";
result = plugin.read_only::get_kv_table_rows(p);
BOOST_REQUIRE_EQUAL(0u, result.rows.size());

p.lower_bound = "boba";
p.upper_bound = {};
result = plugin.read_only::get_kv_table_rows(p);
Expand Down

0 comments on commit b1e1ffa

Please sign in to comment.