]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
func_db: Add function to return cardinality at prefix
authorNaveen Albert <asterisk@phreaknet.org>
Tue, 15 Mar 2022 01:41:29 +0000 (01:41 +0000)
committerJoshua Colp <jcolp@sangoma.com>
Wed, 27 Apr 2022 16:42:15 +0000 (11:42 -0500)
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

doc/CHANGES-staging/func_db.txt [new file with mode: 0644]
funcs/func_db.c

diff --git a/doc/CHANGES-staging/func_db.txt b/doc/CHANGES-staging/func_db.txt
new file mode 100644 (file)
index 0000000..72e333a
--- /dev/null
@@ -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.
index 3f98ed060468570086173d665222935556825769..33d082178068d76d3cc26a5f56b0ab7ce6b135cc 100644 (file)
                        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 @@ static struct ast_custom_function db_keys_function = {
        .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 @@ static int unload_module(void)
        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 @@ static int load_module(void)
        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;
 }