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
77
78
79
80
81
82
|
#pragma once
#include "Hash.hpp"
#include "DataView.hpp"
#include "Signature.hpp"
#include "Encryption.hpp"
#include <vector>
#include <stdexcept>
namespace odhtdb
{
class Group;
class User;
struct DatabaseStorageObject
{
DataView data;
u64 createdTimestamp; // In microseconds
Signature::PublicKey creatorPublicKey;
DatabaseStorageObject(DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey);
};
struct DatabaseStorageObjectList
{
DataView data;
u64 createdTimestamp; // In microseconds
std::vector<Group*> groups;
std::vector<DatabaseStorageObject*> objects;
};
struct DatabaseStorageQuarantineObject
{
DataView data;
u64 createdTimestamp; // In microseconds
u64 storedTimestamp; // In microseconds
Signature::PublicKey creatorPublicKey;
DatabaseStorageQuarantineObject(DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey);
};
class DatabaseStorageAlreadyExists : public std::runtime_error
{
public:
DatabaseStorageAlreadyExists(const std::string &errMsg) : std::runtime_error(errMsg) {}
};
class DatabaseStorageNotFound : public std::runtime_error
{
public:
DatabaseStorageNotFound(const std::string &errMsg) : std::runtime_error(errMsg) {}
};
using DatabaseStorageMap = MapHashKey<DatabaseStorageObjectList*>;
using DatabaseStorageQuarantineMap = Signature::MapPublicKey<std::vector<DatabaseStorageQuarantineObject*>>;
class DatabaseStorage
{
public:
// Throws DatabaseStorageAlreadyExists if data with hash already exists
void createStorage(const Hash &hash, Group *creatorGroup, u64 timestamp, const u8 *data, usize dataSize);
// Throws DatabaseStorageNotFound if data with hash does not exist
void appendStorage(const Hash &hash, const User *creatorUser, u64 timestamp, const u8 *data, usize dataSize);
void addToQuarantine(const Signature::PublicKey &creatorPublicKey, u64 timestamp, const u8 *data, usize dataSize);
// Returns nullptr if no storage with provided hash exists
const DatabaseStorageObjectList* getStorage(const Hash &hash) const;
// Returns nullptr if no node with the user exists
const Hash* getNodeByUserPublicKey(const Signature::PublicKey &userPublicKey) const;
// Returns nullptr if no user with public key exists
const User* getUserByPublicKey(const Signature::PublicKey &userPublicKey) const;
private:
DatabaseStorageMap storageMap;
DatabaseStorageQuarantineMap quarantineStorageMap;
Signature::MapPublicKey<Hash> userPublicKeyNodeMap;
Signature::MapPublicKey<const User*> publicKeyUserMap;
};
}
|