bdb_lib.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * $Id$
  3. *
  4. * db_berkeley module, portions of this code were templated using
  5. * the dbtext and postgres modules.
  6. * Copyright (C) 2007 Cisco Systems
  7. *
  8. * This file is part of SIP-router, a free SIP server.
  9. *
  10. * SIP-router is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * SIP-router is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * --------
  26. * 2007-09-19 genesis (wiquan)
  27. */
  28. /*! \file
  29. * Berkeley DB : Library
  30. *
  31. * \ingroup database
  32. */
  33. #ifndef _BDB_LIB_H_
  34. #define _BDB_LIB_H_
  35. #include <time.h>
  36. #include <stdlib.h>
  37. #include <syslog.h>
  38. #include <sys/stat.h>
  39. #include <db.h>
  40. #include "../../str.h"
  41. #include "../../lib/srdb2/db.h"
  42. #include "../../lib/srdb2/db_fld.h"
  43. /*max number of columns in a table*/
  44. #define MAX_NUM_COLS 32
  45. /*max char width of a table row*/
  46. #define MAX_ROW_SIZE 2048
  47. /*max char width of a table name*/
  48. #define MAX_TABLENAME_SIZE 64
  49. #define METADATA_COLUMNS "METADATA_COLUMNS"
  50. #define METADATA_KEY "METADATA_KEY"
  51. #define METADATA_READONLY "METADATA_READONLY"
  52. #define METADATA_LOGFLAGS "METADATA_LOGFLAGS"
  53. #define METADATA_DEFAULTS "METADATA_DEFAULTS"
  54. /*journal logging flag masks */
  55. #define JLOG_NONE 0
  56. #define JLOG_INSERT 1
  57. #define JLOG_DELETE 2
  58. #define JLOG_UPDATE 4
  59. #define JLOG_FILE 8
  60. #define JLOG_STDOUT 16
  61. #define JLOG_SYSLOG 32
  62. #define DELIM "|"
  63. #define DELIM_LEN (sizeof(DELIM)-1)
  64. #define BDB_VALUE 0
  65. #define BDB_KEY 1
  66. typedef enum db_fld_type bdb_type_t;
  67. typedef struct {
  68. bdb_type_t type; /**< Type of the value */
  69. int nul; /**< Means that the column in database has no value */
  70. int free; /**< Means that the value should be freed */
  71. /** Column value structure that holds the actual data in a union. */
  72. union {
  73. int int_val; /**< integer value */
  74. long long ll_val; /**< long long value */
  75. double double_val; /**< double value */
  76. time_t time_val; /**< unix time_t value */
  77. const char* string_val; /**< zero terminated string */
  78. str str_val; /**< str type string value */
  79. str blob_val; /**< binary object data */
  80. unsigned int bitmap_val; /**< Bitmap data type */
  81. } val;
  82. } bdb_val_t, *bdb_val_p;
  83. // typedef db_val_t bdb_val_t, *bdb_val_p;
  84. typedef struct _bdb_row
  85. {
  86. bdb_val_p fields;
  87. struct _bdb_row *prev;
  88. struct _bdb_row *next;
  89. } bdb_row_t, *bdb_row_p;
  90. typedef struct _bdb_col
  91. {
  92. str name;
  93. str dv; /* default value */
  94. int type;
  95. int flag;
  96. } bdb_col_t, *bdb_col_p;
  97. typedef struct _bdb_table
  98. {
  99. str name;
  100. DB *db;
  101. bdb_col_p colp [MAX_NUM_COLS];
  102. int ncols;
  103. int nkeys;
  104. int ro; /*db readonly flag*/
  105. int logflags; /*flags indication what-where to journal log */
  106. FILE* fp; /*jlog file pointer */
  107. time_t t; /*jlog creation time */
  108. ino_t ino;
  109. } bdb_table_t, *bdb_table_p;
  110. typedef struct _bdb_tcache
  111. {
  112. bdb_table_p dtp;
  113. struct _bdb_tcache *prev;
  114. struct _bdb_tcache *next;
  115. } bdb_tcache_t, *bdb_tcache_p;
  116. typedef struct _bdb_db
  117. {
  118. str name;
  119. DB_ENV *dbenv;
  120. bdb_tcache_p tables;
  121. } bdb_db_t, *bdb_db_p;
  122. typedef struct _bdb_params
  123. {
  124. u_int32_t cache_size;
  125. int auto_reload;
  126. int log_enable;
  127. int journal_roll_interval;
  128. } bdb_params_t, *bdb_params_p;
  129. int bdblib_init(bdb_params_p _parms);
  130. int bdblib_destroy(void);
  131. int bdblib_close(bdb_db_p _db_p, str* _n);
  132. int bdblib_reopen(bdb_db_p _db_p, str* _n);
  133. int bdblib_recover(bdb_table_p _tp, int error_code);
  134. void bdblib_log(int op, bdb_db_p _db, bdb_table_p _tp, char* _msg, int len);
  135. int bdblib_create_dbenv(DB_ENV **dbenv, char* home);
  136. int bdblib_create_journal(bdb_db_p _db_p, bdb_table_p _tp);
  137. bdb_db_p bdblib_get_db(str *_s);
  138. bdb_tcache_p bdblib_get_table(bdb_db_t *_db, str *_s);
  139. bdb_table_p bdblib_create_table(bdb_db_t *_db, str *_s);
  140. int bdb_db_free(bdb_db_p _dbp);
  141. int bdb_tcache_free(bdb_tcache_p _tbc);
  142. int bdb_table_free(bdb_table_p _tp);
  143. int load_metadata_columns(bdb_table_p _tp);
  144. int load_metadata_keys(bdb_table_p _tp);
  145. int load_metadata_readonly(bdb_table_p _tp);
  146. int load_metadata_logflags(bdb_table_p _tp);
  147. int load_metadata_defaults(bdb_table_p _tp);
  148. int bdblib_valtochar(bdb_table_p tp, db_fld_t *fld, int fld_count, char *kout,
  149. int *klen, int ktype);
  150. int bdb_is_database(char *dirpath);
  151. int bdb_get_colpos(bdb_table_t *tp, char *name);
  152. int bdb_str2int(char *s, int *v);
  153. int bdb_str2double(char *s, double *v);
  154. int bdb_str2time(char *s, time_t *v);
  155. #endif