dapp::multi_index

vRAM

dapp::multi_index | code​

1
/**
2
* 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.
3
*
4
* @param {name} code - account that owns table
5
* @param {uint64_t} scope - scope identifier within the code hierarchy
6
* @param {uint32_t} [shards=1024] - amount of shards to include for each table
7
* @param {buckets_per_shard} [buckets_per_shard=64] - number of buckets to use per shard
8
* @param {bool} [pin_shards=false] - persist shards to RAM
9
* @param {bool} [pin_buckets=false] - persist shard buckets to RAM
10
* @param {uint32_t} [cleanup_delay=0] - time in seconds before data loaded in RAM is committed to vRAM (IPFS)
11
*
12
* @return advanced_multi_index container
13
*
14
* Notes
15
* - 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).
16
* - by selecting pin_shards = true, the shards will not be evicted from the ipfsentry table after the transaction that required the data is run
17
*
18
*
19
* Example:
20
*
21
* @code
22
* TABLE testindex {
23
* uint64_t id;
24
* uint64_t sometestnumber;
25
* uint64_t primary_key()const {return id;}
26
* };
27
*
28
* typedef dapp::multi_index<"test"_n, testindex> testindex_t;
29
* typedef eosio::multi_index<".test"_n, testindex> testindex_t_v_abi;
30
* typedef eosio::multi_index<"test"_n, testindex_shardbucket> testindex_t_abi;
31
* @endcode
32
*/
33
​
34
TABLE testindex {
35
uint64_t id;
36
uint64_t sometestnumber;
37
uint64_t primary_key()const {return id;}
38
};
39
40
typedef dapp::multi_index<"test"_n, testindex> testindex_t;
41
typedef eosio::multi_index<".test"_n, testindex> testindex_t_v_abi;
42
typedef eosio::multi_index<"test"_n, testindex_shardbucket> testindex_t_abi;
43
​
44
// get some data
45
[[eosio::action]] void testget(uint64_t id) {
46
testindex_t testset(_self,_self.value, 1024, 64, false, false, 0);
47
auto const& data = testset.get(id,"data not found");
48
}
49
​
50
// add new data row with .emplace
51
[[eosio::action]] void testemplace(uint64_t id) {
52
testindex_t testset(_self,_self.value, 1024, 64, false, false, 0);
53
testset.emplace(_self, [&]( auto& a ){
54
a.id = id;
55
});
56
}
57
​
58
// modify existing data row with .modify
59
[[eosio::action]] void testmodify(uint64_t id, uint64_t new_id) {
60
testindex_t testset(_self,_self.value, 1024, 64, false, false, 0);
61
auto existing = testset.find(id);
62
testset.modify(existing,_self, [&]( auto& a ){
63
a.id = new_id;
64
});
65
}
66
​
67
// test adding a delayed cleanup
68
[[eosio::action]] void testdelay(uint64_t id, uint64_t value, uint32_t delay_sec) {
69
testindex_t testset(_self,_self.value, 1024, 64, false, false, delay_sec);
70
auto existing = testset.find(id);
71
if(existing == testset.end())
72
testset.emplace(_self, [&]( auto& a ){
73
a.id = id;
74
a.sometestnumber = value;
75
});
76
else
77
testset.modify(existing,_self, [&]( auto& a ){
78
a.sometestnumber = value;
79
});
80
}
81
82
// test loading a new manifest file
83
// a manifest file is a snapshot of the current state of the table
84
// manifests can be used to version the database or to revert back
85
[[eosio::action]] void testman(dapp::manifest man) {
86
testindex_t testset(_self,_self.value);
87
testset.load_manifest(man,"Test");
88
}
89
​
90
// increment revision number, reset shards and buckets_per_shard params and next_available_key in vconfig table
91
[[eosio::action]] void testclear() {
92
testindex_t testset(_self,_self.value, 1024, 64, false, false, 0);
93
testset.clear();
94
}
Copied!
Copy link
Contents
vRAM