Skip to content

Commit

Permalink
Add properly AddUniqueKey result
Browse files Browse the repository at this point in the history
  • Loading branch information
mrognor committed Dec 4, 2024
1 parent 847a39f commit f7bdc4e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
6 changes: 2 additions & 4 deletions Source/Vault.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ namespace mvlt
\return Returns false if such a key already exists, otherwise it returns true
*/
template <class T>
bool AddKey(const std::string& key, const T& defaultKeyValue, const bool& isUniqueKey,
VaultOperationResult AddKey(const std::string& key, const T& defaultKeyValue, const bool& isUniqueKey,
std::function<T(std::size_t, const VaultRecordRef&)> uniqueKeyFunction) noexcept;

/**
Expand Down Expand Up @@ -267,7 +267,7 @@ namespace mvlt
\return Returns false if such a key already exists, otherwise it returns true
*/
template <class T>
bool AddUniqueKey(const std::string& key, std::function<T(std::size_t, const VaultRecordRef&)> uniqueKeyFunction) noexcept;
VaultOperationResult AddUniqueKey(const std::string& key, std::function<T(std::size_t, const VaultRecordRef&)> uniqueKeyFunction) noexcept;

/**
\brief Template method to update default key value
Expand Down Expand Up @@ -665,11 +665,9 @@ namespace mvlt
\brief A method for saving data to a table file. The file format is csv
\param [in] fileName The file name to save the data, the extension must be specified manually
\param [in] keys a vector with the keys to save. The keys will be stored in the same order as in the vector
\param [in] separator The symbol that will be used to separate the record elements in the file
\param [in] isSaveKey A variable that determines whether to save keys to a file
The validity of the keys is provided by the user. In case of a non-existent key, an empty column will be written to the file.
\return It will return true if it was possible to open the file and write the data, otherwise it will return false
*/
bool SaveToFile(const std::string& fileName, const std::vector<std::string> keys = {}, const std::string& separator = ",", const bool& isSaveKey = true) const noexcept;
Expand Down
25 changes: 19 additions & 6 deletions Source/Vault.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,19 +346,27 @@ namespace mvlt
}

template <class T>
bool Vault::AddKey(const std::string& key, const T& defaultKeyValue, const bool& isUniqueKey,
VaultOperationResult Vault::AddKey(const std::string& key, const T& defaultKeyValue, const bool& isUniqueKey,
std::function<T(std::size_t, const VaultRecordRef&)> uniqueKeyFunction) noexcept
{
static_assert(!std::is_array<T>::value, "It is not possible to use a c array as a key value. \n\
If you want to use a string as a key, you must specialize the function with a string. Like this: \n\
AddKey<std::string>(\"Key\", \"Value\") or AddKey(\"Key\", std::string(\"Value\"))");

VaultOperationResult res;
res.Key = key;
res.RequestedType = typeid(T);

// Lock Vault to write
WriteLock<RecursiveReadWriteMutex> writeLock(RecursiveReadWriteMtx);

// If the key was added earlier
if (KeysTypes.find(key) != KeysTypes.end())
return false;
{
res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::DuplicateKey;
return res;
}

// Create new hash map to store data with template T key
UnorderedMap<T, VaultRecord*>* TtoVaultRecordHashMap = new UnorderedMap<T, VaultRecord*>(!isUniqueKey);
Expand Down Expand Up @@ -417,7 +425,10 @@ namespace mvlt
{
VaultHashMapStructure.EraseData(key);
VaultMapStructure.EraseData(key);
return false;

res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::UniqueKeyValueAlredyInSet;
return res;
}
}
else
Expand Down Expand Up @@ -538,17 +549,19 @@ namespace mvlt
}
}

return true;
res.IsOperationSuccess = true;
res.ResultCode = VaultOperationResultCode::Success;
return res;
}

template <class T>
bool Vault::AddKey(const std::string& key, const T& defaultKeyValue) noexcept
{
return AddKey(key, defaultKeyValue, false, {[&](std::size_t counter, const VaultRecordRef&) -> T{ return defaultKeyValue; }});
return AddKey(key, defaultKeyValue, false, {[&](std::size_t counter, const VaultRecordRef&) -> T{ return defaultKeyValue; }}).IsOperationSuccess;
}

template <class T>
bool Vault::AddUniqueKey(const std::string& key, std::function<T(std::size_t, const VaultRecordRef&)> uniqueKeyFunction) noexcept
VaultOperationResult Vault::AddUniqueKey(const std::string& key, std::function<T(std::size_t, const VaultRecordRef&)> uniqueKeyFunction) noexcept
{
return AddKey(key, T(), true, uniqueKeyFunction);
}
Expand Down
1 change: 1 addition & 0 deletions Source/VaultOperationResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace mvlt
/// \brief Enum with all error handling codes
enum class VaultOperationResultCode
{
DuplicateKey = -8, ///< This code is returned when trying to add key which alredy in vault
TryToUpdateUniqueKey = -7, ///< This code is returned when trying to update default value in an unique key
RecordAlredyInSet = -6, ///< This code is returned when trying to add an record to the set when it is already in it
ParentVaultNotMatch = -5, ///< This code is returned during operations on VaultRecordSet when objects depend on different Vaults
Expand Down
10 changes: 5 additions & 5 deletions Tests/VaultUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ void Vault_AddUniqueKey_Test()

// Add to empty Vault
// Correct add new key
TEST_ASSERT(vlt.AddUniqueKey<int>("A", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return static_cast<int>(counter); }}) == true, "Failed to add new unique key");
TEST_ASSERT(vlt.AddUniqueKey<int>("A", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return static_cast<int>(counter); }}).IsOperationSuccess == true, "Failed to add new unique key");

// Incorrect try to add exist key
TEST_ASSERT(vlt.AddUniqueKey<int>("A", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return static_cast<int>(counter); }}) == false, "Error when try to add key with existing name");
TEST_ASSERT(vlt.AddUniqueKey<int>("A", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return static_cast<int>(counter); }}).IsOperationSuccess == false, "Error when try to add key with existing name");

// Fill vault
for (int i = 0; i < 10; ++i)
Expand All @@ -35,18 +35,18 @@ void Vault_AddUniqueKey_Test()

// Add to filled vault
// Correct add new key
TEST_ASSERT(vlt.AddUniqueKey<int>("B", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return static_cast<int>(counter * counter); }}) == true, "Failed to add new unique key");
TEST_ASSERT(vlt.AddUniqueKey<int>("B", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return static_cast<int>(counter * counter); }}).IsOperationSuccess == true, "Failed to add new unique key");

// Incorrect try to add exist key
TEST_ASSERT(vlt.AddUniqueKey<int>("C", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return 1; }}) == false, "Error when try to add key with existing name");
TEST_ASSERT(vlt.AddUniqueKey<int>("C", {[](std::size_t counter, const VaultRecordRef& vrf) -> int { return 1; }}).IsOperationSuccess == false, "Error when try to add key with existing name");

// Check lambda
TEST_ASSERT(vlt.AddUniqueKey<int>("C", {[](std::size_t counter, const VaultRecordRef& vrf) -> int
{
int i;
vrf.GetData("B", i);
return static_cast<int>(2 * i);
}}) == true, "Failed to add new unique key");
}}).IsOperationSuccess == true, "Failed to add new unique key");

VaultRecordRef vrf;
for (int i = 0; i < 10; ++i)
Expand Down

0 comments on commit f7bdc4e

Please sign in to comment.