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 #10039 from EOSIO/bdj__iter-store-cache-value_EPE-…
Browse files Browse the repository at this point in the history
…539__stripped

Replacing db intrinsic uses of RocksDB lower_bound when direct read could be used
  • Loading branch information
brianjohnson5972 authored Feb 16, 2021
2 parents d7d944c + d3ff6b7 commit 8e9a063
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
36 changes: 29 additions & 7 deletions libraries/chain/backing_store/db_context_rocksdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ namespace eosio { namespace chain { namespace backing_store {

EOS_ASSERT( !old_key_value.value, db_rocksdb_invalid_operation_exception, "db_store_i64 called with pre-existing key");

primary_lookup.add_table_if_needed(old_key_value.full_key, payer);
primary_lookup.add_table_if_needed(scope_name, table_name, payer);

const payer_payload pp{payer, value, value_size};
set_value(old_key_value.full_key, pp);
Expand Down Expand Up @@ -369,7 +369,32 @@ namespace eosio { namespace chain { namespace backing_store {
}

int32_t db_context_rocksdb::db_find_i64(uint64_t code, uint64_t scope, uint64_t table, uint64_t id) {
return find_i64(name{code}, name{scope}, name{table}, id, comp::equals);
const name code_name {code};
const name scope_name {scope};
const name table_name {table};
const unique_table t { code_name, scope_name, table_name };
const auto table_ei = primary_iter_store.cache_table(t);

key_bundle primary_key { db_key_value_format::create_primary_key(scope_name, table_name, id), code_name };
auto value = current_session.read(primary_key.full_key);
if (!value) {
// check if there is a table key to determine if we can return an end iterator
key_bundle table_key {db_key_value_format::create_table_key(scope_name, table_name), code_name };
auto table_value = current_session.read(table_key.full_key);
if (table_value) {
return table_ei;
}
else {
// since no table entry found the table is empty
return primary_iter_store.invalid_iterator();
}
}

payer_payload pp(*value);
const account_name found_payer = pp.payer;

const auto store_itr = primary_iter_store.add(primary_key_iter(table_ei, id, found_payer));
return store_itr;
}

int32_t db_context_rocksdb::db_lowerbound_i64(uint64_t code, uint64_t scope, uint64_t table, uint64_t id) {
Expand Down Expand Up @@ -698,7 +723,7 @@ namespace eosio { namespace chain { namespace backing_store {

int32_t db_context_rocksdb::find_i64(name code, name scope, name table, uint64_t id, comp comparison) {
// expanding the "in-play" iterator space to include every key type for that table, to ensure we know if
// the key is not found, that there is anything in the table at all (and thus can return an end iterator
// the key is not found, that there is anything in the table at all (and thus can return an end iterator)
// or if an invalid iterator needs to be returned
prefix_bundle primary_and_prefix_keys { db_key_value_format::create_primary_key(scope, table, id),
end_of_prefix::pre_type, code };
Expand All @@ -720,10 +745,7 @@ namespace eosio { namespace chain { namespace backing_store {

std::optional<uint64_t> primary_key;
if (!primary_lookup.match(primary_and_prefix_keys.full_key, (*session_iter).first)) {
if (comparison == comp::equals) {
return table_ei;
}
else if (!primary_lookup.match_prefix(desired_type_prefix, (*session_iter).first)) {
if (!primary_lookup.match_prefix(desired_type_prefix, (*session_iter).first)) {
return table_ei;
}
}
Expand Down
15 changes: 5 additions & 10 deletions libraries/chain/backing_store/db_key_value_any_lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ namespace eosio { namespace chain { namespace backing_store {

const eosio::session::shared_bytes db_key_value_any_lookup::useless_value = make_useless_value();

void db_key_value_any_lookup::add_table_if_needed(const shared_bytes& key, account_name payer) {
auto table_key = db_key_value_format::create_full_key_prefix(key, end_of_prefix::pre_type);
auto session_iter = current_session.lower_bound(table_key);
if (!match_prefix(table_key, session_iter)) {
// need to add the key_type::table to the end
table_key = db_key_value_format::create_table_key(table_key);
const auto legacy_key = db_key_value_format::extract_legacy_key(table_key);
const auto extracted_data = db_key_value_format::get_prefix_thru_key_type(legacy_key);
void db_key_value_any_lookup::add_table_if_needed(name scope, name table, account_name payer) {
auto legacy_table_key = db_key_value_format::create_table_key(scope, table);
auto table_key = db_key_value_format::create_full_key(legacy_table_key, parent.receiver);
const auto key_value = current_session.read(table_key);
if (!key_value) {
std::string event_id;
apply_context& context = parent.context;
const auto& scope = std::get<0>(extracted_data);
const auto& table = std::get<1>(extracted_data);
auto dm_logger = context.control.get_deep_mind_logger();
if (dm_logger != nullptr) {
event_id = db_context::table_event(parent.receiver, scope, table);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace eosio { namespace chain { namespace backing_store {

static key_bundle get_slice(name code, name scope, name table);
static key_bundle get_table_end_slice(name code, name scope, name table);
void add_table_if_needed(const shared_bytes& key, account_name payer);
void add_table_if_needed(name scope, name table, account_name payer);
void remove_table_if_empty(const shared_bytes& key);
template<typename IterStore>
int32_t get_end_iter(name code, name scope, name table, IterStore& iter_store) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace eosio { namespace chain { namespace backing_store {

const sec_pair_bundle secondary_key = get_secondary_slices_in_secondaries(parent.receiver, scope, table, secondary, id);

add_table_if_needed(secondary_key.full_secondary_key, payer);
add_table_if_needed(scope, table, payer);

auto old_value = current_session.read(secondary_key.full_secondary_key);

Expand Down

0 comments on commit 8e9a063

Please sign in to comment.