Skip to content

Commit

Permalink
#270 add functions: get_witness_by_account_id(), get_committee_member…
Browse files Browse the repository at this point in the history
…_by_account_id(), get_workers_by_account_id()
  • Loading branch information
Vlad Dobromyslov committed Feb 8, 2022
1 parent 87e214f commit 65df816
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
47 changes: 37 additions & 10 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,14 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>

// Witnesses
vector<optional<witness_object>> get_witnesses(const vector<witness_id_type> &witness_ids) const;
fc::optional<witness_object> get_witness_by_account_id(account_id_type account) const;
fc::optional<witness_object> get_witness_by_account(const std::string account_id_or_name) const;
map<string, witness_id_type> lookup_witness_accounts(const string &lower_bound_name, uint32_t limit) const;
uint64_t get_witness_count() const;

// Committee members
vector<optional<committee_member_object>> get_committee_members(const vector<committee_member_id_type> &committee_member_ids) const;
fc::optional<committee_member_object> get_committee_member_by_account_id(account_id_type account) const;
fc::optional<committee_member_object> get_committee_member_by_account(const std::string account_id_or_name) const;
map<string, committee_member_id_type> lookup_committee_member_accounts(const string &lower_bound_name, uint32_t limit) const;
uint64_t get_committee_member_count() const;
Expand All @@ -201,6 +203,7 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>

// Workers
vector<optional<worker_object>> get_workers(const vector<worker_id_type> &witness_ids) const;
vector<worker_object> get_workers_by_account_id(account_id_type account) const;
vector<worker_object> get_workers_by_account(const std::string account_id_or_name) const;
map<string, worker_id_type> lookup_worker_accounts(const string &lower_bound_name, uint32_t limit) const;
uint64_t get_worker_count() const;
Expand Down Expand Up @@ -1616,19 +1619,27 @@ vector<optional<witness_object>> database_api_impl::get_witnesses(const vector<w
return result;
}

