km_bdb_lib.h

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * db_berkeley module, portions of this code were templated using
00005  * the dbtext and postgres modules.
00006 
00007  * Copyright (C) 2007 Cisco Systems
00008  *
00009  * This file is part of SIP-router, a free SIP server.
00010  *
00011  * SIP-router is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version
00015  *
00016  * SIP-router is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License 
00022  * along with this program; if not, write to the Free Software 
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  * 
00025  * History:
00026  * --------
00027  * 2007-09-19  genesis (wiquan)
00028  */
00029 
00038 #ifndef _KM_BDB_LIB_H_
00039 #define _KM_BDB_LIB_H_
00040 
00041 #include <stdlib.h>
00042 #include <syslog.h>
00043 #include <sys/stat.h>
00044 #include <db.h>
00045 
00046 #include "../../str.h"
00047 #include "../../lib/srdb1/db.h"
00048 #include "../../lib/srdb1/db_val.h"
00049 #include "../../locking.h"
00050 
00051 /*max number of columns in a table*/
00052 #define MAX_NUM_COLS 32
00053 
00054 /*max char width of a table row*/
00055 #define MAX_ROW_SIZE 2048
00056 
00057 /*max char width of a table name*/
00058 #define MAX_TABLENAME_SIZE 64
00059 
00060 #define METADATA_COLUMNS "METADATA_COLUMNS"
00061 #define METADATA_KEY "METADATA_KEY"
00062 #define METADATA_READONLY "METADATA_READONLY"
00063 #define METADATA_LOGFLAGS "METADATA_LOGFLAGS"
00064 #define METADATA_DEFAULTS "METADATA_DEFAULTS"
00065 
00066 /*journal logging flag masks */
00067 #define JLOG_NONE   0
00068 #define JLOG_INSERT 1
00069 #define JLOG_DELETE 2
00070 #define JLOG_UPDATE 4
00071 #define JLOG_FILE   8
00072 #define JLOG_STDOUT 16
00073 #define JLOG_SYSLOG 32
00074 
00075 #define DELIM "|"
00076 #define DELIM_LEN (sizeof(DELIM)-1)
00077 
00078 typedef db_val_t bdb_val_t, *bdb_val_p;
00079 
00080 typedef struct _row
00081 {
00082         bdb_val_p fields;
00083         struct _row *prev;
00084         struct _row *next;
00085 } row_t, *row_p;
00086 
00087 typedef struct _column
00088 {
00089         str name;
00090         str dv;     /* default value */
00091         int type;
00092         int flag;
00093 } column_t, *column_p;
00094 
00095 typedef struct _table
00096 {
00097         str name;
00098         DB *db;
00099         gen_lock_t sem;
00100         column_p colp [MAX_NUM_COLS];
00101         int ncols;
00102         int nkeys;
00103         int ro;       /*db readonly flag*/
00104         int logflags; /*flags indication what-where to journal log */
00105         FILE* fp;     /*jlog file pointer */
00106         time_t t;     /*jlog creation time */
00107         ino_t ino;
00108 } table_t, *table_p;
00109 
00110 typedef struct _tbl_cache
00111 {
00112         gen_lock_t sem;
00113         table_p dtp;
00114         struct _tbl_cache *prev;
00115         struct _tbl_cache *next;
00116 } tbl_cache_t, *tbl_cache_p;
00117 
00118 typedef struct _database
00119 {
00120         str name;
00121         DB_ENV *dbenv;
00122         tbl_cache_p tables;
00123 } database_t, *database_p;
00124 
00125 typedef struct _db_parms
00126 {
00127         u_int32_t cache_size;
00128         int auto_reload;
00129         int log_enable;
00130         int journal_roll_interval;
00131 } db_parms_t, *db_parms_p;
00132 
00133 
00134 int km_bdblib_init(db_parms_p _parms);
00135 int km_bdblib_destroy(void);
00136 int km_bdblib_close(char* _n);
00137 int km_bdblib_reopen(char* _n);
00138 int km_bdblib_recover(table_p _tp, int error_code);
00139 void km_bdblib_log(int op, table_p _tp, char* _msg, int len);
00140 int km_bdblib_create_dbenv(DB_ENV **dbenv, char* home);
00141 int km_bdblib_create_journal(table_p _tp);
00142 database_p      km_bdblib_get_db(str *_s);
00143 tbl_cache_p     km_bdblib_get_table(database_p _db, str *_s);
00144 table_p         km_bdblib_create_table(database_p _db, str *_s);
00145 
00146 int db_free(database_p _dbp);
00147 int tbl_cache_free(tbl_cache_p _tbc);
00148 int tbl_free(table_p _tp);
00149 
00150 int km_load_metadata_columns(table_p _tp);
00151 int km_load_metadata_keys(table_p _tp);
00152 int km_load_metadata_readonly(table_p _tp);
00153 int km_load_metadata_logflags(table_p _tp);
00154 int km_load_metadata_defaults(table_p _tp);
00155 
00156 int km_bdblib_valtochar(table_p _tp, int* _lres, char* _k, int* _klen, db_val_t* _v, int _n, int _ko);
00157 
00158 #endif