Skip to content

Commit

Permalink
change underlying storage object and operations
Browse files Browse the repository at this point in the history
  • Loading branch information
oxarbitrage committed Aug 24, 2019
1 parent 7e59910 commit 1260169
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 228 deletions.
16 changes: 10 additions & 6 deletions libraries/app/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,14 +738,18 @@ namespace graphene { namespace app {
return optional<htlc_order_object>();
}

optional<account_storage_object> custom_operations_api::get_storage_info(std::string account_id_or_name)const
vector<account_storage_object> custom_operations_api::get_storage_info(std::string account_id_or_name,
std::string catalog)const
{
const auto account_id = database_api.get_account_id_from_string(account_id_or_name);
auto &index = _app.chain_database()->get_index_type<account_storage_index>().indices().get<by_custom_account>();
auto itr = index.find(account_id);
if(itr != index.end())
return *itr;
return optional<account_storage_object>();
vector<account_storage_object> results;
auto &index = _app.chain_database()->get_index_type<account_storage_index>().indices().get<by_account_catalog>();
auto itr = index.lower_bound(make_tuple(account_id, catalog));
while(itr != index.end() && itr->account == account_id && itr->catalog == catalog) {
results.push_back(*itr);
++itr;
}
return results;
}

} } // graphene::app
7 changes: 4 additions & 3 deletions libraries/app/include/graphene/app/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,13 +572,14 @@ namespace graphene { namespace app {
optional<htlc_order_object> get_htlc_offer(htlc_order_id_type id)const;

/**
* @breif Get storage information of an account
* @breif Get all stored objects of an account
*
* @param account Account name to get info from
* @param catalog Category classification
*
* @return The storage information of the account or empty
* @return The vector of objects of the account or empty
*/
optional<account_storage_object> get_storage_info(std::string account)const;
vector<account_storage_object> get_storage_info(std::string account, std::string catalog)const;

private:
application& _app;
Expand Down
102 changes: 47 additions & 55 deletions libraries/plugins/custom_operations/custom_evaluators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,73 +113,65 @@ object_id_type custom_generic_evaluator::do_apply(const take_htlc_order_operatio
return htlc_order_id;
}

void fill_storage_map(account_storage_object& aso, account_id_type account, const account_store_data& op)
object_id_type custom_generic_evaluator::do_apply(const account_storage_map& op)
{
aso.account = account;
if(op.extensions.value.pairs.valid())
{
for(auto const& row: *op.extensions.value.pairs) {
if (op.extensions.value.remove.valid() && *op.extensions.value.remove)
aso.storage_map.erase(row.first);
else
aso.storage_map[row.first] = row.second;
}
}
}
auto &index = _db->get_index_type<account_storage_index>().indices().get<by_account_catalog_key>();

object_id_type custom_generic_evaluator::do_apply(const account_store_data& op)
{
auto &index = _db->get_index_type<account_storage_index>().indices().get<by_custom_account>();

auto itr = index.find(_account);
if( itr != index.end() )
if (op.extensions.value.remove.valid() && *op.extensions.value.remove)
{
_db->modify( *itr, [&op, this]( account_storage_object& aso ) {
fill_storage_map(aso, _account, op);
});
return itr->id;
}
else
{
auto created = _db->create<account_storage_object>( [&op, this]( account_storage_object& aso ) {
fill_storage_map(aso, _account, op);
});
return created.id;
for(auto const& row: *op.extensions.value.key_values) {
auto itr = index.find(make_tuple(_account, *op.extensions.value.catalog, row.first));
if(itr != index.end())
_db->remove(*itr);
}
}
}

void fill_account_list(account_storage_object& aso, account_id_type account, const account_list_data& op)
{
aso.account = account;
if(op.extensions.value.accounts.valid())
{
for(auto const& account: *op.extensions.value.accounts) {
if (op.extensions.value.remove.valid() && *op.extensions.value.remove)
aso.account_list.erase(account);
else {
for(auto const& row: *op.extensions.value.key_values) {
auto itr = index.find(make_tuple(_account, *op.extensions.value.catalog, row.first));
if(itr == index.end())
{
auto created = _db->create<account_storage_object>( [&op, this, &row]( account_storage_object& aso ) {
aso.catalog = *op.extensions.value.catalog;
aso.account = _account;
aso.key = row.first;
aso.value = row.second;
});
}
else
aso.account_list.insert(account);
{
_db->modify(*itr, [&op, this, &row](account_storage_object &aso) {
aso.value = row.second;
});
}
}
}
}

object_id_type custom_generic_evaluator::do_apply(const account_list_data& op)
object_id_type custom_generic_evaluator::do_apply(const account_storage_list& op)
{
auto &index = _db->get_index_type<account_storage_index>().indices().get<by_custom_account>();
auto &index = _db->get_index_type<account_storage_index>().indices().get<by_account_catalog_value>();

auto itr = index.find(_account);
if( itr != index.end() )
if (op.extensions.value.remove.valid() && *op.extensions.value.remove)
{
_db->modify( *itr, [&op, this]( account_storage_object& aso ) {
fill_account_list(aso, _account, op);
});
return itr->id;
for(auto const& list_value: *op.extensions.value.values) {

auto itr = index.find(make_tuple(_account, *op.extensions.value.catalog, list_value));
if(itr != index.end())
_db->remove(*itr);
}
}
else
{
auto created = _db->create<account_storage_object>( [&op, this]( account_storage_object& aso ) {
fill_account_list(aso, _account, op);
});
return created.id;
else {

for(auto const& list_value: *op.extensions.value.values) {
auto itr = index.find(make_tuple(_account, *op.extensions.value.catalog, list_value));
if(itr == index.end())
{
auto created = _db->create<account_storage_object>( [&op, this, &list_value]( account_storage_object& aso ) {
aso.catalog = *op.extensions.value.catalog;
aso.account = _account;
aso.value = list_value;
});
}
}
}
}

Expand Down
18 changes: 10 additions & 8 deletions libraries/plugins/custom_operations/custom_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,23 @@ void take_htlc_order_operation::validate()const
FC_ASSERT(extensions.value.htlc_order_id.valid());
}

void account_store_data::validate()const
void account_storage_map::validate()const
{
FC_ASSERT(extensions.value.pairs.valid());
FC_ASSERT(extensions.value.pairs->size() <= 10);
FC_ASSERT(extensions.value.catalog.valid());
FC_ASSERT(extensions.value.key_values.valid());
FC_ASSERT(extensions.value.key_values->size() <= 10);
}
void account_list_data::validate()const
void account_storage_list::validate()const
{
FC_ASSERT(extensions.value.accounts.valid());
FC_ASSERT(extensions.value.accounts->size() <= 10);
FC_ASSERT(extensions.value.catalog.valid());
FC_ASSERT(extensions.value.values.valid());
FC_ASSERT(extensions.value.values->size() <= 10);
}

} } //graphene::custom_operations

GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_contact_operation )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::custom_operations::create_htlc_order_operation )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::custom_operations::take_htlc_order_operation )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_store_data )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_list_data )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_storage_map )
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_storage_list )
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class custom_generic_evaluator
object_id_type do_apply(const account_contact_operation& o);
object_id_type do_apply(const create_htlc_order_operation& o);
object_id_type do_apply(const take_htlc_order_operation& o);
object_id_type do_apply(const account_store_data& o);
object_id_type do_apply(const account_list_data& o);
object_id_type do_apply(const account_storage_map& o);
object_id_type do_apply(const account_storage_list& o);
};

} }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ struct account_storage_object : public abstract_object<account_storage_object>
static const uint8_t type_id = account_store;

