From 0972eec6f5af5fbf7659fbb179cb54452cba419c Mon Sep 17 00:00:00 2001 From: BadiRodriguez <166084491+Badi-Rodriguez@users.noreply.github.com> Date: Sun, 14 Jul 2024 14:59:44 -0500 Subject: [PATCH 1/2] Thread model implementation I made it --- .idea/workspace.xml | 61 +++++++++++++++------------ src/app/SearchEngine/SearchEngine.cpp | 50 ++++++++++++++++++++++ src/app/SearchEngine/SearchEngine.h | 16 +++++-- 3 files changed, 96 insertions(+), 31 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 66023c0..f10d23b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -38,6 +38,19 @@ + + + + + + + + + + + + + @@ -59,26 +72,26 @@ - { + "keyToString": { + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "RunOnceActivity.cidr.known.project.marker": "true", + "WebServerToolWindowFactoryState": "false", + "cf.first.check.clang-format": "false", + "cidr.known.project.marker": "true", + "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", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "settings.editor.selected.configurable": "preferences.pluginManager", + "structure.view.defaults.are.configured": "true", + "vue.rearranger.settings.migration": "true" } -}]]> +} @@ -87,12 +100,7 @@ - - - - - + @@ -123,9 +131,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 From f9acf6ac3fd914041e1aafce42af46f77342f700 Mon Sep 17 00:00:00 2001 From: BadiRodriguez <166084491+Badi-Rodriguez@users.noreply.github.com> Date: Sun, 14 Jul 2024 15:10:22 -0500 Subject: [PATCH 2/2] Prepare to ammend this Because I did not test it lmao --- src/app/TriePrefix/TrieNode.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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