From be60512e3874251c2d5be81f8aa3c846de08f5ce Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 30 Jan 2025 12:50:21 -0700 Subject: [PATCH] move addqueryfuncs and addhistfuncs to header --- include/addqueryfuncs.h | 54 ++++++++++++++++++++++-------- include/dbutils.h | 1 + include/histogram.h | 36 +++++++++++++++++--- src/addqueryfuncs.c | 27 --------------- src/gufi_vt.c | 28 +--------------- src/histogram.c | 37 ++++++-------------- test/unit/googletest/histogram.cpp | 1 + 7 files changed, 85 insertions(+), 99 deletions(-) diff --git a/include/addqueryfuncs.h b/include/addqueryfuncs.h index 1a69a5798..0d0b10356 100644 --- a/include/addqueryfuncs.h +++ b/include/addqueryfuncs.h @@ -68,6 +68,7 @@ OF SUCH DAMAGE. #include #include "bf.h" +#include "histogram.h" #ifdef __cplusplus extern "C" { @@ -75,20 +76,45 @@ extern "C" { /* list of functions to add to a SQLite3 db handle that do not have user data/context */ -extern void uidtouser(sqlite3_context *context, int argc, sqlite3_value **argv); -extern void gidtogroup(sqlite3_context *context, int argc, sqlite3_value **argv); -extern void modetotxt(sqlite3_context *context, int argc, sqlite3_value **argv); -extern void sqlite3_strftime(sqlite3_context *context, int argc, sqlite3_value **argv); -extern void blocksize(sqlite3_context *context, int argc, sqlite3_value **argv); -extern void human_readable_size(sqlite3_context *context, int argc, sqlite3_value **argv); -extern void sqlite_basename(sqlite3_context *context, int argc, sqlite3_value **argv); -extern void stdev_step(sqlite3_context *context, int argc, sqlite3_value **argv); -extern void stdevs_final(sqlite3_context *context); -extern void stdevp_final(sqlite3_context *context); -extern void median_step(sqlite3_context *context, int argc, sqlite3_value **argv); -extern void median_final(sqlite3_context *context); - -int addqueryfuncs(sqlite3 *db); +void uidtouser(sqlite3_context *context, int argc, sqlite3_value **argv); +void gidtogroup(sqlite3_context *context, int argc, sqlite3_value **argv); +void modetotxt(sqlite3_context *context, int argc, sqlite3_value **argv); +void sqlite3_strftime(sqlite3_context *context, int argc, sqlite3_value **argv); +void blocksize(sqlite3_context *context, int argc, sqlite3_value **argv); +void human_readable_size(sqlite3_context *context, int argc, sqlite3_value **argv); +void sqlite_basename(sqlite3_context *context, int argc, sqlite3_value **argv); +void stdev_step(sqlite3_context *context, int argc, sqlite3_value **argv); +void stdevs_final(sqlite3_context *context); +void stdevp_final(sqlite3_context *context); +void median_step(sqlite3_context *context, int argc, sqlite3_value **argv); +void median_final(sqlite3_context *context); + +static inline int addqueryfuncs(sqlite3 *db) { + return !( + (sqlite3_create_function(db, "uidtouser", 1, SQLITE_UTF8, + NULL, &uidtouser, NULL, NULL) == SQLITE_OK) && + (sqlite3_create_function(db, "gidtogroup", 1, SQLITE_UTF8, + NULL, &gidtogroup, NULL, NULL) == SQLITE_OK) && + (sqlite3_create_function(db, "modetotxt", 1, SQLITE_UTF8, + NULL, &modetotxt, NULL, NULL) == SQLITE_OK) && + (sqlite3_create_function(db, "strftime", 2, SQLITE_UTF8, + NULL, &sqlite3_strftime, NULL, NULL) == SQLITE_OK) && + (sqlite3_create_function(db, "blocksize", 2, SQLITE_UTF8, + NULL, &blocksize, NULL, NULL) == SQLITE_OK) && + (sqlite3_create_function(db, "human_readable_size", 1, SQLITE_UTF8, + NULL, &human_readable_size, NULL, NULL) == SQLITE_OK) && + (sqlite3_create_function(db, "basename", 1, SQLITE_UTF8, + NULL, &sqlite_basename, NULL, NULL) == SQLITE_OK) && + (sqlite3_create_function(db, "stdevs", 1, SQLITE_UTF8, + NULL, NULL, stdev_step, stdevs_final) == SQLITE_OK) && + (sqlite3_create_function(db, "stdevp", 1, SQLITE_UTF8, + NULL, NULL, stdev_step, stdevp_final) == SQLITE_OK) && + (sqlite3_create_function(db, "median", 1, SQLITE_UTF8, + NULL, NULL, median_step, median_final) == SQLITE_OK) && + addhistfuncs(db) + ); +} + int addqueryfuncs_with_context(sqlite3 *db, struct work *work); #ifdef __cplusplus diff --git a/include/dbutils.h b/include/dbutils.h index 34ca0ce3b..95047f0c1 100644 --- a/include/dbutils.h +++ b/include/dbutils.h @@ -194,6 +194,7 @@ int stopdb(sqlite3 *db); void closedb(sqlite3 *db); +/* only used by unit tests */ int copy_columns_callback(void *args, int count, char **data, char **columns); void insertdbfin(sqlite3_stmt *res); diff --git a/include/histogram.h b/include/histogram.h index d2cfcc10b..7e6176cc3 100644 --- a/include/histogram.h +++ b/include/histogram.h @@ -68,19 +68,19 @@ OF SUCH DAMAGE. #include #include -#include "dbutils.h" +#include #ifdef __cplusplus extern "C" { #endif -/* use this to add histogram functions to a sqlite database handle */ -int addhistfuncs(sqlite3 *db); - /* * Public API for parsing returned strings. * * These structs are intended for external use. + * + * Ignore the *_step and *_final functions. They are sqlite3 UDFs that + * need to be exposed here to get linking to work for some reason. */ /* ********************************************* */ @@ -108,6 +108,8 @@ typedef struct log2_hist { size_t ge; /* len >= 2^count */ } log2_hist_t; +void log2_hist_step(sqlite3_context *context, int argc, sqlite3_value **argv); +void log2_hist_final(sqlite3_context *context); log2_hist_t *log2_hist_parse(const char *str); void log2_hist_free(log2_hist_t *hist); /* ********************************************* */ @@ -126,6 +128,8 @@ typedef struct mode_hist { size_t buckets[512]; } mode_hist_t; +void mode_hist_step(sqlite3_context *context, int argc, sqlite3_value **argv); +void mode_hist_final(sqlite3_context *context); mode_hist_t *mode_hist_parse(const char *str); void mode_hist_free(mode_hist_t *hist); /* ********************************************* */ @@ -165,6 +169,8 @@ typedef struct time_hist { time_t ref; } time_hist_t; +void time_hist_step(sqlite3_context *context, int argc, sqlite3_value **argv); +void time_hist_final(sqlite3_context *context); time_hist_t *time_hist_parse(const char *str); void time_hist_free(time_hist_t *hist); /* ********************************************* */ @@ -192,6 +198,9 @@ typedef struct category_hist { size_t count; } category_hist_t; +void category_hist_step(sqlite3_context *context, int argc, sqlite3_value **argv); +void category_hist_combine_step(sqlite3_context *context, int argc, sqlite3_value **argv); +void category_hist_final(sqlite3_context *context); category_hist_t *category_hist_parse(const char *str); category_hist_t *category_hist_combine(category_hist_t *lhs, category_hist_t *rhs); void category_hist_free(category_hist_t *hist); @@ -211,10 +220,29 @@ typedef struct mode_count { size_t count; } mode_count_t; +void mode_count_final(sqlite3_context *context); mode_count_t *mode_count_parse(const char *str); void mode_count_free(mode_count_t *mc); /* ********************************************* */ +/* use this to add histogram functions to a sqlite database handle */ +static inline int addhistfuncs(sqlite3 *db) { + return ( + (sqlite3_create_function(db, "log2_hist", 2, SQLITE_UTF8, + NULL, NULL, log2_hist_step, log2_hist_final) == SQLITE_OK) && + (sqlite3_create_function(db, "mode_hist", 1, SQLITE_UTF8, + NULL, NULL, mode_hist_step, mode_hist_final) == SQLITE_OK) && + (sqlite3_create_function(db, "time_hist", 2, SQLITE_UTF8, + NULL, NULL, time_hist_step, time_hist_final) == SQLITE_OK) && + (sqlite3_create_function(db, "category_hist", 2, SQLITE_UTF8, + NULL, NULL, category_hist_step, category_hist_final) == SQLITE_OK) && + (sqlite3_create_function(db, "category_hist_combine", 1, SQLITE_UTF8, + NULL, NULL, category_hist_combine_step, category_hist_final) == SQLITE_OK) && + (sqlite3_create_function(db, "mode_count", 1, SQLITE_UTF8, + NULL, NULL, category_hist_step, mode_count_final) == SQLITE_OK) + ); +} + #ifdef __cplusplus } #endif diff --git a/src/addqueryfuncs.c b/src/addqueryfuncs.c index ca385b461..a259f4adc 100644 --- a/src/addqueryfuncs.c +++ b/src/addqueryfuncs.c @@ -70,7 +70,6 @@ OF SUCH DAMAGE. #include #include "addqueryfuncs.h" -#include "histogram.h" #include "utils.h" void uidtouser(sqlite3_context *context, int argc, sqlite3_value **argv) @@ -521,32 +520,6 @@ void starting_point(sqlite3_context *context, int argc, sqlite3_value **argv) { sqlite3_result_text(context, root->data, root->len, SQLITE_STATIC); } -int addqueryfuncs(sqlite3 *db) { - return !( - (sqlite3_create_function(db, "uidtouser", 1, SQLITE_UTF8, - NULL, &uidtouser, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "gidtogroup", 1, SQLITE_UTF8, - NULL, &gidtogroup, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "modetotxt", 1, SQLITE_UTF8, - NULL, &modetotxt, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "strftime", 2, SQLITE_UTF8, - NULL, &sqlite3_strftime, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "blocksize", 2, SQLITE_UTF8, - NULL, &blocksize, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "human_readable_size", 1, SQLITE_UTF8, - NULL, &human_readable_size, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "basename", 1, SQLITE_UTF8, - NULL, &sqlite_basename, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "stdevs", 1, SQLITE_UTF8, - NULL, NULL, stdev_step, stdevs_final) == SQLITE_OK) && - (sqlite3_create_function(db, "stdevp", 1, SQLITE_UTF8, - NULL, NULL, stdev_step, stdevp_final) == SQLITE_OK) && - (sqlite3_create_function(db, "median", 1, SQLITE_UTF8, - NULL, NULL, median_step, median_final) == SQLITE_OK) && - addhistfuncs(db) - ); -} - int addqueryfuncs_with_context(sqlite3 *db, struct work *work) { return !( (sqlite3_create_function(db, "path", 0, SQLITE_UTF8, diff --git a/src/gufi_vt.c b/src/gufi_vt.c index 1c8a02b05..770a7f271 100644 --- a/src/gufi_vt.c +++ b/src/gufi_vt.c @@ -131,32 +131,6 @@ typedef struct gufi_vtab_cursor { sqlite_int64 rowid; /* current row id */ } gufi_vtab_cursor; -/* Calling addqueryfuncs causes SQLite3 to set up incorrectly, leading to a segfault at load time */ -static int addvtfuncs(sqlite3 *db) { - return !( - (sqlite3_create_function(db, "uidtouser", 1, SQLITE_UTF8, - NULL, &uidtouser, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "gidtogroup", 1, SQLITE_UTF8, - NULL, &gidtogroup, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "modetotxt", 1, SQLITE_UTF8, - NULL, &modetotxt, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "strftime", 2, SQLITE_UTF8, - NULL, &sqlite3_strftime, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "blocksize", 2, SQLITE_UTF8, - NULL, &blocksize, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "human_readable_size", 1, SQLITE_UTF8, - NULL, &human_readable_size, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "basename", 1, SQLITE_UTF8, - NULL, &sqlite_basename, NULL, NULL) == SQLITE_OK) && - (sqlite3_create_function(db, "stdevs", 1, SQLITE_UTF8, - NULL, NULL, stdev_step, stdevs_final) == SQLITE_OK) && - (sqlite3_create_function(db, "stdevp", 1, SQLITE_UTF8, - NULL, NULL, stdev_step, stdevp_final) == SQLITE_OK) && - (sqlite3_create_function(db, "median", 1, SQLITE_UTF8, - NULL, NULL, median_step, median_final) == SQLITE_OK) - ); -} - /* * run gufi_query, aggregating results into a single db file * @@ -640,7 +614,7 @@ int sqlite3_gufivt_init( SQLITE_EXTENSION_INIT2(pApi); - if (addvtfuncs(db) != 0) { + if (addqueryfuncs(db) != 0) { return SQLITE_ERROR; } diff --git a/src/histogram.c b/src/histogram.c index 6e3512834..6628c6587 100644 --- a/src/histogram.c +++ b/src/histogram.c @@ -119,7 +119,7 @@ int serialize_bucket(sqlite3_context *context, } /* log2_hist(string/value, bucket_count) */ -static void log2_hist_step(sqlite3_context *context, int argc, sqlite3_value **argv) { +void log2_hist_step(sqlite3_context *context, int argc, sqlite3_value **argv) { (void) argc; log2_hist_t *hist = (log2_hist_t *) sqlite3_aggregate_context(context, sizeof(*hist)); if (hist->buckets == NULL) { @@ -169,7 +169,7 @@ static ssize_t serialize_log2_bucket(char *curr, const size_t avail, void *key, return snprintf(curr, avail, "%zu:%zu;", exp, hist->buckets[exp]); } -static void log2_hist_final(sqlite3_context *context) { +void log2_hist_final(sqlite3_context *context) { log2_hist_t *hist = (log2_hist_t *) sqlite3_aggregate_context(context, sizeof(*hist)); if (hist->buckets == NULL) { sqlite3_result_text(context, "0;0;0;", -1, SQLITE_TRANSIENT); @@ -233,7 +233,7 @@ void log2_hist_free(log2_hist_t *hist) { } /* mode_hist(mode) */ -static void mode_hist_step(sqlite3_context *context, int argc, sqlite3_value **argv) { +void mode_hist_step(sqlite3_context *context, int argc, sqlite3_value **argv) { (void) argc; mode_hist_t *hist = (mode_hist_t *) sqlite3_aggregate_context(context, sizeof(*hist)); const mode_t mode = (mode_t) sqlite3_value_int(argv[0]) & 0777; @@ -246,7 +246,7 @@ static ssize_t serialize_mode_bucket(char *curr, const size_t avail, void *key, return snprintf(curr, avail, "%03o:%zu;", (unsigned int) mode, hist->buckets[mode]); } -static void mode_hist_final(sqlite3_context *context) { +void mode_hist_final(sqlite3_context *context) { mode_hist_t *hist = (mode_hist_t *) sqlite3_aggregate_context(context, sizeof(*hist)); size_t size = DEFAULT_HIST_ALLOC; @@ -299,7 +299,7 @@ typedef struct sqlite_time_hist { int init; } sqlite_time_hist_t; -static void time_hist_step(sqlite3_context *context, int argc, sqlite3_value **argv) { +void time_hist_step(sqlite3_context *context, int argc, sqlite3_value **argv) { (void) argc; sqlite_time_hist_t *hist = (sqlite_time_hist_t *) sqlite3_aggregate_context(context, sizeof(*hist)); if (hist->init == 0) { @@ -329,7 +329,7 @@ static ssize_t serialize_time_bucket(char *curr, const size_t avail, void *key, return snprintf(curr, avail, "%zu:%zu;", (size_t) TIME_BUCKETS[bucket].seconds, hist->buckets[bucket]); } -static void time_hist_final(sqlite3_context *context) { +void time_hist_final(sqlite3_context *context) { sqlite_time_hist_t *hist = (sqlite_time_hist_t *) sqlite3_aggregate_context(context, sizeof(*hist)); if (hist->init == 0) { @@ -414,7 +414,7 @@ typedef struct sqlite_category_hist { int keep_1; } sqlite_category_hist_t; -static void category_hist_step(sqlite3_context *context, int argc, sqlite3_value **argv) { +void category_hist_step(sqlite3_context *context, int argc, sqlite3_value **argv) { (void) argc; sqlite_category_hist_t *hist = (sqlite_category_hist_t *) sqlite3_aggregate_context(context, sizeof(*hist)); if (!hist->trie) { @@ -468,7 +468,7 @@ static void free_str(void *ptr) { free(str); } -static void category_hist_final(sqlite3_context *context) { +void category_hist_final(sqlite3_context *context) { sqlite_category_hist_t *hist = (sqlite_category_hist_t *) sqlite3_aggregate_context(context, sizeof(*hist)); if (!hist->trie) { hist->trie = trie_alloc(); @@ -658,7 +658,7 @@ void category_hist_free(category_hist_t *hist) { } /* add a histogram into an existing histogram */ -static void category_hist_combine_step(sqlite3_context *context, int argc, sqlite3_value **argv) { +void category_hist_combine_step(sqlite3_context *context, int argc, sqlite3_value **argv) { (void) argv; (void) argc; sqlite_category_hist_t *hist = (sqlite_category_hist_t *) sqlite3_aggregate_context(context, sizeof(*hist)); if (!hist->trie) { @@ -683,7 +683,7 @@ static ssize_t serialize_mode(char *curr, const size_t avail, void *key, void *d return snprintf(curr, avail, "%zu:%s:%zu;", mode->len, mode->data, *count); } -static void mode_count_final(sqlite3_context *context) { +void mode_count_final(sqlite3_context *context) { sqlite_category_hist_t *hist = (sqlite_category_hist_t *) sqlite3_aggregate_context(context, sizeof(*hist)); if (!hist->trie) { @@ -780,20 +780,3 @@ void mode_count_free(mode_count_t *mc) { free(mc->mode); free(mc); } - -int addhistfuncs(sqlite3 *db) { - return ( - (sqlite3_create_function(db, "log2_hist", 2, SQLITE_UTF8, - NULL, NULL, log2_hist_step, log2_hist_final) == SQLITE_OK) && - (sqlite3_create_function(db, "mode_hist", 1, SQLITE_UTF8, - NULL, NULL, mode_hist_step, mode_hist_final) == SQLITE_OK) && - (sqlite3_create_function(db, "time_hist", 2, SQLITE_UTF8, - NULL, NULL, time_hist_step, time_hist_final) == SQLITE_OK) && - (sqlite3_create_function(db, "category_hist", 2, SQLITE_UTF8, - NULL, NULL, category_hist_step, category_hist_final) == SQLITE_OK) && - (sqlite3_create_function(db, "category_hist_combine", 1, SQLITE_UTF8, - NULL, NULL, category_hist_combine_step, category_hist_final) == SQLITE_OK) && - (sqlite3_create_function(db, "mode_count", 1, SQLITE_UTF8, - NULL, NULL, category_hist_step, mode_count_final) == SQLITE_OK) - ); -} diff --git a/test/unit/googletest/histogram.cpp b/test/unit/googletest/histogram.cpp index 656d08b86..f2450d54f 100644 --- a/test/unit/googletest/histogram.cpp +++ b/test/unit/googletest/histogram.cpp @@ -69,6 +69,7 @@ OF SUCH DAMAGE. #include #include "bf.h" +#include "dbutils.h" #include "histogram.h" static void setup_db(sqlite3 **db) {