km_bdb_lib.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 :
  30. *
  31. * \ingroup database
  32. */
  33. #ifndef _KM_BDB_LIB_H_
  34. #define _KM_BDB_LIB_H_
  35. #include <stdlib.h>
  36. #include <syslog.h>
  37. #include <sys/stat.h>
  38. #include <db.h>
  39. #include "../../str.h"
  40. #include "../../lib/srdb1/db.h"
  41. #include "../../lib/srdb1/db_val.h"
  42. #include "../../locking.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. typedef db_val_t bdb_val_t, *bdb_val_p;
  65. typedef struct _row
  66. {
  67. bdb_val_p fields;
  68. struct _row *prev;
  69. struct _row *next;
  70. } row_t, *row_p;
  71. typedef struct _column
  72. {
  73. str name;
  74. str dv; /* default value */
  75. int type;
  76. int flag;
  77. } column_t, *column_p;
  78. typedef struct _table
  79. {
  80. str name;
  81. DB *db;
  82. gen_lock_t sem;
  83. column_p colp [MAX_NUM_COLS];
  84. int ncols;
  85. int nkeys;
  86. int ro; /*db readonly flag*/
  87. int logflags; /*flags indication what-where to journal log */
  88. FILE* fp; /*jlog file pointer */
  89. time_t t; /*jlog creation time */
  90. ino_t ino;
  91. } table_t, *table_p;
  92. typedef struct _tbl_cache
  93. {
  94. gen_lock_t sem;
  95. table_p dtp;
  96. struct _tbl_cache *prev;
  97. struct _tbl_cache *next;
  98. } tbl_cache_t, *tbl_cache_p;
  99. typedef struct _database
  100. {
  101. str name;
  102. DB_ENV *dbenv;
  103. tbl_cache_p tables;
  104. } database_t, *database_p;
  105. typedef struct _db_parms
  106. {
  107. u_int32_t cache_size;
  108. int auto_reload;
  109. int log_enable;
  110. int journal_roll_interval;
  111. } db_parms_t, *db_parms_p;
  112. int km_bdblib_init(db_parms_p _parms);
  113. int km_bdblib_destroy(void);
  114. int km_bdblib_close(char* _n);
  115. int km_bdblib_reopen(char* _n);
  116. int km_bdblib_recover(table_p _tp, int error_code);
  117. void km_bdblib_log(int op, table_p _tp, char* _msg, int len);
  118. int km_bdblib_create_dbenv(DB_ENV **dbenv, char* home);
  119. int km_bdblib_create_journal(table_p _tp);
  120. database_p km_bdblib_get_db(str *_s);
  121. tbl_cache_p km_bdblib_get_table(database_p _db, str *_s);
  122. table_p km_bdblib_create_table(database_p _db, str *_s);
  123. int db_free(database_p _dbp);
  124. int tbl_cache_free(tbl_cache_p _tbc);
  125. int tbl_free(table_p _tp);
  126. int km_load_metadata_columns(table_p _tp);
  127. int km_load_metadata_keys(table_p _tp);
  128. int km_load_metadata_readonly(table_p _tp);
  129. int km_load_metadata_logflags(table_p _tp);
  130. int km_load_metadata_defaults(table_p _tp);
  131. int km_bdblib_valtochar(table_p _tp, int* _lres, char* _k, int* _klen, db_val_t* _v, int _n, int _ko);
  132. #endif