[Asterisk-code-review] res sorcery astdb: Filter fields to only the registered ones. (asterisk[13])

Joshua Colp asteriskteam at digium.com
Thu May 19 17:47:35 CDT 2016


Joshua Colp has uploaded a new change for review.

  https://gerrit.asterisk.org/2879

Change subject: res_sorcery_astdb: Filter fields to only the registered ones.
......................................................................

res_sorcery_astdb: Filter fields to only the registered ones.

This change introduces the same filtering that is done in res_sorcery_realtime
to the res_sorcery_astdb module. This allows persisted sorcery objects
that may contain unknown fields to still be read in from the AstDB
and used. This is particularly useful when switching between different
versions of Asterisk that may have introduced additional fields.

ASTERISK-26014 #close

Change-Id: Ib655130485a3ccfd635b7ed5546010ca14690fb2
---
M res/res_sorcery_astdb.c
1 file changed, 57 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/79/2879/1

diff --git a/res/res_sorcery_astdb.c b/res/res_sorcery_astdb.c
index c6b5a1b..1aec0be 100644
--- a/res/res_sorcery_astdb.c
+++ b/res/res_sorcery_astdb.c
@@ -79,6 +79,58 @@
 	return ast_db_put(family, ast_sorcery_object_get_id(object), value);
 }
 
+/*! \brief Internal helper function which returns a filtered objectset.
+ *
+ * The following are filtered out of the objectset:
+ * \li Fields that are not registered with sorcery.
+ *
+ * \param objectset Objectset to filter.
+ * \param sorcery The sorcery instance that is requesting an objectset.
+ * \param type The object type
+ *
+ * \return The filtered objectset
+ */
+static struct ast_variable *sorcery_astdb_filter_objectset(struct ast_variable *objectset, const struct ast_sorcery *sorcery,
+	const char *type)
+{
+	struct ast_variable *previous = NULL, *field = objectset;
+	struct ast_sorcery_object_type *object_type;
+
+	object_type = ast_sorcery_get_object_type(sorcery, type);
+	if (!object_type) {
+		ast_log(LOG_WARNING, "Unknown sorcery object type %s. Expect errors\n", type);
+		return objectset;
+	}
+
+	while (field) {
+		struct ast_variable *removed;
+
+		if (ast_sorcery_is_object_field_registered(object_type, field->name)) {
+			previous = field;
+			field = field->next;
+			continue;
+		}
+
+		ast_debug(1, "Filtering out astdb field '%s' from retrieval\n", field->name);
+
+		if (previous) {
+			previous->next = field->next;
+		} else {
+			objectset = field->next;
+		}
+
+		removed = field;
+		field = field->next;
+		removed->next = NULL;
+
+		ast_variables_destroy(removed);
+	}
+
+	ao2_cleanup(object_type);
+
+	return objectset;
+}
+
 /*! \brief Internal helper function which retrieves an object, or multiple objects, using fields for criteria */
 static void *sorcery_astdb_retrieve_fields_common(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields, struct ao2_container *objects)
 {
@@ -103,9 +155,12 @@
 		if (!(json = ast_json_load_string(entry->data, &error))) {
 			return NULL;
 		}
+
 		if (ast_json_to_ast_variables(json, &existing) != AST_JSON_TO_AST_VARS_CODE_SUCCESS) {
 			return NULL;
 		}
+
+		existing = sorcery_astdb_filter_objectset(existing, sorcery, type);
 
 		if (fields && !ast_variable_lists_match(existing, fields, 0)) {
 			continue;
@@ -148,6 +203,7 @@
 	if (ast_db_get_allocated(family, id, &value)
 		|| !(json = ast_json_load_string(value, &error))
 		|| (ast_json_to_ast_variables(json, &objset) != AST_JSON_TO_AST_VARS_CODE_SUCCESS)
+		|| !(objset = sorcery_astdb_filter_objectset(objset, sorcery, type))
 		|| !(object = ast_sorcery_alloc(sorcery, type, id))
 		|| ast_sorcery_objectset_apply(sorcery, object, objset)) {
 		ast_debug(3, "Failed to retrieve object '%s' from astdb\n", id);
@@ -260,6 +316,7 @@
 			continue;
 		} else if (!(json = ast_json_load_string(entry->data, &error))
 			|| (ast_json_to_ast_variables(json, &objset) != AST_JSON_TO_AST_VARS_CODE_SUCCESS)
+			|| !(objset = sorcery_astdb_filter_objectset(objset, sorcery, type))
 			|| !(object = ast_sorcery_alloc(sorcery, type, key))
 			|| ast_sorcery_objectset_apply(sorcery, object, objset)) {
 			regfree(&expression);

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib655130485a3ccfd635b7ed5546010ca14690fb2
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Joshua Colp <jcolp at digium.com>



More information about the asterisk-code-review mailing list