MVault 0.0.1
Loading...
Searching...
No Matches
VaultRequest.hpp
1#pragma once
2
3#include "VaultRequest.h"
4
5#include "Vault.hpp"
6
7namespace mvlt
8{
9 template <VaultRequestType Type>
10 template <class T>
11 VaultRequest<Type>::VaultRequest(const std::string& key, const T& keyValue, std::function<bool(const VaultRecordRef&)> requestPredicat) : Key(key), RequestPredicat(requestPredicat)
12 {
13 static_assert(!std::is_array<T>::value, "It is not possible to use a c array as a key value. \n\
14 If you want to use a string as a key, you must specialize the function with a string. Like this: \n\
15 Less<std::string>(\"Key\", \"Value\") or Greater(\"Key\", std::string(\"Value\")). \n\
16 This works with the following classes: Less, LessOrEqual, Equal, GreaterOrEqual, Greater.");
17
18 DataPtr = static_cast<void*>(new T(keyValue));
19
20 // Set Request func
21 DataRequestFunc = [](const std::string& key, Vault* vlt, std::unordered_set<VaultRecord*>& vaultRecordSet, void* beginValue, void* endValue,
23 {
25 // Simple data request to vlt
26 if (endValue == nullptr)
27 res = vlt->RequestRecordsSet(Type, key, *static_cast<T*>(beginValue), *static_cast<T*>(beginValue), vaultRecordSet, false, false, -1, requestPredicat);
28 else
29 res = vlt->RequestRecordsSet(VaultRequestType::Interval, key, *static_cast<T*>(beginValue), *static_cast<T*>(endValue), vaultRecordSet, isIncludeBeginKeyValue, isIncludeEndKeyValue, -1, requestPredicat);
30
31 // Throw error
32 if (!res.IsOperationSuccess) throw VaultOperationResult(res);
33 };
34
35 DeleteFunc = [](void* ptrToDelete)
36 {
37 delete static_cast<T*>(ptrToDelete);
38 };
39 }
40
41 template <VaultRequestType Type>
42 template <VaultRequestType RequestType1, VaultRequestType RequestType2>
44 {
45 // Switch by request type Or or And
46 switch (Type)
47 {
48 case VaultRequestType::Or:
49 // Set complex request func
50 LogicalRequestFunction = [&](Vault* vlt, std::unordered_set<VaultRecord*>& vaultRecordSet)
51 {
52 std::unordered_set<VaultRecord*> req1Result, req2Result;
53
54 // Make requests in Or(req1, req2)
55 request1.Request(vlt, req1Result);
56 request2.Request(vlt, req2Result);
57
58 // Union requests
60 vaultRecordSet.emplace(record);
62 vaultRecordSet.emplace(record);
63 };
64 break;
65
66 case VaultRequestType::And:
67 // Set complex request func
68 LogicalRequestFunction = [&](Vault* vlt, std::unordered_set<VaultRecord*>& vaultRecordSet)
69 {
70 // Handle interval situation
71 if (request1.Key == request2.Key &&
72 (request1.RequestType == VaultRequestType::Greater || request1.RequestType == VaultRequestType::GreaterOrEqual) &&
73 (request2.RequestType == VaultRequestType::Less || request1.RequestType == VaultRequestType::LessOrEqual))
74 {
75 request1.DataRequestFunc(request1.Key, vlt, vaultRecordSet, request1.DataPtr, request2.DataPtr,
76 (request1.RequestType == VaultRequestType::GreaterOrEqual),
77 (request2.RequestType == VaultRequestType::LessOrEqual), [&](const VaultRecordRef& ref)
78 {
79 if (request1.RequestPredicat(ref) && request2.RequestPredicat(ref))
80 return true;
81 else
82 return false;
83 });
84 }
85 else // Standart and request
86 {
87 std::unordered_set<VaultRecord*> req1Result, req2Result;
88
89 // Make requests in And(req1, req2)
90 request1.Request(vlt, req1Result);
91 request2.Request(vlt, req2Result);
92
93 // Set less request to iterate
94 if (req1Result.size() > req2Result.size())
95 std::swap(req1Result, req2Result);
96
97 // Find intersection
99 if (req2Result.find(record) != req2Result.end())
100 vaultRecordSet.emplace(record);
101 }
102 };
103 break;
104 }
105 }
106
107 template <VaultRequestType Type>
108 void VaultRequest<Type>::Request(Vault* vlt, std::unordered_set<VaultRecord*>& vaultRecords) const
109 {
110 switch (Type)
111 {
112 case VaultRequestType::Less:
113 case VaultRequestType::LessOrEqual:
114 case VaultRequestType::Equal:
115 case VaultRequestType::GreaterOrEqual:
116 case VaultRequestType::Greater:
117 DataRequestFunc(Key, vlt, vaultRecords, DataPtr, nullptr, false, false, RequestPredicat);
118 break;
119 case VaultRequestType::Or:
120 case VaultRequestType::And:
121 LogicalRequestFunction(vlt, vaultRecords);
122 break;
123 }
124 }
125
126 template <VaultRequestType Type>
128 {
129 if (DeleteFunc != nullptr && DataPtr != nullptr)
130 DeleteFunc(DataPtr);
131 }
132
133 template <VaultRequestType LType, VaultRequestType RType>
135 {
137 }
138
139 template <VaultRequestType LType, VaultRequestType RType>
140 VaultRequest<VaultRequestType::Or> operator|| (const VaultRequest<LType>& lhs, const VaultRequest<RType>& rhs) noexcept
141 {
142 return VaultRequest<VaultRequestType::Or>(lhs, rhs);
143 }
144}
Iterator class for all library maps.
Definition Map.h:18
A class for storing data with the ability to quickly search for a variety of different keys of any ty...
Definition Vault.h:33
A class for storing data inside Vault.
Definition VaultRecord.h:18
A class that provides access to data inside Vault.
Definition VaultRecordRef.h:21
friend class VaultRequest
Making the all VaultRequest templates friendly.
Definition VaultRequest.h:44
~VaultRequest() noexcept
Default destructor.
Definition VaultRequest.hpp:127
void Request(Vault *vlt, std::unordered_set< VaultRecord * > &vaultRecords) const
Function for requesting data from Vault.
Definition VaultRequest.hpp:108
Structure for storing the results of MVault operations.
Definition VaultOperationResult.h:40