Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Functions
astdb.h File Reference

Persistent 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_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_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.
 

Detailed Description

Persistent data storage (akin to *doze registry)

Definition in file astdb.h.

Function Documentation

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.

References ast_db_get().

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 
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);
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();
531  ast_mutex_unlock(&dblock);
532 
533  return res;
534 }
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
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 
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));
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();
566  ast_mutex_unlock(&dblock);
567 
568  return res;
569 }
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 
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));
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);
470  ast_mutex_unlock(&dblock);
471 
472  return res;
473 }
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.

Referenced by reload_queue_members().

438 {
439  *out = NULL;
440 
441  return db_get_common(family, key, out, -1);
442 }
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().

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 
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));
638  sqlite3_reset(stmt);
639  ast_mutex_unlock(&dblock);
640  return NULL;
641  }
642 
643  ret = db_gettree_common(stmt);
644  sqlite3_reset(stmt);
645  ast_mutex_unlock(&dblock);
646 
647  return ret;
648 }
Definition: astdb.h:31
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 
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);
667  return NULL;
668  }
669 
670  ret = db_gettree_common(gettree_prefix_stmt);
671  sqlite3_reset(gettree_prefix_stmt);
672  ast_mutex_unlock(&dblock);
673 
674  return ret;
675 }
Definition: astdb.h:31