Thu Oct 11 06:42:49 2012

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.
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, 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 217 of file db.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dbinit(), dblock, LOG_DEBUG, and option_debug.

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

00218 {
00219    char fullkey[256];
00220    DBT key;
00221    int res, fullkeylen;
00222 
00223    ast_mutex_lock(&dblock);
00224    if (dbinit()) {
00225       ast_mutex_unlock(&dblock);
00226       return -1;
00227    }
00228    
00229    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00230    memset(&key, 0, sizeof(key));
00231    key.data = fullkey;
00232    key.size = fullkeylen + 1;
00233    
00234    res = astdb->del(astdb, &key, 0);
00235    astdb->sync(astdb, 0);
00236    
00237    ast_mutex_unlock(&dblock);
00238 
00239    if (res && option_debug)
00240       ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
00241    return res;
00242 }

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:
0 Entries were deleted
-1 An error occurred

Definition at line 100 of file db.c.

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

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

00101 {
00102    char prefix[256];
00103    DBT key, data;
00104    char *keys;
00105    int res;
00106    int pass;
00107    
00108    if (family) {
00109       if (keytree) {
00110          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00111       } else {
00112          snprintf(prefix, sizeof(prefix), "/%s", family);
00113       }
00114    } else if (keytree) {
00115       return -1;
00116    } else {
00117       prefix[0] = '\0';
00118    }
00119    
00120    ast_mutex_lock(&dblock);
00121    if (dbinit()) {
00122       ast_mutex_unlock(&dblock);
00123       return -1;
00124    }
00125    
00126    memset(&key, 0, sizeof(key));
00127    memset(&data, 0, sizeof(data));
00128    pass = 0;
00129    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00130       if (key.size) {
00131          keys = key.data;
00132          keys[key.size - 1] = '\0';
00133       } else {
00134          keys = "<bad key>";
00135       }
00136       if (keymatch(keys, prefix)) {
00137          astdb->del(astdb, &key, 0);
00138       }
00139    }
00140    astdb->sync(astdb, 0);
00141    ast_mutex_unlock(&dblock);
00142    return 0;
00143 }

void ast_db_freetree ( struct ast_db_entry entry  ) 

Free structure created by ast_db_gettree().

Definition at line 461 of file db.c.

References free, last, and ast_db_entry::next.

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

00462 {
00463    struct ast_db_entry *last;
00464    while (dbe) {
00465       last = dbe;
00466       dbe = dbe->next;
00467       free(last);
00468    }
00469 }

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

Get key value specified by family/key.

Definition at line 172 of file db.c.

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

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

00173 {
00174    char fullkey[256] = "";
00175    DBT key, data;
00176    int res, fullkeylen;
00177 
00178    ast_mutex_lock(&dblock);
00179    if (dbinit()) {
00180       ast_mutex_unlock(&dblock);
00181       return -1;
00182    }
00183 
00184    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00185    memset(&key, 0, sizeof(key));
00186    memset(&data, 0, sizeof(data));
00187    memset(value, 0, valuelen);
00188    key.data = fullkey;
00189    key.size = fullkeylen + 1;
00190 
00191    res = astdb->get(astdb, &key, &data, 0);
00192 
00193    /* Be sure to NULL terminate our data either way */
00194    if (res) {
00195       if (option_debug)
00196          ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
00197    } else {
00198 #if 0
00199       printf("Got value of size %d\n", data.size);
00200 #endif
00201       if (data.size) {
00202          ((char *)data.data)[data.size - 1] = '\0';
00203          /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
00204          ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen);
00205       } else {
00206          ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
00207       }
00208    }
00209 
00210    /* Data is not fully isolated for concurrency, so the lock must be extended
00211     * to after the copy to the output buffer. */
00212    ast_mutex_unlock(&dblock);
00213 
00214    return res;
00215 }

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 399 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(), keys, last, LOG_WARNING, ast_db_entry::next, and prefix.

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

00400 {
00401    char prefix[256];
00402    DBT key, data;
00403    char *keys, *values;
00404    int values_len;
00405    int res;
00406    int pass;
00407    struct ast_db_entry *last = NULL;
00408    struct ast_db_entry *cur, *ret=NULL;
00409 
00410    if (!ast_strlen_zero(family)) {
00411       if (!ast_strlen_zero(keytree)) {
00412          /* Family and key tree */
00413          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00414       } else {
00415          /* Family only */
00416          snprintf(prefix, sizeof(prefix), "/%s", family);
00417       }
00418    } else {
00419       prefix[0] = '\0';
00420    }
00421    ast_mutex_lock(&dblock);
00422    if (dbinit()) {
00423       ast_mutex_unlock(&dblock);
00424       ast_log(LOG_WARNING, "Database unavailable\n");
00425       return NULL;   
00426    }
00427    memset(&key, 0, sizeof(key));
00428    memset(&data, 0, sizeof(data));
00429    pass = 0;
00430    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00431       if (key.size) {
00432          keys = key.data;
00433          keys[key.size - 1] = '\0';
00434       } else {
00435          keys = "<bad key>";
00436       }
00437       if (data.size) {
00438          values = data.data;
00439          values[data.size - 1] = '\0';
00440       } else {
00441          values = "<bad value>";
00442       }
00443       values_len = strlen(values) + 1;
00444       if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
00445          cur->next = NULL;
00446          cur->key = cur->data + values_len;
00447          strcpy(cur->data, values);
00448          strcpy(cur->key, keys);
00449          if (last) {
00450             last->next = cur;
00451          } else {
00452             ret = cur;
00453          }
00454          last = cur;
00455       }
00456    }
00457    ast_mutex_unlock(&dblock);
00458    return ret; 
00459 }

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

Store value addressed by family/key.

Definition at line 145 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(), database_put(), dump_agents(), dump_queue_members(), function_db_write(), handle_command_response(), handle_dbput(), iax_provision_build(), manager_dbput(), mgcp_ss(), parse_register_contact(), save_secret(), ss_thread(), and update_registry().

00146 {
00147    char fullkey[256];
00148    DBT key, data;
00149    int res, fullkeylen;
00150 
00151    ast_mutex_lock(&dblock);
00152    if (dbinit()) {
00153       ast_mutex_unlock(&dblock);
00154       return -1;
00155    }
00156 
00157    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00158    memset(&key, 0, sizeof(key));
00159    memset(&data, 0, sizeof(data));
00160    key.data = fullkey;
00161    key.size = fullkeylen + 1;
00162    data.data = value;
00163    data.size = strlen(value) + 1;
00164    res = astdb->put(astdb, &key, &data, 0);
00165    astdb->sync(astdb, 0);
00166    ast_mutex_unlock(&dblock);
00167    if (res)
00168       ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
00169    return res;
00170 }


Generated on Thu Oct 11 06:42:49 2012 for Asterisk - the Open Source PBX by  doxygen 1.5.6