[asterisk-commits] twilson: branch twilson/sqlite3_playground r342059 - in /team/twilson/sqlite3...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Oct 24 12:12:20 CDT 2011


Author: twilson
Date: Mon Oct 24 12:12:16 2011
New Revision: 342059

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=342059
Log:
Add constraint hints to ast_realtime_require_field

The require_field function was insufficient for the requirements of
creating the astdb table since it is important for the backend to return
an error when trying to insert a duplicate key. Also, without an index
on the key, update performance was horrible

This API change required all callers of ast_realtime_require_field to
add a second SENTINEL value if they aren't passing these requirements,
but this was the only way I could think of doing things that wouldn't
require re-writing the realtime backends to support the new feature.

Modified:
    team/twilson/sqlite3_playground/apps/app_meetme.c
    team/twilson/sqlite3_playground/apps/app_queue.c
    team/twilson/sqlite3_playground/apps/app_voicemail.c
    team/twilson/sqlite3_playground/channels/chan_iax2.c
    team/twilson/sqlite3_playground/channels/chan_sip.c
    team/twilson/sqlite3_playground/include/asterisk/config.h
    team/twilson/sqlite3_playground/main/db.c
    team/twilson/sqlite3_playground/main/logger.c
    team/twilson/sqlite3_playground/main/realtime_sqlite3.c

Modified: team/twilson/sqlite3_playground/apps/app_meetme.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/apps/app_meetme.c?view=diff&rev=342059&r1=342058&r2=342059
==============================================================================
--- team/twilson/sqlite3_playground/apps/app_meetme.c (original)
+++ team/twilson/sqlite3_playground/apps/app_meetme.c Mon Oct 24 12:12:16 2011
@@ -2574,7 +2574,7 @@
 		ast_realtime_require_field("meetme",
 			"confno", strlen(conf->confno) > 7 ? RQ_UINTEGER4 : strlen(conf->confno) > 4 ? RQ_UINTEGER3 : RQ_UINTEGER2, strlen(conf->confno),
 			"members", RQ_UINTEGER1, strlen(members),
-			NULL);
+			SENTINEL, SENTINEL);
 		ast_update_realtime("meetme", "confno", conf->confno, "members", members, NULL);
 	}
 	setusercount = 1;
@@ -3878,7 +3878,7 @@
 				ast_realtime_require_field("meetme",
 					"confno", strlen(conf->confno) > 7 ? RQ_UINTEGER4 : strlen(conf->confno) > 4 ? RQ_UINTEGER3 : RQ_UINTEGER2, strlen(conf->confno),
 					"members", RQ_UINTEGER1, strlen(members),
-					NULL);
+					SENTINEL, SENTINEL);
 				ast_update_realtime("meetme", "confno", conf->confno, "members", members, NULL);
 			}
 			if (ast_test_flag64(confflags, CONFFLAG_MARKEDUSER)) {
@@ -7347,7 +7347,7 @@
 	res |= ast_devstate_prov_add("SLA", sla_state);
 
 	res |= ast_custom_function_register(&meetme_info_acf);
-	ast_realtime_require_field("meetme", "confno", RQ_UINTEGER2, 3, "members", RQ_UINTEGER1, 3, NULL);
+	ast_realtime_require_field("meetme", "confno", RQ_UINTEGER2, 3, "members", RQ_UINTEGER1, 3, SENTINEL, SENTINEL);
 
 	return res;
 }

Modified: team/twilson/sqlite3_playground/apps/app_queue.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/apps/app_queue.c?view=diff&rev=342059&r1=342058&r2=342059
==============================================================================
--- team/twilson/sqlite3_playground/apps/app_queue.c (original)
+++ team/twilson/sqlite3_playground/apps/app_queue.c Mon Oct 24 12:12:16 2011
@@ -8686,7 +8686,7 @@
 
 	ast_extension_state_add(NULL, NULL, extension_state_cb, NULL);
 
