MXS-1702: Clean up and convert test to C++

Cleaned up and updated the test; the code was written with a pre-C99
standard in mind.

Added a get() method into mxs::Buffer to make it easier to use with
non-C++ functions.
This commit is contained in:
Markus Mäkelä
2018-03-15 13:46:19 +02:00
parent cb170eb88e
commit e90c29cce2
4 changed files with 104 additions and 90 deletions

View File

@ -1,5 +1,5 @@
# This test no longer requires the query classifier for canonicalization
add_executable(canonizer canonizer.c)
add_executable(canonizer canonizer.cc)
target_link_libraries(canonizer maxscale-common)
add_test(NAME CanonicalQuery COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/canontest.sh
${CMAKE_CURRENT_BINARY_DIR}/test.log

View File

@ -1,89 +0,0 @@
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2020-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <maxscale/query_classifier.h>
#include <maxscale/buffer.h>
#include <maxscale/paths.h>
#include <maxscale/utils.h>
int main(int argc, char** argv)
{
unsigned int psize;
GWBUF* qbuff;
char *tok;
char readbuff[4092];
FILE* infile;
FILE* outfile;
if (argc != 3)
{
printf("Usage: canonizer <input file> <output file>\n");
return 1;
}
if (!utils_init())
{
printf("Utils library init failed.\n");
return 1;
}
set_libdir(strdup("../../../../query_classifier/qc_sqlite/"));
set_datadir(strdup("/tmp"));
set_langdir(strdup("."));
set_process_datadir(strdup("/tmp"));
qc_setup("qc_sqlite", QC_SQL_MODE_DEFAULT, NULL);
qc_process_init(QC_INIT_BOTH);
qc_thread_init(QC_INIT_BOTH);
infile = fopen(argv[1], "rb");
outfile = fopen(argv[2], "wb");
if (infile == NULL || outfile == NULL)
{
printf("Opening files failed.\n");
return 1;
}
while (!feof(infile) && fgets(readbuff, 4092, infile))
{
char* nl = strchr(readbuff, '\n');
if (nl)
{
*nl = '\0';
}
if (strlen(readbuff) > 0)
{
psize = strlen(readbuff) + 1;
qbuff = gwbuf_alloc(psize + 4);
*(qbuff->sbuf->data + 0) = (unsigned char) psize;
*(qbuff->sbuf->data + 1) = (unsigned char) (psize >> 8);
*(qbuff->sbuf->data + 2) = (unsigned char) (psize >> 16);
*(qbuff->sbuf->data + 3) = 0x00;
*(qbuff->sbuf->data + 4) = 0x03;
memcpy(qbuff->start + 5, readbuff, psize - 1);
tok = qc_get_canonical(qbuff);
fprintf(outfile, "%s\n", tok);
free(tok);
gwbuf_free(qbuff);
}
}
fclose(infile);
fclose(outfile);
qc_process_end(QC_INIT_BOTH);
return 0;
}

View File

@ -0,0 +1,90 @@
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2020-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <maxscale/cppdefs.hh>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <string>
#include <maxscale/query_classifier.h>
#include <maxscale/buffer.hh>
#include <maxscale/paths.h>
#include <maxscale/utils.h>
using std::cout;
using std::endl;
int main(int argc, char** argv)
{
if (argc != 3)
{
cout << "Usage: canonizer <input file> <output file>" << endl;
return 1;
}
if (!utils_init())
{
cout << "Utils library init failed." << endl;
return 1;
}
set_libdir(strdup("../../../../query_classifier/qc_sqlite/"));
set_datadir(strdup("/tmp"));
set_langdir(strdup("."));
set_process_datadir(strdup("/tmp"));
qc_setup("qc_sqlite", QC_SQL_MODE_DEFAULT, NULL);
qc_process_init(QC_INIT_BOTH);
qc_thread_init(QC_INIT_BOTH);
std::ifstream infile(argv[1]);
std::ofstream outfile(argv[2]);
if (!infile || !outfile)
{
cout << "Opening files failed." << endl;
return 1;
}
for (std::string line; getline(infile, line);)
{
while (*line.rbegin() == '\n')
{
line.resize(line.size() - 1);
}
if (!line.empty())
{
size_t psize = line.size() + 1;
mxs::Buffer buf(psize + 4);
auto it = buf.begin();
*it++ = (uint8_t)psize;
*it++ = (uint8_t)(psize >> 8);
*it++ = (uint8_t)(psize >> 16);
*it++ = 0;
*it++ = 3;
std::copy(line.begin(), line.end(), it);
char* tok = qc_get_canonical(buf.get());
outfile << tok << endl;
free(tok);
}
}
qc_process_end(QC_INIT_BOTH);
return 0;
}