MVault 0.0.1
Loading...
Searching...
No Matches
VaultRecordSet.hpp
1#pragma once
2
3#include "VaultRecordSet.h"
4
5namespace mvlt
6{
7 template <class T>
8 VaultOperationResult VaultRecordSet::RequestRecords(const VaultRequestType& requestType, const std::string& key, const T& beginKeyValue,
9 const T& endKeyValue, VaultRecordSet& vaultRecordSet, const bool& isIncludeBeginKeyValue,
10 const bool& isIncludeEndKeyValue, const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef&)>& requestPredicat) const noexcept
11 {
12 VaultOperationResult res;
13
14 if (GetIsParentVaultValid())
15 {
16 if (&vaultRecordSet == this)
17 {
18 res.IsOperationSuccess = false;
19 res.ResultCode = VaultOperationResultCode::SameVaultRecordSetInRequest;
20 }
21 else
22 {
23 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
24
25 // Remove old data from vaultRecordSet
26 if (vaultRecordSet.ParentVault != ParentVault)
27 {
28 vaultRecordSet.Reset();
29
30 // Set new parent vault to vaultRecordSet
31 vaultRecordSet.ParentVault = ParentVault;
32
33 // Copy keys from this to vaultRecordSet
34 for (auto& keyCopierIt : VaultKeyCopiers)
35 keyCopierIt.second(&vaultRecordSet);
36
37 // Set proper key order
38 vaultRecordSet.KeysOrder = KeysOrder;
39
40 // Set unique keys
41 vaultRecordSet.UniqueKeys = UniqueKeys;
42 }
43 else vaultRecordSet.Clear();
44
45 // Save vaultRecordSet
46 ParentVault->RecordSetsSet.emplace(&vaultRecordSet);
47
48 res = Vault::RequestRecords(requestType, key, beginKeyValue, endKeyValue, vaultRecordSet, isIncludeBeginKeyValue, isIncludeEndKeyValue, amountOfRecords, requestPredicat);
49 vaultRecordSet.ParentVault = ParentVault;
50 }
51 }
52 else
53 {
54 res.Key = key;
55 res.RequestedType = typeid(beginKeyValue);
56 res.IsOperationSuccess = false;
58 }
59
60 return res;
61 }
62
63 template <class T>
65 {
67
68 if (GetIsParentVaultValid())
69 {
70 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
72 }
73 else
74 {
75 res.Key = key;
76 res.RequestedType = typeid(defaultKeyValue);
77 res.IsOperationSuccess = false;
79 }
80
81 return res;
82 }
83
84 template <class T>
86 {
88
89 if (GetIsParentVaultValid())
90 {
91 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
92
94 vaultRecordRef.Vlt = ParentVault;
95 }
96 else
97 {
98 res.Key = key;
99 res.RequestedType = typeid(keyValue);
100 res.IsOperationSuccess = false;
102 }
103
104 return res;
105 }
106
107 template <class T>
108 VaultOperationResult VaultRecordSet::GetRecords(const std::string& key, const T& keyValue, std::vector<VaultRecordRef>& recordsRefs, const std::size_t& amountOfRecords) const noexcept
109 {
111
112 if (GetIsParentVaultValid())
113 {
114 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
115
118 ref.Vlt = ParentVault;
119 }
120 else
121 {
122 res.Key = key;
123 res.RequestedType = typeid(keyValue);
124 res.IsOperationSuccess = false;
126 }
127
128 return res;
129 }
130
131 template <class T>
133 const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef&)>& requestPredicat) const
134 {
135 return RequestRecords(VaultRequestType::Equal, key, keyValue, keyValue, vaultRecordSet, false, false, amountOfRecords, requestPredicat);
136 }
137
138 template <class T>
140 const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef&)>& requestPredicat) const
141 {
142 return RequestRecords(VaultRequestType::Greater, key, keyValue, keyValue, vaultRecordSet, false, false, amountOfRecords, requestPredicat);
143 }
144
145 template <class T>
147 const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef&)>& requestPredicat) const
148 {
149 return RequestRecords(VaultRequestType::GreaterOrEqual, key, keyValue, keyValue, vaultRecordSet, false, false, amountOfRecords, requestPredicat);
150 }
151
152 template <class T>
154 const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef&)>& requestPredicat) const
155 {
156 return RequestRecords(VaultRequestType::Less, key, keyValue, keyValue, vaultRecordSet, false, false, amountOfRecords, requestPredicat);
157 }
158
159 template <class T>
161 const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef&)>& requestPredicat) const
162 {
163 return RequestRecords(VaultRequestType::LessOrEqual, key, keyValue, keyValue, vaultRecordSet, false, false, amountOfRecords, requestPredicat);
164 }
165
166 template <class T>
169 const bool& isIncludeEndKeyValue, const std::size_t& amountOfRecords,
170 const std::function<bool(const VaultRecordRef&)>& requestPredicat) const
171 {
172 return RequestRecords(VaultRequestType::Interval, key, beginKeyValue,
175 }
176
177 template <VaultRequestType Type>
179 {
181
183 {
184 if (&vaultRecordSet == this)
185 {
186 res.IsOperationSuccess = false;
187 res.ResultCode = VaultOperationResultCode::SameVaultRecordSetInRequest;
188 }
189 else
190 {
191 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
192
193 // Remove old data from vaultRecordSet
194 if (vaultRecordSet.ParentVault != ParentVault)
195 {
196 vaultRecordSet.Reset();
197
198 // Set new parent vault to vaultRecordSet
199 vaultRecordSet.ParentVault = ParentVault;
200
201 // Copy keys from this to vaultRecordSet
202 for (auto& keyCopierIt : VaultKeyCopiers)
204
205 // Set proper key order
206 vaultRecordSet.KeysOrder = KeysOrder;
207
208 // Set unique keys
209 vaultRecordSet.UniqueKeys = UniqueKeys;
210 }
211 else vaultRecordSet.Clear();
212
213 // Save vaultRecordSet
214 ParentVault->RecordSetsSet.emplace(&vaultRecordSet);
215
217 vaultRecordSet.ParentVault = ParentVault;
218 }
219 }
220 else
221 {
222 res.IsOperationSuccess = false;
224 }
225
226 return res;
227 }
228
229 template <class T>
230 VaultOperationResult VaultRecordSet::RemoveRecord(const std::string& key, const T& keyValue) noexcept
231 {
232 static_assert(!std::is_array<T>::value, "It is not possible to use a c array as a key value. \n\
233 If you want to use a string as a key, you must specialize the function with a string. Like this: \n\
234 RemoveRecord<std::string>(\"Key\", \"Value\") or RemoveRecord(\"Key\", std::string(\"Value\"))");
235
237
238 if (GetIsParentVaultValid())
239 {
240 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
242 }
243 else
244 {
245 res.Key = key;
246 res.RequestedType = typeid(keyValue);
247 res.IsOperationSuccess = false;
249 }
250
251 return res;
252 }
253
254 template <class T>
255 VaultOperationResult VaultRecordSet::RemoveRecords(const std::string& key, const T& keyValue, const std::size_t& amountOfRecords) noexcept
256 {
257 static_assert(!std::is_array<T>::value, "It is not possible to use a c array as a key value. \n\
258 If you want to use a string as a key, you must specialize the function with a string. Like this: \n\
259 RemoveRecords<std::string>(\"Key\", \"Value\") or RemoveRecords(\"Key\", std::string(\"Value\"))");
260
262
263 if (GetIsParentVaultValid())
264 {
265 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
267 }
268 else
269 {
270 res.Key = key;
271 res.RequestedType = typeid(keyValue);
272 res.IsOperationSuccess = false;
274 }
275
276 return res;
277 }
278
279 template<class F>
280 void VaultRecordSet::SortBy(const std::string& key, const F&& func, const bool& isReverse, const std::size_t& amountOfRecords) const noexcept
281 {
282 if (GetIsParentVaultValid())
283 {
284 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
286 }
287 }
288}
Iterator class for all library maps.
Definition Map.h:18
bool EraseRecord(const VaultRecordRef &recordRefToErase) noexcept
Method for deleting a record from a Vault.
Definition Vault.cpp:593
VaultOperationResult GetKeyValue(const std::string &key, T &defaultKeyValue) const noexcept
The method for getting a default key value.
Definition Vault.hpp:641
void SortBy(const std::string &key, const F &&func, const bool &isReverse=false, const std::size_t &amountOfRecords=-1) const noexcept
Method for handle sorted records.
Definition Vault.hpp:1085
VaultOperationResult GetRecord(const std::string &key, const T &keyValue, VaultRecordRef &vaultRecordRef) const noexcept
The method for getting a reference to the data inside Vault.
Definition Vault.hpp:691
VaultOperationResult RequestRecords(const VaultRequestType &requestType, const std::string &key, const T &beginKeyValue, const T &endKeyValue, VaultRecordSet &vaultRecordSet, const bool &isIncludeBeginKeyValue, const bool &isIncludeEndKeyValue, const std::size_t &amountOfRecords, const std::function< bool(const VaultRecordRef &)> &requestPredicat) const noexcept
The method for getting the result of the request.
Definition Vault.hpp:304
VaultOperationResult GetRecords(const std::string &key, const T &keyValue, std::vector< VaultRecordRef > &recordsRefs, const std::size_t &amountOfRecords=-1) const noexcept
The method for getting a vector of references to the data inside Vault.
Definition Vault.hpp:746
VaultOperationResult Request(const VaultRequest< Type > &&request, VaultRecordSet &vaultRecordSet) const
A method for complex requests.
Definition Vault.hpp:875
std::unordered_set< VaultRecordSet * > RecordSetsSet
Unordered set with all VaultRecordSet pointers.
Definition Vault.h:112
VaultOperationResult EraseRecords(const std::string &key, const T &keyValue, const std::size_t &amountOfRecords=-1) noexcept
The method for erase records using key and value.
Definition Vault.hpp:1004
A class that provides access to data inside Vault.
Definition VaultRecordRef.h:21
A class for storing query results.
Definition VaultRecordSet.h:14
VaultOperationResult GetRecord(const std::string &key, const T &keyValue, VaultRecordRef &vaultRecordRef) const noexcept
The method for getting a reference to the data inside Vault.
Definition VaultRecordSet.hpp:85
void SortBy(const std::string &key, const F &&func, const bool &isReverse=false, const std::size_t &amountOfRecords=-1) const noexcept
Method for handle sorted records.
Definition VaultRecordSet.hpp:280
VaultOperationResult RequestLess(const std::string &key, const T &keyValue, VaultRecordSet &vaultRecordSet, const std::size_t &amountOfRecords=-1, const std::function< bool(const VaultRecordRef &)> &requestPredicat=DefaultRequestPredicat) const
A method for getting all records that have a value less than keyValue stored by the key key.
Definition VaultRecordSet.hpp:153
VaultOperationResult RequestLessOrEqual(const std::string &key, const T &keyValue, VaultRecordSet &vaultRecordSet, const std::size_t &amountOfRecords=-1, const std::function< bool(const VaultRecordRef &)> &requestPredicat=DefaultRequestPredicat) const
A method for getting all records that have a value less than or equal to keyValue stored by the key k...
Definition VaultRecordSet.hpp:160
VaultOperationResult RequestGreaterOrEqual(const std::string &key, const T &keyValue, VaultRecordSet &vaultRecordSet, const std::size_t &amountOfRecords=-1, const std::function< bool(const VaultRecordRef &)> &requestPredicat=DefaultRequestPredicat) const
A method for getting all records that have a value greater than or equal to keyValue stored by the ke...
Definition VaultRecordSet.hpp:146
VaultOperationResult RequestGreater(const std::string &key, const T &keyValue, VaultRecordSet &vaultRecordSet, const std::size_t &amountOfRecords=-1, const std::function< bool(const VaultRecordRef &)> &requestPredicat=DefaultRequestPredicat) const
A method for getting all records that have a value greater than keyValue stored by the key key.
Definition VaultRecordSet.hpp:139
VaultOperationResult Request(const VaultRequest< Type > &&request, VaultRecordSet &vaultRecordSet) const
A method for complex requests.
Definition VaultRecordSet.hpp:178
VaultOperationResult RequestEqual(const std::string &key, const T &keyValue, VaultRecordSet &vaultRecordSet, const std::size_t &amountOfRecords=-1, const std::function< bool(const VaultRecordRef &)> &requestPredicat=DefaultRequestPredicat) const
A method for getting all records that have a value equal to keyValue stored by the key key.
Definition VaultRecordSet.hpp:132
VaultOperationResult RemoveRecords(const std::string &key, const T &keyValue, const std::size_t &amountOfRecords=-1) noexcept
The method for remove records using key and value.
Definition VaultRecordSet.hpp:255
VaultOperationResult GetRecords(const std::string &key, const T &keyValue, std::vector< VaultRecordRef > &recordsRefs, const std::size_t &amountOfRecords=-1) const noexcept
The method for getting a vector of references to the data inside Vault.
Definition VaultRecordSet.hpp:108
VaultOperationResult RequestInterval(const std::string &key, const T &beginKeyValue, const T &endKeyValue, VaultRecordSet &vaultRecordSet, const bool &isIncludeBeginKeyValue=true, const bool &isIncludeEndKeyValue=true, const std::size_t &amountOfRecords=-1, const std::function< bool(const VaultRecordRef &)> &requestPredicat=DefaultRequestPredicat) const
The method for getting the result of the request.
Definition VaultRecordSet.hpp:167
bool GetIsParentVaultValid() const noexcept
Method for checking the validity of the parent Vault.
Definition VaultRecordSet.cpp:119
VaultOperationResult GetKeyValue(const std::string &key, T &defaultKeyValue) const noexcept
The method for getting a default key value.
Definition VaultRecordSet.hpp:64
@ ParentVaultNotValid
This code is returned when calling VaultRecordSet operations when the parent Vault is not valid.
Structure for storing the results of MVault operations.
Definition VaultOperationResult.h:40