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.
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 *value, int valuelen)
 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.
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 main/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(), AST_TEST_DEFINE(), 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(), manager_dbdel(), mkintf(), process_clearcache(), reload_queue_members(), sorcery_astdb_delete(), stasis_app_device_state_delete(), and update_registry().

00414 {
00415    char fullkey[MAX_DB_FIELD];
00416    size_t fullkey_len;
00417    int res = 0;
00418 
00419    if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
00420       ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
00421       return -1;
00422    }
00423 
00424    fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
00425 
00426    ast_mutex_lock(&dblock);
00427    if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
00428       ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
00429       res = -1;
00430    } else if (sqlite3_step(del_stmt) != SQLITE_DONE) {
00431       ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
00432       res = -1;
00433    }
00434    sqlite3_reset(del_stmt);
00435    db_sync();
00436    ast_mutex_unlock(&dblock);
00437 
00438    return res;
00439 }

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:
-1 An error occurred
>= 0 Number of records deleted

Definition at line 441 of file main/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(), AST_TEST_DEFINE(), deinitialize_sorcery(), deltree_exec(), dundi_flush(), handle_cli_database_deltree(), handle_dbdeltree(), iax_provision_reload(), and manager_dbdeltree().

00442 {
00443    sqlite3_stmt *stmt = deltree_stmt;
00444    char prefix[MAX_DB_FIELD];
00445    int res = 0;
00446 
00447    if (!ast_strlen_zero(family)) {
00448       if (!ast_strlen_zero(keytree)) {
00449          /* Family and key tree */
00450          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00451       } else {
00452          /* Family only */
00453          snprintf(prefix, sizeof(prefix), "/%s", family);
00454       }
00455    } else {
00456       prefix[0] = '\0';
00457       stmt = deltree_all_stmt;
00458    }
00459 
00460    ast_mutex_lock(&dblock);
00461    if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
00462       ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
00463       res = -1;
00464    } else if (sqlite3_step(stmt) != SQLITE_DONE) {
00465       ast_log(LOG_WARNING, "Couldn't execute stmt: %s\n", sqlite3_errmsg(astdb));
00466       res = -1;
00467    }
00468    res = sqlite3_changes(astdb);
00469    sqlite3_reset(stmt);
00470    db_sync();
00471    ast_mutex_unlock(&dblock);
00472 
00473    return res;
00474 }

void ast_db_freetree ( struct ast_db_entry entry  ) 

int ast_db_get ( const char *  family,
const char *  key,
char *  value,
int  valuelen 
)

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:
-1 An error occurred
0 Success

Definition at line 406 of file main/db.c.

References db_get_common(), and NULL.

Referenced by AST_TEST_DEFINE(), reload_queue_members(), and sorcery_astdb_retrieve_id().

00407 {
00408    *out = NULL;
00409 
00410    return db_get_common(family, key, out, -1);
00411 }

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 main/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, NULL, and prefix.

Referenced by AST_TEST_DEFINE(), dundi_show_cache(), dundi_show_hints(), function_db_keys(), handle_cli_devstate_list(), handle_cli_presencestate_list(), load_module(), populate_cache(), process_clearcache(), reload_queue_members(), sorcery_astdb_retrieve_fields_common(), sorcery_astdb_retrieve_regex(), and stasis_app_device_states_to_json().

00477 {
00478    char prefix[MAX_DB_FIELD];
00479    sqlite3_stmt *stmt = gettree_stmt;
00480    struct ast_db_entry *cur, *last = NULL, *ret = NULL;
00481 
00482    if (!ast_strlen_zero(family)) {
00483       if (!ast_strlen_zero(keytree)) {
00484          /* Family and key tree */
00485          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00486       } else {
00487          /* Family only */
00488          snprintf(prefix, sizeof(prefix), "/%s", family);
00489       }
00490    } else {
00491       prefix[0] = '\0';
00492       stmt = gettree_all_stmt;
00493    }
00494 
00495    ast_mutex_lock(&dblock);
00496    if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
00497       ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
00498       sqlite3_reset(stmt);
00499       ast_mutex_unlock(&dblock);
00500       return NULL;
00501    }
00502 
00503    while (sqlite3_step(stmt) == SQLITE_ROW) {
00504       const char *key_s, *value_s;
00505       if (!(key_s = (const char *) sqlite3_column_text(stmt, 0))) {
00506          break;
00507       }
00508       if (!(value_s = (const char *) sqlite3_column_text(stmt, 1))) {
00509          break;
00510       }
00511       if (!(cur = ast_malloc(sizeof(*cur) + strlen(key_s) + strlen(value_s) + 2))) {
00512          break;
00513       }
00514       cur->next = NULL;
00515       cur->key = cur->data + strlen(value_s) + 1;
00516       strcpy(cur->data, value_s);
00517       strcpy(cur->key, key_s);
00518       if (last) {
00519          last->next = cur;
00520       } else {
00521          ret = cur;
00522       }
00523       last = cur;
00524    }
00525    sqlite3_reset(stmt);
00526    ast_mutex_unlock(&dblock);
00527 
00528    return ret;
00529 }

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 main/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(), AST_TEST_DEFINE(), 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(), iax_provision_build(), manager_dbput(), mgcp_ss(), parse_register_contact(), presence_write(), save_secret(), sorcery_astdb_create(), stasis_app_device_state_update(), and update_registry().

00312 {
00313    char fullkey[MAX_DB_FIELD];
00314    size_t fullkey_len;
00315    int res = 0;
00316 
00317    if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
00318       ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
00319       return -1;
00320    }
00321 
00322    fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
00323 
00324    ast_mutex_lock(&dblock);
00325    if (sqlite3_bind_text(put_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
00326       ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
00327       res = -1;
00328    } else if (sqlite3_bind_text(put_stmt, 2, value, -1, SQLITE_STATIC) != SQLITE_OK) {
00329       ast_log(LOG_WARNING, "Couldn't bind value to stmt: %s\n", sqlite3_errmsg(astdb));
00330       res = -1;
00331    } else if (sqlite3_step(put_stmt) != SQLITE_DONE) {
00332       ast_log(LOG_WARNING, "Couldn't execute statment: %s\n", sqlite3_errmsg(astdb));
00333       res = -1;
00334    }
00335 
00336    sqlite3_reset(put_stmt);
00337    db_sync();
00338    ast_mutex_unlock(&dblock);
00339 
00340    return res;
00341 }


Generated on Thu Apr 16 06:28:56 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6