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 _BDB_LIB_H_
00039 #define _BDB_LIB_H_
00040 
00041 #include <time.h>
00042 #include <stdlib.h>
00043 #include <syslog.h>
00044 #include <sys/stat.h>
00045 #include <db.h>
00046 
00047 #include "../../str.h"
00048 #include "../../lib/srdb2/db.h"
00049 #include "../../lib/srdb2/db_fld.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 #define BDB_VALUE 0
00079 #define BDB_KEY   1
00080 
00081 typedef enum db_fld_type bdb_type_t;
00082 
00083 typedef struct {
00084         bdb_type_t type; 
00085         int nul;                
00086         int free;               
00088         union {
00089                 int           int_val;    
00090                 long long     ll_val;     
00091                 double        double_val; 
00092                 time_t        time_val;   
00093                 const char*   string_val; 
00094                 str           str_val;    
00095                 str           blob_val;   
00096                 unsigned int  bitmap_val; 
00097         } val;
00098 } bdb_val_t, *bdb_val_p;
00099 
00100 // typedef db_val_t bdb_val_t, *bdb_val_p;
00101 
00102 typedef struct _bdb_row
00103 {
00104         bdb_val_p fields;
00105         struct _bdb_row *prev;
00106         struct _bdb_row *next;
00107 } bdb_row_t, *bdb_row_p;
00108 
00109 typedef struct _bdb_col
00110 {
00111         str name;
00112         str dv;     /* default value */
00113         int type;
00114         int flag;
00115 } bdb_col_t, *bdb_col_p;
00116 
00117 typedef struct _bdb_table
00118 {
00119         str name;
00120         DB *db;
00121         bdb_col_p colp [MAX_NUM_COLS];
00122         int ncols;
00123         int nkeys;
00124         int ro;       /*db readonly flag*/
00125         int logflags; /*flags indication what-where to journal log */
00126         FILE* fp;     /*jlog file pointer */
00127         time_t t;     /*jlog creation time */
00128         ino_t ino;
00129 } bdb_table_t, *bdb_table_p;
00130 
00131 typedef struct _bdb_tcache
00132 {
00133         bdb_table_p dtp;
00134         struct _bdb_tcache *prev;
00135         struct _bdb_tcache *next;
00136 } bdb_tcache_t, *bdb_tcache_p;
00137 
00138 typedef struct _bdb_db
00139 {
00140         str name;
00141         DB_ENV *dbenv;
00142         bdb_tcache_p tables;
00143 } bdb_db_t, *bdb_db_p;
00144 
00145 typedef struct _bdb_params
00146 {
00147         u_int32_t cache_size;
00148         int auto_reload;
00149         int log_enable;
00150         int journal_roll_interval;
00151 } bdb_params_t, *bdb_params_p;
00152 
00153 
00154 int bdblib_init(bdb_params_p _parms);
00155 int bdblib_destroy(void);
00156 int bdblib_close(bdb_db_p _db_p, str* _n);
00157 int bdblib_reopen(bdb_db_p _db_p, str* _n);
00158 int bdblib_recover(bdb_table_p _tp, int error_code);
00159 void bdblib_log(int op, bdb_db_p _db, bdb_table_p _tp, char* _msg, int len);
00160 int bdblib_create_dbenv(DB_ENV **dbenv, char* home);
00161 int bdblib_create_journal(bdb_db_p _db_p, bdb_table_p _tp);
00162 bdb_db_p        bdblib_get_db(str *_s);
00163 bdb_tcache_p    bdblib_get_table(bdb_db_t *_db, str *_s);
00164 bdb_table_p     bdblib_create_table(bdb_db_t *_db, str *_s);
00165 
00166 int bdb_db_free(bdb_db_p _dbp);
00167 int bdb_tcache_free(bdb_tcache_p _tbc);
00168 int bdb_table_free(bdb_table_p _tp);
00169 
00170 int load_metadata_columns(bdb_table_p _tp);
00171 int load_metadata_keys(bdb_table_p _tp);
00172 int load_metadata_readonly(bdb_table_p _tp);
00173 int load_metadata_logflags(bdb_table_p _tp);
00174 int load_metadata_defaults(bdb_table_p _tp);
00175 
00176 int bdblib_valtochar(bdb_table_p tp, db_fld_t *fld, int fld_count, char *kout,
00177                 int *klen, int ktype);
00178 
00179 int bdb_is_database(char *dirpath);
00180 int bdb_get_colpos(bdb_table_t *tp, char *name);
00181 
00182 int bdb_str2int(char *s, int *v);
00183 int bdb_str2double(char *s, double *v);
00184 int bdb_str2time(char *s, time_t *v);
00185 
00186 #endif