void loadQueries(std::vector<std::vector<uint32_t>> &queries, const std::string &queryfn) { std::ifstream queryfile(queryfn); for (std::string line; std::getline(queryfile, line);) { std::vector<uint32_t> query; utf8to32(line.data(), query); queries.push_back(query); } queryfile.close(); }
// read a file with queries // parse the queries // and output to file as well as stdout void analyze(Indexed* indexed, std::string queryFilename, std::string outputFilename){ std::string line; std::ifstream queryfile(queryFilename.c_str()); std::ofstream outputfile(outputFilename.c_str()); if (!outputfile.is_open()){ std::cout << "could not open output file\n"; return; }; const Rows* rows = indexed->rows; if (queryfile.is_open()){ // read all the lines while ( getline (queryfile, line) ) { Tokens t = tokenize(line); bool valid = true; if (t.size() < 3){ valid = false; }; int colNum1 = 0; int colType1 = 0; int colNum2 = 0; if (valid){ ColsByName::const_iterator colIt = rows->header.find(t[1]); if (colIt == rows->header.end()){ std::cout << "column: " << t[1] << " not found!\n"; valid = false; }else{ colNum2 = colIt->second.pos; if (!typeCanAggregate(colIt->second.type)){ std::cout << "can not perform: " << t[0] << " over " << t[1] << " of type " << typeName(colIt->second.type) << "\n"; valid = false; }; }; colIt = rows->header.find(t[2]); if (colIt == rows->header.end()){ std::cout << "column: " << t[2] << " not found!\n"; valid = false; }else{ colNum1 = colIt->second.pos; colType1 = colIt->second.type; }; }; if (valid){ // output to file and std out std::cout << t[0] << " " << t[1] << " GROUPED BY " << t[2] << "\n"; outputfile << t[0] << " " << t[1] << " GROUPED BY " << t[2] << "\n"; if (t[0] == "AVG"){ doAvg(indexed, colNum1, colNum2, colType1, outputfile); }else if (t[0] == "MIN"){ doMin(indexed, colNum1, colNum2, colType1, outputfile); }else if (t[0] == "MAX"){ doMax(indexed, colNum1, colNum2, colType1, outputfile); }else{ std::cout << "unknown aggregate function: " << t[0] << "\n"; }; // new line std::cout << "\n"; outputfile << "\n"; }; }; }; outputfile.close(); };