FileSystemIndexer v0.1.2-beta
File System Indexer - An API to index files from your system
Loading...
Searching...
No Matches
FSI_Indexer.hpp
Go to the documentation of this file.
1#ifndef INCLUDE_LIB_FSI_INDEXER_HPP_
2#define INCLUDE_LIB_FSI_INDEXER_HPP_
3
4#include <string>
5#include <vector>
6#include <thread>
7
9
10#define __FSI_SUCCESS 0
11#define __FSI_INDEXERERR_CHECK(e) \
12 (e.raise || e.fatal) && e.code != __FSI_SUCCESS
13
14namespace fsi
15{
16 namespace codes
17 {
18 constexpr int ERROR_ADDITION_FAIL = 1;
19 constexpr int ERROR_ERASE_FAIL = 2;
20 constexpr int ERROR_INVALID_PATH = 3;
21 constexpr int ERROR_EMPTY_VALUE = 4;
22 }
23
30
31 typedef struct IndexerFoundInfo
32 {
33 std::string path; // /home/user/projects, ...
34 std::string name; // main, ...
35 std::string extension; // .txt, .png, .cpp, ...
36 std::string extType; // Audio, Image, Text, Code, Other, ...
37
38 utils::TimeUtils_DateTime lastModDT; // Last modified path
40
41 typedef struct IndexerInfo
42 {
43 std::string id; // Optional
44 std::string path; // Dir to add; (Indexer::addInfo())
45 // OR dir to start adding; (Indexer::addExtendedInfo())
46
47 IndexerPathType pathType; // Path type (Dir, File, SymLink, Others)
49
50 typedef struct IndexerError
51 {
52 std::string message; // Message of error
53
54 int code = __FSI_SUCCESS; // Return code (Default is `__FSI_SUCCESS` or `0`)
55
56 bool fatal = false; // Should it count as an important error? Controlled by the developer
57 bool raise = true; // Should raise an exception? Controlled by the developer
59
60 class Indexer
61 {
62 private:
63 std::string id;
64
65 std::vector<IndexerInfo> indexerInfo;
66
68
69 protected:
70 /**
71 * @brief Get if path from function param is a:
72 * * File
73 * * Dir
74 * * SymLink
75 * * Others
76 * @param path Path to get the type from
77 * @return IndexerPathType The type of the function param
78 */
79 virtual IndexerPathType __getPathType(const std::string &path) const;
80
81 /**
82 * @brief Iterate all the files and directories (symlinks too) from `path`
83 * @param path Path to iterate
84 * @return A tree of full path in a vector of strings
85 */
86 virtual std::vector<std::string> __iteratePath(const std::string &path);
87
88 /**
89 * @brief Standard non-threaded extended information
90 * * This will not add multiple extended information to be processed
91 * @param info Get the information from the exact path and deeper
92 * @return IndexerError Return error or success from `IndexerError` type
93 */
95
96 /**
97 * @brief Threaded extended information
98 * * This will add multiple extended information to be processed in the future
99 * @param info Get the information from the exact path and deeper
100 * @return IndexerError Return error or success from `IndexerError` type
101 */
103
104 /**
105 * @brief Search all matching path or ID that contains `path`
106 * @param path Path or ID to search
107 * @return A vector of all the found `path`s
108 */
109 virtual std::vector<std::string> __searchMatchingStandard(const std::string &find);
110
111 /**
112 * @brief Search all matching path or ID that contains `path` using parallelization with multiple CPU threads
113 * @param path Path or ID to search
114 * @return A vector of all the found `path`s
115 */
116 virtual std::vector<std::string> __searchMatchingThreaded(const std::string &find);
117
118 /**
119 * @brief Split the paths found to be used in the future in different CPU cores
120 * * Split the vector in to tiny vectors depending on how many CPU cores the hardware has
121 * @param paths The paths to split according to CPU cores
122 * @return Return the splitted paths
123 */
124 static std::vector<std::vector<std::string>> __splitPathByCores(const std::vector<std::string> &paths)
125 {
126 const unsigned int threads = std::max(1u, std::thread::hardware_concurrency());
127
128 std::vector<std::vector<std::string>> chunks(threads);
129
130 for (size_t i = 0; i < paths.size(); ++i)
131 chunks[i % threads].push_back(paths[i]);
132
133 return chunks;
134 }
135
136 public:
137 Indexer(const std::string &id, const bool threadsImpl = false);
138 ~Indexer();
139
140 /**
141 * @brief Add the exact path to have the info from
142 * @param info Get the information from the exact path
143 * @return IndexerError Return error or success from `IndexerError` type
144 */
145 IndexerError addInfo(const IndexerInfo &info);
146
147 /**
148 * @brief Add the exact path and the sub-paths to have the info from;
149 * * Use `Indexer::__addExtendedInfoStandard()` or `Indexer::__addExtendedInfoThreaded()` if threads are disabled or enabled
150 * @param info Get the information from the exact path and deeper
151 * @return IndexerError Return error or success from `IndexerError` type
152 */
154
155 /**
156 * @brief Remove information from its ID or full path
157 * @param searcher Search for the path or ID and remove them from the indexer
158 * @return IndexerError Return error or success from `IndexerError` type
159 */
160 IndexerError removeInfo(const std::string &searcher);
161
162 /**
163 * @brief Find the index from the vector database from the indexer
164 * @param toFind Search for the path or ID and remove them from the indexer
165 * @return IndexerInfo Find the indexed info from the indexer "data base"
166 */
167 IndexerInfo findIndex(const std::string &toFind);
168
169 /**
170 * @brief Get when the file, dir or symlink was last modified
171 * @param path Path to get date time information
172 * @return Date time info
173 */
174 utils::TimeUtils_DateTime getFileDTInfo(const std::string &path);
175
176 /**
177 * @brief Search for an exact matching path or ID;
178 * @param find Get the ID or path to find
179 * @return The first match found
180 */
181 std::string searchExactMatching(const std::string &find);
182
183 /**
184 * @brief Either use threaded or unthreaded search matching
185 * * Use `Indexer::__searchMatchingThreaded()` or `Indexer::__searchMatchingStandard()`
186 * @param path Path to read the data to search
187 * @return All the data found in a vector of strings
188 */
189 std::vector<std::string> searchMatching(const std::string &path);
190
191 /**
192 * @brief Return indexer information from this object
193 * @return Get all the indexed information that was added
194 */
195 std::vector<IndexerInfo> getIndexerInfo() const;
196
197 /**
198 * @brief Get all the indexed paths from the vector indexes
199 * @return All the indexed paths in a vector
200 */
201 std::vector<std::string> getIndexPaths() const;
202
203 /**
204 * @brief Get this object's ID
205 * @return Object ID
206 */
207 std::string getID() const;
208 };
209}
210
211#endif // INCLUDE_LIB_FSI_INDEXER_HPP_
#define __FSI_SUCCESS
virtual std::vector< std::string > __searchMatchingStandard(const std::string &find)
Search all matching path or ID that contains path.
virtual IndexerPathType __getPathType(const std::string &path) const
Get if path from function param is a:
IndexerInfo findIndex(const std::string &toFind)
Find the index from the vector database from the indexer.
std::vector< IndexerInfo > indexerInfo
static std::vector< std::vector< std::string > > __splitPathByCores(const std::vector< std::string > &paths)
Split the paths found to be used in the future in different CPU cores.
IndexerError addInfo(const IndexerInfo &info)
Add the exact path to have the info from.
IndexerError removeInfo(const std::string &searcher)
Remove information from its ID or full path.
utils::TimeUtils_DateTime getFileDTInfo(const std::string &path)
Get when the file, dir or symlink was last modified.
std::vector< std::string > searchMatching(const std::string &path)
Either use threaded or unthreaded search matching.
Indexer(const std::string &id, const bool threadsImpl=false)
std::vector< std::string > getIndexPaths() const
Get all the indexed paths from the vector indexes.
std::string searchExactMatching(const std::string &find)
Search for an exact matching path or ID;.
virtual IndexerError __addExtendedInfoThreaded(const IndexerInfo &info)
Threaded extended information.
std::string getID() const
Get this object's ID.
IndexerError addExtendedInfo(const IndexerInfo &info)
Add the exact path and the sub-paths to have the info from;.
std::vector< IndexerInfo > getIndexerInfo() const
Return indexer information from this object.
virtual std::vector< std::string > __iteratePath(const std::string &path)
Iterate all the files and directories (symlinks too) from path.
virtual IndexerError __addExtendedInfoStandard(const IndexerInfo &info)
Standard non-threaded extended information.
virtual std::vector< std::string > __searchMatchingThreaded(const std::string &find)
Search all matching path or ID that contains path using parallelization with multiple CPU threads.
std::string id
constexpr int ERROR_ADDITION_FAIL
constexpr int ERROR_INVALID_PATH
constexpr int ERROR_ERASE_FAIL
constexpr int ERROR_EMPTY_VALUE
struct fsi::IndexerFoundInfo IndexerFoundInfo
struct fsi::IndexerInfo IndexerInfo
struct fsi::IndexerError IndexerError
IndexerPathType
std::string message
utils::TimeUtils_DateTime lastModDT
std::string path
IndexerPathType pathType