-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollection.sol
76 lines (60 loc) · 1.75 KB
/
collection.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
pragma solidity ^0.4.11;
import "interfaces.sol";
contract Collection is CollectionAbstract {
mapping (bytes12 => bytes32[256]) private documentByID;
DBAbstract private db;
bytes12[] private documentIDs;
uint32[] private documentLengths;
uint64 private count;
string private name;
string private version = "master-1.0.0";
function Collection(string _name, DBAbstract _db) {
db = _db;
name = _name;
count = 0;
}
function getVersion() constant returns (string) {
return version;
}
function changeDB(DBAbstract _db) {
require(msg.sender == address(db));
db = _db;
}
function getDocumentCount() constant returns (uint64) {
return count;
}
function getName() constant returns (string) {
return name;
}
function getDocumentByteAt(bytes12 id, uint64 i) constant returns (byte) {
uint64 bi = i / 32;
uint64 off = i % 32;
return byte(documentByID[id][bi][off]);
}
function getDocumentIDbyIndex(uint64 i) constant returns (bytes12) {
return documentIDs[i];
}
function getDocumentLengthbyIndex(uint64 i) constant returns (uint32) {
return documentLengths[i];
}
function insertDocument(bytes12 id, byte[] data) {
require(msg.sender == address(db));
require(documentByID[id][0] == 0x0);
uint256 i = 0;
bytes32[] memory data32;
if (data.length % 32 == 0) {
data32 = new bytes32[](data.length / 32);
} else {
data32 = new bytes32[](data.length / 32 + 1);
}
for (i = 0; i < data.length; i++) {
data32[i / 32] |= bytes32(data[i]) >> ((i % 32) * 8);
}
for (i = 0; i < data32.length; i++) {
documentByID[id][i] = data32[i];
}
documentLengths.push(uint32(data.length));
documentIDs.push(id);
count++;
}
}