Skip to content

Commit

Permalink
move addqueryfuncs and addhistfuncs to header
Browse files Browse the repository at this point in the history
  • Loading branch information
calccrypto committed Jan 30, 2025
1 parent fd55004 commit be60512
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 99 deletions.
54 changes: 40 additions & 14 deletions include/addqueryfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,53 @@ OF SUCH DAMAGE.
#include <sqlite3.h>

#include "bf.h"
#include "histogram.h"

#ifdef __cplusplus
extern "C" {
#endif

/* 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
Expand Down
1 change: 1 addition & 0 deletions include/dbutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 32 additions & 4 deletions include/histogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ OF SUCH DAMAGE.
#include <stddef.h>
#include <time.h>

#include "dbutils.h"
#include <sqlite3.h>

#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.
*/

/* ********************************************* */
Expand Down Expand Up @@ -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);
/* ********************************************* */
Expand All @@ -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);
/* ********************************************* */
Expand Down Expand Up @@ -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);
/* ********************************************* */
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
27 changes: 0 additions & 27 deletions src/addqueryfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ OF SUCH DAMAGE.
#include <time.h>

#include "addqueryfuncs.h"
#include "histogram.h"
#include "utils.h"

void uidtouser(sqlite3_context *context, int argc, sqlite3_value **argv)
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 1 addition & 27 deletions src/gufi_vt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -640,7 +614,7 @@ int sqlite3_gufivt_init(

SQLITE_EXTENSION_INIT2(pApi);

if (addvtfuncs(db) != 0) {
if (addqueryfuncs(db) != 0) {
return SQLITE_ERROR;
}

Expand Down
37 changes: 10 additions & 27 deletions src/histogram.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
);
}
1 change: 1 addition & 0 deletions test/unit/googletest/histogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ OF SUCH DAMAGE.
#include <gtest/gtest.h>

#include "bf.h"
#include "dbutils.h"
#include "histogram.h"

static void setup_db(sqlite3 **db) {
Expand Down

0 comments on commit be60512

Please sign in to comment.