MVault 1.0.0
Simple c++ database
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& ref)>& requestPredicat) const noexcept
11 {
12 DBG_LOG_ENTER();
13
14 VaultOperationResult res;
15
16 if (GetIsParentVaultValid())
17 {
18 if (&vaultRecordSet == this)
19 {
20 res.IsOperationSuccess = false;
21 res.Key = key;
23 }
24 else
25 {
26 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
27
28 // Remove old data from vaultRecordSet
29 if (vaultRecordSet.ParentVault != ParentVault)
30 {
31 vaultRecordSet.Reset();
32
33 // Set new parent vault to vaultRecordSet
34 vaultRecordSet.ParentVault = ParentVault;
35
36 // Copy keys from this to vaultRecordSet
37 for (auto& keyCopierIt : VaultKeyCopiers)
38 keyCopierIt.second(&vaultRecordSet);
39
40 // Set proper key order
41 vaultRecordSet.KeysOrder = KeysOrder;
42
43 // Set unique keys
44 vaultRecordSet.UniqueKeys = UniqueKeys;
45 }
46 else vaultRecordSet.Clear();
47
48 // Save vaultRecordSet
49 ParentVault->RecordSetsSet.emplace(&vaultRecordSet);
50
51 res = Vault::RequestRecords(requestType, key, beginKeyValue, endKeyValue, vaultRecordSet, isIncludeBeginKeyValue, isIncludeEndKeyValue, amountOfRecords, requestPredicat);
52 vaultRecordSet.ParentVault = ParentVault;
53 }
54 }
55 else
56 {
57 res.Key = key;
58 res.RequestedType = typeid(beginKeyValue);
59 res.IsOperationSuccess = false;
61 }
62
63 return res;
64 }
65
66 template <class T>
68 {
69 DBG_LOG_ENTER();
70
72
73 if (GetIsParentVaultValid())
74 {
75 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
77 }
78 else
79 {
80 res.Key = key;
81 res.RequestedType = typeid(defaultKeyValue);
82 res.IsOperationSuccess = false;
84 }
85
86 return res;
87 }
88
89 template <class T>
91 {
92 DBG_LOG_ENTER();
93
95
96 if (GetIsParentVaultValid())
97 {
98 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
99
101 vaultRecordRef.Vlt = ParentVault;
102 }
103 else
104 {
105 res.Key = key;
106 res.RequestedType = typeid(keyValue);
107 res.IsOperationSuccess = false;
109 }
110
111 return res;
112 }
113
114 template <class T>
115 VaultOperationResult VaultRecordSet::GetRecords(const std::string& key, const T& keyValue, std::vector<VaultRecordRef>& recordsRefs, const std::size_t& amountOfRecords) const noexcept
116 {
117 DBG_LOG_ENTER();
118
120
121 if (GetIsParentVaultValid())
122 {
123 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
124
127 ref.Vlt = ParentVault;
128 }
129 else
130 {
131 res.Key = key;
132 res.RequestedType = typeid(keyValue);
133 res.IsOperationSuccess = false;
135 }
136
137 return res;
138 }
139
140 template <class T>
142 const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef& ref)>& requestPredicat) const noexcept
143 {
144 DBG_LOG_ENTER();
145
146 return RequestRecords(VaultRequestType::Equal, key, keyValue, keyValue, vaultRecordSet, false, false, amountOfRecords, requestPredicat);
147 }
148
149 template <class T>
151 const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef& ref)>& requestPredicat) const noexcept
152 {
153 DBG_LOG_ENTER();
154
155 return RequestRecords(VaultRequestType::Greater, key, keyValue, keyValue, vaultRecordSet, false, false, amountOfRecords, requestPredicat);
156 }
157
158 template <class T>
160 const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef& ref)>& requestPredicat) const noexcept
161 {
162 DBG_LOG_ENTER();
163
164 return RequestRecords(VaultRequestType::GreaterOrEqual, key, keyValue, keyValue, vaultRecordSet, false, false, amountOfRecords, requestPredicat);
165 }
166
167 template <class T>
169 const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef& ref)>& requestPredicat) const noexcept
170 {
171 DBG_LOG_ENTER();
172
173 return RequestRecords(VaultRequestType::Less, key, keyValue, keyValue, vaultRecordSet, false, false, amountOfRecords, requestPredicat);
174 }
175
176 template <class T>
178 const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef& ref)>& requestPredicat) const noexcept
179 {
180 DBG_LOG_ENTER();
181
182 return RequestRecords(VaultRequestType::LessOrEqual, key, keyValue, keyValue, vaultRecordSet, false, false, amountOfRecords, requestPredicat);
183 }
184
185 template <class T>
188 const bool& isIncludeEndKeyValue, const std::size_t& amountOfRecords,
189 const std::function<bool(const VaultRecordRef& ref)>& requestPredicat) const noexcept
190 {
191 DBG_LOG_ENTER();
192
193 return RequestRecords(VaultRequestType::Interval, key, beginKeyValue,
196 }
197
198 template <VaultRequestType Type>
200 {
201 DBG_LOG_ENTER();
202
204
206 {
207 if (&vaultRecordSet == this)
208 {
209 res.IsOperationSuccess = false;
211 }
212 else
213 {
214 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
215
216 // Remove old data from vaultRecordSet
217 if (vaultRecordSet.ParentVault != ParentVault)
218 {
219 vaultRecordSet.Reset();
220
221 // Set new parent vault to vaultRecordSet
222 vaultRecordSet.ParentVault = ParentVault;
223
224 // Copy keys from this to vaultRecordSet
225 for (auto& keyCopierIt : VaultKeyCopiers)
227
228 // Set proper key order
229 vaultRecordSet.KeysOrder = KeysOrder;
230
231 // Set unique keys
232 vaultRecordSet.UniqueKeys = UniqueKeys;
233 }
234 else vaultRecordSet.Clear();
235
236 // Save vaultRecordSet
237 ParentVault->RecordSetsSet.emplace(&vaultRecordSet);
238
240 vaultRecordSet.ParentVault = ParentVault;
241 }
242 }
243 else
244 {
245 res.IsOperationSuccess = false;
247 }
248
249 return res;
250 }
251
252 template <class T>
253 VaultOperationResult VaultRecordSet::RemoveRecord(const std::string& key, const T& keyValue) noexcept
254 {
255 DBG_LOG_ENTER();
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 RemoveRecord<std::string>(\"Key\", \"Value\") or RemoveRecord(\"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 T>
280 VaultOperationResult VaultRecordSet::RemoveRecords(const std::string& key, const T& keyValue, const std::size_t& amountOfRecords) noexcept
281 {
282 DBG_LOG_ENTER();
283
284 static_assert(!std::is_array<T>::value, "It is not possible to use a c array as a key value. \n\
285 If you want to use a string as a key, you must specialize the function with a string. Like this: \n\
286 RemoveRecords<std::string>(\"Key\", \"Value\") or RemoveRecords(\"Key\", std::string(\"Value\"))");
287
289
290 if (GetIsParentVaultValid())
291 {
292 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
294 }
295 else
296 {
297 res.Key = key;
298 res.RequestedType = typeid(keyValue);
299 res.IsOperationSuccess = false;
301 }
302
303 return res;
304 }
305
306 template <class F>
307 void VaultRecordSet::SortBy(const std::string& key, F&& func, const bool& isReverse, const std::size_t& amountOfRecords) const noexcept
308 {
309 DBG_LOG_ENTER();
310
311 if (GetIsParentVaultValid())
312 {
313 ReadLock<RecursiveReadWriteMutex> readLock(ParentVault->RecursiveReadWriteMtx);
315 }
316 }
317}
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:626
VaultOperationResult GetKeyValue(const std::string &key, T &defaultKeyValue) const noexcept
The method for getting a default key value.
Definition Vault.hpp:625
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 &ref)> &requestPredicat) const noexcept
The method for getting the result of the request.
Definition Vault.hpp:272
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:672
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:729
void SortBy(const std::string &key, const std::function< bool(const VaultRecordRef &ref)> &func, const bool &isReverse=false, const std::size_t &amountOfRecords=-1) const noexcept
Method for handle sorted records.
Definition Vault.cpp:677
std::unordered_set< VaultRecordSet * > RecordSetsSet
Unordered set with all VaultRecordSet pointers.
Definition Vault.h:113
VaultOperationResult Request(const VaultRequest< Type > &request, VaultRecordSet &vaultRecordSet) const
A method for complex requests.
Definition Vault.hpp:875
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:1008
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:90
void SortBy(const std::string &key, F &&func, const bool &isReverse=false, const std::size_t &amountOfRecords=-1) const noexcept
Method for handle sorted records.
Definition VaultRecordSet.hpp:307
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 &ref)> &requestPredicat=DefaultRequestPredicat) const noexcept
The method for getting the result of the request.
Definition VaultRecordSet.hpp:186
VaultOperationResult RequestGreaterOrEqual(const std::string &key, const T &keyValue, VaultRecordSet &vaultRecordSet, const std::size_t &amountOfRecords=-1, const std::function< bool(const VaultRecordRef &ref)> &requestPredicat=DefaultRequestPredicat) const noexcept
A method for getting all records that have a value greater than or equal to keyValue stored by the ke...
Definition VaultRecordSet.hpp:159
VaultOperationResult Request(const VaultRequest< Type > &request, VaultRecordSet &vaultRecordSet) const
A method for complex requests.
Definition VaultRecordSet.hpp:199
VaultOperationResult RequestLessOrEqual(const std::string &key, const T &keyValue, VaultRecordSet &vaultRecordSet, const std::size_t &amountOfRecords=-1, const std::function< bool(const VaultRecordRef &ref)> &requestPredicat=DefaultRequestPredicat) const noexcept
A method for getting all records that have a value less than or equal to keyValue stored by the key k...
Definition VaultRecordSet.hpp:177
VaultOperationResult RequestLess(const std::string &key, const T &keyValue, VaultRecordSet &vaultRecordSet, const std::size_t &amountOfRecords=-1, const std::function< bool(const VaultRecordRef &ref)> &requestPredicat=DefaultRequestPredicat) const noexcept
A method for getting all records that have a value less than keyValue stored by the key key.
Definition VaultRecordSet.hpp:168
VaultOperationResult RequestEqual(const std::string &key, const T &keyValue, VaultRecordSet &vaultRecordSet, const std::size_t &amountOfRecords=-1, const std::function< bool(const VaultRecordRef &ref)> &requestPredicat=DefaultRequestPredicat) const noexcept
A method for getting all records that have a value equal to keyValue stored by the key key.
Definition VaultRecordSet.hpp:141
VaultOperationResult RequestGreater(const std::string &key, const T &keyValue, VaultRecordSet &vaultRecordSet, const std::size_t &amountOfRecords=-1, const std::function< bool(const VaultRecordRef &ref)> &requestPredicat=DefaultRequestPredicat) const noexcept
A method for getting all records that have a value greater than keyValue stored by the key key.
Definition VaultRecordSet.hpp:150
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:280
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:115
bool GetIsParentVaultValid() const noexcept
Method for checking the validity of the parent Vault.
Definition VaultRecordSet.cpp:131
VaultOperationResult GetKeyValue(const std::string &key, T &defaultKeyValue) const noexcept
The method for getting a default key value.
Definition VaultRecordSet.hpp:67
@ SameVaultRecordSet
This code is returned when trying to call operations on the VaultRecordSet object itself.
@ 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:44