Save load and reset dapp::multi_index data

To enable these features, you must include the advanced multi index with: #define USE_ADVANCED_IPFS at the top of the contract file. If you have already deployed a contract that does not use advanced features, do not add this line, as it is not backwards compatible.

With that, you now have the ability to save the list of shards currently being used to represent the table’s current state. With the saved snapshot, a developer can upload it to another contract, or version the snapshot and save it to be loaded to the contract later. This adds much needed flexibility to maintaining database state in a vRAM table.

Save dapp::multi_index data

Using zeus, a backup file can be created with the following command:

zeus backup-table <CONTRACT> <TABLE>

# optional flags:

--endpoint # endpoint of node
# default: localhost:13115
--output # output file name
# default: vram-backup-${CONTRACT}-${TABLE}-0-1578405972.json

# example
zeus backup-table lqdportfolio users --endpoint=http://kylin-dsp-2.liquidapps.io/

Example: vram-backup-lqdportfolio-users-0-1578405972.json

{
  "contract": "lqdportfolio",
  "table": "users",
  "timestamp": "2020-01-07T14:06:12.339Z",
  "revision": 0,
  "manifest": {
    "next_available_key": 0,
    "shards": 1024,
    "buckets_per_shard": 64,
    "shardbuckets": [
      {
        "key": 2,
        "value": "015512202a1de9ce245a8d14b23512badc076aee71aad3aba30900e9c938243ce25b467d"
      },
      {
        "key": 44,
        "value": "015512205b43f739a9786fbe2c384c504af15d06fe1b5a61b72710f51932c6b62592d800"
      },
      ...
    ]
  }
}

Load manifest

Once a manifest is saved, it can be loaded with the following smart contract action.

[[eosio::action]] void testman(dapp::manifest man) {
  testindex_t testset(_self,_self.value);
  // void load_manifest(manifest manifest, string description)
  // description is description item in the backup table
  testset.load_manifest(man,"Test");
}

With a unit test:

let manifest = {
  next_available_key: 556,
  shards: 1024,
  buckets_per_shard: 64,
  shardbuckets
}
await testcontract.testman({
  man: manifest
}, {
  authorization: `${code}@active`,
  broadcast: true,
  sign: true
});

Clear table

By calling the clear command, a table’s version is incremented via the revision param and the next_available_key is reset

TABLE vconfig {
    checksum256 next_available_key = empty256;
    uint32_t shards = 0;
    uint32_t buckets_per_shard = 0;
    uint32_t revision = 0;
};
void clear() {
  vconfig_sgt vconfigsgt(_code,name(TableName).value);
  auto config = vconfigsgt.get_or_default();
  config.revision++;    
  config.next_available_key = empty256; //reset the next available key
  vconfigsgt.set(config,_code);
}
[[eosio::action]] void testclear() {
  testindex_t testset(_self,_self.value);
  testset.clear();
}

Last updated