Skip to content

Commit

Permalink
db_text: new parameter: buffer_size
Browse files Browse the repository at this point in the history
  • Loading branch information
ovidiusas committed Mar 29, 2024
1 parent 4b23a80 commit 3d880e0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
25 changes: 18 additions & 7 deletions modules/db_text/dbt_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ int dbt_check_mtime(const str *tbn, const str *dbn, time_t *mt)
dbt_table_p dbt_load_file(const str *tbn, const str *dbn)
{
FILE *fin=NULL;
char path[512], buf[4096];
char path[512];
char *buf;
int c, crow, ccol, bp, sign, max_auto;
dbt_val_t dtval;
dbt_table_p dtp = NULL;
Expand Down Expand Up @@ -125,6 +126,12 @@ dbt_table_p dbt_load_file(const str *tbn, const str *dbn)
if(!fin)
return NULL;

buf = pkg_malloc(buffer_size);
if(!buf) {
LM_ERR("error allocating read buffer, %i\n", buffer_size);
goto done;
}

dtp = dbt_table_new(tbn, dbn, path);
if(!dtp)
goto done;
Expand Down Expand Up @@ -167,10 +174,10 @@ dbt_table_p dbt_load_file(const str *tbn, const str *dbn)
{
if(c==EOF)
goto clean;
if (bp==4096) {
if (bp==buffer_size) {
LM_ERR("Buffer overflow for file [%s] row=[%d] col=[%d] c=[%c]."
" Required buffer size greater then 4096!\n",
path, crow+1, ccol+1, c);
" Required buffer size greater then %i!\n",
path, crow+1, ccol+1, c, buffer_size);
goto clean;
}
buf[bp++] = c;
Expand Down Expand Up @@ -469,10 +476,10 @@ dbt_table_p dbt_load_file(const str *tbn, const str *dbn)
goto clean;
}
}
if (bp==4096) {
if (bp==buffer_size) {
LM_ERR("Buffer overflow for file [%s] row=[%d] col=[%d] c=[%c]."
" Required buffer size greater then 4096!\n",
path, crow+1, ccol+1, c);
" Required buffer size greater then %i!\n",
path, crow+1, ccol+1, c, buffer_size);
goto clean;
}
buf[bp++] = c;
Expand Down Expand Up @@ -510,6 +517,8 @@ dbt_table_p dbt_load_file(const str *tbn, const str *dbn)
done:
if(fin)
fclose(fin);
if(buf)
pkg_free(buf);
return dtp;
clean:
/// ????? FILL IT IN - incomplete row/column
Expand All @@ -519,6 +528,8 @@ dbt_table_p dbt_load_file(const str *tbn, const str *dbn)
LM_ERR("error at row=%d col=%d c=%c\n", crow+1, ccol+1, c);
if(dtp)
dbt_table_free(dtp);
if(buf)
pkg_free(buf);
return NULL;
}

Expand Down
5 changes: 3 additions & 2 deletions modules/db_text/dbt_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@
#define DBT_DELIM_R '\n'

/*
* * Module parameters variables
* */
* Module parameters variables
*/
extern int db_mode; /* Database usage mode: 0 = no cache, 1 = cache */
extern int buffer_size; /* size of the buffer to allocate when reading file */

typedef db_val_t dbt_val_t, *dbt_val_p;

Expand Down
4 changes: 4 additions & 0 deletions modules/db_text/dbtext.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ static mi_response_t *mi_dbt_reload_1(const mi_params_t *params,
static mi_response_t *mi_dbt_reload_2(const mi_params_t *params,
struct mi_handler *async_hdl);

#define DEFAULT_DB_TEXT_READ_BUFFER_SIZE 4096

/*
* Module parameter variables
*/
int db_mode = 0; /* Database usage mode: 0 = cache, 1 = no cache */
int buffer_size = DEFAULT_DB_TEXT_READ_BUFFER_SIZE;

int dbt_bind_api(const str* mod, db_func_t *dbb);

Expand All @@ -70,6 +73,7 @@ static const cmd_export_t cmds[] = {
*/
static const param_export_t params[] = {
{"db_mode", INT_PARAM, &db_mode},
{"buffer_size", INT_PARAM, &buffer_size},
{0, 0, 0}
};

Expand Down
22 changes: 19 additions & 3 deletions modules/db_text/doc/db_text_admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,6 @@ suser:supasswd:xxx:alpha.org:xxx
</section>
<section id="exported_parameters" xreflabel="Exported Parameters">
<title>Exported Parameters</title>
<para>
<emphasis>None</emphasis>.
</para>
<section id="param_db_mode" xreflabel="db_mode">
<title><varname>db_mode</varname> (integer)</title>
<para>
Expand All @@ -252,6 +249,25 @@ suser:supasswd:xxx:alpha.org:xxx
...
modparam("db_text", "db_mode", 1)
...
</programlisting>
</example>
</section>
<section id="param_buffer_size" xreflabel="buffer_size">
<title><varname>buffer_size</varname> (integer)</title>
<para>
Size of the buffer used to read the text file.
</para>
<para>
<emphasis>
Default value is <quote>4096</quote>.
</emphasis>
</para>
<example>
<title>Set <varname>buffer_size</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("db_text", "buffer_size", 8192)
...
</programlisting>
</example>
</section>
Expand Down

0 comments on commit 3d880e0

Please sign in to comment.