您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何使用EOS多索引表,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
為了深入和清晰的了解多索引表,最終的.cpp文件部分將被進(jìn)一步詳細(xì)討論和討論。注意,完整的.cpp文件可以在頁(yè)面的底部找到。
code
:是指已公布智能合約的account_name
。
scope
:account_name
所涉及數(shù)據(jù)范圍。
table_name
: 存儲(chǔ)在內(nèi)存中的表的名稱。
要在多索引表中存儲(chǔ)的數(shù)據(jù)是limit_order
結(jié)構(gòu)。primary_key()
,get_expiration()
,get_price()
函數(shù)用于返回表。返回的表將根據(jù)調(diào)用的函數(shù)排序。
struct limit_order { uint64_t id; uint128_t price; uint64_t expiration; account_name owner; auto primary_key() const { return id; } uint64_t get_expiration() const { return expiration; } uint128_t get_price() const { return price; } EOSLIB_SERIALIZE( limit_order, ( id )( price )( expiration )( owner ) ) };
auto payer = ilm.get_account(); ...
payer
是保存帳戶的變量,它將賬單元素添加到多索引表中,并修改已經(jīng)在多索引表中的元素。
... eosio::multi_index< N( orders ), limit_order, ...
N(orders)
是多索引表的名稱,limit_order
是要存儲(chǔ)在表中的數(shù)據(jù)。
... indexed_by< N( byexp ), const_mem_fun< limit_order, uint64_t, &limit_order::get_expiration> >, ...
indexed_by< N( byexp ), const_mem_fun< limit_order, uint64_t, &limit_order::get_expiration> >
定義了多索引表的索引方式。N(byexp)
是這個(gè)索引的名稱。const_mem_fun
表示正在查詢的數(shù)據(jù)類型、limit_order
的變量的類型是uint64_t
,將使用get_expiration
函數(shù)獲取變量。
... indexed_by< N( byprice ), const_mem_fun< limit_order, uint128_t, &limit_order::get_price> > ...
indexed_by< N( byprice ), const_mem_fun< limit_order, uint128_t, &limit_order::get_price> >
定義了多索引表的索引方式。N(byprice)
是這個(gè)索引的名稱。const_mem_fun
表示正在查詢的數(shù)據(jù)類型、limit_order
的變量的類型是uint128_t
,將使用get_price
函數(shù)獲取變量。
orders( N( limitorders ), N( limitorders )
orders
即是多索引表。
auto payer = ilm.get_account(); print("Creating multi index table 'orders'.\n"); eosio::multi_index< N( orders ), limit_order, indexed_by< N( byexp ), const_mem_fun< limit_order, uint64_t, &limit_order::get_expiration> >, indexed_by< N( byprice ), const_mem_fun< limit_order, uint128_t, &limit_order::get_price> > > orders( N( limitorders ), N( limitorders ) );
下面,將兩個(gè)limit_order
添加到orders
表中。請(qǐng)注意,payer
是正在修改orders
表的“賬單”帳戶。
orders.emplace( payer, [&]( auto& o ) { o.id = 1; o.expiration = 300; o.owner = N(dan); }); auto order2 = orders.emplace( payer, [&]( auto& o ) { o.id = 2; o.expiration = 200; o.owner = N(thomas); });
默認(rèn)的orders
表按照主鍵排序。
print("Items sorted by primary key:\n"); for( const auto& item : orders ) { print(" ID=", item.id, ", expiration=", item.expiration, ", owner=", name{item.owner}, "\n"); }
orders
表通過(guò)expiration
進(jìn)行排序并分配給expidx
。
auto expidx = orders.get_index<N(byexp)>(); print("Items sorted by expiration:\n"); for( const auto& item : expidx ) { print(" ID=", item.id, ", expiration=", item.expiration, ", owner=", name{item.owner}, "\n"); }
orders
表通過(guò)price
進(jìn)行排序并分配給oridx
。
auto pridx = orders.get_index<N(byprice)>(); print("Items sorted by price:\n"); for( const auto& item : pridx ) { print(" ID=", item.id, ", expiration=", item.expiration, ", owner=", name{item.owner}, "\n"); }
下面,“ID=2”的條目被修改。請(qǐng)注意,payer
是正在修改orders
表的“賬單”帳戶。
print("Modifying expiration of order with ID=2 to 400.\n"); orders.modify( order2, payer, [&]( auto& o ) { o.expiration = 400; });
auto lower = expidx.lower_bound(100); print("First order with an expiration of at least 100 has ID=", lower->id, " and expiration=", lower->get_expiration(), "\n");
#include <eosiolib/eosio.hpp> #include <eosiolib/dispatcher.hpp> #include <eosiolib/multi_index.hpp> using namespace eosio; namespace limit_order_table { struct limit_order { uint64_t id; uint128_t price; uint64_t expiration; account_name owner; auto primary_key() const { return id; } uint64_t get_expiration() const { return expiration; } uint128_t get_price() const { return price; } EOSLIB_SERIALIZE( limit_order, ( id )( price )( expiration )( owner ) ) }; class limit_order_table { public: ACTION( N( limitorders ), issue_limit_order ) { EOSLIB_SERIALIZE( issue_limit_order ) }; static void on( const issue_limit_order& ilm ) { auto payer = ilm.get_account(); print("Creating multi index table 'orders'.\n"); eosio::multi_index< N( orders ), limit_order, indexed_by< N( byexp ), const_mem_fun< limit_order, uint64_t, &limit_order::get_expiration> >, indexed_by< N( byprice ), const_mem_fun< limit_order, uint128_t, &limit_order::get_price> > > orders( N( limitorders ), N( limitorders ) ); orders.emplace( payer, [&]( auto& o ) { o.id = 1; o.expiration = 300; o.owner = N(dan); }); auto order2 = orders.emplace( payer, [&]( auto& o ) { o.id = 2; o.expiration = 200; o.owner = N(thomas); }); print("Items sorted by primary key:\n"); for( const auto& item : orders ) { print(" ID=", item.id, ", expiration=", item.expiration, ", owner=", name{item.owner}, "\n"); } auto expidx = orders.get_index<N(byexp)>(); print("Items sorted by expiration:\n"); for( const auto& item : expidx ) { print(" ID=", item.id, ", expiration=", item.expiration, ", owner=", name{item.owner}, "\n"); } auto pridx = orders.get_index<N(byprice)>(); print("Items sorted by price:\n"); for( const auto& item : pridx ) { print(" ID=", item.id, ", expiration=", item.expiration, ", owner=", name{item.owner}, "\n"); } print("Modifying expiration of order with ID=2 to 400.\n"); orders.modify( order2, payer, [&]( auto& o ) { o.expiration = 400; }); auto lower = expidx.lower_bound(100); print("First order with an expiration of at least 100 has ID=", lower->id, " and expiration=", lower->get_expiration(), "\n"); }; } /// limit_order_table namespace limit_order_table { extern "C" { /// The apply method implements the dispatch of events to this contract void apply( uint64_t code, uint64_t action ) { require_auth( code ); eosio_assert( eosio::dispatch< limit_order_table, limit_order_table::issue_limit_order >( code, action ), "Could not dispatch" ); } } }
表不能直接刪除,但是,在刪除所有行之后,表將自動(dòng)刪除。
關(guān)于如何使用EOS多索引表就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。