diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/DataView.hpp | 6 | ||||
-rw-r--r-- | include/Database.hpp | 5 | ||||
-rw-r--r-- | include/DatabaseStorage.hpp | 48 | ||||
-rw-r--r-- | include/Key.hpp | 31 | ||||
-rw-r--r-- | include/Signature.hpp | 6 | ||||
-rw-r--r-- | include/StagedObject.hpp | 10 |
6 files changed, 95 insertions, 11 deletions
diff --git a/include/DataView.hpp b/include/DataView.hpp index 982cb8a..c020f91 100644 --- a/include/DataView.hpp +++ b/include/DataView.hpp @@ -10,7 +10,7 @@ namespace odhtdb DataView() : data(nullptr), size(0) {} DataView(void *_data, usize _size) : data(_data), size(_size) {} - const void *data; - const usize size; + void *data; + usize size; }; -}
\ No newline at end of file +} diff --git a/include/Database.hpp b/include/Database.hpp index bfc3021..0104a6e 100644 --- a/include/Database.hpp +++ b/include/Database.hpp @@ -4,6 +4,7 @@ #include "Key.hpp" #include "StagedObject.hpp" #include "DataView.hpp" +#include "DatabaseStorage.hpp" #include <opendht/dhtrunner.h> #include <vector> #include <ntp/NtpClient.hpp> @@ -12,6 +13,7 @@ namespace odhtdb { class Group; + class LocalUser; class Database { @@ -21,7 +23,7 @@ namespace odhtdb void seed(); void create(const Key &key, Group *primaryAdminGroup); - void add(const Key &key, DataView data); + void add(const Key &key, DataView data, LocalUser *creator); void commit(); private: void commitStagedCreateObject(const StagedCreateObject &stagedObject); @@ -35,5 +37,6 @@ namespace odhtdb dht::DhtRunner node; std::vector<StagedCreateObject> stagedCreateObjects; std::vector<StagedAddObject> stagedAddObjects; + DatabaseStorage databaseStorage; }; } diff --git a/include/DatabaseStorage.hpp b/include/DatabaseStorage.hpp index fee6b72..863c5d9 100644 --- a/include/DatabaseStorage.hpp +++ b/include/DatabaseStorage.hpp @@ -1,10 +1,58 @@ #pragma once +#include "Key.hpp" +#include "DataView.hpp" +#include "Signature.hpp" +#include <vector> +#include <stdexcept> + namespace odhtdb { + class Group; + + struct DatabaseStorageObject + { + DataView data; + u64 timestamp; // In microseconds + Signature::PublicKey creatorPublicKey; + + DatabaseStorageObject(DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey) : + data(_data), timestamp(_timestamp), creatorPublicKey(_creatorPublicKey) + { + + } + }; + + struct DatabaseStorageObjectList + { + u64 timestamp; // In microseconds + std::vector<Group*> groups; + std::vector<DatabaseStorageObject> objects; + }; + + 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 = KeyMap<DatabaseStorageObjectList*>; + class DatabaseStorage { public: + // Throws DatabaseStorageAlreadyExists if data with key already exists + void createStorage(const Key &key, std::vector<Group*> &&groups, u64 timestamp); + // Throws DatabaseStorageNotFound if data with key does not exist + void appendStorage(const Key &key, DataView &data, u64 timestamp, const Signature::PublicKey &creatorPublicKey); + private: + DatabaseStorageMap storageMap; }; } diff --git a/include/Key.hpp b/include/Key.hpp index 505050d..f7a600b 100644 --- a/include/Key.hpp +++ b/include/Key.hpp @@ -1,6 +1,7 @@ #pragma once #include <opendht/infohash.h> +#include <unordered_map> namespace odhtdb { @@ -12,4 +13,32 @@ namespace odhtdb dht::InfoHash hashedKey; }; -}
\ No newline at end of file + + // Source: https://stackoverflow.com/a/11414104 (public license) + static unsigned int fnvHash(const unsigned char *key, int len) + { + unsigned int h = 2166136261; + for (int i = 0; i < len; i++) + h = (h * 16777619) ^ key[i]; + return h; + } + + struct KeyHash + { + size_t operator()(const Key &key) const + { + return fnvHash(key.hashedKey.data(), key.hashedKey.size()); + } + }; + + struct KeyCompare + { + bool operator()(const Key &lhs, const Key &rhs) const + { + return lhs.hashedKey == rhs.hashedKey; + } + }; + + template <typename ValueType> + using KeyMap = std::unordered_map<Key, ValueType, KeyHash, KeyCompare>; +} diff --git a/include/Signature.hpp b/include/Signature.hpp index 90d5278..ea776ea 100644 --- a/include/Signature.hpp +++ b/include/Signature.hpp @@ -31,8 +31,10 @@ namespace odhtdb { friend class KeyPair; public: + static PublicKey ZERO; + // Throws InvalidSignatureKeySize if size is not PUBLIC_KEY_NUM_BYTES - PublicKey(char *data, size_t size); + PublicKey(const char *data, size_t size); PublicKey(const PublicKey &other); PublicKey& operator=(const PublicKey &other); @@ -51,7 +53,7 @@ namespace odhtdb friend class KeyPair; public: // Throws InvalidSignatureKeySize if size is not PRIVATE_KEY_NUM_BYTES - PrivateKey(char *data, size_t size); + PrivateKey(const char *data, size_t size); PrivateKey(const PrivateKey &other); PrivateKey& operator=(const PrivateKey &other); diff --git a/include/StagedObject.hpp b/include/StagedObject.hpp index 61e1073..dc2aaf4 100644 --- a/include/StagedObject.hpp +++ b/include/StagedObject.hpp @@ -3,6 +3,7 @@ #include "Key.hpp" #include "types.hpp" #include "DataView.hpp" +#include "Signature.hpp" namespace odhtdb { @@ -27,12 +28,13 @@ namespace odhtdb Key key; DataView data; u64 timestamp; // In microseconds + Signature::PublicKey creatorPublicKey; - StagedAddObject() : key(), data(), timestamp(0) {} - StagedAddObject(const Key &_key, const DataView &_data, u64 _timestamp) : - key(_key), data(_data), timestamp(_timestamp) + StagedAddObject() : key(), data(), timestamp(0), creatorPublicKey(Signature::PublicKey::ZERO) {} + StagedAddObject(const Key &_key, const DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey) : + key(_key), data(_data), timestamp(_timestamp), creatorPublicKey(_creatorPublicKey) { } }; -}
\ No newline at end of file +} |