Asterisk - The Open Source Telephony Project GIT-master-7e7a603
Data Structures | Functions
astdb.h File Reference

Persistent 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. More...
 
int ast_db_del2 (const char *family, const char *key)
 Same as ast_db_del, but with more stringent error checking. More...
 
int ast_db_deltree (const char *family, const char *keytree)
 Delete one or more entries in astdb. More...
 
int ast_db_exists (const char *family, const char *key)
 Check if family/key exitsts. More...
 
void ast_db_freetree (struct ast_db_entry *entry)
 Free structure created by ast_db_gettree() More...
 
int ast_db_get (const char *family, const char *key, char *value, int valuelen)
 Get key value specified by family/key. More...
 
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. More...
 
struct ast_db_entryast_db_gettree (const char *family, const char *keytree)
 Get a list of values within the astdb tree. More...
 
struct ast_db_entryast_db_gettree_by_prefix (const char *family, const char *key_prefix)
 Get a list of values with the given key prefix. More...
 
int ast_db_put (const char *family, const char *key, const char *value)
 Store value addressed by family/key. More...
 

Detailed Description

Persistent data storage (akin to *doze registry)

Definition in file astdb.h.

Function Documentation

◆ ast_db_del()

int ast_db_del ( const char *  family,
const char *  key 
)

Delete entry in astdb.

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

477{
478 char fullkey[MAX_DB_FIELD];
479 size_t fullkey_len;
480 int res = 0;
481
482 if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
483 ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
484 return -1;
485 }
486
487 fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
488
490 if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
491 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
492 res = -1;
493 } else if (sqlite3_step(del_stmt) != SQLITE_DONE) {
494 ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
495 res = -1;
496 }
497 sqlite3_reset(del_stmt);
498 db_sync();
500
501 return res;
502}
#define ast_log
Definition: astobj2.c:42
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_WARNING
#define ast_mutex_unlock(a)
Definition: lock.h:190
#define ast_mutex_lock(a)
Definition: lock.h:189
static void db_sync(void)
Definition: main/db.c:1154
static sqlite3 * astdb
Definition: main/db.c:122
static ast_mutex_t dblock
Definition: main/db.c:120
#define MAX_DB_FIELD
Definition: main/db.c:119

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

Referenced by __expire_registry(), add_cert_key_to_astdb(), ast_privacy_set(), AST_TEST_DEFINE(), auth_exec(), cache_lookup_internal(), cleanup_cert_from_astdb_and_fs(), destroy_all_channels(), dialgroup_refreshdb(), do_register_expire(), dump_queue_members(), function_db_delete(), handle_dbdel(), media_cache_item_del_from_astdb(), media_cache_remove_from_astdb(), mkintf(), process_clearcache(), reload_queue_members(), sorcery_astdb_delete(), stasis_app_device_state_delete(), and update_registry().

◆ ast_db_del2()

int ast_db_del2 ( const char *  family,
const char *  key 
)

Same as ast_db_del, but with more stringent error checking.

Unlike ast_db_del, if the key does not exist in the first place, an error is emitted and -1 is returned.

Return values
-1An error occured (including key not found to begin with)
0Successfully deleted

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

505{
506 char fullkey[MAX_DB_FIELD];
507 char tmp[1];
508 size_t fullkey_len;
509 int mres, res = 0;
510
511 if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
512 ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
513 return -1;
514 }
515
516 fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
517
519 if (ast_db_get(family, key, tmp, sizeof(tmp))) {
520 ast_log(LOG_WARNING, "AstDB key %s does not exist\n", fullkey);
521 res = -1;
522 } else if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
523 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
524 res = -1;
525 } else if ((mres = sqlite3_step(del_stmt) != SQLITE_DONE)) {
526 ast_log(LOG_WARNING, "AstDB error (%s): %s\n", fullkey, sqlite3_errstr(mres));
527 res = -1;
528 }
529 sqlite3_reset(del_stmt);
530 db_sync();
532
533 return res;
534}
static int tmp()
Definition: bt_open.c:389
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:427

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

Referenced by handle_cli_database_del(), and manager_dbdel().

◆ ast_db_deltree()

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 536 of file main/db.c.

537{
538 sqlite3_stmt *stmt = deltree_stmt;
539 char prefix[MAX_DB_FIELD];
540 int res = 0;
541
542 if (!ast_strlen_zero(family)) {
543 if (!ast_strlen_zero(keytree)) {
544 /* Family and key tree */
545 snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
546 } else {
547 /* Family only */
548 snprintf(prefix, sizeof(prefix), "/%s", family);
549 }
550 } else {
551 prefix[0] = '\0';
552 stmt = deltree_all_stmt;
553 }
554
556 if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
557 ast_log(LOG_WARNING, "Couldn't bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
558 res = -1;
559 } else if (sqlite3_step(stmt) != SQLITE_DONE) {
560 ast_log(LOG_WARNING, "Couldn't execute stmt: %s\n", sqlite3_errmsg(astdb));
561 res = -1;
562 }
563 res = sqlite3_changes(astdb);
564 sqlite3_reset(stmt);
565 db_sync();
567
568 return res;
569}
static char prefix[MAX_PREFIX]
Definition: http.c:144
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65

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

Referenced by ast_privacy_reset(), AST_TEST_DEFINE(), cleanup_cert_from_astdb_and_fs(), deinitialize_sorcery(), deltree_exec(), dundi_flush(), handle_cli_database_deltree(), handle_dbdeltree(), iax_provision_reload(), manager_dbdeltree(), media_cache_item_del_from_astdb(), and media_cache_remove_from_astdb().

◆ ast_db_exists()

int ast_db_exists ( const char *  family,
const char *  key 
)