account_id_type account;
flat_map<string, string> storage_map;
flat_set<account_id_type> account_list;
string catalog;
optional<string> key;
string value;
};

struct by_custom_id;
Expand Down Expand Up @@ -132,12 +133,45 @@ typedef multi_index_container<

typedef generic_index<htlc_order_object, htlc_orderbook_multi_index_type> htlc_orderbook_index;

struct by_account_catalog;
struct by_account_catalog_key;
struct by_account_catalog_value;
struct by_account_catalog_key_value;

typedef multi_index_container<
account_storage_object,
indexed_by<
ordered_non_unique< tag<by_custom_id>, member< object, object_id_type, &object::id > >,
ordered_unique< tag<by_custom_account>,
member< account_storage_object, account_id_type, &account_storage_object::account > >
ordered_non_unique< tag<by_custom_account>,
member< account_storage_object, account_id_type, &account_storage_object::account > >,
ordered_non_unique< tag<by_account_catalog>,
composite_key< account_storage_object,
member< account_storage_object, account_id_type, &account_storage_object::account >,
member< account_storage_object, string, &account_storage_object::catalog >
>
>,
ordered_non_unique< tag<by_account_catalog_key>,
composite_key< account_storage_object,
member< account_storage_object, account_id_type, &account_storage_object::account >,
member< account_storage_object, string, &account_storage_object::catalog >,
member< account_storage_object, optional<string>, &account_storage_object::key >
>
>,
ordered_unique< tag<by_account_catalog_value>,
composite_key< account_storage_object,
member< account_storage_object, account_id_type, &account_storage_object::account >,
member< account_storage_object, string, &account_storage_object::catalog >,
member< account_storage_object, string, &account_storage_object::value >
>
>,
ordered_unique< tag<by_account_catalog_key_value>,
composite_key< account_storage_object,
member< account_storage_object, account_id_type, &account_storage_object::account >,
member< account_storage_object, string, &account_storage_object::catalog >,
member< account_storage_object, optional<string>, &account_storage_object::key >,
member< account_storage_object, string, &account_storage_object::value >
>
>
>
> account_storage_multi_index_type;

