Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions src/app/SearchEngine/SearchEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> guard(indexMutex); // Protect access to index
index[word].push_back(docId);
}
}

std::vector<std::string> SearchEngine::search(const std::string &query) {
std::vector<int> results = searchIndex(query);
std::vector<std::string> foundDocs;
std::vector<std::thread> threads;
std::mutex foundDocsMutex;

// Use threads to parallelize document retrieval
for (int docId : results) {
threads.emplace_back([&foundDocs, &foundDocsMutex, this, docId]() {
std::lock_guard<std::mutex> guard(foundDocsMutex);
foundDocs.push_back(this->documents[docId]);
});
}

// Join all threads
for (auto &thread : threads) {
thread.join();
}

return foundDocs;
}

std::vector<int> SearchEngine::searchIndex(const std::string &query) {
std::lock_guard<std::mutex> guard(indexMutex); // Protect access to index
if (index.find(query) != index.end()) {
return index[query];
}
return {};
}
16 changes: 12 additions & 4 deletions src/app/SearchEngine/SearchEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
#include <unordered_set>
#include <vector>

//Builder aplicar aca
class SearchEngine {
friend class SearchEngineBuilder;
std::string query;
std::vector<std::string> tags;
public:
SearchEngine();
void addDocument(const std::string &doc);
std::vector<std::string> search(const std::string &query);

private:
std::vector<std::string> documents;
std::map<std::string, std::vector<int>> index;
std::mutex indexMutex; // Mutex to protect the index

void indexDocument(int docId, const std::string &doc);
std::vector<int> searchIndex(const std::string &query);
};

#endif //PROGRA3_SEARCHENGINE_H
4 changes: 3 additions & 1 deletion src/app/TriePrefix/TrieNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
#include <fstream>
#include <queue>
#include <string>
#include <mutex>
#include <thread>

class TrieNodeVector;

struct TrieNode {
TrieNode* childNode[37];
bool wordEnd;
TrieNode* movieNode;
std::mutex nodeMutex;

TrieNode();
virtual ~TrieNode() = default;
Expand All @@ -35,5 +38,4 @@ struct TrieNodeVector : public TrieNode {
explicit TrieNodeVector(const std::unordered_set<Movie *> &vectorPelis);
};


#endif // PROGRA3_TRIENODE_H