From 7e219a9e1c9eb18e840a41390bce0876ebc22792 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 23 Jan 2024 11:11:06 -0700 Subject: [PATCH] gufi_query -Q attach single db file to all threads file is attached at the start of gufi_query instead of being reattached at every single directory --- include/bf.h | 6 ++++++ scripts/gufi_common.py | 1 + src/bf.c | 18 ++++++++++++++++++ src/gufi_query/PoolArgs.c | 19 +++++++++++++++++++ test/unit/googletest/aggregate.cpp.in | 2 +- 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/include/bf.h b/include/bf.h index 16ae5ace0..9e1b874e4 100644 --- a/include/bf.h +++ b/include/bf.h @@ -233,6 +233,12 @@ struct input { /* compress work items (if compression library was found) */ int compress; + + struct { + refstr_t dbname; /* name of single db file */ + refstr_t attachname; /* attach name of db file */ + refstr_t sql; /* SQL used to create view(s) on attached data */ + } attach_single; }; void print_help(const char *prog_name, diff --git a/scripts/gufi_common.py b/scripts/gufi_common.py index 49d11410a..e4d9e1ca9 100644 --- a/scripts/gufi_common.py +++ b/scripts/gufi_common.py @@ -73,6 +73,7 @@ from shlex import quote as sanitize # new in Python 3.3 # table names +ENTRIES = 'entries' SUMMARY = 'summary' PENTRIES = 'pentries' XSUMMARY = 'xsummary' diff --git a/src/bf.c b/src/bf.c index f3501c46f..af79ddad9 100644 --- a/src/bf.c +++ b/src/bf.c @@ -143,6 +143,9 @@ void print_help(const char* prog_name, #if HAVE_ZLIB case 'e': printf(" -e compress work items"); break; #endif + case 'Q': printf(" -Q \n" + " \n" + " Attach a single file across every single db in the index and use the SQL to create views"); break; default: printf("print_help(): unrecognized option '%c'", (char)ch); } printf("\n"); @@ -197,6 +200,9 @@ void show_input(struct input* in, int retval) { printf("in.target_memory_footprint = %" PRIu64 "\n", in->target_memory_footprint); printf("in.subdir_limit = %zu\n", in->subdir_limit); printf("in.compress = %d\n", in->compress); + printf("in.attach_single.dbname = %s\n", in->attach_single.dbname.data); + printf("in.attach_single.attachname = %s\n", in->attach_single.attachname.data); + printf("in.attach_single.sql = %s\n", in->attach_single.sql.data); printf("\n"); printf("retval = %d\n", retval); printf("\n"); @@ -432,6 +438,18 @@ int parse_cmd_line(int argc, break; #endif + case 'Q': + INSTALL_STR(&in->attach_single.dbname, optarg); + optarg = argv[optind]; + + INSTALL_STR(&in->attach_single.attachname, optarg); + optarg = argv[++optind]; + + INSTALL_STR(&in->attach_single.sql, optarg); + optarg = argv[++optind]; + + break; + case '?': // getopt returns '?' when there is a problem. In this case it // also prints, e.g. "getopt_test: illegal option -- z" diff --git a/src/gufi_query/PoolArgs.c b/src/gufi_query/PoolArgs.c index 7cbeb47e8..9c92568aa 100644 --- a/src/gufi_query/PoolArgs.c +++ b/src/gufi_query/PoolArgs.c @@ -109,6 +109,25 @@ int PoolArgs_init(PoolArgs_t *pa, struct input *in, pthread_mutex_t *global_mute } #endif + /* handle -Q */ + if (in->attach_single.dbname.len) { + /* attach single db to thread instance */ + if (!attachdb(in->attach_single.dbname.data, ta->outdb, + in->attach_single.attachname.data, + SQLITE_OPEN_READONLY, 1)) { + break; + } + + /* set up views */ + char *err = NULL; + if (sqlite3_exec(ta->outdb, in->attach_single.sql.data, NULL, NULL, &err) != SQLITE_OK) { + fprintf(stderr, "Error: Could not run SQL Init \"%s\" on %s: %s\n", + in->sql.init.data, ta->dbname, err); + sqlite3_free(err); + break; + } + } + /* run -I */ if (in->sql.init.len) { char *err = NULL; diff --git a/test/unit/googletest/aggregate.cpp.in b/test/unit/googletest/aggregate.cpp.in index e8aec66d0..0282c4f63 100644 --- a/test/unit/googletest/aggregate.cpp.in +++ b/test/unit/googletest/aggregate.cpp.in @@ -95,7 +95,7 @@ TEST(gufi_query, aggregate) { const size_t row_count = std::uniform_int_distribution (1, 8)(gen); - struct input in; + struct input in{}; in.output = STDOUT; in.maxthreads = std::uniform_int_distribution (1, 8)(gen); in.delim = ' ';