多索引表 multi_index.hpp源代码
Posted thefist11
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多索引表 multi_index.hpp源代码相关的知识,希望对你有一定的参考价值。
1. 在线hpp源代码
https://developers.eos.io/manuals/eosio.cdt/v1.7/group__multiindex
1.1 get_code和get_scope
//Returns the code member property.
name eosio::multi_index< TableName, T, Indices >::get_code() const
//Returns the scope member property.
uint64_t eosio::multi_index< TableName, T, Indices >::get_scope() const
1.2 迭代
const_iterator eosio::multi_index< TableName, T, Indices >::cbegin() const
const_iterator eosio::multi_index< TableName, T, Indices >::begin() const
const_iterator eosio::multi_index< TableName, T, Indices >::cend() const
const_iterator eosio::multi_index< TableName, T, Indices >::end() const
const_reverse_iterator eosio::multi_index< TableName, T, Indices >::crbegin() const
const_reverse_iterator eosio::multi_index< TableName, T, Indices >::rbegin() const
const_reverse_iterator eosio::multi_index< TableName, T, Indices >::crend() const
const_reverse_iterator eosio::multi_index< TableName, T, Indices >::rend() const
const_iterator eosio::multi_index< TableName, T, Indices >::lower_bound(
uint64_t primary
) const
const_iterator eosio::multi_index< TableName, T, Indices >::upper_bound(
uint64_t primary
) const
eg.
void myaction() {
// create reference to address_index - see emplace example below
// add dan account to table - see emplace example below
// add additional account - brendan
addresses.emplace(payer, [&](auto& address) {
address.account_name = "brendan"_n;
address.first_name = "Brendan";
address.last_name = "Blumer";
address.street = "1 EOS Way";
address.city = "Hong Kong";
address.state = "HK";
address.zip = 93445;
});
uint32_t zipnumb = 93445;
auto zip_index = addresses.get_index<name("zip")>();
auto itr = zip_index.lower_bound(zipnumb);
eosio::check(itr->account_name == name("brendan"), "Lock arf, Incorrect First Lower Bound Record ");
itr++;
eosio::check(itr->account_name == name("dan"), "Lock arf, Incorrect Second Lower Bound Record");
itr++;
eosio::check(itr == zip_index.end(), "Lock arf, Incorrect End of Iterator");
}
1.3 available_primary_key
uint64_t eosio::multi_index< TableName, T, Indices >::available_primary_key() const
eg.
void myaction() {
address_index addresses(_self, _self.value); // code, scope
// add to table, first argument is account to bill for storage
addresses.emplace(payer, [&](auto& address) {
address.key = addresses.available_primary_key();
address.first_name = "Daniel";
address.last_name = "Larimer";
address.street = "1 EOS Way";
address.city = "Blacksburg";
address.state = "VA";
});
}
1.4 get_index
template< IndexName> auto eosio::multi_index< TableName, T, Indices >::get_index()
eg.
#include <eosiolib/eosio.hpp>
using namespace eosio;
using namespace std;
class addressbook: contract {
struct address {
uint64_t account_name;
string first_name;
string last_name;
string street;
string city;
string state;
uint32_t zip = 0;
uint64_t primary_key() const { return account_name; }
uint64_t by_zip() const { return zip; }
};
public:
addressbook(name receiver, name code, datastream<const char*> ds):contract(receiver, code, ds) {}
typedef eosio::multi_index< name("address"), address, indexed_by< name("zip"), const_mem_fun<address, uint64_t, &address::by_zip> > address_index;
void myaction() {
// create reference to address_index - see emplace example below
// add dan account to table - see emplace example below
uint32_t zipnumb = 93446;
auto zip_index = addresses.get_index<name("zip")>();
auto itr = zip_index.find(zipnumb);
eosio::check(itr->account_name == name("dan"), "Lock arf, Incorrect Record ");
}
}
EOSIO_DISPATCH( addressbook, (myaction) )
// This assumes the code from the get_index() example. Replace myaction() {...}
void myaction() {
// create reference to address_index - see emplace example below
// add dan account to table - see emplace example below
// add additional account - brendan
addresses.emplace(payer, [&](auto& address) {
address.account_name = "brendan"_n;
address.first_name = "Brendan";
address.last_name = "Blumer";
address.street = "1 EOS Way";
address.city = "Hong Kong";
address.state = "HK";
address.zip = 93445;
});
uint32_t zipnumb = 93445;
auto zip_index = addresses.get_index<name("zip")>();
auto itr = zip_index.upper_bound(zipnumb);
eosio::check(itr->account_name == name("dan"), "Lock arf, Incorrect First Upper Bound Record ");
itr++;
eosio::check(itr == zip_index.end(), "Lock arf, Incorrect End of Iterator");
}
}
EOSIO_DISPATCH( addressbook, (myaction) )
1.5 iterator_to
const_iterator eosio::multi_index< TableName, T, Indices >::iterator_to(
const T & obj
) const
eg.
// This assumes the code from the get_index() example. Replace myaction() {...}
void myaction() {
// create reference to address_index - see emplace example below
// add dan account to table - see emplace example below
// add additional account - brendan
addresses.emplace(payer, [&](auto& address) {
address.account_name = "brendan"_n;
address.first_name = "Brendan";
address.last_name = "Blumer";
address.street = "1 EOS Way";
address.city = "Hong Kong";
address.state = "HK";
address.zip = 93445;
});
auto user = addresses.get("dan"_n);
auto itr = address.find("dan"_n);
eosio::check(iterator_to(user) == itr, "Invalid iterator");
}
}
EOSIO_DISPATCH( addressbook, (myaction) )
1.6 get
const T& eosio::multi_index< TableName, T, Indices >::get(
uint64_t primary,
const char * error_msg = "unable to find key"
) const
eg.
void myaction() {
// create reference to address_index - see emplace example
// add dan account to table - see emplace example
auto user = addresses.get("dan"_n);
eosio::check(user.first_name == "Daniel", "Couldn't get him.");
}
以上是关于多索引表 multi_index.hpp源代码的主要内容,如果未能解决你的问题,请参考以下文章