Browse code

db_text: updated to the new mod interface

Victor Seva authored on 28/09/2018 09:09:15
Showing 1 changed files
... ...
@@ -78,18 +78,16 @@ static param_export_t params[] = {
78 78
 static rpc_export_t rpc_methods[];
79 79
 
80 80
 struct module_exports exports = {
81
-	"db_text",
81
+	"db_text",       /* module name */
82 82
 	DEFAULT_DLFLAGS, /* dlopen flags */
83
-	cmds,     /* Exported functions */
84
-	params,   /* Exported parameters */
85
-	0,        /* exported statistics */
86
-	0,        /* exported MI functions */
87
-	0,        /* exported pseudo-variables */
88
-	0,        /* extra processes */
89
-	mod_init, /* module initialization function */
90
-	0,        /* response function*/
91
-	destroy,  /* destroy function */
92
-	0         /* per-child init function */
83
+	cmds,            /* cmd (cfg function) exports */
84
+	params,          /* param exports */
85
+	0,               /* RPC method exports */
86
+	0,               /* pseudo-variables exports */
87
+	0,               /* response handling function */
88
+	mod_init,        /* module init function */
89
+	0,               /* per-child init function */
90
+	destroy          /* module destroy function */
93 91
 };
94 92
 
95 93
 int mod_register(char *path, int *dlflags, void *p1, void *p2)
Browse code

db_text: add db_delim parameter

- make the delimiter between fields in th db_text file configurable;
useful when one would need to escape a lot of appearances of the
default delimiter (';' in IPv6 addresses).

Lucian Balaceanu authored on 19/04/2017 10:34:37
Showing 1 changed files
... ...
@@ -47,6 +47,8 @@ int db_mode = 0;  /* Database usage mode: 0 = cache, 1 = no cache */
47 47
 int empty_string = 0;  /* Treat empty string as "" = 0, 1 = NULL */
48 48
 int _db_text_read_buffer_size = DEFAULT_DB_TEXT_READ_BUFFER_SIZE;
49 49
 int _db_text_max_result_rows = DEFAULT_MAX_RESULT_ROWS;
50
+int _dbt_delim = ':'; /* ':' is the default delim */
51
+str _dbt_delim_str = str_init(":"); /* ':' is the default delim */
50 52
 str dbt_default_connection = str_init("");
51 53
 
52 54
 int dbt_bind_api(db_func_t *dbb);
... ...
@@ -69,6 +71,7 @@ static param_export_t params[] = {
69 71
 	{"file_buffer_size", INT_PARAM, &_db_text_read_buffer_size},
70 72
 	{"max_result_rows", INT_PARAM, &_db_text_max_result_rows},
71 73
 	{"default_connection", PARAM_STR, &dbt_default_connection},
74
+	{"db_delim", PARAM_STR, &_dbt_delim_str},
72 75
 	{0, 0, 0}
73 76
 };
74 77
 
... ...
@@ -103,6 +106,14 @@ int mod_register(char *path, int *dlflags, void *p1, void *p2)
103 106
 
104 107
 static int mod_init(void)
105 108
 {
109
+	if (_dbt_delim_str.len != 1) {
110
+		LM_ERR("db_delim must be a character, defaulting to \":\"\n");
111
+		pkg_free(_dbt_delim_str.s);
112
+		_dbt_delim_str.s = ":";
113
+		_dbt_delim_str.len = 1;
114
+	}
115
+	_dbt_delim = _dbt_delim_str.s[0];
116
+
106 117
 	if(dbt_init_cache())
107 118
 		return -1;
108 119
 	/* return make_demo(); */
Browse code

db_text: rpc command query requires linux for open_memstream()

- use the os define to enable/disable it at compile time

Daniel-Constantin Mierla authored on 13/03/2017 14:14:05
Showing 1 changed files
... ...
@@ -164,6 +164,7 @@ static const char *rpc_query_doc[2] = {
164 164
 
165 165
 /* rpc function implementations */
166 166
 static void rpc_query(rpc_t *rpc, void *ctx) {
167
+#ifdef __OS_linux
167 168
         str sql;
168 169
         db1_con_t *con;
169 170
         db1_res_t* _r;
... ...
@@ -226,6 +227,9 @@ static void rpc_query(rpc_t *rpc, void *ctx) {
226 227
 
227 228
 end:
228 229
         dbt_close(con);
230
+#else
231
+		rpc->fault(ctx, 500, "Command available on Linux only");
232
+#endif
229 233
 }
230 234
 
231 235
 static rpc_export_t rpc_methods[] = {
Browse code

db_text : add rpc command for live query

Luis Azedo authored on 03/03/2017 17:37:04
Showing 1 changed files
... ...
@@ -47,6 +47,7 @@ int db_mode = 0;  /* Database usage mode: 0 = cache, 1 = no cache */
47 47
 int empty_string = 0;  /* Treat empty string as "" = 0, 1 = NULL */
48 48
 int _db_text_read_buffer_size = DEFAULT_DB_TEXT_READ_BUFFER_SIZE;
49 49
 int _db_text_max_result_rows = DEFAULT_MAX_RESULT_ROWS;
50
+str dbt_default_connection = str_init("");
50 51
 
51 52
 int dbt_bind_api(db_func_t *dbb);
52 53
 
... ...
@@ -67,6 +68,7 @@ static param_export_t params[] = {
67 68
 	{"emptystring", INT_PARAM, &empty_string},
68 69
 	{"file_buffer_size", INT_PARAM, &_db_text_read_buffer_size},
69 70
 	{"max_result_rows", INT_PARAM, &_db_text_max_result_rows},
71
+	{"default_connection", PARAM_STR, &dbt_default_connection},
70 72
 	{0, 0, 0}
71 73
 };
72 74
 
... ...
@@ -156,8 +158,78 @@ static void rpc_dump(rpc_t *rpc, void *c) {
156 158
 	return;
157 159
 }
158 160
 
159
-static rpc_export_t rpc_methods[] = {
160
-	{"db_text.dump", rpc_dump, rpc_dump_doc, 0},
161
-	{0, 0, 0, 0}
161
+static const char *rpc_query_doc[2] = {
162
+        "Perform Live Query", 0
162 163
 };
163 164
 
165
+/* rpc function implementations */
166
+static void rpc_query(rpc_t *rpc, void *ctx) {
167
+        str sql;
168
+        db1_con_t *con;
169
+        db1_res_t* _r;
170
+        int res;
171
+        int n;
172
+        char *buf;
173
+        size_t len;
174
+        FILE *stream;
175
+        dbt_table_p tab;
176
+        dbt_row_p rowp;
177
+
178
+        rpc->scan(ctx, "S", &sql);
179
+
180
+        con = dbt_init(&dbt_default_connection);
181
+        if(con == NULL) {
182
+                rpc->rpl_printf(ctx, "invalid connection : %s", dbt_default_connection.s);
183
+                return;
184
+        }
185
+
186
+        res = dbt_raw_query(con, &sql, &_r);
187
+        if(res != 0) {
188
+                rpc->rpl_printf(ctx, "error executing sql statement");
189
+                goto end;
190
+        }
191
+
192
+
193
+        if(_r) {
194
+                tab = (dbt_table_p)_r->ptr;
195
+                if(_r->n == 0) {
196
+                        rpc->rpl_printf(ctx, "statement returned 0 rows");
197
+                } else {
198
+                        stream = open_memstream (&buf, &len);
199
+                        if (stream == NULL) {
200
+                                rpc->rpl_printf(ctx, "error opening stream");
201
+                                goto end;
202
+                        }
203
+                        dbt_print_table_header(tab, stream);
204
+                        fflush (stream);
205
+                        buf[len] = '\0';
206
+                        rpc->rpl_printf(ctx, "%s", buf);
207
+                        rowp = tab->rows;
208
+                        for(n=0; n < _r->n; n++) {
209
+                                fseeko (stream, 0, SEEK_SET);
210
+                                dbt_print_table_row_ex(tab, rowp, stream, 0);
211
+                                fflush (stream);
212
+                                buf[len] = '\0';
213
+                                rpc->rpl_printf(ctx, "%s", buf);
214
+                                rowp = rowp->next;
215
+                        }
216
+                        fclose (stream);
217
+                        free (buf);
218
+                        rpc->rpl_printf(ctx, "\ntotal rows %d / %d", _r->n, tab->nrrows);
219
+                }
220
+        } else {
221
+                rpc->rpl_printf(ctx, "%d affected rows", ((dbt_con_p)con->tail)->affected);
222
+        }
223
+
224
+        if(_r)
225
+                dbt_free_result(con, _r);
226
+
227
+end:
228
+        dbt_close(con);
229
+}
230
+
231
+static rpc_export_t rpc_methods[] = {
232
+        {"db_text.dump", rpc_dump, rpc_dump_doc, 0},
233
+        {"db_text.query", rpc_query, rpc_query_doc, 0},
234
+        {0, 0, 0, 0}
235
+};
Browse code

db_text: implement fetch and memory constraints

when dealing with large db_text files, pkg_memory is not suitable for
operating the database.

implementing fetch allows modules like presence & registrar & usrloc
to query large tables without constraints on pkg_memory.

creates tmp tables in shared memory for query results

(cherry picked from commit 18c64d2c9ff1527655055f75aa22e7d68c307874)

Conflicts:
src/modules/db_text/db_text.c
src/modules/db_text/dbt_api.c
src/modules/db_text/dbt_base.c
src/modules/db_text/dbt_lib.c

lazedo authored on 18/01/2017 10:00:22 • Luis Azedo committed on 18/01/2017 10:30:32
Showing 1 changed files
... ...
@@ -38,6 +38,7 @@ static int mod_init(void);
38 38
 static void destroy(void);
39 39
 
40 40
 #define DEFAULT_DB_TEXT_READ_BUFFER_SIZE 16384
41
+#define DEFAULT_MAX_RESULT_ROWS 100000;
41 42
 
42 43
 /*
43 44
  * Module parameter variables
... ...
@@ -45,6 +46,7 @@ static void destroy(void);
45 46
 int db_mode = 0;  /* Database usage mode: 0 = cache, 1 = no cache */
46 47
 int empty_string = 0;  /* Treat empty string as "" = 0, 1 = NULL */
47 48
 int _db_text_read_buffer_size = DEFAULT_DB_TEXT_READ_BUFFER_SIZE;
49
+int _db_text_max_result_rows = DEFAULT_MAX_RESULT_ROWS;
48 50
 
49 51
 int dbt_bind_api(db_func_t *dbb);
50 52
 
... ...
@@ -64,6 +66,7 @@ static param_export_t params[] = {
64 66
 	{"db_mode", INT_PARAM, &db_mode},
65 67
 	{"emptystring", INT_PARAM, &empty_string},
66 68
 	{"file_buffer_size", INT_PARAM, &_db_text_read_buffer_size},
69
+	{"max_result_rows", INT_PARAM, &_db_text_max_result_rows},
67 70
 	{0, 0, 0}
68 71
 };
69 72
 
... ...
@@ -108,7 +111,7 @@ static int mod_init(void)
108 111
 static void destroy(void)
109 112
 {
110 113
 	LM_DBG("destroy ...\n");
111
-	dbt_cache_print(0);
114
+	dbt_cache_print2(0, 0);
112 115
 	dbt_cache_destroy();
113 116
 }
114 117
 
... ...
@@ -125,6 +128,7 @@ int dbt_bind_api(db_func_t *dbb)
125 128
 	dbb->init        = dbt_init;
126 129
 	dbb->close       = dbt_close;
127 130
 	dbb->query       = (db_query_f)dbt_query;
131
+	dbb->fetch_result = (db_fetch_result_f) dbt_fetch_result;
128 132
 	dbb->free_result = dbt_free_result;
129 133
 	dbb->insert      = (db_insert_f)dbt_insert;
130 134
 	dbb->delete      = (db_delete_f)dbt_delete;
... ...
@@ -132,8 +136,7 @@ int dbt_bind_api(db_func_t *dbb)
132 136
 	dbb->replace     = (db_replace_f)dbt_replace;
133 137
 	dbb->affected_rows = (db_affected_rows_f) dbt_affected_rows;
134 138
 	dbb->raw_query   = (db_raw_query_f) dbt_raw_query;
135
-	dbb->cap         = DB_CAP_ALL | DB_CAP_AFFECTED_ROWS | DB_CAP_RAW_QUERY
136
-							| DB_CAP_REPLACE;
139
+	dbb->cap         = DB_CAP_ALL | DB_CAP_AFFECTED_ROWS | DB_CAP_RAW_QUERY | DB_CAP_REPLACE | DB_CAP_FETCH;
137 140
 
138 141
 	return 0;
139 142
 }
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -25,9 +25,9 @@
25 25
 #include <stdio.h>
26 26
 #include <unistd.h>
27 27
 
28
-#include "../../sr_module.h"
28
+#include "../../core/sr_module.h"
29 29
 #include "../../lib/srdb1/db.h"
30
-#include "../../rpc_lookup.h"
30
+#include "../../core/rpc_lookup.h"
31 31
 #include "db_text.h"
32 32
 #include "dbt_lib.h"
33 33
 #include "dbt_api.h"
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,160 @@
1
+/*
2
+ * DBText module interface
3
+ *
4
+ * Copyright (C) 2001-2003 FhG Fokus
5
+ * Copyright (C) 2014 Edvina AB, Olle E. Johansson
6
+ *
7
+ * This file is part of Kamailio, a free SIP server.
8
+ *
9
+ * Kamailio is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * Kamailio is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program; if not, write to the Free Software
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
+ *
23
+ */
24
+
25
+#include <stdio.h>
26
+#include <unistd.h>
27
+
28
+#include "../../sr_module.h"
29
+#include "../../lib/srdb1/db.h"
30
+#include "../../rpc_lookup.h"
31
+#include "db_text.h"
32
+#include "dbt_lib.h"
33
+#include "dbt_api.h"
34
+
35
+MODULE_VERSION
36
+
37
+static int mod_init(void);
38
+static void destroy(void);
39
+
40
+#define DEFAULT_DB_TEXT_READ_BUFFER_SIZE 16384
41
+
42
+/*
43
+ * Module parameter variables
44
+ */
45
+int db_mode = 0;  /* Database usage mode: 0 = cache, 1 = no cache */
46
+int empty_string = 0;  /* Treat empty string as "" = 0, 1 = NULL */
47
+int _db_text_read_buffer_size = DEFAULT_DB_TEXT_READ_BUFFER_SIZE;
48
+
49
+int dbt_bind_api(db_func_t *dbb);
50
+
51
+/*
52
+ * Exported functions
53
+ */
54
+static cmd_export_t cmds[] = {
55
+	{"db_bind_api",    (cmd_function)dbt_bind_api,   0, 0, 0, 0},
56
+	{0, 0, 0, 0, 0, 0}
57
+};
58
+
59
+
60
+/*
61
+ * Exported parameters
62
+ */
63
+static param_export_t params[] = {
64
+	{"db_mode", INT_PARAM, &db_mode},
65
+	{"emptystring", INT_PARAM, &empty_string},
66
+	{"file_buffer_size", INT_PARAM, &_db_text_read_buffer_size},
67
+	{0, 0, 0}
68
+};
69
+
70
+static rpc_export_t rpc_methods[];
71
+
72
+struct module_exports exports = {
73
+	"db_text",
74
+	DEFAULT_DLFLAGS, /* dlopen flags */
75
+	cmds,     /* Exported functions */
76
+	params,   /* Exported parameters */
77
+	0,        /* exported statistics */
78
+	0,        /* exported MI functions */
79
+	0,        /* exported pseudo-variables */
80
+	0,        /* extra processes */
81
+	mod_init, /* module initialization function */
82
+	0,        /* response function*/
83
+	destroy,  /* destroy function */
84
+	0         /* per-child init function */
85
+};
86
+
87
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
88
+{
89
+	if (rpc_register_array(rpc_methods)!=0) {
90
+		LM_ERR("failed to register RPC commands\n");
91
+		return -1;
92
+	}
93
+
94
+	if(db_api_init()<0)
95
+		return -1;
96
+	return 0;
97
+}
98
+
99
+static int mod_init(void)
100
+{
101
+	if(dbt_init_cache())
102
+		return -1;
103
+	/* return make_demo(); */
104
+
105
+	return 0;
106
+}
107
+
108
+static void destroy(void)
109
+{
110
+	LM_DBG("destroy ...\n");
111
+	dbt_cache_print(0);
112
+	dbt_cache_destroy();
113
+}
114
+
115
+
116
+
117
+int dbt_bind_api(db_func_t *dbb)
118
+{
119
+	if(dbb==NULL)
120
+		return -1;
121
+
122
+	memset(dbb, 0, sizeof(db_func_t));
123
+
124
+	dbb->use_table   = dbt_use_table;
125
+	dbb->init        = dbt_init;
126
+	dbb->close       = dbt_close;
127
+	dbb->query       = (db_query_f)dbt_query;
128
+	dbb->free_result = dbt_free_result;
129
+	dbb->insert      = (db_insert_f)dbt_insert;
130
+	dbb->delete      = (db_delete_f)dbt_delete;
131
+	dbb->update      = (db_update_f)dbt_update;
132
+	dbb->replace     = (db_replace_f)dbt_replace;
133
+	dbb->affected_rows = (db_affected_rows_f) dbt_affected_rows;
134
+	dbb->raw_query   = (db_raw_query_f) dbt_raw_query;
135
+	dbb->cap         = DB_CAP_ALL | DB_CAP_AFFECTED_ROWS | DB_CAP_RAW_QUERY
136
+							| DB_CAP_REPLACE;
137
+
138
+	return 0;
139
+}
140
+
141
+/* rpc function documentation */
142
+static const char *rpc_dump_doc[2] = {
143
+	"Write back to disk modified tables", 0
144
+};
145
+
146
+/* rpc function implementations */
147
+static void rpc_dump(rpc_t *rpc, void *c) {
148
+	if (0!=dbt_cache_print(0))
149
+		rpc->rpl_printf(c, "Dump failed");
150
+	else
151
+		rpc->rpl_printf(c, "Dump OK");
152
+
153
+	return;
154
+}
155
+
156
+static rpc_export_t rpc_methods[] = {
157
+	{"db_text.dump", rpc_dump, rpc_dump_doc, 0},
158
+	{0, 0, 0, 0}
159
+};
160
+