准备�
上面说了很多关于合约方面的操作, 那么主要说下multi_index
/// @abi table people i64
struct people {
uint64_t id; //用户唯一识别Id
account_name account;//用户账户名
string name; //用户名
string address;//用户地址
/// Define the people primary key
/// 主键索引
uint64_t primary_key() const { return account; }
//用户id索引
uint64_t get_id() const { return id; }
/// Serialization people
EOSLIB_SERIALIZE(people, (account)(id)(name)(address))
};
/// 默认通过主键索引,使用indexed_by,可以通过自定义函数进行索引
typedef eosio::multi_index<N(people), people,
indexed_by<N(byid), const_mem_fun<people, uint64_t, &people::get_id>>
> people_index;
解释一下,使用上面的方式来定义索引,eosio::multi_index<...>的参数解释如下:
people: multi_index容器的表名
people : 智能合约重定义的struct结构体名称,也可以理解成表中的一行记录;
indexed_by<...> :
N(byid) : 给索引起个名字 - byid
const_mem_fun<...> :
uint64_t : 索引的类型
&people ::get_id:通过people 结构体中的get_id函数获得(索引)
实例化:
multi_index(uint64_t code, uint64_t scope)
说明: scope 和 code 都是用来为表建立访问权限
code 拥有这张mult_index 表的账户, 该账户拥有对合约数据的读写权限
scope 用户账户名下的区域, 可以在code下定义多个scope, 把属于不同的scope的表隔离开;
people_index peopmul (_self, _self);
说明: peoplemul 就是一张新表, _self 就是当前调用方法的账户
网友评论