FileSystemIndexer v0.1.2-beta
File System Indexer - An API to index files from your system
Loading...
Searching...
No Matches
fsi::Indexer Class Reference

#include <FSI_Indexer.hpp>

Collaboration diagram for fsi::Indexer:

Public Member Functions

 Indexer (const std::string &id, const bool threadsImpl=false)
 ~Indexer ()
IndexerError addExtendedInfo (const IndexerInfo &info)
 Add the exact path and the sub-paths to have the info from;.
IndexerError addInfo (const IndexerInfo &info)
 Add the exact path to have the info from.
IndexerInfo findIndex (const std::string &toFind)
 Find the index from the vector database from the indexer.
utils::TimeUtils_DateTime getFileDTInfo (const std::string &path)
 Get when the file, dir or symlink was last modified.
std::string getID () const
 Get this object's ID.
std::vector< IndexerInfogetIndexerInfo () const
 Return indexer information from this object.
std::vector< std::string > getIndexPaths () const
 Get all the indexed paths from the vector indexes.
IndexerError removeInfo (const std::string &searcher)
 Remove information from its ID or full path.
std::string searchExactMatching (const std::string &find)
 Search for an exact matching path or ID;.
std::vector< std::string > searchMatching (const std::string &path)
 Either use threaded or unthreaded search matching.

Protected Member Functions

virtual IndexerError __addExtendedInfoStandard (const IndexerInfo &info)
 Standard non-threaded extended information.
virtual IndexerError __addExtendedInfoThreaded (const IndexerInfo &info)
 Threaded extended information.
virtual IndexerPathType __getPathType (const std::string &path) const
 Get if path from function param is a:
virtual std::vector< std::string > __iteratePath (const std::string &path)
 Iterate all the files and directories (symlinks too) from path.
virtual std::vector< std::string > __searchMatchingStandard (const std::string &find)
 Search all matching path or ID that contains path.
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.

Static Protected Member Functions

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.

Private Attributes

std::string id
std::vector< IndexerInfoindexerInfo
bool threadsImpl

Detailed Description

Definition at line 60 of file FSI_Indexer.hpp.

Constructor & Destructor Documentation

◆ Indexer()

fsi::Indexer::Indexer ( const std::string & id,
const bool threadsImpl = false )

Definition at line 167 of file FSI_Indexer.cpp.

169 { }
std::string id

References Indexer(), id, and threadsImpl.

Referenced by Indexer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~Indexer()

fsi::Indexer::~Indexer ( )

Definition at line 172 of file FSI_Indexer.cpp.

173 { }

Member Function Documentation

◆ __addExtendedInfoStandard()

IndexerError fsi::Indexer::__addExtendedInfoStandard ( const IndexerInfo & info)
protectedvirtual

Standard non-threaded extended information.

  • This will not add multiple extended information to be processed
    Parameters
    infoGet the information from the exact path and deeper
    Returns
    IndexerError Return error or success from IndexerError type

Definition at line 69 of file FSI_Indexer.cpp.

70 {
71 IndexerError error = this->addInfo(info);
72 IndexerInfo totalInfo = info;
73
74 if (__FSI_INDEXERERR_CHECK(error))
75 return error;
76
77 const std::vector<std::string> iteratedPaths = this->__iteratePath(info.path);
78
79 for (const auto &x : iteratedPaths)
80 {
81 totalInfo.pathType = this->__getPathType(totalInfo.path);
82 totalInfo.path = x;
83
84 this->indexerInfo.emplace_back(totalInfo);
85 }
86
87 return error;
88 }
#define __FSI_INDEXERERR_CHECK(e)
virtual IndexerPathType __getPathType(const std::string &path) const
Get if path from function param is a:
std::vector< IndexerInfo > indexerInfo
IndexerError addInfo(const IndexerInfo &info)
Add the exact path to have the info from.
virtual std::vector< std::string > __iteratePath(const std::string &path)
Iterate all the files and directories (symlinks too) from path.
struct fsi::IndexerInfo IndexerInfo
struct fsi::IndexerError IndexerError

References __addExtendedInfoStandard(), __FSI_INDEXERERR_CHECK, __getPathType(), __iteratePath(), addInfo(), indexerInfo, fsi::IndexerInfo::path, and fsi::IndexerInfo::pathType.

Referenced by __addExtendedInfoStandard(), and addExtendedInfo().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ __addExtendedInfoThreaded()

IndexerError fsi::Indexer::__addExtendedInfoThreaded ( const IndexerInfo & info)
protectedvirtual

