Skip to content

Commit

Permalink
sql_cacher: new module parameter: bigint_to_str to control bigint output
Browse files Browse the repository at this point in the history
 - allow bigint to be returned as string
  • Loading branch information
ovidiusas committed Apr 29, 2024
1 parent faeb932 commit 15f5229
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
55 changes: 50 additions & 5 deletions modules/sql_cacher/sql_cacher.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static str columns_delimiter = str_init(DEFAULT_COLUMNS_DELIM);
static int fetch_nr_rows = DEFAULT_FETCH_NR_ROWS;
static int full_caching_expire = DEFAULT_FULL_CACHING_EXPIRE;
static int reload_interval = DEFAULT_RELOAD_INTERVAL;
static int sql_cacher_bigint2str = DEFAULT_BIGINT2STR;

static cache_entry_t **entry_list;
static struct queried_key **queries_in_progress;
Expand All @@ -77,6 +78,7 @@ static const param_export_t mod_params[] = {
{"full_caching_expire", INT_PARAM, &full_caching_expire},
{"reload_interval", INT_PARAM, &reload_interval},
{"cache_table", STR_PARAM|USE_FUNC_PARAM, (void *)&parse_cache_entry},
{"bigint_to_str", INT_PARAM, &sql_cacher_bigint2str},
{0,0,0}
};

Expand Down Expand Up @@ -479,10 +481,18 @@ static int get_column_types(cache_entry_t *c_entry, db_val_t *values, int nr_col
val_type = VAL_TYPE(values + i);
switch (val_type) {
case DB_INT:
case DB_BIGINT:
c_entry->nr_ints++;
c_entry->column_types &= ~(1LL << i);
break;
case DB_BIGINT:
if (sql_cacher_bigint2str) {
c_entry->nr_strs++;
c_entry->column_types |= (1LL << i);
} else {
c_entry->nr_ints++;
c_entry->column_types &= ~(1LL << i);
}
break;
case DB_STRING:
case DB_STR:
case DB_BLOB:
Expand Down Expand Up @@ -515,10 +525,18 @@ static int build_column_types(cache_entry_t *c_entry, db_key_t *names, db_type_t
val_type = types[i];
switch (val_type) {
case DB_INT:
case DB_BIGINT:
c_entry->nr_ints++;
c_entry->column_types &= ~(1LL << i);
break;
case DB_BIGINT:
if (sql_cacher_bigint2str) {
c_entry->nr_strs++;
c_entry->column_types |= (1LL << i);
} else {
c_entry->nr_ints++;
c_entry->column_types &= ~(1LL << i);
}
break;
case DB_STRING:
case DB_STR:
case DB_BLOB:
Expand Down Expand Up @@ -560,6 +578,13 @@ static unsigned int get_cdb_val_size(cache_entry_t *c_entry, db_val_t *values, i
case DB_DOUBLE:
len += DOUBLE2STR_MAX_LEN;
break;
case DB_BIGINT:
if (sql_cacher_bigint2str) {
len += BIGINT2STR_MAX_LEN;
break;
} else {
continue;
}
default: continue;
}
}
Expand Down Expand Up @@ -605,8 +630,12 @@ static int insert_in_cachedb(cache_entry_t *c_entry, db_handlers_t *db_hdls,
int_val = VAL_INT(values + i);
break;
case DB_BIGINT:
int_val = (int)VAL_BIGINT(values + i);
break;
if (!sql_cacher_bigint2str) {
int_val = (int)VAL_BIGINT(values + i);
break;
} else {
continue;
}
default: continue;
}
if (VAL_NULL(values + i))
Expand Down Expand Up @@ -640,6 +669,13 @@ static int insert_in_cachedb(cache_entry_t *c_entry, db_handlers_t *db_hdls,
case DB_DOUBLE:
str_val.s = double2str(VAL_DOUBLE(values + i), &str_val.len);
break;
case DB_BIGINT:
if (sql_cacher_bigint2str) {
str_val.s = bigint2str(VAL_BIGINT(values + i), &str_val.len);
break;
} else {
continue;
}
default: continue;
}
if (VAL_NULL(values + i))
Expand Down Expand Up @@ -1755,7 +1791,16 @@ static int on_demand_load(pv_name_fix_t *pv_name, str *str_res, int *int_res,
*int_res = VAL_INT(values + pv_name->col_nr);
break;
case DB_BIGINT:
*int_res = (int)VAL_BIGINT(values + pv_name->col_nr);
if (sql_cacher_bigint2str) {
st.s = bigint2str(VAL_BIGINT(values + pv_name->col_nr), &st.len);
if (pkg_str_dup(str_res, &st) != 0) {
LM_ERR("oom\n");
rc = -1;
goto out_free_res;
}
} else {
*int_res = (int)VAL_BIGINT(values + pv_name->col_nr);
}
break;
case DB_DOUBLE:
st.s = double2str(VAL_DOUBLE(values + pv_name->col_nr), &st.len);
Expand Down
1 change: 1 addition & 0 deletions modules/sql_cacher/sql_cacher.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#define DEFAULT_FULL_CACHING_EXPIRE 86400 /* 24h */
#define DEFAULT_RELOAD_INTERVAL 60
#define DEFAULT_FETCH_NR_ROWS 100
#define DEFAULT_BIGINT2STR 0
#define TEST_QUERY_STR "sql_cacher_test_query_key"
#define TEST_QUERY_INT 555666555
#define CDB_TEST_KEY_STR "sql_cacher_cdb_test_key"
Expand Down

0 comments on commit 15f5229

Please sign in to comment.