多索引表 表操作

Posted thefist11

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多索引表 表操作相关的知识,希望对你有一定的参考价值。

1. EOSIO Multi-Index API使用Multi-Index表

提供了C++接口,它的原型实际是Boost Multi-index Containers;

Multi-Index DB是一种在RAM中缓存状态或数据以便快速访问的方法。它支持创建,读取,更新和删除操作,特点就和它的名字一样——多索引。

  • 增加 emplace
  • 修改 modify
  • 删除 erase
  • 查找包括find和get,以及迭代器操作

1.1 find

const_iterator eosio::multi_index< TableName, T, Indices >::find(
    uint64_t primary
) const

const_iterator eosio::multi_index< TableName, T, Indices >::require_find(
    uint64_t primary,
    const char * error_msg = "unable to find key"
) const

1.2 delete

void eosio::multi_index< TableName, T, Indices >::erase(
    const T & obj
)

eg.

void myaction() {
      auto itr = addresses.find("dan"_n);
      eosio::check(itr != addresses.end(), "Record is not found");
      addresses.erase(*itr);
      itr = addresses.find("dan"_n);
      eosio::check(itr == addresses.end(), "Record is not deleted");
}

 
void myaction() {
      // create reference to address_index  - see emplace example
      // add dan account to table           - see emplace example
      auto itr = addresses.find("dan"_n);
      eosio::check(itr != addresses.end(), "Address for account not found");
      addresses.erase( itr );
      eosio::check(itr != addresses.end(), "Everting lock arf, Address not erased properly");
    }

[[eosio::action]] void multi_index_example::del( name user ) {
  // check if the user already exists
  auto itr = testtab.find(user.value);
+  if ( itr == testtab.end() ) {
+    printf("user does not exist in table, nothing to delete" );
+    return;
+  }

+  testtab.erase( itr );
}

1.3 insert

template<typename Lambda>
const_iterator eosio::multi_index< TableName, T, Indices >::emplace(
    name payer,
    Lambda && constructor
)

eg.

[[eosio::action]] void multi_index_example::set( name user ) {
  // check if the user already exists
  auto itr = testtab.find(user.value);

+  if ( itr == testtab.end() ) {
+    testtab.emplace( _self, [&]( auto& u ) {
+      u.test_primary = user;
+      u.secondary = "second"_n;
+      u.datum = 0;
+    });
+  }
}

1.4 modify

template<typename Lambda>
void eosio::multi_index< TableName, T, Indices >::modify(
    const_iterator itr,
    name payer,
    Lambda && updater
)

eg.

void myaction() {
      // create reference to address_index  - see emplace example
      // add dan account to table           - see emplace example

      auto itr = addresses.find("dan"_n);
      eosio::check(itr != addresses.end(), "Address for account not found");
      addresses.modify( *itr, payer, [&]( auto& address ) {
        address.city = "San Luis Obispo";
        address.state = "CA";
      });
      eosio::check(itr->city == "San Luis Obispo", "Lock arf, Address not modified");
    }
}
 
void myaction() {
      // create reference to address_index  - see emplace example
      // add dan account to table           - see emplace example

      auto itr = addresses.find("dan"_n);
      eosio::check(itr != addresses.end(), "Address for account not found");
      addresses.modify( itr, account payer, [&]( auto& address ) {
        address.city = "San Luis Obispo";
        address.state = "CA";
      });
    }


[[eosio::action]] void multi_index_example::mod( name user, uint32_t value ) {
  // check if the user already exists
  auto itr = testtab.find(user.value);
  check( itr != testtab.end(), "user does not exist in table" );

+  testtab.modify( itr, _self, [&]( auto& row ) {
+    row.secondary = user;
+    row.datum = value;
+  });
}

以上是关于多索引表 表操作的主要内容,如果未能解决你的问题,请参考以下文章

多索引表 multi_index.hpp源代码

多索引表 (10)iterator迭代器多索引

使用 Pandas 从多索引表中绘制特定列

多索引表 iterator迭代器

多索引表 定义secondary index

多索引表 创建singleton实例