Check if family/key exitsts.

Parameters
family
key
Return values
1if family/key exists
0if family/key does not exist or an error occurred

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

445{
446 int result;
447 char fullkey[MAX_DB_FIELD];
448 size_t fullkey_len;
449 int res = 0;
450
451 fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
452 if (fullkey_len >= sizeof(fullkey)) {
453 ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
454 return -1;
455 }
456
458 res = sqlite3_bind_text(exists_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC);
459 if (res != SQLITE_OK) {
460 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %d:%s\n", res, sqlite3_errmsg(astdb));
461 res = 0;
462 } else if (sqlite3_step(exists_stmt) != SQLITE_ROW) {
463 res = 0;
464 } else if (!(result = sqlite3_column_int(exists_stmt, 0))) {
465 res = 0;
466 } else {
467 res = result;
468 }
469 sqlite3_reset(exists_stmt);
471
472 return res;
473}
static PGresult * result
Definition: cel_pgsql.c:84

References ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, dblock, LOG_WARNING, MAX_DB_FIELD, and result.

Referenced by cleanup_cert_from_astdb_and_fs(), and retrieve_cert_from_cache().

◆ ast_db_freetree()

void ast_db_freetree ( struct ast_db_entry entry)

◆ ast_db_get()

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

◆ ast_db_get_allocated()

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 437 of file main/db.c.

438{
439 *out = NULL;
440
441 return db_get_common(family, key, out, -1);
442}
FILE * out
Definition: utils/frame.c:33

References db_get_common(), NULL, and out.

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

◆ ast_db_gettree()

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 610 of file main/db.c.

611{
612 char prefix[MAX_DB_FIELD];
613 sqlite3_stmt *stmt = gettree_stmt;
614 size_t res = 0;
615 struct ast_db_entry *ret;
616
617 if (!ast_strlen_zero(family)) {
618 if (!ast_strlen_zero(keytree)) {
619 /* Family and key tree */
620 res = snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
621 } else {
622 /* Family only */
623 res = snprintf(prefix, sizeof(prefix), "/%s", family);
624 }
625
626 if (res >= sizeof(prefix)) {
627 ast_log(LOG_WARNING, "Requested prefix is too long: %s\n", keytree);
628 return NULL;
629 }
630 } else {
631 prefix[0] = '\0';
632 stmt = gettree_all_stmt;
633 }
634
636 if (res && (sqlite3_bind_text(stmt, 1, prefix, res, SQLITE_STATIC) != SQLITE_OK)) {
637 ast_log(LOG_WARNING, "Could not bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
638 sqlite3_reset(stmt);
640 return NULL;
641 }
642
643 ret = db_gettree_common(stmt);
644 sqlite3_reset(stmt);
646
647 return ret;
648}
static struct ast_db_entry * db_gettree_common(sqlite3_stmt *stmt)
Definition: main/db.c:571

References ast_log, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), astdb, db_gettree_common(), dblock, LOG_WARNING, MAX_DB_FIELD, NULL, and prefix.

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

◆ ast_db_gettree_by_prefix()

struct ast_db_entry * ast_db_gettree_by_prefix ( const char *  family,
const char *  key_prefix 
)

Get a list of values with the given key prefix.

Parameters
familyThe family to search under
key_prefixThe key prefix to search under
Return values
NULLAn error occurred

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

651{
652 char prefix[MAX_DB_FIELD];
653 size_t res;
654 struct ast_db_entry *ret;
655
656 res = snprintf(prefix, sizeof(prefix), "/%s/%s", family, key_prefix);
657 if (res >= sizeof(prefix)) {
658 ast_log(LOG_WARNING, "Requested key prefix is too long: %s\n", key_prefix);
659 return NULL;
660 }
661
663 if (sqlite3_bind_text(gettree_prefix_stmt, 1, prefix, res, SQLITE_STATIC) != SQLITE_OK) {
664 ast_log(LOG_WARNING, "Could not bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
665 sqlite3_reset(gettree_prefix_stmt);
667 return NULL;
668 }
669
670 ret = db_gettree_common(gettree_prefix_stmt);
671 sqlite3_reset(gettree_prefix_stmt);
673
674 return ret;
675}

References ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, db_gettree_common(), dblock, LOG_WARNING, MAX_DB_FIELD, NULL, and prefix.

Referenced by sorcery_astdb_retrieve_prefix().

◆ ast_db_put()

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

Store value addressed by family/key.

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

343{
344 char fullkey[MAX_DB_FIELD];
345 size_t fullkey_len;
346 int res = 0;
347
348 if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
349 ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
350 return -1;
351 }
352
353 fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
354
356 if (sqlite3_bind_text(put_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
357 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
358 res = -1;
359 } else if (sqlite3_bind_text(put_stmt, 2, value, -1, SQLITE_STATIC) != SQLITE_OK) {
360 ast_log(LOG_WARNING, "Couldn't bind value to stmt: %s\n", sqlite3_errmsg(astdb));
361 res = -1;
362 } else if (sqlite3_step(put_stmt) != SQLITE_DONE) {
363 ast_log(LOG_WARNING, "Couldn't execute statement: %s\n", sqlite3_errmsg(astdb));
364 res = -1;
365 }
366
367 sqlite3_reset(put_stmt);
368 db_sync();
370
371 return res;
372}

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

Referenced by __analog_ss_thread(), add_cert_expiration_to_astdb(), add_cert_key_to_astdb(), ast_privacy_set(), AST_TEST_DEFINE(), asterisk_daemon(), 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(), media_cache_item_sync_to_astdb(), metadata_sync_to_astdb(), presence_write(), save_secret(), sorcery_astdb_create(), stasis_app_device_state_update(), and update_registry().