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