Sat Apr 26 2014 22:01:56

Asterisk developer's documentation


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 one or more entries in astdb If both parameters are NULL, the entire database will be purged. If only keytree is NULL, all entries within the family will be purged. It is an error for keytree to have a value when family is NULL.
void ast_db_freetree (struct ast_db_entry *entry)
 Free structure created by ast_db_gettree()
int ast_db_get (const char *family, const char *key, char *out, int outlen)
 Get key value specified by family/key.
int ast_db_get_allocated (const char *family, const char *key, char **out)
 Get key value specified by family/key as a heap allocated string.
struct ast_db_entryast_db_gettree (const char *family, const char *keytree)
 Get a list of values within the astdb tree If family is specified, only those keys will be returned. If keytree is specified, subkeys are expected to exist (separated from the key with a slash). If subkeys do not exist and keytree is specified, the tree will consist of either a single entry or NULL will be returned.
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 413 of file db.c.

References ast_debug, ast_log(), ast_mutex_lock, ast_mutex_unlock, db_sync(), dblock, LOG_WARNING, and MAX_DB_FIELD.

Referenced by __expire_registry(), ast_privacy_set(), auth_exec(), cache_lookup_internal(), del_exec(), destroy_all_channels(), destroy_association(), dialgroup_refreshdb(), dump_queue_members(), function_db_delete(), handle_cli_database_del(), handle_dbdel(), handle_pri_service_generic(), manager_dbdel(), mkintf(), pri_dchannel(), process_clearcache(), reload_queue_members(), and update_registry().

{
   char fullkey[MAX_DB_FIELD];
   size_t fullkey_len;
   int res = 0;

   if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
      ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
      return -1;
   }

   fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);

   ast_mutex_lock(&dblock);
   if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
      ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
      res = -1;
   } else if (sqlite3_step(del_stmt) != SQLITE_DONE) {
      ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
      res = -1;
   }
   sqlite3_reset(del_stmt);
   db_sync();
   ast_mutex_unlock(&dblock);

   return res;
}
int ast_db_deltree ( const char *  family,
const char *  keytree 
)

Delete one or more entries in astdb If both parameters are NULL, the entire database will be purged. If only keytree is NULL, all entries within the family will be purged. It is an error for keytree to have a value when family is NULL.

Return values:
-1An error occurred
>=0 Number of records deleted

Definition at line 441 of file db.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), db_sync(), dblock, LOG_WARNING, MAX_DB_FIELD, and prefix.

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

{
   sqlite3_stmt *stmt = deltree_stmt;
   char prefix[MAX_DB_FIELD];
   int res = 0;

   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';
      stmt = deltree_all_stmt;
   }

   ast_mutex_lock(&dblock);
   if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
      ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
      res = -1;
   } else if (sqlite3_step(stmt) != SQLITE_DONE) {
      ast_log(LOG_WARNING, "Couldn't execute stmt: %s\n", sqlite3_errmsg(astdb));
      res = -1;
   }
   res = sqlite3_changes(astdb);
   sqlite3_reset(stmt);
   db_sync();
   ast_mutex_unlock(&dblock);

   return res;
}
void ast_db_freetree ( struct ast_db_entry entry)

Free structure created by ast_db_gettree()

Definition at line 531 of file db.c.

References ast_free, last, and ast_db_entry::next.

Referenced by dundi_show_cache(), dundi_show_hints(), function_db_keys(), handle_cli_devstate_list(), handle_cli_presencestate_list(), load_module(), process_clearcache(), and reload_queue_members().

{
   struct ast_db_entry *last;
   while (dbe) {
      last = dbe;
      dbe = dbe->next;
      ast_free(last);
   }
}
int ast_db_get_allocated ( const char *  family,
const char *  key,
char **  out 
)

Get key value specified by family/key as a heap allocated string.

Given a family and key, sets out to a pointer to a heap allocated string. In the event of an error, out will be set to NULL. The string must be freed by calling ast_free().

Return values:
-1An error occurred
0Success

Definition at line 406 of file db.c.

References db_get_common().

Referenced by reload_queue_members().

{
   *out = NULL;

   return db_get_common(family, key, out, -1);
}
struct ast_db_entry* ast_db_gettree ( const char *  family,
const char *  keytree 
) [read]

Get a list of values within the astdb tree If family is specified, only those keys will be returned. If keytree is specified, subkeys are expected to exist (separated from the key with a slash). If subkeys do not exist and keytree is specified, the tree will consist of either a single entry or NULL will be returned.

Resulting tree should be freed by passing the return value to ast_db_freetree() when usage is concluded.

Definition at line 476 of file db.c.

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

Referenced by dundi_show_cache(), dundi_show_hints(), function_db_keys(), handle_cli_devstate_list(), handle_cli_presencestate_list(), load_module(), process_clearcache(), and reload_queue_members().

{
   char prefix[MAX_DB_FIELD];
   sqlite3_stmt *stmt = gettree_stmt;
   struct ast_db_entry *cur, *last = NULL, *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';
      stmt = gettree_all_stmt;
   }

   ast_mutex_lock(&dblock);
   if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
      ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
      sqlite3_reset(stmt);
      ast_mutex_unlock(&dblock);
      return NULL;
   }

   while (sqlite3_step(stmt) == SQLITE_ROW) {
      const char *key_s, *value_s;
      if (!(key_s = (const char *) sqlite3_column_text(stmt, 0))) {
         break;
      }
      if (!(value_s = (const char *) sqlite3_column_text(stmt, 1))) {
         break;
      }
      if (!(cur = ast_malloc(sizeof(*cur) + strlen(key_s) + strlen(value_s) + 2))) {
         break;
      }
      cur->next = NULL;
      cur->key = cur->data + strlen(value_s) + 1;
      strcpy(cur->data, value_s);
      strcpy(cur->key, key_s);
      if (last) {
         last->next = cur;
      } else {
         ret = cur;
      }
      last = cur;
   }
   sqlite3_reset(stmt);
   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 311 of file db.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, db_sync(), dblock, LOG_WARNING, and MAX_DB_FIELD.

Referenced by __analog_ss_thread(), ast_privacy_set(), cache_save(), cache_save_hint(), database_increment(), devstate_write(), dialgroup_refreshdb(), dump_queue_members(), function_db_write(), handle_cli_database_put(), handle_cli_devstate_change(), handle_cli_presencestate_change(), handle_command_response(), handle_dbput(), handle_pri_service_generic(), iax_provision_build(), manager_dbput(), mgcp_ss(), parse_register_contact(), presence_write(), pri_dchannel(), save_secret(), and update_registry().

{
   char fullkey[MAX_DB_FIELD];
   size_t fullkey_len;
   int res = 0;

   if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
      ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
      return -1;
   }

   fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);

   ast_mutex_lock(&dblock);
   if (sqlite3_bind_text(put_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
      ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
      res = -1;
   } else if (sqlite3_bind_text(put_stmt, 2, value, -1, SQLITE_STATIC) != SQLITE_OK) {
      ast_log(LOG_WARNING, "Couldn't bind value to stmt: %s\n", sqlite3_errmsg(astdb));
      res = -1;
   } else if (sqlite3_step(put_stmt) != SQLITE_DONE) {
      ast_log(LOG_WARNING, "Couldn't execute statment: %s\n", sqlite3_errmsg(astdb));
      res = -1;
   }

   sqlite3_reset(put_stmt);
   db_sync();
   ast_mutex_unlock(&dblock);

   return res;
}