Threaded extended information.

  • This will add multiple extended information to be processed in the future
    Parameters
    infoGet the information from the exact path and deeper
    Returns
    IndexerError Return error or success from IndexerError type

Definition at line 90 of file FSI_Indexer.cpp.

91 {
92 IndexerError error = this->addInfo(info);
93
94 if (__FSI_INDEXERERR_CHECK(error))
95 return error;
96
97 const auto paths = this->__iteratePath(info.path);
98 const auto &chunks = this->__splitPathByCores(paths);
99
100 std::vector<std::future<std::vector<IndexerInfo>>> tasks;
101
102 for (auto &chunk : chunks)
103 {
104 if (chunk.empty())
105 continue;
106
107 tasks.emplace_back(std::async(std::launch::async,
108 [&, chunk]() -> std::vector<IndexerInfo>
109 {
110 std::vector<IndexerInfo> local;
111
112 local.reserve(chunk.size());
113
114 for (const auto &p : chunk)
115 {
116 IndexerInfo tmp = info;
117 tmp.path = p;
118 tmp.pathType = this->__getPathType(p);
119 local.emplace_back(std::move(tmp));
120 }
121
122 return local;
123 }));
124 }
125
126 // Merging phase
127 for (auto &t : tasks)
128 {
129 auto local = t.get();
130
131 this->indexerInfo.insert(
132 this->indexerInfo.end(),
133 std::make_move_iterator(local.begin()),
134 std::make_move_iterator(local.end()));
135 }
136
137 return error;
138 }
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.
std::string path

References __addExtendedInfoThreaded(), __FSI_INDEXERERR_CHECK, __getPathType(), __iteratePath(), __splitPathByCores(), addInfo(), indexerInfo, fsi::IndexerInfo::path, and fsi::IndexerInfo::pathType.

Referenced by __addExtendedInfoThreaded(), and addExtendedInfo().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ __getPathType()

IndexerPathType fsi::Indexer::__getPathType ( const std::string & path) const
protectedvirtual

Get if path from function param is a:

  • File
  • Dir
  • SymLink
  • Others
    Parameters
    pathPath to get the type from
    Returns
    IndexerPathType The type of the function param

Definition at line 20 of file FSI_Indexer.cpp.

21 {
22 if (fs::is_directory(path)) return IndexerPathType::Directory;
23 else if (fs::is_symlink(path)) return IndexerPathType::SymLink;
24
26 }

References fsi::Directory, fsi::File, and fsi::SymLink.

Referenced by __addExtendedInfoStandard(), __addExtendedInfoThreaded(), and addInfo().

Here is the caller graph for this function:

◆ __iteratePath()

std::vector< std::string > fsi::Indexer::__iteratePath ( const std::string & path)
protectedvirtual

Iterate all the files and directories (symlinks too) from path.

Parameters
pathPath to iterate
Returns
A tree of full path in a vector of strings

Definition at line 140 of file FSI_Indexer.cpp.

141 {
142 std::vector<std::string> paths;
143
144 CVEC tmpPathsVec = cvec_init(-1, sizeof(char*));
145
146 fsi_walk(&tmpPathsVec, path.c_str());
147
148 for (size_t i = 0; i < tmpPathsVec.size; ++i)
149 {
150 char* p = *(char**)cvec_get(&tmpPathsVec, i);
151
152 if (p)
153 {
154 paths.emplace_back(p, strlen(p));
155
156 FSI_FREE(p);
157 }
158 }
159
160 cvec_destroy(&tmpPathsVec);
161
162 return paths;
163 }
void fsi_walk(CVEC *vec, const char *path)
Walk to every directory and file and set vec to a CVEC value.
#define FSI_FREE(x)

References __iteratePath(), FSI_FREE, and fsi_walk().

Referenced by __addExtendedInfoStandard(), __addExtendedInfoThreaded(), and __iteratePath().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ __searchMatchingStandard()

std::vector< std::string > fsi::Indexer::__searchMatchingStandard ( const std::string & find)
protectedvirtual

Search all matching path or ID that contains path.

Parameters
pathPath or ID to search
Returns
A vector of all the found paths

Definition at line 28 of file FSI_Indexer.cpp.

29 {
30 if (find.empty())
31 return {};
32
33 std::vector<std::string> vec;
34
35 for (const auto &x : this->indexerInfo)
36 {
37 if (x.path.find(find) != std::string::npos)
38 vec.emplace_back(x.path);
39 }
40
41 return vec;
42 }

