[asterisk-commits] twilson: branch twilson/sqlite_astdb r323928 - in /team/twilson/sqlite_astdb:...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jun 15 23:51:22 CDT 2011


Author: twilson
Date: Wed Jun 15 23:51:15 2011
New Revision: 323928

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=323928
Log:
Bug fixes and improvements

1) Better logs for conversion failures
2) Add "database query" CLI command to execute arbitrary SQL statements on
   the astdb. Of course this is highly dangerous since a "drop table astdb"
   would be particularly bad. This may or may not stick around, but is
   certainly useful for debugging right now.
3) Fix a bug with ast_db_put that crept in when changing things around for
   handling conversion via an external tool.
4) Register an atexit function to sync the database and close it.
5) Move astdb_init up in order of initialization. It is fairly self contained
   and provides an API for other parts of Asterisk. One wouldn't want someone
   to call ast_db_put before we had initialized the database.
6) Fix two bugs with the conersion utility--don't put the trailing null in the
   key and be sure and close the database.

Modified:
    team/twilson/sqlite_astdb/main/asterisk.c
    team/twilson/sqlite_astdb/main/db.c
    team/twilson/sqlite_astdb/utils/astdb2sqlite3.c

Modified: team/twilson/sqlite_astdb/main/asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite_astdb/main/asterisk.c?view=diff&rev=323928&r1=323927&r2=323928
==============================================================================
--- team/twilson/sqlite_astdb/main/asterisk.c (original)
+++ team/twilson/sqlite_astdb/main/asterisk.c Wed Jun 15 23:51:15 2011
@@ -3742,6 +3742,11 @@
 
 	ast_autoservice_init();
 
