Mon Sep 20 2010 00:26:02

Asterisk developer's documentation


Data Structures | Functions

astdb.h File Reference

Persistant data storage (akin to *doze registry). More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_db_entry

Functions

int ast_db_del (const char *family, const char *key)
 Delete entry in astdb.
int ast_db_deltree (const char *family, const char *keytree)
 Delete a whole family (for some reason also called "tree".
void ast_db_freetree (struct ast_db_entry *entry)
 Free in-memory data.
int ast_db_get (const char *family, const char *key, char *out, int outlen)
 Get key value specified by family/key.
struct ast_db_entryast_db_gettree (const char *family, const char *keytree)
 Get a whole family.
int ast_db_put (const char *family, const char *key, const char *value)
 Store value addressed by family/key.

Detailed Description

Persistant data storage (akin to *doze registry).

Definition in file astdb.h.


Function Documentation

int ast_db_del ( const char *  family,
const char *  key 
)

Delete entry in astdb.

Definition at line 209 of file db.c.

References ast_debug, ast_mutex_lock(), ast_mutex_unlock(), dbinit(), and dblock.

Referenced by __expire_registry(), ast_privacy_set(), auth_exec(), cache_lookup_internal(), del_exec(), destroy_association(), dialgroup_refreshdb(), dump_agents(), dump_queue_members(), function_db_delete(), handle_cli_database_del(), handle_dbdel(), manager_dbdel(), process_clearcache(), reload_agents(), reload_queue_members(), and update_registry().

{
   char fullkey[256];
   DBT key;
   int res, fullkeylen;

   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      return -1;
   }
   
   fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
   memset(&key, 0, sizeof(key));
   key.data = fullkey;
   key.size = fullkeylen + 1;
   
   res = astdb->del(astdb, &key, 0);
   astdb->sync(astdb, 0);
   
   ast_mutex_unlock(&dblock);

   if (res) {
      ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
   }
   return res;
}

int ast_db_deltree ( const char *  family,
const char *  keytree 
)

Delete a whole family (for some reason also called "tree".

Definition at line 91 of file db.c.

References ast_mutex_lock(), ast_mutex_unlock(), dbinit(), dblock, keymatch(), pass, and prefix.

Referenced by ast_privacy_reset(), deltree_exec(), dundi_flush(), handle_cli_database_deltree(), handle_dbdeltree(), iax_provision_reload(), and manager_dbdeltree().

{
   char prefix[256];
   DBT key, data;
   char *keys;
   int res;
   int pass;
   int counter = 0;
   
   if (family) {
      if (keytree) {
         snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
      } else {
         snprintf(prefix, sizeof(prefix), "/%s", family);
      }
   } else if (keytree) {
      return -1;
   } else {
      prefix[0] = '\0';
   }
   
   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      return -1;
   }
   
   memset(&key, 0, sizeof(key));
   memset(&data, 0, sizeof(data));
   pass = 0;
   while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
      if (key.size) {
         keys = key.data;
         keys[key.size - 1] = '\0';
      } else {
         keys = "<bad key>";
      }
      if (keymatch(keys, prefix)) {
         astdb->del(astdb, &key, 0);
         counter++;
      }
   }
   astdb->sync(astdb, 0);
   ast_mutex_unlock(&dblock);
   return counter;
}

void ast_db_freetree ( struct ast_db_entry entry  ) 

Free in-memory data.

Definition at line 535 of file db.c.

References ast_free, last, and ast_db_entry::next.

Referenced by handle_cli_devstate_list(), load_module(), process_clearcache(), reload_agents(), and reload_queue_members().

{
   struct ast_db_entry *last;
   while (dbe) {
      last = dbe;
      dbe = dbe->next;
      ast_free(last);
   }
}

int ast_db_get ( const char *  family,
const char *  key,
char *  out,
int  outlen 
)

Get key value specified by family/key.

Definition at line 165 of file db.c.

References ast_copy_string(), ast_debug, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dbinit(), dblock, and LOG_NOTICE.

Referenced by ast_privacy_check(), auth_exec(), blacklist_read(), cache_lookup_internal(), check_access(), create_addr(), custom_devstate_callback(), database_increment(), function_db_delete(), function_db_exists(), function_db_read(), handle_cli_database_get(), handle_dbget(), iax_provision_version(), load_password(), manager_dbget(), populate_addr(), reg_source_db(), reload_agents(), and reload_queue_members().

