all files / contracts/ KVStore.sol

100% Statements 7/7
100% Branches 6/6
100% Functions 3/3
100% Lines 8/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40                                  10×                              
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0;
 
contract KVStore {
    uint private constant MAX_STRING_LENGTH = 1000;
    uint private constant BULK_MAX_COUNT = 20;
    mapping(address => mapping(string => string)) private store;
 
    event DataSaved(address indexed sender, string key, string value);
 
    function get(
        address _account,
        string memory _key
    ) public view returns (string memory) {
        return store[_account][_key];
    }
 
    function set(string memory _key, string memory _value) public {
        require(
            bytes(_key).length <= MAX_STRING_LENGTH &&
                bytes(_value).length <= MAX_STRING_LENGTH,
            'Maximum string length'
        );
        store[msg.sender][_key] = _value;
        emit DataSaved(msg.sender, _key, _value);
    }
 
    function setBulk(string[] memory _keys, string[] memory _values) public {
        require(
            _keys.length == _values.length,
            'Keys and values must have the same length'
        );
        require(_keys.length < BULK_MAX_COUNT, 'Too many entries');
 
        for (uint i = 0; i < _keys.length; i++) {
            set(_keys[i], _values[i]);
        }
    }
}