makeRadiantDb.cpp 2.55 KB
Newer Older
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
1
#include "makeRadiantDb.hpp"
Carsten Kemena's avatar
Carsten Kemena committed
2 3 4 5

using namespace std;
namespace po = boost::program_options;
namespace fs = boost::filesystem;
6

Carsten Kemena's avatar
Carsten Kemena committed
7

Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
8 9 10
/**
 * \brief Write database to file
 * @param database The database to write
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
11
 * @param  outDir The file to write the database into
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
12
 */
Carsten Kemena's avatar
Carsten Kemena committed
13
void
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
14
write2file(std::unordered_map<PrefixType, std::map<CodedSuffix, unsigned short > > &database, const std::string &outDir)
Carsten Kemena's avatar
Carsten Kemena committed
15
{
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
16

Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
17
	std::ofstream fout(outDir, std::ios::out | std::ios::binary);
Carsten Kemena's avatar
Carsten Kemena committed
18 19 20 21
	size_t val= database.size();
	fout.write((char*)&val, sizeof(size_t));
	for (auto it=database.begin(); it!=database.end(); ++it)
	{
Dr. Carsten Kemena's avatar
changes  
Dr. Carsten Kemena committed
22 23
		size_t val= it->second.size();
		fout.write((char*)&it->first, sizeof(PrefixType));
Carsten Kemena's avatar
Carsten Kemena committed
24 25 26
		fout.write((char*)&val, sizeof(size_t));
		for (auto it2=it->second.begin(); it2!=it->second.end(); ++it2)
		{
27
			fout.write((char*)&(it2->first), sizeof(CodedSuffix));
Carsten Kemena's avatar
Carsten Kemena committed
28 29 30 31 32 33 34 35 36
			fout.write((char*)&(it2->second), sizeof(unsigned short));
		}
	}
 	fout.close();
}




Carsten Kemena's avatar
Carsten Kemena committed
37 38 39
int
main(int argc, char const *argv[])
{
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
40
	fs::path inFile, outDir;
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
41
	size_t nThreads;
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
42
	std::string radiantDBVersion(std::string(STR(MAJOR_VERSION)) + "." + std::string(STR(MINOR_VERSION)) + "." + std::string(STR(PATCH_VERSION)) );
Carsten Kemena's avatar
Carsten Kemena committed
43 44 45 46
	po::options_description allOpts("makeRadiantDB " + radiantDBVersion + " (C) 2017  Carsten Kemena\nThis program comes with ABSOLUTELY NO WARRANTY;\n\nAllowed options are displayed below.");
	po::options_description general("General options");
	general.add_options()
		("help,h", "Produces this help message")
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
47
		("in,i", po::value<fs::path>(&inFile)->required()->value_name("FILE"), "The input file")
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
48
		("out,o", po::value<fs::path>(&outDir)->required()->value_name("DIRECTORY"), "The directory for the output files")
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
49
		("nThreads,t", po::value<size_t>(&nThreads)->default_value(1), "The number of threads to use")
Carsten Kemena's avatar
Carsten Kemena committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	;

	allOpts.add(general);
	try
	{
		po::variables_map vm;
		po::store(po::command_line_parser(argc, argv).options(allOpts).run(), vm);
		if (vm.count("help"))
		{
			cout << allOpts<< "\n";
			return EXIT_SUCCESS;
		}
		po::notify(vm);
	}
	catch (boost::program_options::error &e)
	{
		cerr << "An error occurred parsing the commandline: \n";
		cerr << e.what() << "\n";
		cerr << "Please use -h/--help for more information.\n";
		return EXIT_FAILURE;
	}
71

Carsten Kemena's avatar
Carsten Kemena committed
72

Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
73
	omp_set_num_threads(nThreads);
74
	std::unordered_map<PrefixType, std::map<CodedSuffix, unsigned short > > database;
75
	turnFile2db(inFile, database, false);
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
76
	write2file(database, outDir.string() + "/forward.db");
77

Carsten Kemena's avatar
Carsten Kemena committed
78 79
	database.clear();
	turnFile2db(inFile, database, true);
80
	cout << "Write to file" << endl;
Dr. Carsten Kemena's avatar
Dr. Carsten Kemena committed
81
	write2file(database, outDir.string() + "/reverse.db");
Carsten Kemena's avatar
Carsten Kemena committed
82

Carsten Kemena's avatar
Carsten Kemena committed
83
	return 0;
84
}