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