[asterisk-commits] eliel: branch group/data_api_gsoc2009 r204985 - in /team/group/data_api_gsoc2...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jul 6 16:29:11 CDT 2009


Author: eliel
Date: Mon Jul  6 16:29:08 2009
New Revision: 204985

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=204985
Log:
Generate the search tree once and pass it to the callback, then
release it inside the data api.


Modified:
    team/group/data_api_gsoc2009/include/asterisk/data.h
    team/group/data_api_gsoc2009/main/data.c
    team/group/data_api_gsoc2009/tests/test_data.c

Modified: team/group/data_api_gsoc2009/include/asterisk/data.h
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/include/asterisk/data.h?view=diff&rev=204985&r1=204984&r2=204985
==============================================================================
--- team/group/data_api_gsoc2009/include/asterisk/data.h (original)
+++ team/group/data_api_gsoc2009/include/asterisk/data.h Mon Jul  6 16:29:08 2009
@@ -100,7 +100,7 @@
 /*! \brief opaque definition of an ast_data_search structure. */
 struct ast_data_search;
 
-typedef struct ast_data *(*ast_data_get_cb)(void);
+typedef struct ast_data *(*ast_data_get_cb)(const struct ast_data_search *search);
 typedef int *(*ast_data_put_cb)(void);
 typedef char **(*ast_data_tokenizer)(void);
 
@@ -137,8 +137,9 @@
 		/*! \brief Filter string, return the internal nodes specified here.
 		 *         Setting it to NULL will return every internal node. */
 		AST_STRING_FIELD(filter);
+		/*! \brief Search condition. */
+		AST_STRING_FIELD(search);
 	);
-	struct ast_data_search *search;
 };
 
 /*!
@@ -186,18 +187,6 @@
 #define ast_data_unregister(path) __ast_data_unregister(path, __FILE__)
 
 /*!
- * \brief Generate a search structure.
- * XXX
- */
-struct ast_data_search *ast_data_search_create(const char *search_format, ...);
-
-/*!
- * \brief Release a search structure.
- * XXX
- */
-void ast_data_search_release(struct ast_data_search *search_query);
-
-/*!
  * \brief Retrieve a subtree from the asterisk data API.
  * \param[in] query The query structure specifying what nodes to retrieve.
  * \retval NULL on error.

Modified: team/group/data_api_gsoc2009/main/data.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/main/data.c?view=diff&rev=204985&r1=204984&r2=204985
==============================================================================
--- team/group/data_api_gsoc2009/main/data.c (original)
+++ team/group/data_api_gsoc2009/main/data.c Mon Jul  6 16:29:08 2009
@@ -90,20 +90,8 @@
 
 /*! \brief The list of nodes with their search requirement. */
 struct ast_data_search {
-	/*! \brief The data type we are trying to
-	 * compare. */
-	enum ast_data_type value_type;
-
-	/* The value we are trying to compare. */
-	union {
-		int32_t sint;
-		uint32_t uint;
-		double dbl;
-		unsigned int boolean:1;
-		char *str;
-		struct in_addr ipaddr;
-		void *ptr;
-	} value;
+	/*! \brief The value of the comparison. */
+	void *value;
 
 	/*! \brief The type of comparison. */
 	enum data_search_comparison cmp_type;
@@ -604,27 +592,29 @@
 }
 #endif
 
