[asterisk-commits] twilson: branch twilson/res_config_sqlite3 r334354 - /team/twilson/res_config...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Sep 2 15:41:07 CDT 2011


Author: twilson
Date: Fri Sep  2 15:40:56 2011
New Revision: 334354

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=334354
Log:
Clean up some stuff, add some minimal documentation

Modified:
    team/twilson/res_config_sqlite3/res/res_config_sqlite3.c

Modified: team/twilson/res_config_sqlite3/res/res_config_sqlite3.c
URL: http://svnview.digium.com/svn/asterisk/team/twilson/res_config_sqlite3/res/res_config_sqlite3.c?view=diff&rev=334354&r1=334353&r2=334354
==============================================================================
--- team/twilson/res_config_sqlite3/res/res_config_sqlite3.c (original)
+++ team/twilson/res_config_sqlite3/res/res_config_sqlite3.c Fri Sep  2 15:40:56 2011
@@ -143,6 +143,14 @@
 	*db = NULL;
 }
 
+static int stop_batch_cb(void *obj, void *arg, int flags)
+{
+	struct realtime_sqlite3_db *db = obj;
+
+	db_stop_batch(db);
+	return CMP_MATCH;
+}
+
 static int mark_dirty_cb(void *obj, void *arg, int flags)
 {
 	struct realtime_sqlite3_db *db = obj;
@@ -181,13 +189,14 @@
 	return REALTIME_SQLITE3_REQ_WARN;
 }
 
-/* Since this is called while a query is executing, we should already hold the db lock */
+/*! \note Since this is called while a query is executing, we should already hold the db lock */
 static void trace_cb(void *arg, const char *sql)
 {
 	struct realtime_sqlite3_db *db = arg;
 	ast_debug(3, "DB: %s SQL: %s\n", db->name, sql);
 }
 
+/*! \brief Wrap commands in transactions increased write performance */
 static void *db_sync_thread(void *data)
 {
 	struct realtime_sqlite3_db *db = data;
@@ -216,6 +225,7 @@
 	return NULL;
 }
 
+/*! \brief Open a database and appropriately set debugging on the db handle */
 static int db_open(struct realtime_sqlite3_db *db)
 {
 	ao2_lock(db);
@@ -260,6 +270,9 @@
 	}
 }
 
+/*! \brief Create a db object based on a config category
+ * \note Opening the db handle and linking to databases must be handled outside of this function
+ */
 static struct realtime_sqlite3_db *new_realtime_sqlite3_db(struct ast_config *config, const char *cat)
 {
 	struct ast_variable *var;
@@ -300,6 +313,11 @@
 	return db;
 }
 
+/*! \brief Update an existing db object based on config data
+ * \param db The database object to update
+ * \param config The configuration data with which to update the db
+ * \param cat The config category (which becomes db->name)
+ */
 static int update_realtime_sqlite3_db(struct realtime_sqlite3_db *db, struct ast_config *config, const char *cat)
 {
 	struct realtime_sqlite3_db *new;
@@ -344,6 +362,7 @@
 	return 0;
 }
 
+/*! \brief Create a varlist from a single sqlite3 result row */
 static int row_to_varlist(void *arg, int num_columns, char **values, char **columns)
 {
 	struct ast_variable **head = arg;
@@ -360,6 +379,7 @@
 	return 0;
 }
 
+/*! \brief Callback for creating an ast_config from a successive sqlite3 result rows */ 
 static int append_row_to_cfg(void *arg, int num_columns, char **values, char **columns)
 {
 	struct ast_config *cfg = arg;
@@ -384,7 +404,7 @@
 }
 
 /*!
- * Structure sent to the SQLite callback function for static configuration.
+ * Structure sent to the SQLite 3 callback function for static configuration.
  *
  * \see static_realtime_cb()
  */
@@ -396,6 +416,56 @@
 	const char *who_asked;
 };
 
