[Asterisk-code-review] realtime: Centralize some common realtime backend code (asterisk[master])

Sean Bright asteriskteam at digium.com
Tue Feb 21 09:04:10 CST 2017


Sean Bright has uploaded a new change for review. ( https://gerrit.asterisk.org/5047 )

Change subject: realtime: Centralize some common realtime backend code
......................................................................

realtime: Centralize some common realtime backend code

All of the realtime backends create artificial ast_categorys to pass
back into the core as query results. These categories have no filename
or line number information associated with them and the backends differ
slightly on how they create them. So create a couple helper macros to
help make things more consistent.

Also updated the call sites to use the same verbiage when creating a
category fails.

Note that res_config_ldap sets the category filename to the 'table name'
but that is not read by anything in the core, so I've dropped it.

Change-Id: I3a1fd91e0c807dea1ce3b643b0a6fe5be9002897
---
M addons/res_config_mysql.c
M include/asterisk/config.h
M res/res_config_curl.c
M res/res_config_ldap.c
M res/res_config_odbc.c
M res/res_config_pgsql.c
M res/res_config_sqlite.c
M res/res_config_sqlite3.c
8 files changed, 43 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/47/5047/1

diff --git a/addons/res_config_mysql.c b/addons/res_config_mysql.c
index f2ef949..ace8c1f 100644
--- a/addons/res_config_mysql.c
+++ b/addons/res_config_mysql.c
@@ -523,9 +523,8 @@
 
 		while ((row = mysql_fetch_row(result))) {
 			var = NULL;
-			cat = ast_category_new("", "", -1);
-			if (!cat) {
-				ast_log(LOG_WARNING, "Out of memory!\n");
+			if (!(cat = ast_category_new_anonymous())) {
+				ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 				continue;
 			}
 			for (i = 0; i < numFields; i++) {
@@ -934,8 +933,8 @@
 			}
 
 			if (strcmp(last, row[0]) || last_cat_metric != atoi(row[3])) {
-				if (!(cur_cat = ast_category_new(row[0], "", -1))) {
-					ast_log(LOG_WARNING, "Out of memory!\n");
+				if (!(cur_cat = ast_category_new_dynamic(row[0]))) {
+					ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 					break;
 				}
 				strcpy(last, row[0]);
diff --git a/include/asterisk/config.h b/include/asterisk/config.h
index 4944a3a..f57966b 100644
--- a/include/asterisk/config.h
+++ b/include/asterisk/config.h
@@ -834,6 +834,18 @@
 struct ast_category *ast_category_new(const char *name, const char *in_file, int lineno);
 
 /*!
+ * \brief Create a category that is not backed by a file
+ *
+ * \param name name of new category
+ */
+#define ast_category_new_dynamic(name) ast_category_new(name, "", -1)
+
+/*!
+ * \brief Create a nameless category that is not backed by a file
+ */
+#define ast_category_new_anonymous() ast_category_new_dynamic("")
+
+/*!
  * \brief Create a category making it a template
  *
  * \param name name of new template
diff --git a/res/res_config_curl.c b/res/res_config_curl.c
index 9ad7a6e..eb102d4 100644
--- a/res/res_config_curl.c
+++ b/res/res_config_curl.c
@@ -182,7 +182,8 @@
 			continue;
 		}
 
-		if (!(cat = ast_category_new("", "", 99999))) {
+		if (!(cat = ast_category_new_anonymous())) {
+			ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 			continue;
 		}
 
@@ -569,8 +570,10 @@
 		}
 
 		if (!cat || strcmp(category, cur_cat) || last_cat_metric != cat_metric) {
-			if (!(cat = ast_category_new(category, "", 99999)))
+			if (!(cat = ast_category_new_dynamic(category))) {
+				ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 				break;
+			}
 			cur_cat = category;
 			last_cat_metric = cat_metric;
 			ast_category_append(cfg, cat);
diff --git a/res/res_config_ldap.c b/res/res_config_ldap.c
index 5e95853..f888f9e 100644
--- a/res/res_config_ldap.c
+++ b/res/res_config_ldap.c
@@ -1044,9 +1044,8 @@
 
 			while (*p) {
 				struct ast_category *cat = NULL;
-				cat = ast_category_new("", table_name, -1);
-				if (!cat) {
-					ast_log(LOG_ERROR, "Unable to create a new category!\n");
+				if (!(cat = ast_category_new_anonymous())) {
+					ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 					break;
 				} else {
 					struct ast_variable *var = *p;
@@ -1195,8 +1194,8 @@
 		if (!last_category || strcmp(last_category, categories[i].name) ||
 			last_category_metric != categories[i].metric) {
 
-			cur_cat = ast_category_new(categories[i].name, table_name, -1);
-			if (!cur_cat) {
+			if (!(cur_cat = ast_category_new_dynamic(categories[i].name))) {
+				ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 				break;
 			}
 			last_category = categories[i].name;
diff --git a/res/res_config_odbc.c b/res/res_config_odbc.c
index 5a25b6b..e016353 100644
--- a/res/res_config_odbc.c
+++ b/res/res_config_odbc.c
@@ -407,9 +407,8 @@
 			ast_log(LOG_WARNING, "SQL Fetch error! [%s]\n", ast_str_buffer(sql));
 			continue;
 		}
-		cat = ast_category_new("","",99999);
-		if (!cat) {
-			ast_log(LOG_WARNING, "Out of memory!\n");
+		if (!(cat = ast_category_new_anonymous())) {
+			ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 			continue;
 		}
 		for (x=0;x<colcount;x++) {
@@ -1016,9 +1015,8 @@
 			continue;
 		} 
 		if (strcmp(last, q.category) || last_cat_metric != q.cat_metric) {
-			cur_cat = ast_category_new(q.category, "", 99999);
-			if (!cur_cat) {
-				ast_log(LOG_WARNING, "Out of memory!\n");
+			if (!(cur_cat = ast_category_new_dynamic(q.category))) {
+				ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 				break;
 			}
 			strcpy(last, q.category);
diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c
index f085961..04c4660 100644
--- a/res/res_config_pgsql.c
+++ b/res/res_config_pgsql.c
@@ -648,8 +648,10 @@
 
 		for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
 			var = NULL;
-			if (!(cat = ast_category_new("","",99999)))
+			if (!(cat = ast_category_new_anonymous())) {
+				ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 				continue;
+			}
 			for (i = 0; i < numFields; i++) {
 				stringp = PQgetvalue(result, rowIndex, i);
 				while (stringp) {
@@ -1129,9 +1131,10 @@
 			}
 
 			if (strcmp(last, field_category) || last_cat_metric != atoi(field_cat_metric)) {
-				cur_cat = ast_category_new(field_category, "", 99999);
-				if (!cur_cat)
+				if (!(cur_cat = ast_category_new_dynamic(field_category))) {
+					ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 					break;
+				}
 				ast_copy_string(last, field_category, sizeof(last));
 				last_cat_metric = atoi(field_cat_metric);
 				ast_category_append(cfg, cur_cat);
diff --git a/res/res_config_sqlite.c b/res/res_config_sqlite.c
index 6859497..3aaacc9 100644
--- a/res/res_config_sqlite.c
+++ b/res/res_config_sqlite.c
@@ -874,10 +874,10 @@
 	}
 
 	if (!args->cat_name || strcmp(args->cat_name, argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY])) {
-		args->cat = ast_category_new(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY], "", 99999);
+		args->cat = ast_category_new_dynamic(argv[RES_CONFIG_SQLITE_CONFIG_CATEGORY]);
 
 		if (!args->cat) {
-			ast_log(LOG_WARNING, "Unable to allocate category\n");
+			ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 			return 1;
 		}
 
@@ -1086,8 +1086,8 @@
 		return 1;
 	}
 
-	if (!(cat = ast_category_new(cat_name, "", 99999))) {
-		ast_log(LOG_WARNING, "Unable to allocate category\n");
+	if (!(cat = ast_category_new_anonymous())) {
+		ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 		return 1;
 	}
 
diff --git a/res/res_config_sqlite3.c b/res/res_config_sqlite3.c
index 8c514b0..5e15fd2 100644
--- a/res/res_config_sqlite3.c
+++ b/res/res_config_sqlite3.c
@@ -503,7 +503,8 @@
 	struct ast_category *cat;
 	int i;
 
-	if (!(cat = ast_category_new("", "", 99999))) {
+	if (!(cat = ast_category_new_anonymous())) {
+		ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 		return SQLITE_ABORT;
 	}
 
@@ -723,8 +724,8 @@
 	}
 
 	if (!args->cat_name || strcmp(args->cat_name, values[COL_CATEGORY])) {
-		if (!(args->cat = ast_category_new(values[COL_CATEGORY], "", 99999))) {
-			ast_log(LOG_WARNING, "Unable to allocate category\n");
+		if (!(args->cat = ast_category_new_dynamic(values[COL_CATEGORY]))) {
+			ast_log(LOG_ERROR, "Out of memory allocating new category\n");
 			return SQLITE_ABORT;
 		}
 

-- 
To view, visit https://gerrit.asterisk.org/5047
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3a1fd91e0c807dea1ce3b643b0a6fe5be9002897
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>



More information about the asterisk-code-review mailing list