-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_server.cpp
More file actions
138 lines (126 loc) · 3.85 KB
/
Copy pathsearch_server.cpp
File metadata and controls
138 lines (126 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "search_server.h"
#include "iterator_range.h"
#include "profile.h"
#include <algorithm>
#include <iterator>
#include <sstream>
#include <iostream>
#include <future>
#include <mutex>
#include <thread>
#include <chrono>
const size_t MAX_DOC = 50'000;
const size_t MAX_WORD = 1'000;
const size_t MAX_ALIKE = 10'000;
const size_t MAX_CHAR = 100;
const size_t MAX_QUERY = 500'000;
const size_t MAX_WORD_PER_QUERY = 10;
vector<string> SplitIntoWords(const string& line) {
istringstream words_input(line);
return {istream_iterator<string>(words_input), istream_iterator<string>()};
}
SearchServer::SearchServer(istream& document_input) {
UpdateDocumentBase(document_input);
}
// Update
void SearchServer::UpdateDocumentBaseThread(istream& document_input, InvertedIndex& index)
{
InvertedIndex new_index;
for (string current_document; getline(document_input, current_document); ) {//N
new_index.Add(move(current_document));//K
}
lock_guard<mutex> g(index_mutex);
swap(index, new_index);
}
void SearchServer::UpdateDocumentBase(istream& document_input)
{ //NK = 50'000*1'000=50'000'000
index_mutex.lock();
size_t n = index.GetSize();
index_mutex.unlock();
if (n == 0)
{
UpdateDocumentBaseThread(document_input, index);
}
else
{
futures.push_back(
async(&SearchServer::UpdateDocumentBaseThread, this, ref(document_input), ref(index))
);
}
}
// Search
void SearchServer::AddQueriesStream(istream& query_input, ostream& search_results_output)
{
futures.push_back(
async(&SearchServer::AddQueriesStreamThread, this, ref(query_input), ref(index), ref(search_results_output))
);
this_thread::sleep_for(chrono::milliseconds(5));
}
void SearchServer::AddQueriesStreamThread(
istream& query_input, InvertedIndex& index, ostream& search_results_output)
{
index_mutex.lock();
size_t n = index.GetSize();
index_mutex.unlock();
vector<pair<size_t, size_t> > docid_count(n);
for (string current_query; getline(query_input, current_query);) { //C=500'000
const auto words = SplitIntoWords(current_query);//Q = 10
docid_count.assign(n, {0, 0});
// Calculation
for (const auto& word : words) {//10* N
{
index_mutex.lock();
auto entry = index.Lookup(word);
index_mutex.unlock();
for (const auto stat : entry) {//N
docid_count[stat.first].first = stat.first;
docid_count[stat.first].second += stat.second;
}
}
}
size_t mid = min<size_t>(5, docid_count.size());
partial_sort( //NLogN = 16*50'000 = 750'000
begin(docid_count),
begin(docid_count) + mid,
end(docid_count),
[](pair<size_t, size_t> lhs, pair<size_t, size_t> rhs) {
int64_t lhs_docid = lhs.first;
auto lhs_hit_count = lhs.second;
int64_t rhs_docid = rhs.first;
auto rhs_hit_count = rhs.second;
return make_pair(lhs_hit_count, -lhs_docid) > make_pair(rhs_hit_count, -rhs_docid);
}
);
lock_guard<mutex> g(output_mutex);
search_results_output << current_query << ':';
for (auto [docid, hitcount] : Head(docid_count, 5)) { //N
if (hitcount > 0)
{
search_results_output << " {"
<< "docid: " << docid << ", "
<< "hitcount: " << hitcount << '}';
}
}
search_results_output << '\n';
}
}
// Add
void InvertedIndex::Add(const string& document) {
//docs.push_back(document);
const size_t docid = cur_size++;
unordered_map<string, size_t> temp;
for (const auto& word : SplitIntoWords(document)) { // aba aba aba ddc // 1000
temp[word]++;
}//K
for (const auto& [word, count]: temp)//500
{
index[word].push_back(make_pair(docid, count));
}
}
vector<pair<size_t, size_t> > InvertedIndex::Lookup(const string& word) const {
if (auto it = index.find(word); it != index.end()) {
return it->second;
} else {
return {};
}
}