|
| 1 | +#include <eosio/eosio.hpp> |
| 2 | +#include <eosio/asset.hpp> |
| 3 | + |
| 4 | +extern "C" __attribute__((eosio_wasm_import)) void set_kv_parameters_packed(const void* params, uint32_t size); |
| 5 | + |
| 6 | +using namespace eosio; |
| 7 | + |
| 8 | +struct kvtest_record { |
| 9 | + std::string id; |
| 10 | + std::string data; |
| 11 | +}; |
| 12 | + |
| 13 | +class [[eosio::contract]] kvtest : public contract { |
| 14 | + struct [[eosio::table]] kvtest_table : eosio::kv::table<kvtest_record, "eosio"_n> { |
| 15 | + KV_NAMED_INDEX("by.id", id); |
| 16 | + kvtest_table(name contract) { init(contract, id); } |
| 17 | + }; |
| 18 | + |
| 19 | + public: |
| 20 | + using contract::contract; |
| 21 | + |
| 22 | + kvtest(eosio::name receiver, eosio::name code, eosio::datastream<const char *> ds) : |
| 23 | + contract(receiver, code, ds) |
| 24 | + { |
| 25 | + uint32_t limits[4]; |
| 26 | + limits[0] = 0; |
| 27 | + limits[1] = 1024 * 1024; |
| 28 | + limits[2] = 9 * 1024 * 1024; |
| 29 | + limits[3] = 10; |
| 30 | + set_kv_parameters_packed(limits, sizeof(limits)); |
| 31 | + } |
| 32 | + |
| 33 | + [[eosio::action]] |
| 34 | + void smalltest() { |
| 35 | + require_auth(_self); |
| 36 | + kvtest_table table{"eosio"_n}; |
| 37 | + |
| 38 | + uint64_t id = 1; |
| 39 | + std::string data = "test"; |
| 40 | + |
| 41 | + for (; id < 5000; ++id) { |
| 42 | + table.put({std::to_string(id), data + std::to_string(id)}, get_self()); |
| 43 | + } |
| 44 | + |
| 45 | + for (id = 1; id < 5000; ++id) { |
| 46 | + auto itr = table. id.find(std::to_string(id)); |
| 47 | + if (itr != table.id.end()) { |
| 48 | + std::string val = itr.value().data; |
| 49 | + eosio::check(val.compare(data + std::to_string(id)) == 0, "The value for the key " + std::to_string(id) + " is " + val); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + [[eosio::action]] |
| 55 | + void largetest() { |
| 56 | + require_auth(_self); |
| 57 | + |
| 58 | + kvtest_table table{"eosio"_n}; |
| 59 | + |
| 60 | + std::string data = std::string(8 * 1024 * 1024, 't'); |
| 61 | + |
| 62 | + uint64_t id = 1; |
| 63 | + for (; id < 200; ++id) |
| 64 | + table.put({std::to_string(id), std::to_string(id) + data}, get_self()); |
| 65 | + |
| 66 | + for (id = 1; id < 200; ++id) { |
| 67 | + auto itr = table.id.find(std::to_string(id)); |
| 68 | + if (itr != table.id.end()) { |
| 69 | + std::string val = itr.value().data.substr(0, 10); |
| 70 | + std::string expected = (std::to_string(id) + data).substr(0, 10); |
| 71 | + eosio::check(val.compare(expected) == 0, "The value for the key " + std::to_string(id) + " is " + val + "..."); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +}; |
0 commit comments