References indexerInfo.

Referenced by searchMatching().

Here is the caller graph for this function:

◆ __searchMatchingThreaded()

std::vector< std::string > fsi::Indexer::__searchMatchingThreaded ( const std::string & find)
protectedvirtual

Search all matching path or ID that contains path using parallelization with multiple CPU threads.

Parameters
pathPath or ID to search
Returns
A vector of all the found paths

Definition at line 44 of file FSI_Indexer.cpp.

45 {
46 if (find.empty())
47 return {};
48
49 std::vector<std::string> result;
50 std::mutex mutex;
51
52 std::for_each(
53 std::execution::par,
54 indexerInfo.begin(),
55 indexerInfo.end(),
56 [&](const auto& x)
57 {
58 if (x.path.find(find) != std::string::npos)
59 {
60 std::lock_guard<std::mutex> lock(mutex);
61 result.emplace_back(x.path);
62 }
63 }
64 );
65
66 return result;
67 }

References indexerInfo.

Referenced by searchMatching().

Here is the caller graph for this function:

◆ __splitPathByCores()

std::vector< std::vector< std::string > > fsi::Indexer::__splitPathByCores ( const std::vector< std::string > & paths)
inlinestaticprotected

Split the paths found to be used in the future in different CPU cores.

  • Split the vector in to tiny vectors depending on how many CPU cores the hardware has
    Parameters
    pathsThe paths to split according to CPU cores
    Returns
    Return the splitted paths

Definition at line 124 of file FSI_Indexer.hpp.

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 }

Referenced by __addExtendedInfoThreaded().

Here is the caller graph for this function:

◆ addExtendedInfo()

IndexerError fsi::Indexer::addExtendedInfo ( const IndexerInfo & info)

Add the exact path and the sub-paths to have the info from;.

Definition at line 175 of file FSI_Indexer.cpp.

176 {
177 if (this->threadsImpl)
178 return this->__addExtendedInfoThreaded(info);
179
180 return this->__addExtendedInfoStandard(info);
181 }
virtual IndexerError __addExtendedInfoThreaded(const IndexerInfo &info)
Threaded extended information.
virtual IndexerError __addExtendedInfoStandard(const IndexerInfo &info)
Standard non-threaded extended information.

References __addExtendedInfoStandard(), __addExtendedInfoThreaded(), and threadsImpl.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addInfo()

IndexerError fsi::Indexer::addInfo ( const IndexerInfo & info)

Add the exact path to have the info from.

Parameters
infoGet the information from the exact path
Returns
IndexerError Return error or success from IndexerError type

Definition at line 198 of file FSI_Indexer.cpp.

199 {
200 IndexerError error;
201 IndexerInfo totalInfo = info;
202
203 // Do checks before adding
204 if (totalInfo.path.empty())
205 {
207 error.message = "Path is empty";
208 error.fatal = false;
209 error.raise = false;
210
211 return error;
212 }
213
214 if (!fs::exists(totalInfo.path))
215 {
217 error.message = ciof::format("Unknown path `%1`; No such file or directory exists", totalInfo.path);
218 error.fatal = true;
219
220 return error;
221 }
222
223
224 // Set path type
225 totalInfo.pathType = this->__getPathType(totalInfo.path);
226
227 // Add final information
228
229 this->indexerInfo.emplace_back(totalInfo);
230
231 return error;
232 }
constexpr int ERROR_ADDITION_FAIL
constexpr int ERROR_INVALID_PATH
IndexerPathType pathType

References __getPathType(), fsi::IndexerError::code, fsi::codes::ERROR_ADDITION_FAIL, fsi::codes::ERROR_INVALID_PATH, fsi::IndexerError::fatal, indexerInfo, fsi::IndexerError::message, fsi::IndexerInfo::path, fsi::IndexerInfo::pathType, and fsi::IndexerError::raise.

Referenced by __addExtendedInfoStandard(), and __addExtendedInfoThreaded().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ findIndex()

IndexerInfo fsi::Indexer::findIndex ( const std::string & toFind)

Find the index from the vector database from the indexer.

Parameters
toFindSearch for the path or ID and remove them from the indexer
Returns
IndexerInfo Find the indexed info from the indexer "data base"

Definition at line 234 of file FSI_Indexer.cpp.

235 {
236 for (const auto &x : this->indexerInfo)
237 {
238 if (x.path == toFind || x.id == toFind)
239 return x;
240 }
241
242 return { };
243 }

References indexerInfo.