+/*! Exeute an SQL statement given the database object
+ *
+ * \retval -1 ERROR
+ * \retval > -1 Number of rows changed
+ */
+static int realtime_sqlite3_execute_handle(struct realtime_sqlite3_db *db, const char *sql, int (*callback)(void*, int, char **, char **), void *arg, int sync)
+{
+	int res = 0;
+	char *errmsg;
+
+	ao2_lock(db);
+	if (sqlite3_exec(db->handle, sql, callback, arg, &errmsg) != SQLITE_OK) {
+		ast_log(LOG_WARNING, "Could not execute '%s': %s\n", sql, errmsg);
+		sqlite3_free(errmsg);
+		res = -1;
+	} else {
+		res = sqlite3_changes(db->handle);
+	}
+	ao2_unlock(db);
+
+	if (sync) {
+		db_sync(db);
+	}
+
+	return res;
+}
+
+/*! Exeute an SQL statement give the database name
+ *
+ * \retval -1 ERROR
+ * \retval > -1 Number of rows changed
+ */
+static int realtime_sqlite3_execute(const char *database, const char *sql, int (*callback)(void*, int, char **, char **), void *arg, int sync)
+{
+	struct realtime_sqlite3_db *db;
+	int res;
+
+	if (!(db = find_database(database))) {
+		ast_log(LOG_WARNING, "Could not find database: %s\n", database);
+		return -1;
+	}
+
+	res = realtime_sqlite3_execute_handle(db, sql, callback, arg, sync);
+	ao2_ref(db, -1);
+
+	return res;
+}
+
+/*! \note It is important that the COL_* enum matches the order of the columns selected in static_sql */
+static const char *static_sql = "SELECT category, var_name, var_val FROM %Q WHERE filename = %Q AND commented = 0 ORDER BY cat_metric ASC, var_metric ASC";
 enum {
 	COL_CATEGORY,
 	COL_VAR_NAME,
@@ -448,55 +518,7 @@
 	return 0;
 }
 
-/*! Exeute an SQL statement
- *
- * \retval -1 ERROR
- * \retval > -1 Number of rows changed
- */
-static int realtime_sqlite3_execute_handle(struct realtime_sqlite3_db *db, const char *sql, int (*callback)(void*, int, char **, char **), void *arg, int sync)
-{
-	int res = 0;
-	char *errmsg;
-
-	ao2_lock(db);
-	if (sqlite3_exec(db->handle, sql, callback, arg, &errmsg) != SQLITE_OK) {
-		ast_log(LOG_WARNING, "Could not execute '%s': %s\n", sql, errmsg);
-		sqlite3_free(errmsg);
-		res = -1;
-	} else {
-		res = sqlite3_changes(db->handle);
-	}
-	ao2_unlock(db);
-
-	if (sync) {
-		db_sync(db);
-	}
-
-	return res;
-}
-
-/*! Exeute an SQL statement
- *
- * \retval -1 ERROR
- * \retval > -1 Number of rows changed
- */
-static int realtime_sqlite3_execute(const char *database, const char *sql, int (*callback)(void*, int, char **, char **), void *arg, int sync)
-{
-	struct realtime_sqlite3_db *db;
-	int res;
-
-	if (!(db = find_database(database))) {
-		ast_log(LOG_WARNING, "Could not find database: %s\n", database);
-		return -1;
-	}
-
-	res = realtime_sqlite3_execute_handle(db, sql, callback, arg, sync);
-	ao2_ref(db, -1);
-
-	return res;
-}
-
-/*!
+/*! \brief Realtime callback for static realtime
  * \return ast_config on success, NULL on failure
  */
 static struct ast_config *realtime_sqlite3_load(const char *database, const char *table, const char *configfile, struct ast_config *config, struct ast_flags flags, const char *suggested_include_file, const char *who_asked)
@@ -509,7 +531,7 @@
 		return NULL;
 	}
 
-	if (!(sql = sqlite3_mprintf("SELECT category, var_name, var_val FROM %Q WHERE filename = %Q AND commented = 0 ORDER BY cat_metric ASC, var_metric ASC", table, configfile))) {
+	if (!(sql = sqlite3_mprintf(static_sql, table, configfile))) {
 		ast_log(LOG_WARNING, "Couldn't allocate query\n");
 		return NULL;	
 	};
@@ -527,6 +549,7 @@
 	return config;
 }
 
+/*! \brief Helper function for single and multi-row realtime load functions */
 static int realtime_sqlite3_helper(const char *database, const char *table, va_list ap, int is_multi, void *arg)
 {
 	struct ast_str *sql;
@@ -565,7 +588,7 @@
 	return 0;
 }
 
-/*!
+/*! \brief Realtime callback for a single row query
  * \return ast_variable list for single result on success, NULL on empty/failure
  */
 static struct ast_variable *realtime_sqlite3(const char *database, const char *table, va_list ap)
@@ -577,7 +600,7 @@
 	return result_row;
 }
 
-/*!
+/*! \brief Realtime callback for a multi-row query
  * \return ast_config containing possibly many results on success, NULL on empty/failure
  */
 static struct ast_config *realtime_sqlite3_multi(const char *database, const char *table, va_list ap)
