-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathStorage.sol
41 lines (37 loc) · 1.26 KB
/
Storage.sol
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
41
pragma solidity ^0.4.17;
import "./Restrictable.sol";
contract Storage is Restrictable {
uint public constant LOAD_CHUNK_SIZE = 256;
mapping (string => bytes) stringMap;
constructor() Restrictable() public {
}
function getBytesByString(string key) public view reader returns (bytes) {
return stringMap[key];
}
function getRangeBytesByString(string key, uint offset) public view reader returns (byte[256], uint) {
bytes memory bs = stringMap[key];
byte[256] memory rbs;
uint remain = bs.length - offset;
if (remain < 0) {
return (rbs, 0);
} else if (remain > 256) {
remain = offset + 256;
} else {
remain = bs.length;
}
for (uint i = offset; i < remain; i++) {
rbs[i - offset] = bs[i];
}
return (rbs, remain);
}
function getBytesLengthByString(string key) public view reader returns (uint) {
bytes memory bs = stringMap[key];
return bs.length;
}
function setBytesByString(string key, bytes data) public writer returns (bool) {
bytes memory prev = stringMap[key];
stringMap[key] = data;
assert(stringMap[key].length > 0);
return prev.length > 0;
}
}