-struct ast_data_search *ast_data_search_create(const char *search_format, ...)
-{
-	va_list values;
-	struct ast_str *name, *format, *comparison;
-	char *elements, *search_format_dup;
+/*!
+ * \internal
+ * \brief Allocate a tree with the search string parsed.
+ * \TODO complete documentation.
+ */
+static struct ast_data_search *data_search_parse(const char *search_string)
+{
+	struct ast_str *name, *value, *comparison;
+	char *elements, *search_string_dup;
 	int i;
 
-	if (!search_format) {
+	if (!search_string) {
 		ast_log(LOG_ERROR, "You must pass a valid search string.\n");
 		return NULL;
 	}
 
 	name = ast_str_alloca(64);
-	format = ast_str_alloca(64);
+	value = ast_str_alloca(256);
 	comparison = ast_str_alloca(64);
 
-	va_start(values, search_format);
-
-	search_format_dup = ast_strdupa(search_format);
-
-	elements = strtok(search_format_dup, ",");
+	search_string_dup = ast_strdupa(search_string);
+
+	elements = strtok(search_string_dup, ",");
 
 	while (elements) {
 		/* Parse the name */
@@ -644,36 +634,34 @@
 
 		/* parse the comparison string. */
 		ast_str_reset(comparison);
-		for (; elements[i] != '%' && elements[i]; i++) {
+		for (; data_search_comparison_char(elements[i]) && elements[i]; i++) {
 			ast_str_append(&comparison, 0, "%c", elements[i]);
 		}
 
-		/* check if the syntax is ok. */
-		if (elements[i] != '%') {
-			ast_log(LOG_ERROR, "Invalid search string!\n");
-			break;
-		}
-
-		/* parse the format string. */
-		ast_str_reset(format);
+		/* parse the value string. */
+		ast_str_reset(value);
 		for (; elements[i]; i++) {
-			ast_str_append(&format, 0, "%c", elements[i]);
+			ast_str_append(&value, 0, "%c", elements[i]);
 		}
 
 		ast_log(LOG_ERROR, "NAME = %s\n", ast_str_buffer(name));
-		ast_log(LOG_ERROR, "FORMAT = %s\n", ast_str_buffer(format));
+		ast_log(LOG_ERROR, "VALUE = %s\n", ast_str_buffer(value));
 		ast_log(LOG_ERROR, "CMP = %s\n", ast_str_buffer(comparison));
 
 		/* add this node to the tree. */
 
 		elements = strtok(NULL, ",");
 	}
-	va_end(values);
 
 	return NULL;
 }
 
-void ast_data_search_release(struct ast_data_search *search)
+/*!
+ * \internal
+ * \brief
+ * \TODO complete documentation.
+ */
+static void data_search_release(struct ast_data_search *search)
 {
 
 }
@@ -809,7 +797,8 @@
 static struct ast_data *__data_result_generate_node(const struct ast_data_query *query,
 	const struct data_provider *root_provider,
 	const char *parent_node_name,
-	struct data_filter *filter)
+	struct data_filter *filter,
+	const struct ast_data_search *search)
 {
 	struct ast_data *generated, *node;
 	struct ao2_iterator i;
@@ -827,7 +816,7 @@
 
 	/* if this is a terminal node, just run the callback function. */
 	if (root_provider->handler && root_provider->handler->get) {
-		generated = root_provider->handler->get();
+		generated = root_provider->handler->get(search);
 		if (generated) {
 			data_result_add_child(node, generated);
 			ao2_ref(generated, -1);
@@ -844,7 +833,7 @@
 	/* if this is not a terminal node, generate every child node. */
 	i = ao2_iterator_init(root_provider->children, 0);
 	while ((provider = ao2_iterator_next(&i))) {
-		generated = __data_result_generate_node(query, provider, provider->name, filter);
+		generated = __data_result_generate_node(query, provider, provider->name, filter, search);
 		if (generated) {
 			data_result_add_child(node, generated);
 			ao2_ref(generated, -1);
@@ -861,14 +850,16 @@
  * \param[in] query The query executed.
  * \param[in] root_provider The provider specified in the path of the query.
  * \param[in] parent_node_name The root node name.
+ * \param[in] search
  * \retval NULL on error.
  * \retval non-NULL The generated result tree.
  */
 static struct ast_data *data_result_generate_node(const struct ast_data_query *query,
 	const struct data_provider *root_provider,
-	const char *parent_node_name)
-{
-	return __data_result_generate_node(query, root_provider, parent_node_name, NULL);
+	const char *parent_node_name,
+	const struct ast_data_search *search)
+{
+	return __data_result_generate_node(query, root_provider, parent_node_name, NULL, search);
 }
 
 /*!
@@ -885,6 +876,7 @@
 	char *node_name, *tmp_path;
 	struct data_provider *provider_child, *tmp_provider_child;
 	struct ast_data *result;
+	struct ast_data_search *search = NULL;
 
 	if (!search_path) {
 		/* generate all the trees?. */
@@ -919,9 +911,17 @@
 		return NULL;
 	}
 
-	result = data_result_generate_node(query, provider_child, provider_child->name);
+	if (query->search) {
+		search = data_search_parse(query->search);
+	}
+
+	result = data_result_generate_node(query, provider_child, provider_child->name, search);
 
 	ao2_ref(provider_child, -1);
+
+	if (search) {
+		data_search_release(search);
+	}
 
 	return result;
 }

Modified: team/group/data_api_gsoc2009/tests/test_data.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/tests/test_data.c?view=diff&rev=204985&r1=204984&r2=204985
==============================================================================
--- team/group/data_api_gsoc2009/tests/test_data.c (original)
+++ team/group/data_api_gsoc2009/tests/test_data.c Mon Jul  6 16:29:08 2009
@@ -39,7 +39,7 @@
 #include "asterisk/cli.h"
 #include "asterisk/data.h"
 
-static struct ast_data *test_data_full_provider(void)
+static struct ast_data *test_data_full_provider(const struct ast_data_search *search)
 {
 	struct ast_data *res, *internal;
 
@@ -62,7 +62,7 @@
 	return res;
 }
 
-static struct ast_data *test_data_null_provider(void)
+static struct ast_data *test_data_null_provider(const struct ast_data_search *search)
 {
 	return NULL;
 }
@@ -93,7 +93,6 @@
 	struct ast_data_query query_null = {
 		.version = AST_DATA_QUERY_VERSION,
 		.path = "test/null",
-		.search = ast_data_search_create("test/prueba=%d,test/otraprueba=%d", cmd, cmd)
 	};
 	struct ast_data_query query_full = {
 		.version = AST_DATA_QUERY_VERSION,
@@ -138,7 +137,8 @@
 	struct ast_data_iterator *i;
 	struct ast_data_query query = {
 		.version = AST_DATA_QUERY_VERSION,
-		.path = "test",
+		.path = "test/node1/node11/node111",
+		.search = "node111/root_test/data1=10"
 	};
 
 	switch (cmd) {




More information about the asterisk-commits mailing list