-	ast_realtime_require_field("queue_members", "paused", RQ_INTEGER1, 1, "uniqueid", RQ_UINTEGER2, 5, SENTINEL);
+	ast_realtime_require_field("queue_members", "paused", RQ_INTEGER1, 1, "uniqueid", RQ_UINTEGER2, 5, SENTINEL, SENTINEL);
 
 	return res ? AST_MODULE_LOAD_DECLINE : 0;
 }

Modified: team/twilson/sqlite3_playground/apps/app_voicemail.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/apps/app_voicemail.c?view=diff&rev=342059&r1=342058&r2=342059
==============================================================================
--- team/twilson/sqlite3_playground/apps/app_voicemail.c (original)
+++ team/twilson/sqlite3_playground/apps/app_voicemail.c Mon Oct 24 12:12:16 2011
@@ -1286,7 +1286,7 @@
 	}
 
 	if (strlen(password) > 10) {
-		ast_realtime_require_field("voicemail", "password", RQ_CHAR, strlen(password), SENTINEL);
+		ast_realtime_require_field("voicemail", "password", RQ_CHAR, strlen(password), SENTINEL, SENTINEL);
 	}
 	if (ast_update2_realtime("voicemail", "context", vmu->context, "mailbox", vmu->mailbox, SENTINEL, "password", password, SENTINEL) > 0) {
 		ast_test_suite_event_notify("PASSWORDCHANGED", "Message: realtime engine updated with new password\r\nPasswordSource: realtime");
@@ -12991,8 +12991,8 @@
 	ast_data_register_multiple(vm_data_providers, ARRAY_LEN(vm_data_providers));
 
 	ast_install_vm_functions(has_voicemail, inboxcount, inboxcount2, messagecount, sayname);
-	ast_realtime_require_field("voicemail", "uniqueid", RQ_UINTEGER3, 11, "password", RQ_CHAR, 10, SENTINEL);
-	ast_realtime_require_field("voicemail_data", "filename", RQ_CHAR, 30, "duration", RQ_UINTEGER3, 5, SENTINEL);
+	ast_realtime_require_field("voicemail", "uniqueid", RQ_UINTEGER3, 11, "password", RQ_CHAR, 10, SENTINEL, SENTINEL);
+	ast_realtime_require_field("voicemail_data", "filename", RQ_CHAR, 30, "duration", RQ_UINTEGER3, 5, SENTINEL, SENTINEL);
 
 	return res;
 }

Modified: team/twilson/sqlite3_playground/channels/chan_iax2.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/channels/chan_iax2.c?view=diff&rev=342059&r1=342058&r2=342059
==============================================================================
--- team/twilson/sqlite3_playground/channels/chan_iax2.c (original)
+++ team/twilson/sqlite3_playground/channels/chan_iax2.c Mon Oct 24 12:12:16 2011
@@ -14870,7 +14870,7 @@
 	reload_firmware(0);
 	iax_provision_reload(0);
 
-	ast_realtime_require_field("iaxpeers", "name", RQ_CHAR, 10, "ipaddr", RQ_CHAR, 15, "port", RQ_UINTEGER2, 5, "regseconds", RQ_UINTEGER2, 6, SENTINEL);
+	ast_realtime_require_field("iaxpeers", "name", RQ_CHAR, 10, "ipaddr", RQ_CHAR, 15, "port", RQ_UINTEGER2, 5, "regseconds", RQ_UINTEGER2, 6, SENTINEL, SENTINEL);
 
 	network_change_event_subscribe();
 

Modified: team/twilson/sqlite3_playground/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/channels/chan_sip.c?view=diff&rev=342059&r1=342058&r2=342059
==============================================================================
--- team/twilson/sqlite3_playground/channels/chan_sip.c (original)
+++ team/twilson/sqlite3_playground/channels/chan_sip.c Mon Oct 24 12:12:16 2011
@@ -30536,7 +30536,7 @@
 		"regserver", RQ_CHAR, 20,
 		"useragent", RQ_CHAR, 20,
 		"lastms", RQ_INTEGER4, 11,