{
   char fullkey[256] = "";
   DBT key, data;
   int res, fullkeylen;

   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      return -1;
   }

   fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
   memset(&key, 0, sizeof(key));
   memset(&data, 0, sizeof(data));
   memset(value, 0, valuelen);
   key.data = fullkey;
   key.size = fullkeylen + 1;

   res = astdb->get(astdb, &key, &data, 0);

   /* Be sure to NULL terminate our data either way */
   if (res) {
      ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
   } else {
#if 0
      printf("Got value of size %d\n", data.size);
#endif
      if (data.size) {
         ((char *)data.data)[data.size - 1] = '\0';
         /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
         ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen);
      } else {
         ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
      }
   }

   /* Data is not fully isolated for concurrency, so the lock must be extended
    * to after the copy to the output buffer. */
   ast_mutex_unlock(&dblock);

   return res;
}

struct ast_db_entry* ast_db_gettree ( const char *  family,
const char *  keytree 
) [read]

Get a whole family.

Definition at line 473 of file db.c.

References ast_log(), ast_malloc, ast_mutex_lock(), ast_mutex_unlock(), ast_strlen_zero(), ast_db_entry::data, dbinit(), dblock, ast_db_entry::key, keymatch(), last, LOG_WARNING, ast_db_entry::next, pass, and prefix.

Referenced by handle_cli_devstate_list(), load_module(), process_clearcache(), reload_agents(), and reload_queue_members().

{
   char prefix[256];
   DBT key, data;
   char *keys, *values;
   int values_len;
   int res;
   int pass;
   struct ast_db_entry *last = NULL;
   struct ast_db_entry *cur, *ret=NULL;

   if (!ast_strlen_zero(family)) {
      if (!ast_strlen_zero(keytree)) {
         /* Family and key tree */
         snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
      } else {
         /* Family only */
         snprintf(prefix, sizeof(prefix), "/%s", family);
      }
   } else {
      prefix[0] = '\0';
   }
   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      ast_log(LOG_WARNING, "Database unavailable\n");
      return NULL;   
   }
   memset(&key, 0, sizeof(key));
   memset(&data, 0, sizeof(data));
   pass = 0;
   while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
      if (key.size) {
         keys = key.data;
         keys[key.size - 1] = '\0';
      } else {
         keys = "<bad key>";
      }
      if (data.size) {
         values = data.data;
         values[data.size - 1] = '\0';
      } else {
         values = "<bad value>";
      }
      values_len = strlen(values) + 1;
      if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
         cur->next = NULL;
         cur->key = cur->data + values_len;
         strcpy(cur->data, values);
         strcpy(cur->key, keys);
         if (last) {
            last->next = cur;
         } else {
            ret = cur;
         }
         last = cur;
      }
   }
   ast_mutex_unlock(&dblock);
   return ret; 
}

int ast_db_put ( const char *  family,
const char *  key,
const char *  value 
)

Store value addressed by family/key.

Definition at line 138 of file db.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dbinit(), dblock, and LOG_WARNING.

Referenced by ast_privacy_set(), cache_save(), cache_save_hint(), database_increment(), devstate_write(), dialgroup_refreshdb(), dump_agents(), dump_queue_members(), function_db_write(), handle_cli_database_put(), handle_cli_devstate_change(), handle_command_response(), handle_dbput(), iax_provision_build(), manager_dbput(), mgcp_ss(), parse_register_contact(), save_secret(), ss_thread(), and update_registry().

{
   char fullkey[256];
   DBT key, data;
   int res, fullkeylen;

   ast_mutex_lock(&dblock);
   if (dbinit()) {
      ast_mutex_unlock(&dblock);
      return -1;
   }

   fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
   memset(&key, 0, sizeof(key));
   memset(&data, 0, sizeof(data));
   key.data = fullkey;
   key.size = fullkeylen + 1;
   data.data = (char *) value;
   data.size = strlen(value) + 1;
   res = astdb->put(astdb, &key, &data, 0);
   astdb->sync(astdb, 0);
   ast_mutex_unlock(&dblock);
   if (res)
      ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
   return res;
}