Expand All @@ -157,7 +191,7 @@ FC_REFLECT_DERIVED( graphene::custom_operations::htlc_order_object, (graphene::d
(blockchain_asset_precision)(token_contract)(tag)(taker_bitshares_account)
(taker_blockchain_account)(close_time))
FC_REFLECT_DERIVED( graphene::custom_operations::account_storage_object, (graphene::db::object),
(account)(storage_map)(account_list))
(account)(catalog)(key)(value))
FC_REFLECT_ENUM( graphene::custom_operations::types, (account_contact)(create_htlc)(take_htlc)(account_store)
(account_list))
FC_REFLECT_ENUM( graphene::custom_operations::blockchains, (eos)(bitcoin)(ripple)(ethereum) )
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,27 @@ struct take_htlc_order_operation : chain::base_operation
void validate()const;
};

struct account_store_data : chain::base_operation
struct account_storage_map : chain::base_operation
{
struct ext
{
optional<bool> remove;
optional<flat_map<string, string>> pairs;
optional<string> catalog;
optional<flat_map<string, string>> key_values;
};

graphene::protocol::extension<ext> extensions;

void validate()const;
};

struct account_list_data : chain::base_operation
struct account_storage_list : chain::base_operation
{
struct ext
{
optional<bool> remove;
optional<flat_set<account_id_type>> accounts;
optional<string> catalog;
optional<flat_set<string>> values;
};

graphene::protocol::extension<ext> extensions;
Expand All @@ -125,17 +127,16 @@ FC_REFLECT( graphene::custom_operations::take_htlc_order_operation::ext, (htlc_o
FC_REFLECT_TYPENAME( graphene::protocol::extension<graphene::custom_operations::take_htlc_order_operation::ext> )
FC_REFLECT( graphene::custom_operations::take_htlc_order_operation, (extensions) )

FC_REFLECT( graphene::custom_operations::account_store_data::ext, (pairs)(remove) )
FC_REFLECT_TYPENAME( graphene::protocol::extension<graphene::custom_operations::account_store_data::ext> )
FC_REFLECT( graphene::custom_operations::account_store_data, (extensions) )
FC_REFLECT( graphene::custom_operations::account_storage_map::ext, (remove)(catalog)(key_values) )
FC_REFLECT_TYPENAME( graphene::protocol::extension<graphene::custom_operations::account_storage_map::ext> )
FC_REFLECT( graphene::custom_operations::account_storage_map, (extensions) )

FC_REFLECT( graphene::custom_operations::account_list_data::ext, (accounts)(remove) )
FC_REFLECT_TYPENAME( graphene::protocol::extension<graphene::custom_operations::account_list_data::ext> )
FC_REFLECT( graphene::custom_operations::account_list_data, (extensions) )
FC_REFLECT( graphene::custom_operations::account_storage_list::ext, (catalog)(values)(remove) )
FC_REFLECT_TYPENAME( graphene::protocol::extension<graphene::custom_operations::account_storage_list::ext> )
FC_REFLECT( graphene::custom_operations::account_storage_list, (extensions) )

GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_contact_operation )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::custom_operations::create_htlc_order_operation )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::custom_operations::take_htlc_order_operation )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_store_data )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_list_data )

GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_storage_map )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::custom_operations::account_storage_list )
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ typedef fc::static_variant<
account_contact_operation,
create_htlc_order_operation,
take_htlc_order_operation,
account_store_data,
account_list_data
account_storage_map,
account_storage_list
> custom_plugin_operation;

struct custom_operation_wrapper {
Expand Down
Loading

0 comments on commit 1260169

Please sign in to comment.