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
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#pragma once
#include "types.hpp"
#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 = MapHash<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 @nodeHash hash has not been created yet.
// Throws DatabaseStorageAlreadyExists if same data has been added before (hash of @data, in @dataHash)
void appendStorage(const Hash &nodeHash, const Hash &dataHash, const User *creatorUser, u64 timestamp, const u8 *data, usize dataSize);
// Throws DatabaseStorageAlreadyExists if same data has been added before (hash of @data, in @dataHash)
void addToQuarantine(const Hash &dataHash, const Signature::PublicKey &creatorPublicKey, u64 timestamp, const u8 *data, usize dataSize);
void addUser(User *user, const Hash &hash);
// 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;
// Returns nullptr if a group with id @groupId doesn't exist
Group* getGroupById(uint8_t groupId[16]);
// Update storage state (remove quarantine objects if they are too old, etc)
void update();
private:
DatabaseStorageMap storageMap;
DatabaseStorageQuarantineMap quarantineStorageMap;
SetHash storedDataHash; // Prevent duplicate data from being added
Signature::MapPublicKey<Hash*> userPublicKeyNodeMap;
Signature::MapPublicKey<const User*> publicKeyUserMap;
DataViewMap<Group*> groupByIdMap;
};
}
|