MVault 0.0.1
Loading...
Searching...
No Matches
Vault.h
1#pragma once
2
3#include <list>
4#include <vector>
5#include <functional>
6
7#include "VaultClasses.h"
8
9#include "Map.h"
10#include "VaultRecord.h"
11#include "VaultRequest.h"
12#include "VaultRecordRef.h"
13#include "VaultParamInput.h"
14#include "VaultOperationResult.h"
15
16#include "Functions/CsvParser.h"
17#include "Functions/PrintAsTable.h"
18
19namespace mvlt
20{
32 class Vault
33 {
34 private:
35
36 // Template of data stored inside Vault
37 VaultRecord RecordTemplate;
38
39 /*
40 Hash map to store records
41 A string with the name of the key is used as the key. All keys are the same as in Vault.
42 The value stores a pointer to std::unordered_multimap<T, VaultRecord*>.
43 The key type is same as the Vault key value type.
44 The value is a pointer to VaultRecord.
45
46 Such a complex structure is needed to quickly, in O(1), search for each key with any type.
47 */
48 mutable DataHashMap VaultHashMapStructure;
49
50 /*
51 Map to store records
52 A string with the name of the key is used as the key. All keys are the same as in Vault.
53 The value stores a pointer to std::multimap<T, VaultRecord*>.
54 The key type is same as the Vault key value type.
55 The value is a pointer to VaultRecord.
56
57 Such a complex structure is necessary in order to quickly, in O(log n),
58 find a set of elements that meet a certain requirement, for example, more than a certain value or less than this value
59 */
60 mutable DataMap VaultMapStructure;
61
62 // Unordered map with keys names and their types
63 std::unordered_map<std::string, std::type_index> KeysTypes;
64
65 // Unordered_map of functions that add a new element to the VaultStructureHashMap
66 std::unordered_map<std::string, std::function<bool(VaultRecord*)>> VaultRecordAdders;
67
68 // Unordered_map of functions which clean up the Vault structure, but do not delete records
69 std::unordered_map<std::string, std::function<void()>> VaultRecordClearers;
70
71 // Unordered_map of functions that erase record from the unordered_map's
72 std::unordered_map<std::string, std::function<void(VaultRecord* newRecord)>> VaultRecordErasers;
73
74 // Unordered_map of functions for getting sorted data.
75 // The key is a string with the name of the key from the Vault.
76 // The std::function is used as the value, in which the lambda function is written at the time of adding the key.
77 // Lambda accepts a function that is called for each record inside. VaultRecordRef and Vault pointer is passed to it.
78 // By default, iteration by records occurs in ascending order.
79 // isReverse parameter is used for the reverse order.
80 std::unordered_map<std::string, std::function<void( std::function<bool(const VaultRecordRef&)> functionToSortedData, Vault* vltPtr, bool isReverse )>> VaultRecordSorters;
81
82 // Unordered_map of functions that copy keys from this to VaultRecordSet
83 std::unordered_map<std::string, std::function<void(Vault* vaultRecordSet)>> VaultKeyCopiers;
84
85 // List to store keys order
86 std::list<std::string> KeysOrder;
87
88 // Set with all unique keys
89 std::set<std::string> UniqueKeys;
90
91 // Vector with all invalid records in last readed file
92 std::vector<std::pair<std::size_t, std::string>> InvalidFileRecords;
93
94 // Recursive mutex for thread safety
95 mutable RecursiveReadWriteMutex RecursiveReadWriteMtx;
96 protected:
97
100 {
101 VaultBase,
103 };
104
107
109 std::unordered_set<VaultRecord*> RecordsSet;
110
112 mutable std::unordered_set<VaultRecordSet*> RecordSetsSet;
113
125 template <class T>
126 VaultOperationResult SetDataToRecord(VaultRecord* dataRecord, const std::string& key, const T& data) noexcept;
127
138 std::unordered_set<VaultRecord *>::iterator RemoveRecord(VaultRecord* recordToErase, bool* wasDeleted) noexcept;
139
158 template <class T>
159 VaultOperationResult RequestRecordsSet(const VaultRequestType& requestType, const std::string& key, const T& beginKeyValue,
160 const T& endKeyValue, std::unordered_set<VaultRecord*>& recordsSet, const bool& isIncludeBeginKeyValue,
161 const bool& isIncludeEndKeyValue, const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef&)>& requestPredicat) const noexcept;
162
181 template <class T>
182 VaultOperationResult RequestRecords(const VaultRequestType& requestType, const std::string& key, const T& beginKeyValue,
184 const bool& isIncludeEndKeyValue, const std::size_t& amountOfRecords, const std::function<bool(const VaultRecordRef&)>& requestPredicat) const noexcept;
185
199 template <class T>
200 VaultOperationResult AddKey(const std::string& key, const T& defaultKeyValue, const bool& isUniqueKey, const bool& isUniqueKeyWithoutLambda,
201 std::function<T(std::size_t, const VaultRecordRef&)> uniqueKeyFunction) noexcept;
202
217 bool ReadFile(const std::string& fileName, const bool& isPreprocessRecord, std::function<void (const std::vector<std::string>&, std::vector<std::string>&)> recordHandler,
218 const char& separator, const bool& isLoadKeys, const std::vector<std::string> userKeys) noexcept;
219
220 public:
221
224
227
229 template <VaultRequestType Type>
230 friend class VaultRequest;
231
233 friend bool operator==(const VaultRecordSet& a, const VaultRecordSet& b);
234
236 Vault() noexcept;
237
244
253
260
269
282
296
317
332
340 bool IsKeyExist(const std::string& key) const noexcept;
341
354
364
370 std::vector<std::string> GetKeys() const noexcept;
371
378
386 bool RemoveKey(const std::string& key) noexcept;
387
444
458
475
491
508 const std::size_t& amountOfRecords = -1, const std::function<bool(const VaultRecordRef&)>& requestPredicat = DefaultRequestPredicat) const noexcept;
509
526 const std::size_t& amountOfRecords = -1, const std::function<bool(const VaultRecordRef&)>& requestPredicat = DefaultRequestPredicat) const noexcept;
527
544 const std::size_t& amountOfRecords = -1, const std::function<bool(const VaultRecordRef&)>& requestPredicat = DefaultRequestPredicat) const noexcept;
545
562 const std::size_t& amountOfRecords = -1, const std::function<bool(const VaultRecordRef&)>& requestPredicat = DefaultRequestPredicat) const noexcept;
563
580 const std::size_t& amountOfRecords = -1, const std::function<bool(const VaultRecordRef&)>& requestPredicat = DefaultRequestPredicat) const noexcept;
581
603 const std::function<bool(const VaultRecordRef&)>& requestPredicat = DefaultRequestPredicat) const noexcept;
604
611 template <VaultRequestType Type>
613
619 void DropVault() noexcept;
620
622 void DropData() noexcept;
623
630
643
659
664 std::size_t Size() const noexcept;
665
677
697 void SortBy(const std::string& key, const F&& func, const bool& isReverse = false, const std::size_t& amountOfRecords = -1) const noexcept;
698
709 std::string ToJson(const bool& isFormat = false, const std::size_t& tabSize = 2, const bool& isUseRecordTemplate = true,
711
716 void PrintVault(const std::size_t& amountOfRecords = -1) const noexcept;
717
727 void PrintAsTable(bool isPrintId = false, const std::size_t& amountOfRecords = -1, std::string primaryKey = "", const bool& isReverse = false,
728 const std::list<std::string> keys = {}) const noexcept;
729
739 bool SaveToFile(const std::string& fileName, const std::vector<std::string> keys = {}, const std::string& separator = ",", const bool& isSaveKey = true) const noexcept;
740
751 bool ReadFile(const std::string& fileName, const char& separator = ',', const bool& isLoadKeys = true, const std::vector<std::string> keys = {}) noexcept;
752
765 bool ReadFile(const std::string& fileName, const char& separator, const bool& isLoadKeys, std::function<void (const std::vector<std::string>&, std::vector<std::string>&)> recordHandler) noexcept;
766
774 std::vector<std::pair<std::size_t, std::string>> GetErrorsInLastReadedFile() const noexcept;
775
777 ~Vault() noexcept;
778
780 friend VaultOperationResult Intersection(const VaultRecordSet& a, const VaultRecordSet& b, VaultRecordSet& res) noexcept;
781 };
782}
Iterator class for all library maps.
Definition Map.h:18
Specialization of class DataContainer, used as a type std::unordered_map<std::string,...
Definition DataContainer.h:287
Specialization of class DataContainer, used as a type std::map<std::string, DataSaver>
Definition DataContainer.h:309
A class for synchronizing threads.
Definition ReadWriteMutex.h:70
A class for storing data with the ability to quickly search for a variety of different keys of any ty...
Definition Vault.h:33
bool EraseRecord(const VaultRecordRef &recordRefToErase) noexcept
Method for deleting a record from a Vault.
Definition Vault.cpp:593
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 noexcept
A method for getting all records that have a value greater than keyValue stored by the key key.
Definition Vault.hpp:817
friend VaultOperationResult Intersection(const VaultRecordSet &a, const VaultRecordSet &b, VaultRecordSet &res) noexcept
Making Intersection function friendly.
Definition VaultRecordSet.cpp:515
std::size_t Size() const noexcept
Method for getting the number of records.
Definition Vault.cpp:604
std::unordered_set< VaultRecord * >::iterator RemoveRecord(VaultRecord *recordToErase, bool *wasDeleted) noexcept
Method for removing a record from a Vault.
Definition Vault.cpp:9
VaultOperationResult GetKeyValue(const std::string &key, T &defaultKeyValue) const noexcept
The method for getting a default key value.
Definition Vault.hpp:641
void DropData() noexcept
A method for deleting all data, but keeping all keys.
Definition Vault.cpp:572
bool GetKeyType(const std::string &key, std::type_index &keyType) const noexcept
The method for getting a key type.
Definition Vault.cpp:308
std::vector< std::pair< std::size_t, std::string > > GetErrorsInLastReadedFile() const noexcept
A method for getting errors in the last read file.
Definition Vault.cpp:946
VaultOperationResult SetDataToRecord(VaultRecord *dataRecord, const std::string &key, const T &data) noexcept
Method for setting a new value in VaultRecord and Vault.
Definition Vault.hpp:13
std::vector< std::string > GetUniqueKeys() const noexcept
The method for getting the unique keys.
Definition Vault.cpp:334
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 noexcept
A method for getting all records that have a value equal to keyValue stored by the key key.
Definition Vault.hpp:806
VaultDerivedClasses
Enum for storing the object type during inheritance.
Definition Vault.h:100
@ VaultRecordSetDerived
VaultRecordSet class.
@ VaultBase
Vault class itself.
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 noexcept
A method for getting all records that have a value less than keyValue stored by the key key.
Definition Vault.hpp:839
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 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 noexcept
A method for getting all records that have a value greater than or equal to keyValue stored by the ke...
Definition Vault.hpp:828
bool RemoveKey(const std::string &key) noexcept
The method for deleting the key.
Definition Vault.cpp:346
void PrintVault(const std::size_t &amountOfRecords=-1) const noexcept
A method for displaying the contents of a Vault on the screen.
Definition Vault.cpp:685
std::string ToJson(const bool &isFormat=false, const std::size_t &tabSize=2, const bool &isUseRecordTemplate=true, const std::string &recordTemplate="Record") const noexcept
Method for saving the contents of the Vault in json.
Definition Vault.cpp:638
std::unordered_set< VaultRecord * > RecordsSet
Unordered set with all VaultRecord pointers.
Definition Vault.h:109
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
std::vector< VaultRecordRef > GetSortedRecords(const std::string &key, const bool &isReverse=false, const std::size_t &amountOfRecords=-1) const noexcept
Method for getting sorted records.
Definition Vault.cpp:614
VaultDerivedClasses VaultDerivedClass
A variable for storing the object type in the Vault inheritance tree.
Definition Vault.h:106
std::vector< std::string > GetKeys() const noexcept
The method for getting all the keys.
Definition Vault.cpp:322
friend VaultRecordRef
Making the VaultRecordRef class friendly so that it has access to the internal members of the Vault c...
Definition Vault.h:223
VaultOperationResult CreateRecord(const std::vector< std::pair< std::string, VaultParamInput > > &params) noexcept
Method to create new VaultRecord.
Definition Vault.cpp:402
bool ReadFile(const std::string &fileName, const bool &isPreprocessRecord, std::function< void(const std::vector< std::string > &, std::vector< std::string > &)> recordHandler, const char &separator, const bool &isLoadKeys, const std::vector< std::string > userKeys) noexcept
A method for reading a csv file and loading data from it into memory.
Definition Vault.cpp:38
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 noexcept
The method for getting the result of the request.
Definition Vault.hpp:861
VaultOperationResult Request(const VaultRequest< Type > &&request, VaultRecordSet &vaultRecordSet) const
A method for complex requests.
Definition Vault.hpp:875
void PrintAsTable(bool isPrintId=false, const std::size_t &amountOfRecords=-1, std::string primaryKey="", const bool &isReverse=false, const std::list< std::string > keys={}) const noexcept
A method for displaying the contents of a Vault as a table on the screen.
Definition Vault.cpp:709
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 noexcept
A method for getting all records that have a value less than or equal to keyValue stored by the key k...
Definition Vault.hpp:850
void DropVault() noexcept
A method for deleting all data and keys.
Definition Vault.cpp:512
Vault() noexcept
Default constructor.
Definition Vault.cpp:176
bool SaveToFile(const std::string &fileName, const std::vector< std::string > keys={}, const std::string &separator=",", const bool &isSaveKey=true) const noexcept
A method for saving data to a table file. The file format is csv.
Definition Vault.cpp:850
std::unordered_set< VaultRecordSet * > RecordSetsSet
Unordered set with all VaultRecordSet pointers.
Definition Vault.h:112
friend bool operator==(const VaultRecordSet &a, const VaultRecordSet &b)
Making VaultRecordSet operator comparison function friendly.
Definition VaultRecordSet.cpp:487
VaultOperationResult AddKey(const std::string &key, const T &defaultKeyValue, const bool &isUniqueKey, const bool &isUniqueKeyWithoutLambda, std::function< T(std::size_t, const VaultRecordRef &)> uniqueKeyFunction) noexcept
Template method to add new key with default value to Vault.
Definition Vault.hpp:355
friend VaultRecordSet
Making the VaultRecordSet class friendly so that it has access to the internal members of the Vault c...
Definition Vault.h:226
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
VaultOperationResult RequestRecordsSet(const VaultRequestType &requestType, const std::string &key, const T &beginKeyValue, const T &endKeyValue, std::unordered_set< VaultRecord * > &recordsSet, 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 in the std::set.
Definition Vault.hpp:118
VaultOperationResult UpdateKey(const std::string &key, const T &defaultKeyValue) noexcept
Template method to update default key value.
Definition Vault.hpp:594
VaultOperationResult AddUniqueKey(const std::string &key) noexcept
Template method to add new unique key to Vault.
Definition Vault.hpp:578
bool IsKeyExist(const std::string &key) const noexcept
The method for getting a default key value.
Definition Vault.cpp:300
A class for transferring data to the Vault.
Definition VaultParamInput.h:18
A class for storing data inside Vault.
Definition VaultRecord.h:18
A class that provides access to data inside Vault.
Definition VaultRecordRef.h:21
A class for storing query results.
Definition VaultRecordSet.h:14
A class for requests to Vault.
Definition VaultRequest.h:16
Structure for storing the results of MVault operations.
Definition VaultOperationResult.h:40