-		SENTINEL);
+		SENTINEL, SENTINEL);
 
 
 	sip_register_tests();

Modified: team/twilson/sqlite3_playground/include/asterisk/config.h
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/include/asterisk/config.h?view=diff&rev=342059&r1=342058&r2=342059
==============================================================================
--- team/twilson/sqlite3_playground/include/asterisk/config.h (original)
+++ team/twilson/sqlite3_playground/include/asterisk/config.h Mon Oct 24 12:12:16 2011
@@ -74,6 +74,13 @@
 	RQ_DATETIME,
 } require_type;
 
+typedef enum {
+	RQ_NONE        = (1 << 0),
+	RQ_PRIMARY_KEY = (1 << 1),
+	RQ_NOT_NULL    = (1 << 2),
+	RQ_UNIQUE      = (1 << 3),
+} require_constraint;
+
 /*! \brief Structure for variables, used for configurations and for channel variables */
 struct ast_variable {
 	/*! Variable name.  Stored in stuff[] at struct end. */
@@ -342,6 +349,13 @@
  * a timeout value may reasonably be specified as an INTEGER2, with size 5.
  * Even though values above 32767 seconds are possible, they are unlikely
  * to be useful, and we should not complain about that size).
+ *
+ * As of Asterisk 11, there is a second set of fields after the first
+ * SENTINEL that contain constraints for each field. Example constraints
+ * would be RQ_NOT_NULL and RQ_UNIQUE. Any field without constraints must
+ * be have RQ_NONE set. The second set of fields is optional, and support
+ * for parsing them is realtime-backend dependent. It is important that
+ * calls to ast_realtime_require_field have two SENTINELs.
  *
  * \retval 0 Required fields met specified standards
  * \retval -1 One or more fields was missing or insufficient

Modified: team/twilson/sqlite3_playground/main/db.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/main/db.c?view=diff&rev=342059&r1=342058&r2=342059
==============================================================================
--- team/twilson/sqlite3_playground/main/db.c (original)
+++ team/twilson/sqlite3_playground/main/db.c Mon Oct 24 12:12:16 2011
@@ -620,7 +620,13 @@
 
 int astdb_init(void)
 {
-	if (ast_realtime_require_family(ast_db_family, ast_db_key, RQ_CHAR, 256, ast_db_value, RQ_CHAR, 256, SENTINEL)) {
+	if (ast_realtime_require_family(ast_db_family,
+			ast_db_key, RQ_CHAR, 256,
+			ast_db_value, RQ_CHAR, 256,
+			SENTINEL,
+			RQ_PRIMARY_KEY,
+			RQ_NOT_NULL,
+			SENTINEL)) {
 		ast_log(LOG_ERROR, "Could not find proper internal database backend.\n");
 		return -1;
 	}

Modified: team/twilson/sqlite3_playground/main/logger.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/main/logger.c?view=diff&rev=342059&r1=342058&r2=342059
==============================================================================
--- team/twilson/sqlite3_playground/main/logger.c (original)
+++ team/twilson/sqlite3_playground/main/logger.c Mon Oct 24 12:12:16 2011
@@ -526,7 +526,7 @@
 				"data3", RQ_CHAR, strlen(S_OR(args.data[2], "")),
 				"data4", RQ_CHAR, strlen(S_OR(args.data[3], "")),
 				"data5", RQ_CHAR, strlen(S_OR(args.data[4], "")),
-				SENTINEL);
+				SENTINEL, SENTINEL);
 
 			/* Store the log */
 			ast_store_realtime("queue_log", "time", time_str,
@@ -680,7 +680,7 @@
 			"data3", RQ_CHAR, 20,
 			"data4", RQ_CHAR, 20,
 			"data5", RQ_CHAR, 20,
-			SENTINEL)) {
+			SENTINEL, SENTINEL)) {
 			logfiles.queue_adaptive_realtime = 1;
 		} else {
 			logfiles.queue_adaptive_realtime = 0;

Modified: team/twilson/sqlite3_playground/main/realtime_sqlite3.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite3_playground/main/realtime_sqlite3.c?view=diff&rev=342059&r1=342058&r2=342059
==============================================================================
--- team/twilson/sqlite3_playground/main/realtime_sqlite3.c (original)
+++ team/twilson/sqlite3_playground/main/realtime_sqlite3.c Mon Oct 24 12:12:16 2011
@@ -933,6 +933,24 @@
 	return "TEXT";
 }
 
