Skip to content

Commit

Permalink
Update VaultRecordSet operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mrognor committed Dec 24, 2024
1 parent 6851f08 commit 88a4f21
Show file tree
Hide file tree
Showing 10 changed files with 414 additions and 93 deletions.
8 changes: 4 additions & 4 deletions Examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ cmake_minimum_required(VERSION 3.6)

if(WIN32)
if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(CMAKE_CXX_FLAGS "-Wall -O3")
set(CMAKE_CXX_FLAGS "-Wextra -O3")
else()
set(CMAKE_CXX_FLAGS "-Wall -g")
set(CMAKE_CXX_FLAGS "-Wextra -g")
endif()
else()
if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(CMAKE_CXX_FLAGS "-Wall -O3")
set(CMAKE_CXX_FLAGS "-Wextra -O3")
else()
set(CMAKE_CXX_FLAGS "-Wall -fsanitize=address -fsanitize=leak -lgcov --coverage -g")
set(CMAKE_CXX_FLAGS "-Wextra -fsanitize=address -fsanitize=leak -lgcov --coverage -g")
endif()
endif()

Expand Down
10 changes: 4 additions & 6 deletions Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ cmake_minimum_required(VERSION 3.6)

if(WIN32)
if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(CMAKE_CXX_FLAGS "-Wall -O3")
set(CMAKE_CXX_FLAGS "-Wextra -O3")
else()
set(CMAKE_CXX_FLAGS "-Wall -g")
set(CMAKE_CXX_FLAGS "-Wextra -g")
endif()
else()
if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(CMAKE_CXX_FLAGS "-Wall -O3")
set(CMAKE_CXX_FLAGS "-Wextra -O3")
else()
set(CMAKE_CXX_FLAGS "-Wall -fsanitize=address -fsanitize=leak -lgcov --coverage -g")
set(CMAKE_CXX_FLAGS "-Wextra -fsanitize=address -fsanitize=leak -lgcov --coverage -g")
endif()
endif()

Expand All @@ -36,7 +36,6 @@ set(MVaultHeaders
Functions/CsvParser.h
Functions/FromString.h
Functions/PrintAsTable.h
Functions/VaultRecordSetOperations.h
)

set(MVaultTemplates
Expand All @@ -61,7 +60,6 @@ set(MVaultSource
VaultRecordSet.cpp

Functions/CsvParser.cpp
Functions/VaultRecordSetOperations.cpp
)

project(MVault)
Expand Down
59 changes: 0 additions & 59 deletions Source/Functions/VaultRecordSetOperations.cpp

This file was deleted.

10 changes: 0 additions & 10 deletions Source/Functions/VaultRecordSetOperations.h

This file was deleted.

1 change: 0 additions & 1 deletion Source/MVault.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "Functions/ToString.h"
#include "Functions/CsvParser.h"
#include "Functions/PrintAsTable.h"
#include "Functions/VaultRecordSetOperations.h"

#include "Vault.hpp"
#include "VaultRequest.hpp"
Expand Down
2 changes: 1 addition & 1 deletion Source/Vault.h
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,6 @@ namespace mvlt
~Vault() noexcept;

/// Making Intersection function friendly
friend void Intersection(const VaultRecordSet& a, const VaultRecordSet& b, VaultRecordSet& res) noexcept;
friend VaultOperationResult Intersection(const VaultRecordSet& a, const VaultRecordSet& b, VaultRecordSet& res) noexcept;
};
}
177 changes: 171 additions & 6 deletions Source/VaultRecordSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace mvlt
RecursiveReadWriteMtx.Disable();
}

VaultRecordSet::VaultRecordSet(const VaultRecordSet& other) noexcept
VaultRecordSet::VaultRecordSet(const VaultRecordSet& other) noexcept : Vault()
{
if (other.GetIsParentVaultValid())
CopySet(other);
Expand Down Expand Up @@ -340,7 +340,7 @@ namespace mvlt
res.ResultCode = VaultOperationResultCode::ParentVaultNotMatch;
}

if (this == &a)
if (res.IsOperationSuccess && this == &a)
{
res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::SameVaultRecordSetInRequest;
Expand All @@ -365,9 +365,37 @@ namespace mvlt
return res;
}

void VaultRecordSet::Exclude(const VaultRecordSet& a) noexcept
VaultOperationResult VaultRecordSet::Exclude(const VaultRecordSet& a) noexcept
{
if (GetIsParentVaultValid() && a.GetIsParentVaultValid())
VaultOperationResult res;
res.IsOperationSuccess = true;

// Handle errors
if (!GetIsParentVaultValid())
{
res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::ParentVaultNotValid;
}

if (res.IsOperationSuccess && !a.GetIsParentVaultValid())
{
res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::OtherParentVaultNotValid;
}

if (res.IsOperationSuccess && a.ParentVault != ParentVault)
{
res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::ParentVaultNotMatch;
}

if (res.IsOperationSuccess && this == &a)
{
res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::SameVaultRecordSetInRequest;
}

if (res.IsOperationSuccess)
{
ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);

Expand All @@ -380,11 +408,41 @@ namespace mvlt
++recordsSetIt;
}
}