+	if (astdb_init()) {
+		printf("%s", term_quit());
+		exit(1);
+	}
+
 	if (ast_timing_init()) {
 		printf("%s", term_quit());
 		exit(1);
@@ -3828,11 +3833,6 @@
 	ast_features_init();
 
 	if (init_framer()) {
-		printf("%s", term_quit());
-		exit(1);
-	}
-
-	if (astdb_init()) {
 		printf("%s", term_quit());
 		exit(1);
 	}

Modified: team/twilson/sqlite_astdb/main/db.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite_astdb/main/db.c?view=diff&rev=323928&r1=323927&r2=323928
==============================================================================
--- team/twilson/sqlite_astdb/main/db.c (original)
+++ team/twilson/sqlite_astdb/main/db.c Wed Jun 15 23:51:15 2011
@@ -106,6 +106,8 @@
 AST_MUTEX_DEFINE_STATIC(dblock);
 static ast_cond_t dbcond;
 static sqlite3 *astdb;
+static pthread_t syncthread;
+static int doexit;
 
 static void db_sync(void);
 
@@ -194,7 +196,15 @@
 
 	if (stat(dbname, &dont_care) && !stat(ast_config_AST_DB, &dont_care)) {
 		if (convert_bdb_to_sqlite3()) {
-			ast_log(LOG_WARNING, "Database conversion failed!\n");
+			ast_log(LOG_ERROR, "*** Database conversion failed!\n");
+			ast_log(LOG_ERROR, "*** Asterisk now uses SQLite3 for its internal\n");
+			ast_log(LOG_ERROR, "*** database. Conversion from the old astdb\n");
+			ast_log(LOG_ERROR, "*** failed. Most likely the astdb2sqlite3 utility\n");
+			ast_log(LOG_ERROR, "*** was not selected for build. To convert the\n");
+			ast_log(LOG_ERROR, "*** old astdb, please delete '%s'\n", dbname);
+			ast_log(LOG_ERROR, "*** and re-run 'make menuselect' and select astdb2sqlite3\n");
+			ast_log(LOG_ERROR, "*** in the Utilities section, them 'make && make install'.\n");
+			sleep(5);
 		} else {
 			ast_log(LOG_NOTICE, "Database conversion succeeded!\n");
 		}
@@ -229,12 +239,12 @@
  * return rows and that shouldn't affect syncing--i.e. transaction calls.
  * We purposely don't lock around the sqlite3 call because the transaction
  * calls will be called with the database lock held. */
-static int db_execute_transaction_sql(const char *sql)
+static int db_execute_sql(const char *sql, int (*callback)(void *, int, char **, char **), void *arg)
 {
 	char *errmsg = NULL;
 	int res =0;
 
-	sqlite3_exec(astdb, sql, NULL, NULL, &errmsg);
+	sqlite3_exec(astdb, sql, callback, arg, &errmsg);
 	if (errmsg) {
 		ast_log(LOG_WARNING, "Error executing SQL: %s\n", errmsg);
 		sqlite3_free(errmsg);
@@ -246,17 +256,17 @@
 
 static int ast_db_begin_transaction(void)
 {
-	return db_execute_transaction_sql("BEGIN TRANSACTION");
+	return db_execute_sql("BEGIN TRANSACTION", NULL, NULL);
 }
 
 static int ast_db_commit_transaction(void)
 {
-	return db_execute_transaction_sql("COMMIT");
+	return db_execute_sql("COMMIT", NULL, NULL);
 }
 
 static int ast_db_rollback_transaction(void)
 {
-	return db_execute_transaction_sql("ROLLBACK");
+	return db_execute_sql("ROLLBACK", NULL, NULL);
 }
 
 int ast_db_put(const char *family, const char *key, const char *value)
@@ -273,7 +283,7 @@
 	fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
 
 	ast_mutex_lock(&dblock);
-	if (sqlite3_bind_text(put_stmt, 1, key, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
+	if (sqlite3_bind_text(put_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
 		ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
 		res = -1;
 	} else if (sqlite3_bind_text(put_stmt, 2, value, -1, SQLITE_STATIC) != SQLITE_OK) {
@@ -672,13 +682,53 @@
 	return CLI_SUCCESS;
 }
 
+static int display_results(void *arg, int columns, char **values, char **colnames)
+{
+	struct ast_cli_args *a = arg;
+	size_t x;
+
+	for (x = 0; x < columns; x++) {
+		ast_cli(a->fd, "%-5s: %-50s\n", colnames[x], values[x]);
+	}
+	ast_cli(a->fd, "\n");
+
+	return 0;
+}
+
+static char *handle_cli_database_query(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "database query";
+		e->usage =
+			"Usage: database query \"<SQL Statement>\"\n"
+			"       Run a user-specified SQL query on the database. Be careful.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != 3) {
+		return CLI_SHOWUSAGE;
+	}
+
+	ast_mutex_lock(&dblock);
+	db_execute_sql(a->argv[2], display_results, a);
+	db_sync(); /* Go ahead and sync the db in case they write */
+	ast_mutex_unlock(&dblock);
+
+	return CLI_SUCCESS;
+}
+
 static struct ast_cli_entry cli_database[] = {
 	AST_CLI_DEFINE(handle_cli_database_show,    "Shows database contents"),
 	AST_CLI_DEFINE(handle_cli_database_showkey, "Shows database contents"),
 	AST_CLI_DEFINE(handle_cli_database_get,     "Gets database value"),
 	AST_CLI_DEFINE(handle_cli_database_put,     "Adds/updates database value"),
 	AST_CLI_DEFINE(handle_cli_database_del,     "Removes database key/value"),
-	AST_CLI_DEFINE(handle_cli_database_deltree, "Removes database subfamily/values")
+	AST_CLI_DEFINE(handle_cli_database_deltree, "Removes database subfamily/values"),
+	AST_CLI_DEFINE(handle_cli_database_query,   "Run a user-specified query on the astdb"),
 };
 
 static int manager_dbput(struct mansession *s, const struct message *m)
@@ -827,6 +877,10 @@
 		if (ast_db_commit_transaction()) {
 			ast_db_rollback_transaction();
 		}
+		if (doexit) {
+			ast_mutex_unlock(&dblock);
+			break;
+		}
 		ast_db_begin_transaction();
 		ast_mutex_unlock(&dblock);
 		sleep(1);
@@ -836,10 +890,18 @@
 	return NULL;
 }
 
+static void astdb_atexit(void)
+{
+	doexit = 1;
+	db_sync();
+	pthread_join(syncthread, NULL);
+	ast_mutex_lock(&dblock);
+	sqlite3_close(astdb);
+	ast_mutex_unlock(&dblock);
+}
+
 int astdb_init(void)
 {
-	pthread_t syncthread;
-
 	if (db_init()) {
 		return -1;
 	}
@@ -849,6 +911,7 @@
 		return -1;
 	}
 
+	ast_register_atexit(astdb_atexit);
 	ast_cli_register_multiple(cli_database, ARRAY_LEN(cli_database));
 	ast_manager_register_xml("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget);
 	ast_manager_register_xml("DBPut", EVENT_FLAG_SYSTEM, manager_dbput);

Modified: team/twilson/sqlite_astdb/utils/astdb2sqlite3.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/sqlite_astdb/utils/astdb2sqlite3.c?view=diff&rev=323928&r1=323927&r2=323928
==============================================================================
--- team/twilson/sqlite_astdb/utils/astdb2sqlite3.c (original)
+++ team/twilson/sqlite_astdb/utils/astdb2sqlite3.c Wed Jun 15 23:51:15 2011
@@ -120,7 +120,7 @@
 	for (res = bdb->seq(bdb, &key, &value, R_FIRST);
 			!res; res = bdb->seq(bdb, &key, &value, R_NEXT)) {
 		last = !strcmp(key.data, last_key_s);
-		db_put_raw((const char *) key.data, key.size, (const char *) value.data, value.size);
+		db_put_raw((const char *) key.data, key.size - 1, (const char *) value.data, value.size - 1);
 		if (last) {
 			break;
 		}
@@ -222,7 +222,9 @@
 	if (convert_bdb_to_sqlite3(argv[1])) {
 		fprintf(stderr, "Database conversion failed!\n");
 		exit(-1);
-	}
-
-	return 0;
-}
+		sqlite3_close(astdb);
+	}
+
+	sqlite3_close(astdb);
+	return 0;
+}




More information about the asterisk-commits mailing list