Referenced by main(), and searchExactMatching().

Here is the caller graph for this function:

◆ getFileDTInfo()

utils::TimeUtils_DateTime fsi::Indexer::getFileDTInfo ( const std::string & path)

Get when the file, dir or symlink was last modified.

Parameters
pathPath to get date time information
Returns
Date time info

Definition at line 275 of file FSI_Indexer.cpp.

276 {
277 const fs::file_time_type &lastWriteTime = fs::last_write_time(path);
278
279 return utils::fsClockDataToDT(lastWriteTime);
280 }
TimeUtils_DateTime fsClockDataToDT(const fs::file_time_type &time)
Set std::filesystem clock data to an actual usable Date and Time format.

References fsi::utils::fsClockDataToDT().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getID()

std::string fsi::Indexer::getID ( ) const

Get this object's ID.

Returns
Object ID

Definition at line 295 of file FSI_Indexer.cpp.

296 { return this->id; }

References id.

◆ getIndexerInfo()

std::vector< IndexerInfo > fsi::Indexer::getIndexerInfo ( ) const

Return indexer information from this object.

Returns
Get all the indexed information that was added

Definition at line 282 of file FSI_Indexer.cpp.

283 { return this->indexerInfo; }

References indexerInfo.

◆ getIndexPaths()

std::vector< std::string > fsi::Indexer::getIndexPaths ( ) const

Get all the indexed paths from the vector indexes.

Returns
All the indexed paths in a vector

Definition at line 285 of file FSI_Indexer.cpp.

286 {
287 std::vector<std::string> additions;
288
289 for (const auto &x : this->indexerInfo)
290 { additions.emplace_back(x.path); }
291
292 return additions;
293 }

References indexerInfo.

◆ removeInfo()

IndexerError fsi::Indexer::removeInfo ( const std::string & searcher)

Remove information from its ID or full path.

Parameters
searcherSearch for the path or ID and remove them from the indexer
Returns
IndexerError Return error or success from IndexerError type

Definition at line 245 of file FSI_Indexer.cpp.

246 {
247 IndexerError error;
248
249 // Do checks
250
251 if (searcher.empty())
252 {
254 error.message = "Empty value to remove from the Indexer Information list";
255 error.fatal = false;
256 error.raise = false;
257
258 return error;
259 }
260
261 // Erase the found object (IndexerInfo) value by ID or Path
262 this->indexerInfo.erase(
263 std::remove_if(
264 this->indexerInfo.begin(),
265 this->indexerInfo.end(),
266 [&](const IndexerInfo &info) {
267 return (info.id == searcher || info.path == searcher);
268 }),
269 this->indexerInfo.end()
270 );
271
272 return error;
273 }
constexpr int ERROR_EMPTY_VALUE

References fsi::IndexerError::code, fsi::codes::ERROR_EMPTY_VALUE, fsi::IndexerError::fatal, indexerInfo, fsi::IndexerError::message, and fsi::IndexerError::raise.

◆ searchExactMatching()

std::string fsi::Indexer::searchExactMatching ( const std::string & find)

Search for an exact matching path or ID;.

Parameters
findGet the ID or path to find
Returns
The first match found

Definition at line 183 of file FSI_Indexer.cpp.

184 {
185 if (find.empty())
186 return "";
187
188 return this->findIndex(find).path;
189 }
IndexerInfo findIndex(const std::string &toFind)
Find the index from the vector database from the indexer.

References findIndex(), and fsi::IndexerInfo::path.

Here is the call graph for this function:

◆ searchMatching()

std::vector< std::string > fsi::Indexer::searchMatching ( const std::string & path)

Either use threaded or unthreaded search matching.

Definition at line 191 of file FSI_Indexer.cpp.

192 {
193 if (this->threadsImpl) return this->__searchMatchingThreaded(find);
194
195 return this->__searchMatchingStandard(find);
196 }
virtual std::vector< std::string > __searchMatchingStandard(const std::string &find)
Search all matching path or ID that contains path.
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.

References __searchMatchingStandard(), __searchMatchingThreaded(), and threadsImpl.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ id

std::string fsi::Indexer::id
private

Definition at line 63 of file FSI_Indexer.hpp.

Referenced by Indexer(), and getID().

◆ indexerInfo

◆ threadsImpl

bool fsi::Indexer::threadsImpl
private

Definition at line 67 of file FSI_Indexer.hpp.

Referenced by Indexer(), addExtendedInfo(), and searchMatching().


The documentation for this class was generated from the following files: