-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexingProcessorMapPerAndMerge.cpp
More file actions
189 lines (170 loc) · 6.36 KB
/
indexingProcessorMapPerAndMerge.cpp
File metadata and controls
189 lines (170 loc) · 6.36 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
#include <iostream>
#include <unordered_map>
#include <filesystem>
#include <thread>
#include <windows.h>
#include <algorithm>
#include <unordered_set>
#include <mutex>
#include <queue>
#include <fstream>
namespace fs = std::filesystem;
/*
Plan:
start with cmd line input from for directory and worker thread num
N + 1 total threads onethread for searching file path
main thread can simply iterate through checking every file extension and spawning threads/queueing threads as necessary
worker threads need to share a slot in memory, so we needa have locks for that
workers need to be able to add to the table every unique word encountered, probably want to use hashmap,
with key of word and value of encounters
IMPORTANT: Case insensitive and ignore punctuation
finished product will output top 10 words and their counts
show what each thread is doing (have outputs for each saying what file they are on)
*/
bool sortbysec(const std::pair<std::string, int> &a,
const std::pair<std::string, int> &b)
{
return (a.second > b.second);
}
// queue of files to deal with
struct WorkerQueue
{
std::mutex mut;
std::queue<fs::path> queue;
};
// indicates when all the workers are finished
std::mutex workersRunningMut;
int workersRunning;
void workerThread(WorkerQueue &wQueue, std::unordered_map<std::string, int> &wordCount)
{
std::string word;
while (1)
{
// try lock until take
if (wQueue.mut.try_lock())
{
//std::cout << "Took Lock on " << std::this_thread::get_id() << std::endl;
// if queue is empty we are done, cleanup and leave
if (wQueue.queue.empty())
{
wQueue.mut.unlock();
//std::cout << "Unlocked Lock on " << std::this_thread::get_id() << std::endl;
// try to take lock on the running workers and -- it so we have accurate count of still running workers
while (1)
{
if (workersRunningMut.try_lock())
{
workersRunning--;
workersRunningMut.unlock();
return;
}
}
}
// grab front of queue path and pop it while we have lock
fs::path path = wQueue.queue.front();
wQueue.queue.pop();
//std::cout << "Unlocked Lock on " << std::this_thread::get_id() << std::endl;
// unlock after we used the queue
wQueue.mut.unlock();
// iterate through file and grab words,
// TODO delimit by anything other than alphanumeric, atm its by whitespace, case insensitive
std::ifstream stream(path.c_str(), std::ios::binary);
while (stream >> word)
{
//fprintf(stderr, "word: %s found %d times.\n", word.c_str(), wordCount[word] + 1);
wordCount[word]++;
}
}
}
}
// main thread to iterate through file system
int searcher(fs::path path, std::unordered_map<std::string, bool> exts, WorkerQueue &wQueue)
{
for (const auto &file : fs::recursive_directory_iterator(path))
{
//checking for extension in valid list
if (exts[file.path().extension().string()])
{
printf("%ls is a valid file\n", file.path().c_str());
// we don't need to lock here since no other threads will work with this until this finishes
wQueue.queue.emplace(file.path());
}
}
return 0;
}
// expect two arguements while command line
// args in order are
// program path numWorkers <space seperated extensions>
int main(int argc, char **argv)
{
std::unordered_map<std::string, bool> extensions;
if (argc < 3)
{
printf("Expects two arguements only received %d\nUsage: indexingProcessor <directoryPath> <workerthread count> <space seperated extensions>\n", argc - 1);
return 1;
}
for (int i = 3; i < argc; i++)
{
// populate map with extension and boolean possibly unordered set is better
printf("Accepted Extension Added %s\n", argv[i]);
extensions[argv[i]] = true;
}
// setup file queue
WorkerQueue wQueue;
// start file searcher
std::thread s(searcher, argv[1], extensions, std::ref(wQueue));
s.join();
// setup worker threads whatever is smaller max hardware or asked for threads, -1 for hardware because we need a main thread
int numThreads = std::min((int)std::thread::hardware_concurrency() - 1, atoi(argv[2]));
//printf("numThreads: %d, hardward thing: %d, argv[2]: %d\n", numThreads, (int)std::thread::hardware_concurrency(), atoi(argv[2]));
// setup thread pool and startup worker threads with maps to put info in
std::vector<std::thread> threadPool;
std::vector<std::unordered_map<std::string, int>> workerThreadCounts(numThreads);
// counter for use on 145, waiting for workers to finish
workersRunning = numThreads;
for (int i = 0; i < numThreads; i++)
{
threadPool.push_back(std::thread(workerThread, std::ref(wQueue), std::ref(workerThreadCounts[i])));
}
for (std::thread &every_thread : threadPool)
{
every_thread.detach();
}
// wait for workers to finish up
while (workersRunning)
{
//fprintf(stderr, "%d\n", workersRunning);
}
// merge maps into one and print it out for now
// TODO: grab top ten
std::unordered_map<std::string, int> retMap;
for (std::unordered_map<std::string, int> map : workerThreadCounts)
{
//retMap.merge(map);
for (auto elem : map)
{
if (retMap[elem.first])
{
retMap[elem.first] += elem.second;
}
else
{
retMap[elem.first] = elem.second;
}
}
//std::cout << "\n\n";
}
std::vector<std::pair<std::string, int>> sortWords;
for (auto elem : retMap)
{
sortWords.push_back(std::pair<std::string, int>(elem.first, elem.second));
//std::cout << "new thing in vector\n";
}
std::sort(sortWords.begin(), sortWords.end(), sortbysec);
for (int i = 0; i < std::min(10, (int)sortWords.size()); i++)
{
std::cout << sortWords.at(i).first << " : " << sortWords.at(i).second << "\n";
}
printf("10 or size of vector: %lld\n", sortWords.size());
return 0;
}