dapp::multi_index

vRAM

dapp::multi_index | code

/**
  *  DAPP Network version of the eosio::multi_index container.  Enables storage of information in IPFS (vRAM) when not needed in RAM.  When data is warmed up, it is checked against the merkle root stored on-chain to ensure integrity and prevent a DAPP Service Provider from needing to be a trusted entity.
  *
  *  @param {name} code - account that owns table
  *  @param {uint64_t} scope - scope identifier within the code hierarchy
  *  @param {uint32_t} [shards=1024] - amount of shards to include for each table
  *  @param {buckets_per_shard} [buckets_per_shard=64] - number of buckets to use per shard
  *  @param {bool} [pin_shards=false] - persist shards to RAM
  *  @param {bool} [pin_buckets=false] - persist shard buckets to RAM
  *  @param {uint32_t} [cleanup_delay=0] - time in seconds before data loaded in RAM is committed to vRAM (IPFS)
  *
  *  @return advanced_multi_index container
  *
  *  Notes
  *  - by utilizing the cleanup_delay param, data persists to RAM and can be used until committed.  One use case for this is a session based application where a user does not need their data committed to RAM after each transaction.  The cleanup_delay is reset each time a user uses the data.  If a user is inactive for say 120 seconds, then their data can be committed.  Utilizing the cleanup_delay also prevents the latency associated with warming up data into RAM from vRAM (IPFS).
  *  - by selecting pin_shards = true, the shards will not be evicted from the ipfsentry table after the transaction that required the data is run
  * 
  * 
  *  Example:
  *
  *  @code
  *  TABLE testindex {
  *   uint64_t id;
  *   uint64_t sometestnumber;
  *   uint64_t primary_key()const {return id;}
  *  };
  *  
  *  typedef dapp::multi_index<"test"_n, testindex> testindex_t;
  *  typedef eosio::multi_index<".test"_n, testindex> testindex_t_v_abi;
  *  typedef eosio::multi_index<"test"_n, testindex_shardbucket> testindex_t_abi;
  *  @endcode
  */

  TABLE testindex {
    uint64_t id;
    uint64_t sometestnumber;
    uint64_t primary_key()const {return id;}
  };
  
  typedef dapp::multi_index<"test"_n, testindex> testindex_t;
  typedef eosio::multi_index<".test"_n, testindex> testindex_t_v_abi;
  typedef eosio::multi_index<"test"_n, testindex_shardbucket> testindex_t_abi;

  // get some data
  [[eosio::action]] void testget(uint64_t id) {
    testindex_t testset(_self,_self.value, 1024, 64, false, false, 0);
    auto const& data = testset.get(id,"data not found");
  }

  // add new data row with .emplace
  [[eosio::action]] void testemplace(uint64_t id) {
    testindex_t testset(_self,_self.value, 1024, 64, false, false, 0);
    testset.emplace(_self, [&]( auto& a ){
      a.id = id;
    });
  }

  // modify existing data row with .modify
  [[eosio::action]] void testmodify(uint64_t id, uint64_t new_id) {
    testindex_t testset(_self,_self.value, 1024, 64, false, false, 0);
    auto existing = testset.find(id);
    testset.modify(existing,_self, [&]( auto& a ){
      a.id = new_id;
    });
  }  

  // test adding a delayed cleanup
  [[eosio::action]] void testdelay(uint64_t id, uint64_t value, uint32_t delay_sec) {
    testindex_t testset(_self,_self.value, 1024, 64, false, false, delay_sec);
    auto existing = testset.find(id);
    if(existing == testset.end())
      testset.emplace(_self, [&]( auto& a ){
        a.id = id;
        a.sometestnumber = value;
      });
    else
      testset.modify(existing,_self, [&]( auto& a ){
        a.sometestnumber = value;
      });
  }
  
  // test loading a new manifest file
  // a manifest file is a snapshot of the current state of the table
  // manifests can be used to version the database or to revert back
  [[eosio::action]] void testman(dapp::manifest man) {
    testindex_t testset(_self,_self.value);
    testset.load_manifest(man,"Test");
  }

  // increment revision number, reset shards and buckets_per_shard params and next_available_key in vconfig table
  [[eosio::action]] void testclear() {
    testindex_t testset(_self,_self.value, 1024, 64, false, false, 0);
    testset.clear();
  }

Last updated