return res;
}

void VaultRecordSet::Intersect(const VaultRecordSet& a) noexcept
VaultOperationResult VaultRecordSet::Intersect(const VaultRecordSet& a) noexcept
{
if (GetIsParentVaultValid() && a.GetIsParentVaultValid())
VaultOperationResult res;
res.IsOperationSuccess = true;

// Handle errors
if (!GetIsParentVaultValid())
{
res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::ParentVaultNotValid;
}

if (res.IsOperationSuccess && !a.GetIsParentVaultValid())
{
res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::OtherParentVaultNotValid;
}

if (res.IsOperationSuccess && a.ParentVault != ParentVault)
{
res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::ParentVaultNotMatch;
}

if (res.IsOperationSuccess && this == &a)
{
res.IsOperationSuccess = false;
res.ResultCode = VaultOperationResultCode::SameVaultRecordSetInRequest;
}

if (res.IsOperationSuccess)
{
ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);

Expand All @@ -397,6 +455,8 @@ namespace mvlt
++recordsSetIt;
}
}

return res;
}

bool VaultRecordSet::SaveToFile(const std::string& fileName, const std::vector<std::string> keys, const std::string& separator, const bool& isSaveKey) const noexcept
Expand Down Expand Up @@ -427,4 +487,109 @@ namespace mvlt

return res;
}

VaultOperationResult Union(const VaultRecordSet& a, const VaultRecordSet& b, VaultRecordSet& res) noexcept
{
VaultOperationResult vor;

if (&a == &b || &a == &res || &b == &res)
{
vor.IsOperationSuccess = false;
vor.ResultCode = VaultOperationResultCode::SameVaultRecordSetInRequest;
}
else
{
res = a;
vor = res.Join(b);
}

return vor;
}

VaultOperationResult Intersection(const VaultRecordSet& a, const VaultRecordSet& b, VaultRecordSet& res) noexcept
{
VaultOperationResult vor;
vor.IsOperationSuccess = true;

// Handle errors
if (&a == &b || &a == &res || &b == &res)
{
vor.IsOperationSuccess = false;
vor.ResultCode = VaultOperationResultCode::SameVaultRecordSetInRequest;
}

if (vor.IsOperationSuccess && !a.GetIsParentVaultValid())
{
vor.IsOperationSuccess = false;
vor.ResultCode = VaultOperationResultCode::ParentVaultNotValid;
}

if (vor.IsOperationSuccess && !b.GetIsParentVaultValid())
{
vor.IsOperationSuccess = false;
vor.ResultCode = VaultOperationResultCode::OtherParentVaultNotValid;
}

if (vor.IsOperationSuccess && a.ParentVault != b.ParentVault)
{
vor.IsOperationSuccess = false;
vor.ResultCode = VaultOperationResultCode::ParentVaultNotMatch;
}

if (vor.IsOperationSuccess)
{
if (res.GetIsParentVaultValid()) res.Reset();

a.ParentVault->RecursiveReadWriteMtx.ReadLock();

res.ParentVault = a.ParentVault;

for (auto& keyCopierIt : a.VaultKeyCopiers)
keyCopierIt.second(&res);

// Set proper key order
res.KeysOrder = a.KeysOrder;

// Set unique keys
res.UniqueKeys = a.UniqueKeys;

a.ParentVault->RecordSetsSet.emplace(&res);

// Pick less set to iterate
if (a.RecordsSet.size() < b.RecordsSet.size())
{
// Iterate over all records in a
for (VaultRecord* record : a.RecordsSet)
{
// if record in b then add it to the res
if (b.RecordsSet.find(record) != b.RecordsSet.end())
{
res.RecordsSet.emplace(record);

for (auto& adder : res.VaultRecordAdders)
adder.second(record);
}
}
}
else
{
// Iterate over all records in b
for (VaultRecord* record : b.RecordsSet)
{
// if record in b then add it to the res
if (a.RecordsSet.find(record) != a.RecordsSet.end())
{
res.RecordsSet.emplace(record);

for (auto& adder : res.VaultRecordAdders)
adder.second(record);
}
}
}

a.ParentVault->RecursiveReadWriteMtx.ReadUnlock();
}

return vor;
}
}
Loading

0 comments on commit 88a4f21

Please sign in to comment.