Secondary Indices

Secondary Indices

  EOSIO has the ability to sort tables by up to 16 indices.  A table's struct cannot be modified when it has data in it. \

1、添加第二列索引 member 和 getter。

  The secondary index needs to be numeric field, so a uint64_t was chosen for the age variable

  

2、Add secondary index to `addresses` table configuration

typedef eosio::multi_index<"people"_n, person, 
indexed_by<"byage"_n, const_mem_fun<person, uint64_t, &person::get_secondary_1>>
  > address_index;

  1) indexed_by 是一个 struct.

  2) first parameter set name of index as "byage"

  3) the second type parameter as a function call operator should extract a const value as an index key.

3、compile & deploy

eosio-cpp -o addressbook.wasm addressbook.cpp --abigen

cleos set contract addressbook /home/ubuntu/contracts/addressbook

4、Test

cleos push action addressbook upsert '["alice", "alice", "liddell", 9, "123 drink me way", "wonderland", "amsterdam"]' -p alice@active

cleos push action addressbook upsert '["bob", "bob", "is a guy", 49, "doesnt exist", "somewhere", "someplace"]' -p bob@active

  查看一下插入结果,注意--upper 10 是指定上限,即只查看索引值小于10的row.

cleos get table addressbook addressbook people --upper 10 \
--key-type i64 \
--index 2

  上面命令会看到结果 

  

参考:https://developers.eos.io/eosio-home/docs/secondary-indices

猜你喜欢

转载自www.cnblogs.com/tekkaman/p/10006482.html