+static const char *constraint_to_str(unsigned int flags)
+{
+	switch (flags) {
+	case RQ_NONE :
+		return "";
+	case RQ_PRIMARY_KEY:
+		return "PRIMARY KEY ASC";
+	case RQ_UNIQUE | RQ_NOT_NULL :
+		return "NOT NULL UNIQUE";
+	case RQ_UNIQUE:
+		return "UNIQUE";
+	case RQ_NOT_NULL:
+		return "NOT NULL";
+	default :
+		return "";
+	}
+}
+
 /*! \brief Create a table if ast_realtime_require shows that we are configured to handle the data
  */
 static int handle_missing_table(struct realtime_sqlite3_db *db, const char *table, va_list ap)
@@ -941,20 +959,41 @@
 	int type, first = 1, res;
 	size_t sz;
 	struct ast_str *sql;
+	va_list aq;
+	unsigned int flags, has_flags = 0;
 
 	if (!(sql = ast_str_create(128))) {
 		return -1;
+	}
+
+	va_copy(aq, ap);
+	/* In the copy, skip to the first SENTINEL */
+	while ((column = va_arg(aq, typeof(column))) && ((type = va_arg(aq, typeof(type))) >= 0) && (sz = va_arg(aq, typeof(sz)))) {
+		;
+	}
+
+	/* Check to see if there are flags after the SENTINEL as they are optional */
+	if ((flags = va_arg(aq, typeof(flags)))) { /* flags is a bitmask, so must be > 0 to be valid */
+		has_flags = 1;
+		ast_log(LOG_NOTICE, "Got flag: %u\n", flags);
 	}
 
 	while ((column = va_arg(ap, typeof(column))) && ((type = va_arg(ap, typeof(type))) >= 0) && (sz = va_arg(ap, typeof(sz)))) {
 		if (first) {
-			ast_str_set(&sql, 0, "CREATE TABLE IF NOT EXISTS %s (%s %s", sqlite3_escape_table(table),
-					sqlite3_escape_column(column), get_sqlite_column_type(type));
+			ast_str_set(&sql, 0, "CREATE TABLE IF NOT EXISTS %s (%s %s %s", sqlite3_escape_table(table),
+					sqlite3_escape_column(column), get_sqlite_column_type(type), has_flags ? constraint_to_str(flags) : "");
 			first = 0;
 		} else {
-			ast_str_append(&sql, 0, ", %s %s", sqlite3_escape_column(column), get_sqlite_column_type(type));
-		}
-	}
+			if (has_flags) {
+				/* There must be a flag for each column */
+				flags = va_arg(aq, typeof(flags));
+				ast_str_append(&sql, 0, ", %s %s %s", sqlite3_escape_column(column), get_sqlite_column_type(type), constraint_to_str(flags));
+			} else {
+				ast_str_append(&sql, 0, ", %s %s", sqlite3_escape_column(column), get_sqlite_column_type(type));
+			}
+		}
+	}
+	va_end(aq);
 
 	ast_str_append(&sql, 0, ")");
 




More information about the asterisk-commits mailing list