Skip to content

Commit

Permalink
account_notes - posting authority, batch get_values
Browse files Browse the repository at this point in the history
  • Loading branch information
isteric-senior committed Dec 14, 2020
1 parent b65e6ab commit 181b90d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
46 changes: 33 additions & 13 deletions plugins/account_notes/account_notes_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class account_notes_plugin::account_notes_plugin_impl final {

~account_notes_plugin_impl() = default;

string get_value(
account_name_type account, string key
key_values get_values(
account_name_type account, std::set<std::string> keys
) const;

account_notes_plugin& plugin_;
Expand All @@ -45,16 +45,36 @@ class account_notes_plugin::account_notes_plugin_impl final {
std::shared_ptr<generic_custom_operation_interpreter<account_notes_plugin_operation>> custom_operation_interpreter_;
};

string account_notes_plugin::account_notes_plugin_impl::get_value(
key_values account_notes_plugin::account_notes_plugin_impl::get_values(
account_name_type account,
string key
std::set<std::string> keys
) const {
string result;

const auto& notes_idx = _db.get_index<account_note_index, by_account_key>();
auto notes_itr = notes_idx.find(std::make_tuple(account, key));
if (notes_itr != notes_idx.end()) {
result = to_string(notes_itr->value);
key_values result;

auto has_keys = !!keys.size();
auto key_itr = keys.begin();

const auto& idx = _db.get_index<account_note_index, by_account_key>();
auto itr = idx.lower_bound(account);
for (; itr != idx.end() && itr->account == account; ) {
if (has_keys) {
if (key_itr == keys.end() || result.size() == keys.size()) break;
}

const auto key = to_string(itr->key);

if (has_keys && key != *key_itr) {
if (key < *key_itr) {
++itr;
} else {
key_itr = keys.erase(key_itr);
}
continue;
}

result[key] = to_string(itr->value);
if (has_keys) ++key_itr;
++itr;
}

return result;
Expand Down Expand Up @@ -136,13 +156,13 @@ void account_notes_plugin::plugin_shutdown() {

// Api Defines

DEFINE_API(account_notes_plugin, get_value) {
DEFINE_API(account_notes_plugin, get_values) {
PLUGIN_API_VALIDATE_ARGS(
(account_name_type, account)
(string, key)
(std::set<std::string>, keys, std::set<std::string>())
)
return my->_db.with_weak_read_lock([&]() {
return my->get_value(account, key);
return my->get_values(account, keys);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,24 @@ struct set_value_operation : base_operation {

void validate() const;

bool is_posting() const {
std::string pst = "pst.";
std::string g_pst = "g.pst.";
if (key.size() >= pst.size()) {
if (key.substr(0, pst.size()) == pst) return true;
} else {
return false;
}
if (key.substr(0, g_pst.size()) == g_pst) return true;
return false;
}

void get_required_active_authorities(flat_set<account_name_type>& a) const {
a.insert(account);
if (!is_posting()) a.insert(account);
}

void get_required_posting_authorities(flat_set<account_name_type>& a) const {
if (is_posting()) a.insert(account);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ namespace golos { namespace plugins { namespace account_notes {

using namespace golos::chain;

DEFINE_API_ARGS(get_value, json_rpc::msg_pack, std::string)
using key_values = std::map<std::string, std::string>;

DEFINE_API_ARGS(get_values, json_rpc::msg_pack, key_values)
DEFINE_API_ARGS(get_values_settings, json_rpc::msg_pack, account_notes_settings_api_object)

/**
Expand Down Expand Up @@ -38,7 +40,7 @@ namespace golos { namespace plugins { namespace account_notes {
static const std::string& name();

DECLARE_API(
(get_value)
(get_values)
(get_values_settings)
)

Expand Down

0 comments on commit 181b90d

Please sign in to comment.