[Asterisk-code-review] func_db: Add function to return cardinality at prefix (asterisk[16])

N A asteriskteam at digium.com
Tue Apr 26 17:08:46 CDT 2022


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/18458 )


Change subject: func_db: Add function to return cardinality at prefix
......................................................................

func_db: Add function to return cardinality at prefix

Adds the DB_KEYCOUNT function, which can be used to retrieve
the number of keys at a given prefix in AstDB.

ASTERISK-29968 #close

Change-Id: Ib2393b77b7e962dbaae6192f8576bc3f6ba92d09
---
A doc/CHANGES-staging/func_db.txt
M funcs/func_db.c
2 files changed, 78 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/58/18458/1

diff --git a/doc/CHANGES-staging/func_db.txt b/doc/CHANGES-staging/func_db.txt
new file mode 100644
index 0000000..72e333a
--- /dev/null
+++ b/doc/CHANGES-staging/func_db.txt
@@ -0,0 +1,6 @@
+Subject: func_db
+
+The function DB_KEYCOUNT has been added, which
+returns the cardinality of the keys at a specified
+prefix in AstDB, i.e. the number of keys at a
+given prefix.
diff --git a/funcs/func_db.c b/funcs/func_db.c
index 3f98ed0..33d0821 100644
--- a/funcs/func_db.c
+++ b/funcs/func_db.c
@@ -95,6 +95,25 @@
 			at the prefix specified within the Asterisk database.  If no argument is
 			provided, then a list of key families will be returned.</para>
 		</description>
+		<see-also>
+			<ref type="function">DB_KEYCOUNT</ref>
+		</see-also>
+	</function>
+	<function name="DB_KEYCOUNT" language="en_US">
+		<synopsis>
+			Obtain the number of keys at a prefix within the Asterisk database.
+		</synopsis>
+		<syntax>
+			<parameter name="prefix" />
+		</syntax>
+		<description>
+			<para>This function will return the number of keys that exist
+			at the prefix specified within the Asterisk database.  If no argument is
+			provided, then the number of all key families will be returned.</para>
+		</description>
+		<see-also>
+			<ref type="function">DB_KEYS</ref>
+		</see-also>
 	</function>
 	<function name="DB_DELETE" language="en_US">
 		<synopsis>
@@ -286,6 +305,57 @@
 	.read2 = function_db_keys,
 };
 
+static int function_db_keycount(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
+{
+	size_t parselen = strlen(parse);
+	struct ast_db_entry *dbe, *orig_dbe;
+	const char *last = "";
+	int keycount = 0;
+
+	/* Remove leading and trailing slashes */
+	while (parse[0] == '/') {
+		parse++;
+		parselen--;
+	}
+	while (parse[parselen - 1] == '/') {
+		parse[--parselen] = '\0';
+	}
+
+	/* Nothing within the database at that prefix? */
+	if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) {
+		snprintf(buf, len, "%d", keycount);
+		return 0;
+	}
+
+	for (; dbe; dbe = dbe->next) {
+		/* Find the current component */
+		char *curkey = &dbe->key[parselen + 1], *slash;
+		if (*curkey == '/') {
+			curkey++;
+		}
+		/* Remove everything after the current component */
+		if ((slash = strchr(curkey, '/'))) {
+			*slash = '\0';
+		}
+
+		/* Skip duplicates */
+		if (!strcasecmp(last, curkey)) {
+			continue;
+		}
+		last = curkey;
+
+		keycount++;
+	}
+	ast_db_freetree(orig_dbe);
+	snprintf(buf, len, "%d", keycount);
+	return 0;
+}
+
+static struct ast_custom_function db_keycount_function = {
+	.name = "DB_KEYCOUNT",
+	.read = function_db_keycount,
+};
+
 static int function_db_delete(struct ast_channel *chan, const char *cmd,
 			      char *parse, char *buf, size_t len)
 {
@@ -347,6 +417,7 @@
 	res |= ast_custom_function_unregister(&db_exists_function);
 	res |= ast_custom_function_unregister(&db_delete_function);
 	res |= ast_custom_function_unregister(&db_keys_function);
+	res |= ast_custom_function_unregister(&db_keycount_function);
 
 	return res;
 }
@@ -359,6 +430,7 @@
 	res |= ast_custom_function_register(&db_exists_function);
 	res |= ast_custom_function_register_escalating(&db_delete_function, AST_CFE_READ);
 	res |= ast_custom_function_register(&db_keys_function);
+	res |= ast_custom_function_register(&db_keycount_function);
 
 	return res;
 }

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/18458
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: Ib2393b77b7e962dbaae6192f8576bc3f6ba92d09
Gerrit-Change-Number: 18458
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220426/25f8b2e0/attachment-0001.html>


More information about the asterisk-code-review mailing list