diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9c88eab..3da4123 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -31,6 +31,19 @@ + + + + + + + + + + + + + @@ -60,7 +73,7 @@ "WebServerToolWindowFactoryState": "false", "cf.first.check.clang-format": "false", "cidr.known.project.marker": "true", - "git-widget-placeholder": "flauta2", + "git-widget-placeholder": "main", "last_opened_file_path": "C:/Users/flauta/progra3/proyecto/progra3/CMakeLists.txt", "node.js.detected.package.eslint": "true", "node.js.detected.package.tslint": "true", @@ -80,12 +93,7 @@ - - - - - + @@ -116,9 +124,8 @@ - - + \ No newline at end of file diff --git a/src/app/SearchEngine/SearchEngine.cpp b/src/app/SearchEngine/SearchEngine.cpp index 672c169..b52b848 100644 --- a/src/app/SearchEngine/SearchEngine.cpp +++ b/src/app/SearchEngine/SearchEngine.cpp @@ -3,3 +3,53 @@ // #include "SearchEngine.h" + +SearchEngine::SearchEngine() {} + +void SearchEngine::addDocument(const std::string &doc) { + int docId = documents.size(); + documents.push_back(doc); + + // Parallelize indexing using std::thread + std::thread indexThread(&SearchEngine::indexDocument, this, docId, doc); + indexThread.join(); // Ensure indexing is complete before proceeding +} + +void SearchEngine::indexDocument(int docId, const std::string &doc) { + std::istringstream stream(doc); + std::string word; + while (stream >> word) { + std::lock_guard guard(indexMutex); // Protect access to index + index[word].push_back(docId); + } +} + +std::vector SearchEngine::search(const std::string &query) { + std::vector results = searchIndex(query); + std::vector foundDocs; + std::vector threads; + std::mutex foundDocsMutex; + + // Use threads to parallelize document retrieval + for (int docId : results) { + threads.emplace_back([&foundDocs, &foundDocsMutex, this, docId]() { + std::lock_guard guard(foundDocsMutex); + foundDocs.push_back(this->documents[docId]); + }); + } + + // Join all threads + for (auto &thread : threads) { + thread.join(); + } + + return foundDocs; +} + +std::vector SearchEngine::searchIndex(const std::string &query) { + std::lock_guard guard(indexMutex); // Protect access to index + if (index.find(query) != index.end()) { + return index[query]; + } + return {}; +} \ No newline at end of file diff --git a/src/app/SearchEngine/SearchEngine.h b/src/app/SearchEngine/SearchEngine.h index a2d54cd..e2910a4 100644 --- a/src/app/SearchEngine/SearchEngine.h +++ b/src/app/SearchEngine/SearchEngine.h @@ -4,11 +4,19 @@ #include #include -//Builder aplicar aca class SearchEngine { - friend class SearchEngineBuilder; - std::string query; - std::vector tags; +public: + SearchEngine(); + void addDocument(const std::string &doc); + std::vector search(const std::string &query); + +private: + std::vector documents; + std::map> index; + std::mutex indexMutex; // Mutex to protect the index + + void indexDocument(int docId, const std::string &doc); + std::vector searchIndex(const std::string &query); }; #endif //PROGRA3_SEARCHENGINE_H diff --git a/src/app/TriePrefix/TrieNode.h b/src/app/TriePrefix/TrieNode.h index efe9d6a..8fe070e 100644 --- a/src/app/TriePrefix/TrieNode.h +++ b/src/app/TriePrefix/TrieNode.h @@ -7,6 +7,8 @@ #include #include #include +#include +#include class TrieNodeVector; @@ -14,6 +16,7 @@ struct TrieNode { TrieNode* childNode[37]; bool wordEnd; TrieNode* movieNode; + std::mutex nodeMutex; TrieNode(); virtual ~TrieNode() = default; @@ -35,5 +38,4 @@ struct TrieNodeVector : public TrieNode { explicit TrieNodeVector(const std::unordered_set &vectorPelis); }; - #endif // PROGRA3_TRIENODE_H \ No newline at end of file