Wed Aug 18 22:34:04 2010

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 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.
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().

00210 {
00211    char fullkey[256];
00212    DBT key;
00213    int res, fullkeylen;
00214 
00215    ast_mutex_lock(&dblock);
00216    if (dbinit()) {
00217       ast_mutex_unlock(&dblock);
00218       return -1;
00219    }
00220    
00221    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00222    memset(&key, 0, sizeof(key));
00223    key.data = fullkey;
00224    key.size = fullkeylen + 1;
00225    
00226    res = astdb->del(astdb, &key, 0);
00227    astdb->sync(astdb, 0);
00228    
00229    ast_mutex_unlock(&dblock);
00230 
00231    if (res) {
00232       ast_debug(1, "Unable to find key '%s' in family '%s'\n", keys, family);
00233    }
00234    return res;
00235 }

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().

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

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(), handle_cli_funcdevstate_list(), load_module(), process_clearcache(), reload_agents(), and reload_queue_members().

00536 {
00537    struct ast_db_entry *last;
00538    while (dbe) {
00539       last = dbe;
00540       dbe = dbe->next;
00541       ast_free(last);
00542    }
00543 }

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().

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

struct ast_db_entry* ast_db_gettree ( const char *  family,
const char *  keytree 
)

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, devstate_change::next, pass, and prefix.

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

00474 {
00475    char prefix[256];
00476    DBT key, data;
00477    char *keys, *values;
00478    int values_len;
00479    int res;
00480    int pass;
00481    struct ast_db_entry *last = NULL;
00482    struct ast_db_entry *cur, *ret=NULL;
00483 
00484    if (!ast_strlen_zero(family)) {
00485       if (!ast_strlen_zero(keytree)) {
00486          /* Family and key tree */
00487          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00488       } else {
00489          /* Family only */
00490          snprintf(prefix, sizeof(prefix), "/%s", family);
00491       }
00492    } else {
00493       prefix[0] = '\0';
00494    }
00495    ast_mutex_lock(&dblock);
00496    if (dbinit()) {
00497       ast_mutex_unlock(&dblock);
00498       ast_log(LOG_WARNING, "Database unavailable\n");
00499       return NULL;   
00500    }
00501    memset(&key, 0, sizeof(key));
00502    memset(&data, 0, sizeof(data));
00503    pass = 0;
00504    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00505       if (key.size) {
00506          keys = key.data;
00507          keys[key.size - 1] = '\0';
00508       } else {
00509          keys = "<bad key>";
00510       }
00511       if (data.size) {
00512          values = data.data;
00513          values[data.size - 1] = '\0';
00514       } else {
00515          values = "<bad value>";
00516       }
00517       values_len = strlen(values) + 1;
00518       if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
00519          cur->next = NULL;
00520          cur->key = cur->data + values_len;
00521          strcpy(cur->data, values);
00522          strcpy(cur->key, keys);
00523          if (last) {
00524             last->next = cur;
00525          } else {
00526             ret = cur;
00527          }
00528          last = cur;
00529       }
00530    }
00531    ast_mutex_unlock(&dblock);
00532    return ret; 
00533 }

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().

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


Generated on Wed Aug 18 22:34:04 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7