-
Notifications
You must be signed in to change notification settings - Fork 933
/
query_freq.cc
256 lines (222 loc) · 5.19 KB
/
query_freq.cc
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
// answer to http://weibo.com/1915548291/z2UtyzuvQ
// see also http://www.cnblogs.com/baiyanhuang/archive/2012/11/11/2764914.html
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <fstream>
#include <iostream>
#include <unordered_map>
#ifdef STD_STRING
#warning "STD STRING"
#include <string>
using std::string;
#else
#include <ext/vstring.h>
typedef __gnu_cxx::__sso_string string;
#endif
typedef boost::function<void(const string& query, int64_t count)> Callback;
const size_t kMaxSize = 10 * 1000 * 1000;
void processOneInput(const char* file, const Callback& cb)
{
std::unordered_map<string, int64_t> queries;
std::ifstream in(file);
string query;
while (in && !in.eof())
{
queries.clear();
while (getline(in, query))
{
queries[query] += 1;
if (queries.size() > kMaxSize)
{
std::cout << " split\n";
break;
}
}
for (auto kv : queries)
{
cb(kv.first, kv.second);
}
}
}
class LocalSink : boost::noncopyable
{
public:
explicit LocalSink(int idx, int nbuckets)
{
char buf[256];
snprintf(buf, sizeof buf, "shard-%05d-of-%05d", idx, nbuckets);
out.open(buf);
}
void output(const string& query, int64_t count)
{
out << query << '\t' << count << '\n';
}
private:
std::ofstream out;
};
// class RemoteSink;
// template<typename SINK>
class Shuffler : boost::noncopyable
{
public:
explicit Shuffler(int nbuckets)
: buckets_(nbuckets)
{
for (int i = 0; i < nbuckets; ++i)
{
buckets_.push_back(new LocalSink(i, nbuckets));
}
assert(buckets_.size() == static_cast<size_t>(nbuckets));
}
void output(const string& query, int64_t count)
{
int idx = std::hash<string>()(query) % buckets_.size();
buckets_[idx].output(query, count);
}
protected:
boost::ptr_vector<LocalSink> buckets_;
};
void shuffle(int nbuckets, int argc, char* argv[])
{
Shuffler shuffler(nbuckets);
for (int i = 1; i < argc; ++i)
{
std::cout << " processing " << argv[i] << std::endl;
processOneInput(argv[i],
boost::bind(&Shuffler::output, &shuffler, _1, _2));
}
std::cout << "shuffling done" << std::endl;
}
// ======= reduce =======
std::unordered_map<string, int64_t> read_shard(int idx, int nbuckets)
{
std::unordered_map<string, int64_t> queries;
char buf[256];
snprintf(buf, sizeof buf, "shard-%05d-of-%05d", idx, nbuckets);
std::cout << " reading " << buf << std::endl;
{
std::ifstream in(buf);
string line;
while (getline(in, line))
{
size_t tab = line.find('\t');
if (tab != string::npos)
{
int64_t count = strtol(line.c_str() + tab, NULL, 10);
if (count > 0)
{
queries[line.substr(0, tab)] += count;
}
}
}
}
::unlink(buf);
return queries;
}
void reduce(const int nbuckets)
{
for (int i = 0; i < nbuckets; ++i)
{
std::unordered_map<string, int64_t> queries(read_shard(i, nbuckets));
// std::cout << " sorting " << std::endl;
std::vector<std::pair<int64_t, string>> counts;
for (auto entry : queries)
{
counts.push_back(make_pair(entry.second, entry.first));
}
std::sort(counts.begin(), counts.end());
char buf[256];
snprintf(buf, sizeof buf, "count-%05d-of-%05d", i, nbuckets);
std::ofstream out(buf);
std::cout << " writing " << buf << std::endl;
for (auto it = counts.rbegin(); it != counts.rend(); ++it)
{
out << it->first << '\t' << it->second << '\n';
}
}
std::cout << "reducing done" << std::endl;
}
// ======= merge =======
class Source
{
public:
explicit Source(std::ifstream* in)
: in_(in),
count_(0),
query_()
{
}
bool next()
{
string line;
if (getline(*in_, line))
{
size_t tab = line.find('\t');
if (tab != string::npos)
{
count_ = strtol(line.c_str(), NULL, 10);
if (count_ > 0)
{
query_ = line.substr(tab+1);
return true;
}
}
}
return false;
}
bool operator<(const Source& rhs) const
{
return count_ < rhs.count_;
}
void output(std::ostream& out)
{
out << count_ << '\t' << query_ << '\n';
}
private:
std::ifstream* in_;
int64_t count_;
string query_;
};
void merge(const int nbuckets)
{
boost::ptr_vector<std::ifstream> inputs;
std::vector<Source> keys;
for (int i = 0; i < nbuckets; ++i)
{
char buf[256];
snprintf(buf, sizeof buf, "count-%05d-of-%05d", i, nbuckets);
inputs.push_back(new std::ifstream(buf));
Source rec(&inputs.back());
if (rec.next())
{
keys.push_back(rec);
}
::unlink(buf);
}
std::ofstream out("output");
std::make_heap(keys.begin(), keys.end());
while (!keys.empty())
{
std::pop_heap(keys.begin(), keys.end());
keys.back().output(out);
// out.writeRecord(keys.back().data);
if (keys.back().next())
{
std::push_heap(keys.begin(), keys.end());
}
else
{
keys.pop_back();
}
}
std::cout << "merging done\n";
}
int main(int argc, char* argv[])
{
int nbuckets = 10;
shuffle(nbuckets, argc, argv);
reduce(nbuckets);
merge(nbuckets);
}