From 0e62cb8e5ed06d906ad84321cdda22acfcc952c9 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 9 Mar 2018 10:26:55 +0100 Subject: Partially implement 'add' operation --- src/DatabaseStorage.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 8 deletions(-) (limited to 'src/DatabaseStorage.cpp') diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp index 25d41fb..638eac0 100644 --- a/src/DatabaseStorage.cpp +++ b/src/DatabaseStorage.cpp @@ -1,13 +1,28 @@ #include "../include/DatabaseStorage.hpp" +#include "../include/Group.hpp" +#include "../include/User.hpp" #include +#include using namespace std; namespace odhtdb { + DatabaseStorageObject::DatabaseStorageObject(DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey) : + data(_data), createdTimestamp(_timestamp), creatorPublicKey(_creatorPublicKey) + { + + } + + DatabaseStorageQuarantineObject::DatabaseStorageQuarantineObject(DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey) : + data(_data), createdTimestamp(_timestamp), creatorPublicKey(_creatorPublicKey) + { + auto time = chrono::high_resolution_clock::now().time_since_epoch(); + storedTimestamp = chrono::duration_cast(time).count(); + } + void DatabaseStorage::createStorage(const Hash &hash, Group *creatorGroup, u64 timestamp, const u8 *data, usize dataSize) { - /* if(storageMap.find(hash) != storageMap.end()) { string errMsg = "Database storage with hash "; @@ -15,18 +30,22 @@ namespace odhtdb errMsg += " already exists"; throw DatabaseStorageAlreadyExists(errMsg); } - */ DatabaseStorageObjectList *databaseStorageObjectList = new DatabaseStorageObjectList(); - databaseStorageObjectList->timestamp = timestamp; + databaseStorageObjectList->createdTimestamp = timestamp; databaseStorageObjectList->groups.push_back(creatorGroup); - databaseStorageObjectList->createData = new u8[dataSize]; - memcpy(databaseStorageObjectList->createData, data, dataSize); - databaseStorageObjectList->createDataSize = dataSize; + databaseStorageObjectList->data = DataView(new u8[dataSize], dataSize); + memcpy(databaseStorageObjectList->data.data, data, dataSize); storageMap[hash] = databaseStorageObjectList; + + for(auto user : creatorGroup->getUsers()) + { + userPublicKeyNodeMap[user->getPublicKey()] = hash; + publicKeyUserMap[user->getPublicKey()] = user; + } } - void DatabaseStorage::appendStorage(const Hash &hash, DataView &data, u64 timestamp, const Signature::PublicKey &creatorPublicKey) + void DatabaseStorage::appendStorage(const Hash &hash, const User *creatorUser, u64 timestamp, const u8 *data, usize dataSize) { auto it = storageMap.find(hash); if(it == storageMap.end()) @@ -36,7 +55,18 @@ namespace odhtdb errMsg += " not found. Storage for a hash needs to be created before data can be appended to it"; throw DatabaseStorageNotFound(errMsg); } - it->second->objects.push_back({data, timestamp, creatorPublicKey}); + + DataView storageData { new u8[dataSize], dataSize }; + DatabaseStorageObject *databaseStorageObject = new DatabaseStorageObject(storageData, timestamp, creatorUser->getPublicKey()); + it->second->objects.push_back(databaseStorageObject); + } + + void DatabaseStorage::addToQuarantine(const Signature::PublicKey &creatorPublicKey, u64 timestamp, const u8 *data, usize dataSize) + { + DataView storageData { new u8[dataSize], dataSize }; + memcpy(storageData.data, data, dataSize); + DatabaseStorageQuarantineObject *databaseQuarantineStorageObject = new DatabaseStorageQuarantineObject(storageData, timestamp, creatorPublicKey); + quarantineStorageMap[creatorPublicKey].emplace_back(databaseQuarantineStorageObject); } const DatabaseStorageObjectList* DatabaseStorage::getStorage(const Hash &hash) const @@ -46,4 +76,21 @@ namespace odhtdb return it->second; return nullptr; } + + const Hash* DatabaseStorage::getNodeByUserPublicKey(const Signature::PublicKey &userPublicKey) const + { + auto it = userPublicKeyNodeMap.find(userPublicKey); + if(it != userPublicKeyNodeMap.end()) + return &it->second; + return nullptr; + } + + // Returns nullptr if no user with public key exists + const User* DatabaseStorage::getUserByPublicKey(const Signature::PublicKey &userPublicKey) const + { + auto it = publicKeyUserMap.find(userPublicKey); + if(it != publicKeyUserMap.end()) + return it->second; + return nullptr; + } } -- cgit v1.2.3