-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmd_build_sbt.cc
370 lines (308 loc) · 10 KB
/
cmd_build_sbt.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// cmd_build_sbt.cc-- build a sequence bloom tree from a topology file
#include <string>
#include <cstdlib>
#include <cstdint>
#include <iostream>
#include <vector>
#include <random>
#include "utilities.h"
#include "bloom_tree.h"
#include "file_manager.h"
#include "support.h"
#include "commands.h"
#include "cmd_build_sbt.h"
using std::string;
using std::vector;
using std::cout;
using std::cerr;
using std::endl;
void BuildSBTCommand::short_description
(std::ostream& s)
{
s << commandName << "-- build a sequence bloom tree from a topology file and leaves" << endl;
}
void BuildSBTCommand::usage
(std::ostream& s,
const string& message)
{
if (!message.empty())
{
s << message << endl;
s << endl;
}
short_description(s);
s << "usage: " << commandName << " <filename> [options]" << endl;
// 123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789
s << " <filename> name of the tree toplogy file" << endl;
s << " --outtree=<filename> name of topology file to write tree consisting of the" << endl;
s << " filters built" << endl;
s << " (by default we derive a name for the resulting topology" << endl;
s << " from the input filename; but by default no tree is)" << endl;
s << " written for --simple, as it would be the same as the" << endl;
s << " input tree)" << endl;
s << " --simple create tree nodes as simple bloom filters" << endl;
s << " (this is the default)" << endl;
s << " --howde equivalent to --determined,brief --rrr" << endl;
s << " --allsome create tree nodes as all/some bloom filters" << endl;
s << " --determined create tree nodes as determined/how bloom filters" << endl;
s << " --determined,brief create tree nodes as determined/how, but only store" << endl;
s << " active bits" << endl;
s << " --uncompressed create the nodes as uncompressed bit vector(s)" << endl;
s << " (this is the default)" << endl;
s << " --rrr create the nodes as rrr-compressed bit vector(s)" << endl;
s << " --roar create the nodes as roar-compressed bit vector(s)" << endl;
}
void BuildSBTCommand::debug_help
(std::ostream& s)
{
s << "--debug= options" << endl;
s << " trackmemory" << endl;
s << " reportrankselect" << endl;
s << " bfsimplify" << endl;
s << " btunload" << endl;
s << " bfcreation" << endl;
s << " bfmanager" << endl;
s << " bvcreation" << endl;
s << " topology" << endl;
s << " load" << endl;
s << " traversal" << endl;
s << " nochildupdate" << endl;
}
void BuildSBTCommand::parse
(int _argc,
char** _argv)
{
int argc;
char** argv;
// defaults
bfKind = bfkind_simple;
compressor = bvcomp_uncompressed;
BloomTree::inhibitBvSimplify = false;
// skip command name
argv = _argv+1; argc = _argc - 1;
if (argc <= 0) chastise ();
//////////
// scan arguments
//////////
for (int argIx=0 ; argIx<argc ; argIx++)
{
string arg = argv[argIx];
string argVal;
if (arg.empty()) continue;
string::size_type argValIx = arg.find('=');
if (argValIx == string::npos) argVal = "";
else argVal = arg.substr(argValIx+1);
// --help, etc.
if ((arg == "--help")
|| (arg == "-help")
|| (arg == "--h")
|| (arg == "-h")
|| (arg == "?")
|| (arg == "-?")
|| (arg == "--?"))
{ usage (cerr); std::exit (EXIT_SUCCESS); }
if ((arg == "--help=debug")
|| (arg == "--help:debug")
|| (arg == "?debug"))
{ debug_help(cerr); std::exit (EXIT_SUCCESS); }
// --outtree=<filename>
if (is_prefix_of (arg, "--outtree="))
{ outTreeFilename = argVal; continue; }
// node type
if ((arg == "--simple")
|| (arg == "--union")
|| (arg == "--cup"))
{ bfKind = bfkind_simple; continue; }
if ((arg == "--howde")
|| (arg == "--HowDe")
|| (arg == "--howdesbt")
|| (arg == "--HowDeSBT"))
{
bfKind = bfkind_determined_brief;
compressor = bvcomp_rrr;
continue;
}
if ((arg == "--allsome")
|| (arg == "--all/some")
|| (arg == "--all-some")
|| (arg == "--all_some"))
{ bfKind = bfkind_allsome; continue; }
if ((arg == "--determined")
|| (arg == "--determinedhow")
|| (arg == "--determined/how")
|| (arg == "--determined-how")
|| (arg == "--determined_how")
|| (arg == "--how/de")
|| (arg == "--how-de")
|| (arg == "--how_de")
|| (arg == "--how/det")
|| (arg == "--how-det")
|| (arg == "--how_det")
|| (arg == "--det")
|| (arg == "--dethow")
|| (arg == "--det/how")
|| (arg == "--det-how")
|| (arg == "--det_how"))
{ bfKind = bfkind_determined; continue; }
if ((arg == "--determined,brief")
|| (arg == "--determinedhow,brief")
|| (arg == "--determined/how,brief")
|| (arg == "--determined-how,brief")
|| (arg == "--determined_how,brief")
|| (arg == "--how/de,brief")
|| (arg == "--how-de,brief")
|| (arg == "--how_de,brief")
|| (arg == "--how/det,brief")
|| (arg == "--how-det,brief")
|| (arg == "--how_det,brief")
|| (arg == "--det,brief")
|| (arg == "--dethow,brief")
|| (arg == "--det/how,brief")
|| (arg == "--det-how,brief")
|| (arg == "--det_how,brief"))
{ bfKind = bfkind_determined_brief; continue; }
// (unadvertised) intersection node type
if ((arg == "--intersect")
|| (arg == "--intersection")
|| (arg == "--cap"))
{ bfKind = bfkind_intersection; continue; }
// compression type
if (arg == "--uncompressed")
{ compressor = bvcomp_uncompressed; continue; }
if ((arg == "--rrr")
|| (arg == "--RRR"))
{ compressor = bvcomp_rrr; continue; }
if ((arg == "--roar")
|| (arg == "--roaring"))
{ compressor = bvcomp_roar; continue; }
// (unadvertised) --tree=<filename>, --topology=<filename>
if ((is_prefix_of (arg, "--tree="))
|| (is_prefix_of (arg, "--intree="))
|| (is_prefix_of (arg, "--topology=")))
{
if (not inTreeFilename.empty())
chastise ("unrecognized option: \"" + arg + "\""
"\ntree topology file was already given as \"" + inTreeFilename + "\"");
inTreeFilename = argVal;
continue;
}
// (unadvertised) --nobvsimplify
if (arg == "--nobvsimplify")
{ BloomTree::inhibitBvSimplify = true; continue; }
// (unadvertised) debug options
if (arg == "--debug")
{ debug.insert ("debug"); continue; }
if (is_prefix_of (arg, "--debug="))
{
for (const auto& field : parse_comma_list(argVal))
debug.insert(to_lower(field));
continue;
}
// unrecognized --option
if (is_prefix_of (arg, "--"))
chastise ("unrecognized option: \"" + arg + "\"");
// <filename>
if (not inTreeFilename.empty())
chastise ("unrecognized option: \"" + arg + "\""
"\ntree topology file was already given as \"" + inTreeFilename + "\"");
inTreeFilename = arg;
}
// sanity checks
if (inTreeFilename.empty())
chastise ("you have to provide a tree topology file");
if ((not outTreeFilename.empty()) and (inTreeFilename.empty()))
chastise ("cannot use --outtree unless you provide the input tree");
if (bfKind == bfkind_intersection)
outTreeFilename = "";
else if ((bfKind != bfkind_simple) and (outTreeFilename.empty()))
{
string bfKindStr = BloomFilter::filter_kind_to_string(bfKind);
outTreeFilename = strip_file_path(inTreeFilename);
string::size_type dotIx = outTreeFilename.find_last_of(".");
if (dotIx == string::npos)
outTreeFilename = outTreeFilename + "." + bfKindStr + ".sbt";
else if (is_suffix_of(outTreeFilename,".sbt"))
outTreeFilename = outTreeFilename.substr(0,dotIx) + "." + bfKindStr + ".sbt";
else
outTreeFilename = outTreeFilename + "." + bfKindStr + ".sbt";
cout << "topology will be written to \"" << outTreeFilename << "\"" << endl;
}
return;
}
int BuildSBTCommand::execute()
{
if (contains(debug,"trackmemory"))
{
FileManager::trackMemory = true;
BloomTree::trackMemory = true;
BloomFilter::trackMemory = true;
BitVector::trackMemory = true;
}
if (contains(debug,"reportrankselect"))
BitVector::reportRankSelect = true;
if (contains(debug,"bfsimplify"))
BloomFilter::reportSimplify = true;
if (contains(debug,"btunload"))
BloomTree::reportUnload = true;
if (contains(debug,"bfcreation"))
BloomFilter::reportCreation = true;
if (contains(debug,"bfmanager"))
BloomFilter::reportManager = true;
if (contains(debug,"bvcreation"))
BitVector::reportCreation = true;
BloomTree* root = BloomTree::read_topology(inTreeFilename);
if (contains(debug,"topology"))
root->print_topology(cerr,/*level*/0,/*format*/topofmt_nodeNames);
vector<BloomTree*> order;
root->post_order(order);
if (contains(debug,"load"))
{
for (const auto& node : order)
node->reportLoad = true;
}
bool hasOnlyChildren = false;
for (const auto& node : order)
{
node->reportSave = true;
node->dbgTraversal = (contains(debug,"traversal"));
node->dbgInhibitChildUpdate = (contains(debug,"nochildupdate"));
if (node->num_children() == 1)
{
hasOnlyChildren = true;
cerr << "warning: " << node->child(0)->bfFilename
<< " is an only child" << endl;
}
}
if (hasOnlyChildren)
fatal ("error: tree contains at least one only child");
switch (bfKind)
{
case bfkind_simple:
root->construct_union_nodes (compressor);
break;
case bfkind_allsome:
root->construct_allsome_nodes (compressor);
break;
case bfkind_determined:
root->construct_determined_nodes (compressor);
break;
case bfkind_determined_brief:
root->construct_determined_brief_nodes (compressor);
break;
case bfkind_intersection: // to assist in debugging
root->construct_intersection_nodes (compressor);
break;
default:
fatal ("error: in BuildSBTCommand::execute():"
" bad filter code: \"" + std::to_string(bfKind) + "\"");
}
if (not outTreeFilename.empty())
{
std::ofstream out(outTreeFilename);
root->print_topology(out);
}
FileManager::close_file(); // make sure the last bloom filter file we
// .. opened for read gets closed
delete root;
return EXIT_SUCCESS;
}