<p>Joshua Colp <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/18458">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Joshua Colp: Looks good to me, but someone else must approve; Approved for Submit
Benjamin Keith Ford: Looks good to me, approved
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">func_db: Add function to return cardinality at prefix<br><br>Adds the DB_KEYCOUNT function, which can be used to retrieve<br>the number of keys at a given prefix in AstDB.<br><br>ASTERISK-29968 #close<br><br>Change-Id: Ib2393b77b7e962dbaae6192f8576bc3f6ba92d09<br>---<br>A doc/CHANGES-staging/func_db.txt<br>M funcs/func_db.c<br>2 files changed, 78 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/doc/CHANGES-staging/func_db.txt b/doc/CHANGES-staging/func_db.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..72e333a</span><br><span>--- /dev/null</span><br><span>+++ b/doc/CHANGES-staging/func_db.txt</span><br><span>@@ -0,0 +1,6 @@</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: func_db</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+The function DB_KEYCOUNT has been added, which</span><br><span style="color: hsl(120, 100%, 40%);">+returns the cardinality of the keys at a specified</span><br><span style="color: hsl(120, 100%, 40%);">+prefix in AstDB, i.e. the number of keys at a</span><br><span style="color: hsl(120, 100%, 40%);">+given prefix.</span><br><span>diff --git a/funcs/func_db.c b/funcs/func_db.c</span><br><span>index 3f98ed0..33d0821 100644</span><br><span>--- a/funcs/func_db.c</span><br><span>+++ b/funcs/func_db.c</span><br><span>@@ -95,6 +95,25 @@</span><br><span> at the prefix specified within the Asterisk database. If no argument is</span><br><span> provided, then a list of key families will be returned.</para></span><br><span> </description></span><br><span style="color: hsl(120, 100%, 40%);">+ <see-also></span><br><span style="color: hsl(120, 100%, 40%);">+ <ref type="function">DB_KEYCOUNT</ref></span><br><span style="color: hsl(120, 100%, 40%);">+ </see-also></span><br><span style="color: hsl(120, 100%, 40%);">+ </function></span><br><span style="color: hsl(120, 100%, 40%);">+ <function name="DB_KEYCOUNT" language="en_US"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ Obtain the number of keys at a prefix within the Asterisk database.</span><br><span style="color: hsl(120, 100%, 40%);">+ </synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ <syntax></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="prefix" /></span><br><span style="color: hsl(120, 100%, 40%);">+ </syntax></span><br><span style="color: hsl(120, 100%, 40%);">+ <description></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>This function will return the number of keys that exist</span><br><span style="color: hsl(120, 100%, 40%);">+ at the prefix specified within the Asterisk database. If no argument is</span><br><span style="color: hsl(120, 100%, 40%);">+ provided, then the number of all key families will be returned.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </description></span><br><span style="color: hsl(120, 100%, 40%);">+ <see-also></span><br><span style="color: hsl(120, 100%, 40%);">+ <ref type="function">DB_KEYS</ref></span><br><span style="color: hsl(120, 100%, 40%);">+ </see-also></span><br><span> </function></span><br><span> <function name="DB_DELETE" language="en_US"></span><br><span> <synopsis></span><br><span>@@ -286,6 +305,57 @@</span><br><span> .read2 = function_db_keys,</span><br><span> };</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static int function_db_keycount(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t parselen = strlen(parse);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_db_entry *dbe, *orig_dbe;</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *last = "";</span><br><span style="color: hsl(120, 100%, 40%);">+ int keycount = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Remove leading and trailing slashes */</span><br><span style="color: hsl(120, 100%, 40%);">+ while (parse[0] == '/') {</span><br><span style="color: hsl(120, 100%, 40%);">+ parse++;</span><br><span style="color: hsl(120, 100%, 40%);">+ parselen--;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ while (parse[parselen - 1] == '/') {</span><br><span style="color: hsl(120, 100%, 40%);">+ parse[--parselen] = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Nothing within the database at that prefix? */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf, len, "%d", keycount);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (; dbe; dbe = dbe->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Find the current component */</span><br><span style="color: hsl(120, 100%, 40%);">+ char *curkey = &dbe->key[parselen + 1], *slash;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (*curkey == '/') {</span><br><span style="color: hsl(120, 100%, 40%);">+ curkey++;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Remove everything after the current component */</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((slash = strchr(curkey, '/'))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ *slash = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Skip duplicates */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!strcasecmp(last, curkey)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ last = curkey;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ keycount++;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_db_freetree(orig_dbe);</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(buf, len, "%d", keycount);</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct ast_custom_function db_keycount_function = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .name = "DB_KEYCOUNT",</span><br><span style="color: hsl(120, 100%, 40%);">+ .read = function_db_keycount,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static int function_db_delete(struct ast_channel *chan, const char *cmd,</span><br><span> char *parse, char *buf, size_t len)</span><br><span> {</span><br><span>@@ -347,6 +417,7 @@</span><br><span> res |= ast_custom_function_unregister(&db_exists_function);</span><br><span> res |= ast_custom_function_unregister(&db_delete_function);</span><br><span> res |= ast_custom_function_unregister(&db_keys_function);</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_custom_function_unregister(&db_keycount_function);</span><br><span> </span><br><span> return res;</span><br><span> }</span><br><span>@@ -359,6 +430,7 @@</span><br><span> res |= ast_custom_function_register(&db_exists_function);</span><br><span> res |= ast_custom_function_register_escalating(&db_delete_function, AST_CFE_READ);</span><br><span> res |= ast_custom_function_register(&db_keys_function);</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_custom_function_register(&db_keycount_function);</span><br><span> </span><br><span> return res;</span><br><span> }</span><br><span></span><br></pre><div style="white-space:pre-wrap"></div><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/18458">change 18458</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/18458"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: Ib2393b77b7e962dbaae6192f8576bc3f6ba92d09 </div>
<div style="display:none"> Gerrit-Change-Number: 18458 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: N A <mail@interlinked.x10host.com> </div>
<div style="display:none"> Gerrit-Reviewer: Benjamin Keith Ford <bford@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>