fc::optional<witness_object> database_api::get_witness_by_account(const std::string account_id_or_name) const {
return my->get_witness_by_account(account_id_or_name);
fc::optional<witness_object> database_api::get_witness_by_account_id(account_id_type account) const {
return my->get_witness_by_account_id(account);
}

fc::optional<witness_object> database_api_impl::get_witness_by_account(const std::string account_id_or_name) const {
fc::optional<witness_object> database_api_impl::get_witness_by_account_id(account_id_type account) const {
const auto &idx = _db.get_index_type<witness_index>().indices().get<by_account>();
const account_id_type account = get_account_from_string(account_id_or_name)->id;
auto itr = idx.find(account);
if (itr != idx.end())
return *itr;
return {};
}

fc::optional<witness_object> database_api::get_witness_by_account(const std::string account_id_or_name) const {
return my->get_witness_by_account(account_id_or_name);
}

fc::optional<witness_object> database_api_impl::get_witness_by_account(const std::string account_id_or_name) const {
const account_id_type account = get_account_from_string(account_id_or_name)->id;
return get_witness_by_account_id(account);
}

map<string, witness_id_type> database_api::lookup_witness_accounts(const string &lower_bound_name, uint32_t limit) const {
return my->lookup_witness_accounts(lower_bound_name, limit);
}
Expand Down Expand Up @@ -1687,19 +1698,27 @@ vector<optional<committee_member_object>> database_api_impl::get_committee_membe
return result;
}

fc::optional<committee_member_object> database_api::get_committee_member_by_account(const std::string account_id_or_name) const {
return my->get_committee_member_by_account(account_id_or_name);
fc::optional<committee_member_object> database_api::get_committee_member_by_account_id(account_id_type account) const {
return my->get_committee_member_by_account_id(account);
}

fc::optional<committee_member_object> database_api_impl::get_committee_member_by_account(const std::string account_id_or_name) const {
fc::optional<committee_member_object> database_api_impl::get_committee_member_by_account_id(account_id_type account) const {
const auto &idx = _db.get_index_type<committee_member_index>().indices().get<by_account>();
const account_id_type account = get_account_from_string(account_id_or_name)->id;
auto itr = idx.find(account);
if (itr != idx.end())
return *itr;
return {};
}

fc::optional<committee_member_object> database_api::get_committee_member_by_account(const std::string account_id_or_name) const {
return my->get_committee_member_by_account(account_id_or_name);
}

fc::optional<committee_member_object> database_api_impl::get_committee_member_by_account(const std::string account_id_or_name) const {
const account_id_type account = get_account_from_string(account_id_or_name)->id;
return get_committee_member_by_account_id(account);
}

map<string, committee_member_id_type> database_api::lookup_committee_member_accounts(const string &lower_bound_name, uint32_t limit) const {
return my->lookup_committee_member_accounts(lower_bound_name, limit);
}
Expand Down Expand Up @@ -1943,6 +1962,10 @@ vector<optional<worker_object>> database_api::get_workers(const vector<worker_id
return my->get_workers(worker_ids);
}

vector<worker_object> database_api::get_workers_by_account_id(account_id_type account) const {
return my->get_workers_by_account_id(account);
}

vector<worker_object> database_api::get_workers_by_account(const std::string account_id_or_name) const {
return my->get_workers_by_account(account_id_or_name);
}
Expand All @@ -1967,9 +1990,8 @@ vector<optional<worker_object>> database_api_impl::get_workers(const vector<work
return result;
}

vector<worker_object> database_api_impl::get_workers_by_account(const std::string account_id_or_name) const {
vector<worker_object> database_api_impl::get_workers_by_account_id(account_id_type account) const {
const auto &idx = _db.get_index_type<worker_index>().indices().get<by_account>();
const account_id_type account = get_account_from_string(account_id_or_name)->id;
auto itr = idx.find(account);
vector<worker_object> result;

Expand All @@ -1981,6 +2003,11 @@ vector<worker_object> database_api_impl::get_workers_by_account(const std::strin
return result;
}

vector<worker_object> database_api_impl::get_workers_by_account(const std::string account_id_or_name) const {
const account_id_type account = get_account_from_string(account_id_or_name)->id;
return get_workers_by_account_id(account);
}

map<string, worker_id_type> database_api_impl::lookup_worker_accounts(const string &lower_bound_name, uint32_t limit) const {
FC_ASSERT(limit <= api_limit_lookup_worker_accounts,
"Number of querying accounts can not be greater than ${configured_limit}",
Expand Down
26 changes: 25 additions & 1 deletion libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,13 @@ class database_api {
* @param account The ID of the account whose witness should be retrieved
* @return The witness object, or null if the account does not have a witness
*/
fc::optional<witness_object> get_witness_by_account_id(account_id_type account) const;

/**
* @brief Get the witness owned by a given account
* @param account_id_or_name The ID or name of the account whose witness should be retrieved
* @return The witness object, or null if the account does not have a witness
*/
fc::optional<witness_object> get_witness_by_account(const std::string account_name_or_id) const;

/**
Expand Down Expand Up @@ -588,6 +595,13 @@ class database_api {
*/
vector<optional<committee_member_object>> get_committee_members(const vector<committee_member_id_type> &committee_member_ids) const;

/**
* @brief Get the committee_member owned by a given account
* @param account The ID of the account whose committee_member should be retrieved
* @return The committee_member object, or null if the account does not have a committee_member
*/
fc::optional<committee_member_object> get_committee_member_by_account_id(account_id_type account) const;

/**
* @brief Get the committee_member owned by a given account
* @param account_id_or_name The ID or name of the account whose committee_member should be retrieved
Expand Down Expand Up @@ -727,7 +741,14 @@ class database_api {

/**
* @brief Return the worker objects associated with this account.
* @param account_id_or_name The ID or name of the account whose worker should be retrieved
* @param account The ID of the account whose workers should be retrieved
* @return The worker object or null if the account does not have a worker
*/
vector<worker_object> get_workers_by_account_id(account_id_type account) const;

/**
* @brief Return the worker objects associated with this account.
* @param account_id_or_name The ID or name of the account whose workers should be retrieved
* @return The worker object or null if the account does not have a worker
*/
vector<worker_object> get_workers_by_account(const std::string account_id_or_name) const;
Expand Down Expand Up @@ -1104,12 +1125,14 @@ FC_API(graphene::app::database_api,

// Witnesses
(get_witnesses)
(get_witness_by_account_id)
(get_witness_by_account)
(lookup_witness_accounts)
(get_witness_count)

// Committee members
(get_committee_members)
(get_committee_member_by_account_id)
(get_committee_member_by_account)
(lookup_committee_member_accounts)
(get_committee_member_count)
Expand All @@ -1135,6 +1158,7 @@ FC_API(graphene::app::database_api,

// Workers
(get_workers)
(get_workers_by_account_id)
(get_workers_by_account)
(lookup_worker_accounts)
(get_worker_count)
Expand Down

0 comments on commit 65df816

Please sign in to comment.