Mon Jun 27 16:51:02 2011

Asterisk developer's documentation


astdb.h File Reference

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

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

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

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(), manager_dbdel(), process_clearcache(), reload_queue_members(), and update_registry().

00265 {
00266    char fullkey[256];
00267    DBT key;
00268    int res, fullkeylen;
00269 
00270    ast_mutex_lock(&dblock);
00271    if (dbinit()) {
00272       ast_mutex_unlock(&dblock);
00273       return -1;
00274    }
00275    
00276    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00277    memset(&key, 0, sizeof(key));
00278    key.data = fullkey;
00279    key.size = fullkeylen + 1;
00280    
00281    res = astdb->del(astdb, &key, 0);
00282    db_sync();
00283    
00284    ast_mutex_unlock(&dblock);
00285 
00286    if (res) {
00287       ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
00288    }
00289    return res;
00290 }

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

References ast_mutex_lock, ast_mutex_unlock, db_sync(), 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().

00147 {
00148    char prefix[256];
00149    DBT key, data;
00150    char *keys;
00151    int res;
00152    int pass;
00153    int counter = 0;
00154    
00155    if (family) {
00156       if (keytree) {
00157          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00158       } else {
00159          snprintf(prefix, sizeof(prefix), "/%s", family);
00160       }
00161    } else if (keytree) {
00162       return -1;
00163    } else {
00164       prefix[0] = '\0';
00165    }
00166    
00167    ast_mutex_lock(&dblock);
00168    if (dbinit()) {
00169       ast_mutex_unlock(&dblock);
00170       return -1;
00171    }
00172    
00173    memset(&key, 0, sizeof(key));
00174    memset(&data, 0, sizeof(data));
00175    pass = 0;
00176    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00177       if (key.size) {
00178          keys = key.data;
00179          keys[key.size - 1] = '\0';
00180       } else {
00181          keys = "<bad key>";
00182       }
00183       if (keymatch(keys, prefix)) {
00184          astdb->del(astdb, &key, 0);
00185          counter++;
00186       }
00187    }
00188    db_sync();
00189    ast_mutex_unlock(&dblock);
00190    return counter;
00191 }

void ast_db_freetree ( struct ast_db_entry entry  ) 

Free structure created by ast_db_gettree().

Definition at line 590 of file db.c.

References ast_free, last, and ast_db_entry::next.

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

00591 {
00592    struct ast_db_entry *last;
00593    while (dbe) {
00594       last = dbe;
00595       dbe = dbe->next;
00596       ast_free(last);
00597    }
00598 }

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

Get key value specified by family/key.

Definition at line 220 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(), destroy_all_channels(), 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(), and reload_queue_members().

00221 {
00222    char fullkey[256] = "";
00223    DBT key, data;
00224    int res, fullkeylen;
00225 
00226    ast_mutex_lock(&dblock);
00227    if (dbinit()) {
00228       ast_mutex_unlock(&dblock);
00229       return -1;
00230    }
00231 
00232    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00233    memset(&key, 0, sizeof(key));
00234    memset(&data, 0, sizeof(data));
00235    memset(value, 0, valuelen);
00236    key.data = fullkey;
00237    key.size = fullkeylen + 1;
00238 
00239    res = astdb->get(astdb, &key, &data, 0);
00240 
00241    /* Be sure to NULL terminate our data either way */
00242    if (res) {
00243       ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
00244    } else {
00245 #if 0
00246       printf("Got value of size %d\n", data.size);
00247 #endif
00248       if (data.size) {
00249          ((char *)data.data)[data.size - 1] = '\0';
00250          /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
00251          ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen);
00252       } else {
00253          ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
00254       }
00255    }
00256 
00257    /* Data is not fully isolated for concurrency, so the lock must be extended
00258     * to after the copy to the output buffer. */
00259    ast_mutex_unlock(&dblock);
00260 
00261    return res;
00262 }

struct ast_db_entry* ast_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.

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

Definition at line 528 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, sla_ringing_trunk::next, pass, and prefix.

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

00529 {
00530    char prefix[256];
00531    DBT key, data;
00532    char *keys, *values;
00533    int values_len;
00534    int res;
00535    int pass;
00536    struct ast_db_entry *last = NULL;
00537    struct ast_db_entry *cur, *ret=NULL;
00538 
00539    if (!ast_strlen_zero(family)) {
00540       if (!ast_strlen_zero(keytree)) {
00541          /* Family and key tree */
00542          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00543       } else {
00544          /* Family only */
00545          snprintf(prefix, sizeof(prefix), "/%s", family);
00546       }
00547    } else {
00548       prefix[0] = '\0';
00549    }
00550    ast_mutex_lock(&dblock);
00551    if (dbinit()) {
00552       ast_mutex_unlock(&dblock);
00553       ast_log(LOG_WARNING, "Database unavailable\n");
00554       return NULL;   
00555    }
00556    memset(&key, 0, sizeof(key));
00557    memset(&data, 0, sizeof(data));
00558    pass = 0;
00559    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00560       if (key.size) {
00561          keys = key.data;
00562          keys[key.size - 1] = '\0';
00563       } else {
00564          keys = "<bad key>";
00565       }
00566       if (data.size) {
00567          values = data.data;
00568          values[data.size - 1] = '\0';
00569       } else {
00570          values = "<bad value>";
00571       }
00572       values_len = strlen(values) + 1;
00573       if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
00574          cur->next = NULL;
00575          cur->key = cur->data + values_len;
00576          strcpy(cur->data, values);
00577          strcpy(cur->key, keys);
00578          if (last) {
00579             last->next = cur;
00580          } else {
00581             ret = cur;
00582          }
00583          last = cur;
00584       }
00585    }
00586    ast_mutex_unlock(&dblock);
00587    return ret; 
00588 }

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

Store value addressed by family/key.

Definition at line 193 of file db.c.

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

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_command_response(), handle_dbput(), iax_provision_build(), manager_dbput(), mgcp_ss(), parse_register_contact(), save_secret(), and update_registry().

00194 {
00195    char fullkey[256];
00196    DBT key, data;
00197    int res, fullkeylen;
00198 
00199    ast_mutex_lock(&dblock);
00200    if (dbinit()) {
00201       ast_mutex_unlock(&dblock);
00202       return -1;
00203    }
00204 
00205    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00206    memset(&key, 0, sizeof(key));
00207    memset(&data, 0, sizeof(data));
00208    key.data = fullkey;
00209    key.size = fullkeylen + 1;
00210    data.data = (char *) value;
00211    data.size = strlen(value) + 1;
00212    res = astdb->put(astdb, &key, &data, 0);
00213    db_sync();
00214    ast_mutex_unlock(&dblock);
00215    if (res)
00216       ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
00217    return res;
00218 }


Generated on Mon Jun 27 16:51:02 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7