aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAleksi Lindeman <0xdec05eba@gmail.com>2018-03-13 03:03:11 +0100
committerAleksi Lindeman <0xdec05eba@gmail.com>2018-03-13 03:06:45 +0100
commitf84aba861d26601ae4c7203daa39752e7c95cfd8 (patch)
tree2b39728d07fb370e87b05ba2fb63958e5ad8fb28 /include
parent1328d943c5016dd1662a4e46d4a408bca010cffc (diff)
Fix add data operation not working correctly
Reminder: do not get reference to hash map value... duh Add thread-safe logging (log is in order now!). Store data immediately to database when WE add it instead of waiting for response from remote peers. TODO: Test with multiple peers (not only localhost)
Diffstat (limited to 'include')
-rw-r--r--include/Database.hpp4
-rw-r--r--include/DatabaseStorage.hpp13
-rw-r--r--include/Hash.hpp5
-rw-r--r--include/Log.hpp12
4 files changed, 26 insertions, 8 deletions
diff --git a/include/Database.hpp b/include/Database.hpp
index 64a381c..6bc7bc5 100644
--- a/include/Database.hpp
+++ b/include/Database.hpp
@@ -141,9 +141,9 @@ namespace odhtdb
void commitStagedAddObject(const std::unique_ptr<StagedObject> &stagedObject);
ntp::NtpTimestamp getSyncedTimestampUtc() const;
DatabaseCreateRequest deserializeCreateRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<char*> encryptionKey);
- void deserializeAddRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<char*> encryptionKey);
+ void deserializeAddRequest(const std::shared_ptr<dht::Value> &value, const Hash &requestDataHash, const std::shared_ptr<char*> encryptionKey);
bool listenCreateData(std::shared_ptr<dht::Value> value, const Hash &hash, const std::shared_ptr<char*> encryptionKey);
- bool listenAddData(std::shared_ptr<dht::Value> value, const Hash &hash, const std::shared_ptr<char*> encryptionKey);
+ bool listenAddData(std::shared_ptr<dht::Value> value, const Hash &requestDataHash, const std::shared_ptr<char*> encryptionKey);
private:
dht::DhtRunner node;
std::vector<std::unique_ptr<StagedObject>> stagedCreateObjects;
diff --git a/include/DatabaseStorage.hpp b/include/DatabaseStorage.hpp
index ee4d2ad..ad4f70b 100644
--- a/include/DatabaseStorage.hpp
+++ b/include/DatabaseStorage.hpp
@@ -52,7 +52,7 @@ namespace odhtdb
DatabaseStorageNotFound(const std::string &errMsg) : std::runtime_error(errMsg) {}
};
- using DatabaseStorageMap = MapHashKey<DatabaseStorageObjectList*>;
+ using DatabaseStorageMap = MapHash<DatabaseStorageObjectList*>;
using DatabaseStorageQuarantineMap = Signature::MapPublicKey<std::vector<DatabaseStorageQuarantineObject*>>;
class DatabaseStorage
@@ -61,10 +61,12 @@ namespace odhtdb
// 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);
+ // 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);
- void addToQuarantine(const Signature::PublicKey &creatorPublicKey, 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);
@@ -85,7 +87,8 @@ namespace odhtdb
private:
DatabaseStorageMap storageMap;
DatabaseStorageQuarantineMap quarantineStorageMap;
- Signature::MapPublicKey<Hash> userPublicKeyNodeMap;
+ SetHash storedDataHash; // Prevent duplicate data from being added
+ Signature::MapPublicKey<Hash*> userPublicKeyNodeMap;
Signature::MapPublicKey<const User*> publicKeyUserMap;
DataViewMap<Group*> groupByIdMap;
};
diff --git a/include/Hash.hpp b/include/Hash.hpp
index bd87b69..9dce168 100644
--- a/include/Hash.hpp
+++ b/include/Hash.hpp
@@ -3,6 +3,7 @@
#include "utils.hpp"
#include <stdexcept>
#include <unordered_map>
+#include <unordered_set>
namespace odhtdb
{
@@ -51,5 +52,7 @@ namespace odhtdb
};
template <typename ValueType>
- using MapHashKey = std::unordered_map<Hash, ValueType, HashHasher>;
+ using MapHash = std::unordered_map<Hash, ValueType, HashHasher>;
+
+ using SetHash = std::unordered_set<Hash, HashHasher>;
}
diff --git a/include/Log.hpp b/include/Log.hpp
new file mode 100644
index 0000000..d09c2a2
--- /dev/null
+++ b/include/Log.hpp
@@ -0,0 +1,12 @@
+#pragma once
+
+namespace odhtdb
+{
+ class Log
+ {
+ public:
+ static void debug(const char *fmt, ...);
+ static void warn(const char *fmt, ...);
+ static void error(const char *fmt, ...);
+ };
+}