@@ -596,7 +619,7 @@
 	return cfg;
 }
 
-/*!
+/*! \brief Realtime callback for updating a row based on a single criteria
  * \return Number of rows affected or -1 on error
  */
 static int realtime_sqlite3_update(const char *database, const char *table, const char *keyfield, const char *entity, va_list ap)
@@ -631,7 +654,7 @@
 	return tmp;
 }
 
-/*!
+/*! \brief Realtime callback for updating a row based on multiple criteria
  * \return Number of rows affected or -1 on error
  */
 static int realtime_sqlite3_update2(const char *database, const char *table, va_list ap)
@@ -684,7 +707,7 @@
 	return tmp;
 }
 
-/*!
+/*! \brief Realtime callback for inserting a row
  * \return Number of rows affected or -1 on error
  */
 static int realtime_sqlite3_store(const char *database, const char *table, va_list ap)
@@ -728,7 +751,7 @@
 	return tmp;
 }
 
-/*!
+/*! \brief Realtime callback for deleting a row
  * \return Number of rows affected or -1 on error
  */
 static int realtime_sqlite3_destroy(const char *database, const char *table, const char *keyfield, const char *entity, va_list ap)
@@ -762,13 +785,14 @@
 	return tmp;
 }
 
+/*! \brief Convert Asterisk realtime types to SQLite 3 types
+ * \note SQLite 3 has NULL, INTEGER, REAL, TEXT, and BLOB types. Any column other than
+ * an INTEGER PRIMARY KEY will actually store any kind of data due to its dynamic
+ * typing. When we create columns, we'll go ahead and use these base types instead
+ * of messing with column widths, etc. */
+
 static const char *get_sqlite_column_type(int type)
 {
-	/* SQLite 3 has NULL, INTEGER, REAL, TEXT, and BLOB types. Any column other than
-	 * an INTEGER PRIMARY KEY will actually store any kind of data due to its dynamic
-	 * typing. When we create columns, we'll go ahead and use these base types instead
-	 * of messing with column widths, etc. */
-
 	switch(type) {
 	case RQ_INTEGER1 :
 	case RQ_UINTEGER1 :
@@ -794,6 +818,8 @@
 	return "TEXT";
 }
 
+/*! \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)
 {
 	const char *column;
@@ -822,6 +848,8 @@
 	return tmp;
 }
 
+/*! \brief If ast_realtime_require sends info about a column we don't have, create it
+ */
 static int handle_missing_column(struct realtime_sqlite3_db *db, const char *table, const char *column, int type, size_t sz)
 {
 	char *sql;
@@ -857,6 +885,8 @@
 	return !strcasecmp((const char *) obj, (const char *) arg);
 }
 
+/*! \brief Callback for creating a hash of column names for comparison in realtime_sqlite3_require
+ */
 static int add_column_name(void *arg, int num_columns, char **values, char **columns)
 {
 	char *column;
@@ -875,7 +905,7 @@
 	return 0;
 }
 
-/*!
+/*! \brief Callback for ast_realtime_require
  * \retval 0 Required fields met specified standards
  * \retval -1 One or more fields was missing or insufficient
  */
@@ -950,7 +980,8 @@
 	return 0;
 }
 
-/*!
+/*! \brief Callback for clearing any cached info
+ * \note We don't currently cache anything
  * \retval 0 If any cache was purged
  * \retval -1 If no cache was found
  */
@@ -960,6 +991,8 @@
 	return -1;
 }
 
+/*! \brief Parse the res_config_sqlite3 config file
+ */
 static int parse_config(int reload)
 {
 	struct ast_config *config;
@@ -1023,25 +1056,14 @@
 	return 0;
 }
 
-static int stop_batch_cb(void *obj, void *arg, int flags)
-{
-	struct realtime_sqlite3_db *db = obj;
-
-	db_stop_batch(db);
-	return CMP_MATCH;
-}
-
 static int unload_module(void)
 {
 	ast_mutex_lock(&config_lock);
-	/* Go ahead and manually unlink everything so that the the batch threads can
-	 * release their refs */
 	ao2_callback(databases, OBJ_MULTIPLE | OBJ_NODATA | OBJ_UNLINK, stop_batch_cb, NULL);
 	ao2_ref(databases, -1);
 	databases = NULL;
+	ast_config_engine_deregister(&sqlite3_config_engine);
 	ast_mutex_unlock(&config_lock);
-
-	ast_config_engine_deregister(&sqlite3_config_engine);
 
 	return 0;
 }




More information about the asterisk-commits mailing list