-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIndexer.cpp
More file actions
283 lines (232 loc) · 8.82 KB
/
Copy pathFileIndexer.cpp
File metadata and controls
283 lines (232 loc) · 8.82 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <iostream>
#include <algorithm>
#include <cctype>
#include <chrono>
#include <fstream>
#include <queue>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
// for a given version, calculates the word-level index
class VersionedIndex {
private:
string versionName;
unordered_map<string, int> wordCounts;
public:
VersionedIndex(string version) {
versionName = version;
}
string getVersion(){
return versionName;
}
void addWord(const string& word) {
wordCounts[word]++;
}
int getCount(const string& word) {
if (wordCounts.count(word) > 0) return wordCounts[word];
return 0;
}
vector<pair<string, int>> getTopk(int k){
priority_queue<pair<int, string>, vector<pair<int, string>>, greater<pair<int, string>>> minHeap;
for (auto const& pair : wordCounts) {
minHeap.push({pair.second, pair.first});
if (minHeap.size() > k) minHeap.pop();
}
vector<pair<string, int>> result;
while (!minHeap.empty()) {
result.push_back(pair<string, int> (minHeap.top().second, minHeap.top().first));
minHeap.pop();
}
reverse(result.begin(), result.end());
return result;
}
};
// goes through buffer and updates wordCounts
class Tokenizer {
private:
string cutWord;
public:
Tokenizer() {
cutWord = "";
}
static char toLower(char c) {
if (c >= 'A' && c <= 'Z') {
return c + 32;
}
return c;
}
static void toLower(string& word) {
for (char& c : word) {
c = toLower(c);
}
}
void process(const char* buffer, size_t bytesRead, VersionedIndex& index, bool isEOF) {
for (size_t i = 0; i < bytesRead; ++i) {
char c = buffer[i];
// for every c, if its alphanumeric, add it to cutWord
// if not, that means word ended, so we can add the cutWord to wordCounts
if (isalnum(c)) {
cutWord += toLower(c);
}
else if (cutWord != "") {
index.addWord(cutWord);
cutWord = "";
}
}
// if cutWord has some word, since its EOF, we can safely end that word and add it to wordCounts
if (isEOF && cutWord != "") {
index.addWord(cutWord);
cutWord = "";
}
// if we havent reached EOF, the cut off word(if any) will be stored in the object for next iteration
}
};
// reads file in buffer chunks and sends them to tokenzier to read
class BufferedFileReader {
private:
string filePath;
size_t bufferSizeKB;
public:
BufferedFileReader(string path, size_t kb){
filePath = path;
bufferSizeKB = kb;
}
void read(VersionedIndex& index) {
size_t num_of_bytes = bufferSizeKB * 1024;
ifstream file(filePath);
if (!file.is_open()) {
throw invalid_argument("Could not open file " + filePath);
}
char* buffer = new char[num_of_bytes];
Tokenizer reader;
// if the required buffer size num of bytes have been read, then read them into buffer
// or if required num of bytes were not been able to read, i.e. file ends before fulfilling buffer size,
// read only that many amt of bytes into buffer
while (file.read(buffer, num_of_bytes) || file.gcount() > 0){
reader.process(buffer, file.gcount(), index, file.eof());
}
delete[] buffer;
file.close();
}
};
// gives output according to query asked
class Query {
public:
virtual void execute() = 0;
};
class WordQuery : public Query {
private:
VersionedIndex& index;
string targetWord;
public:
WordQuery(VersionedIndex& idx, string word) : index(idx), targetWord(word) {}
void execute() {
cout << "Version Name: " << index.getVersion() << endl;
cout << "Count: " << index.getCount(targetWord) << endl;
}
};
class DiffQuery : public Query {
private:
VersionedIndex& index1;
VersionedIndex& index2;
string targetWord;
public:
DiffQuery(VersionedIndex& idx1, VersionedIndex& idx2, string word) : index1(idx1), index2(idx2), targetWord(word) {}
void execute() {
int count1 = index1.getCount(targetWord);
int count2 = index2.getCount(targetWord);
cout << "Version Names: " << index1.getVersion() << " and " << index2.getVersion() << endl;
cout << "Difference: " << count2 - count1 << endl;
}
};
class TopKQuery : public Query {
private:
VersionedIndex& index;
int k;
public:
TopKQuery(VersionedIndex& idx, int topK) : index(idx), k(topK) {}
void execute() {
cout << "Version Name: " << index.getVersion() << endl;
cout << "Top " << k << " elements are: " << endl;
vector<pair<string, int>> result = index.getTopk(k);
for (int i = 0; i < k; i++){
cout << result[i].first << " " << result[i].second <<endl;
}
}
};
// finds and converts command line arguments to give data type
template <typename T> T argValue(char** begin, char** end, const string& attribute) {
char** itr = find(begin, end, attribute);
// if attribute found and the has something following it
if (itr != end && ++itr != end) {
stringstream ss(*itr);
T result;
ss >> result;
return result;
}
throw invalid_argument("Invalid argument for " + attribute);
}
int main(int argc, char* argv[]) {
try {
auto start = chrono::high_resolution_clock::now();
size_t kb = argValue<int>(argv, argv + argc, "--buffer");
if (kb < 256 || kb > 1024) throw invalid_argument("Buffer size must be between 256 KB and 1024 KB.");
string queryType = argValue<string>(argv, argv + argc, "--query");
Query* unknownQuery = nullptr;
if (queryType == "word") {
string versionName = argValue<string>(argv, argv + argc, "--version");
VersionedIndex idx(versionName);
string path = argValue<string>(argv, argv + argc, "--file");
BufferedFileReader read(path, kb);
read.read(idx);
string word = argValue<string>(argv, argv + argc, "--word");
Tokenizer::toLower(word);
WordQuery query(idx, word);
unknownQuery = &query;
unknownQuery->execute();
cout << "Buffer Size: " << kb << "KB" << endl;
}
else if (queryType == "diff") {
string version1 = argValue<string>(argv, argv + argc, "--version1");
VersionedIndex idx1(version1);
string version2 = argValue<string>(argv, argv + argc, "--version2");
VersionedIndex idx2(version2);
string path1 = argValue<string>(argv, argv + argc, "--file1");
BufferedFileReader read1(path1, kb);
read1.read(idx1);
string path2 = argValue<string>(argv, argv + argc, "--file2");
BufferedFileReader read2(path2, kb);
read2.read(idx2);
string word = argValue<string>(argv, argv + argc, "--word");
Tokenizer::toLower(word);
DiffQuery query(idx1, idx2, word);
unknownQuery = &query;
unknownQuery->execute();
cout << "Buffer Size: " << kb << "KB" << endl;
}
else if (queryType == "top") {
string versionName = argValue<string>(argv, argv + argc, "--version");
VersionedIndex idx(versionName);
string path = argValue<string>(argv, argv + argc, "--file");
BufferedFileReader read(path, kb);
read.read(idx);
int k = argValue<int>(argv, argv + argc, "--top");
TopKQuery query(idx, k);
unknownQuery = &query;
unknownQuery->execute();
cout << "Buffer Size: " << kb << "KB" << endl;
}
else throw invalid_argument("Invalid query type.");
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> duration = end - start;
cout << "Execution time: " << duration.count() << " seconds\n";
}
catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
return 1;
}
return 0;
}