aboutsummaryrefslogtreecommitdiff
path: root/src/DatabaseStorage.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/DatabaseStorage.cpp')
-rw-r--r--src/DatabaseStorage.cpp31
1 files changed, 25 insertions, 6 deletions
diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp
index 7c86d18..0f75d1f 100644
--- a/src/DatabaseStorage.cpp
+++ b/src/DatabaseStorage.cpp
@@ -47,33 +47,52 @@ namespace odhtdb
}
}
- void DatabaseStorage::appendStorage(const Hash &hash, const User *creatorUser, u64 timestamp, const u8 *data, usize dataSize)
+ void DatabaseStorage::appendStorage(const Hash &nodeHash, const Hash &dataHash, const User *creatorUser, u64 timestamp, const u8 *data, usize dataSize)
{
- auto it = storageMap.find(hash);
+ auto it = storageMap.find(nodeHash);
if(it == storageMap.end())
{
string errMsg = "Database storage with hash ";
- errMsg += hash.toString();
+ errMsg += nodeHash.toString();
errMsg += " not found. Storage for a hash needs to be created before data can be appended to it";
throw DatabaseStorageNotFound(errMsg);
}
+ auto storedDataIt = storedDataHash.find(dataHash);
+ if(storedDataIt != storedDataHash.end())
+ {
+ string errMsg = "Database already contains data with hash: ";
+ errMsg += dataHash.toString();
+ throw DatabaseStorageAlreadyExists(errMsg);
+ }
+
DataView storageData { new u8[dataSize], dataSize };
+ memcpy(storageData.data, data, dataSize);
DatabaseStorageObject *databaseStorageObject = new DatabaseStorageObject(storageData, timestamp, creatorUser->getPublicKey());
it->second->objects.push_back(databaseStorageObject);
+ storedDataHash.insert(dataHash);
}
- void DatabaseStorage::addToQuarantine(const Signature::PublicKey &creatorPublicKey, u64 timestamp, const u8 *data, usize dataSize)
+ void DatabaseStorage::addToQuarantine(const Hash &dataHash, const Signature::PublicKey &creatorPublicKey, u64 timestamp, const u8 *data, usize dataSize)
{
+ auto storedDataIt = storedDataHash.find(dataHash);
+ if(storedDataIt != storedDataHash.end())
+ {
+ string errMsg = "Database already contains data with hash: ";
+ errMsg += dataHash.toString();
+ throw DatabaseStorageAlreadyExists(errMsg);
+ }
+
DataView storageData { new u8[dataSize], dataSize };
memcpy(storageData.data, data, dataSize);
DatabaseStorageQuarantineObject *databaseQuarantineStorageObject = new DatabaseStorageQuarantineObject(storageData, timestamp, creatorPublicKey);
quarantineStorageMap[creatorPublicKey].emplace_back(databaseQuarantineStorageObject);
+ storedDataHash.insert(dataHash);
}
void DatabaseStorage::addUser(User *user, const Hash &hash)
{
- userPublicKeyNodeMap[user->getPublicKey()] = hash;
+ userPublicKeyNodeMap[user->getPublicKey()] = new Hash(hash);
publicKeyUserMap[user->getPublicKey()] = user;
}
@@ -89,7 +108,7 @@ namespace odhtdb
{
auto it = userPublicKeyNodeMap.find(userPublicKey);
if(it != userPublicKeyNodeMap.end())
- return &it->second;
+ return it->second;
return nullptr;
}