Persistent data storage (akin to *doze registry)
More...
Go to the source code of this file.
|
int | ast_db_del (const char *family, const char *key) |
| Delete entry in astdb.
|
|
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()
|
|
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. More...
|
|
struct ast_db_entry * | ast_db_gettree (const char *family, const char *keytree) |
| Get a list of values within the astdb tree. More...
|
|
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. More...
|
|
int | ast_db_put (const char *family, const char *key, const char *value) |
| Store value addressed by family/key.
|
|
Persistent data storage (akin to *doze registry)
Definition in file astdb.h.
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
-
-1 | An error occured (including key not found to begin with) |
0 | Successfully deleted |
Definition at line 504 of file main/db.c.
References ast_db_get().
506 char fullkey[MAX_DB_FIELD];
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);
516 fullkey_len = snprintf(fullkey,
sizeof(fullkey),
"/%s/%s", family, key);
518 ast_mutex_lock(&dblock);
519 if (
ast_db_get(family, key, tmp,
sizeof(tmp))) {
520 ast_log(LOG_WARNING,
"AstDB key %s does not exist\n", fullkey);
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));
525 }
else if ((mres = sqlite3_step(del_stmt) != SQLITE_DONE)) {
526 ast_log(LOG_WARNING,
"AstDB error (%s): %s\n", fullkey, sqlite3_errstr(mres));
529 sqlite3_reset(del_stmt);
531 ast_mutex_unlock(&dblock);
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
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 536 of file main/db.c.
538 sqlite3_stmt *stmt = deltree_stmt;
539 char prefix[MAX_DB_FIELD];
542 if (!ast_strlen_zero(family)) {
543 if (!ast_strlen_zero(keytree)) {
545 snprintf(prefix,
sizeof(prefix),
"/%s/%s", family, keytree);
548 snprintf(prefix,
sizeof(prefix),
"/%s", family);
552 stmt = deltree_all_stmt;
555 ast_mutex_lock(&dblock);
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));
559 }
else if (sqlite3_step(stmt) != SQLITE_DONE) {
560 ast_log(LOG_WARNING,
"Couldn't execute stmt: %s\n", sqlite3_errmsg(astdb));
563 res = sqlite3_changes(astdb);
566 ast_mutex_unlock(&dblock);
int ast_db_exists |
( |
const char * |
family, |
|
|
const char * |
key |
|
) |
| |
Check if family/key exitsts.
- Parameters
-
- Return values
-
1 | if family/key exists |
0 | if family/key does not exist or an error occurred |
Definition at line 444 of file main/db.c.
447 char fullkey[MAX_DB_FIELD];
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);
457 ast_mutex_lock(&dblock);
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));
462 }
else if (sqlite3_step(exists_stmt) != SQLITE_ROW) {
464 }
else if (!(result = sqlite3_column_int(exists_stmt, 0))) {
469 sqlite3_reset(exists_stmt);
470 ast_mutex_unlock(&dblock);
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 437 of file main/db.c.
Referenced by reload_queue_members().
441 return db_get_common(family, key, out, -1);
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.
Referenced by reload_queue_members(), sorcery_astdb_retrieve_fields_common(), and stasis_app_device_states_to_json().
612 char prefix[MAX_DB_FIELD];
613 sqlite3_stmt *stmt = gettree_stmt;
617 if (!ast_strlen_zero(family)) {
618 if (!ast_strlen_zero(keytree)) {
620 res = snprintf(prefix,
sizeof(prefix),
"/%s/%s", family, keytree);
623 res = snprintf(prefix,
sizeof(prefix),
"/%s", family);
626 if (res >=
sizeof(prefix)) {
627 ast_log(LOG_WARNING,
"Requested prefix is too long: %s\n", keytree);
632 stmt = gettree_all_stmt;
635 ast_mutex_lock(&dblock);
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));
639 ast_mutex_unlock(&dblock);
643 ret = db_gettree_common(stmt);
645 ast_mutex_unlock(&dblock);
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
-
family | The family to search under |
key_prefix | The key prefix to search under |
- Return values
-
Definition at line 650 of file main/db.c.
652 char prefix[MAX_DB_FIELD];
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);
662 ast_mutex_lock(&dblock);
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);
666 ast_mutex_unlock(&dblock);
670 ret = db_gettree_common(gettree_prefix_stmt);
671 sqlite3_reset(gettree_prefix_stmt);
672 ast_mutex_unlock(&dblock);