下面是多索引表的使用指南。編程
爲了深刻和清晰的瞭解多索引表,最終的.cpp文件部分將被進一步詳細討論和討論。注意,完整的.cpp文件能夠在頁面的底部找到。app
code
:是指已公佈智能合約的account_name
。scope
:account_name
所涉及數據範圍。table_name
: 存儲在內存中的表的名稱。要在多索引表中存儲的數據是limit_order
結構。primary_key()
,get_expiration()
,get_price()
函數用於返回表。返回的表將根據調用的函數排序。ide
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
是保存賬戶的變量,它將帳單元素添加到多索引表中,並修改已經在多索引表中的元素。函數
... eosio::multi_index< N( orders ), limit_order, ...
N(orders)
是多索引表的名稱,limit_order
是要存儲在表中的數據。工具
... 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)
是這個索引的名稱。const_mem_fun
表示正在查詢的數據類型、limit_order
的變量的類型是uint64_t
,將使用get_expiration
函數獲取變量。區塊鏈
... 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)
是這個索引的名稱。const_mem_fun
表示正在查詢的數據類型、limit_order
的變量的類型是uint128_t
,將使用get_price
函數獲取變量。ui
orders( N( limitorders ), N( limitorders )
orders
便是多索引表。this
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 ) );
下面,將兩個limit_order
添加到orders
表中。請注意,payer
是正在修改orders
表的「帳單」賬戶。spa
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); });
默認的orders
表按照主鍵排序。code
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
表經過expiration
進行排序並分配給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
表經過price
進行排序並分配給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」的條目被修改。請注意,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" ); } } }
表不能直接刪除,可是,在刪除全部行以後,表將自動刪除。
======================================================================
分享一個交互式的在線編程實戰,EOS智能合約與DApp開發入門:
本課程幫助你快速入門EOS區塊鏈去中心化應用的開發,內容涵蓋EOS工具鏈、帳戶與錢包、發行代幣、智能合約開發與部署、使用代碼與智能合約交互等核心知識點,最後綜合運用各知識點完成一個便籤DApp的開發。
這裏是原文