1 |
|
/* |
2 |
|
* DotGNU Execution Environment Core |
3 |
|
* (c)2003 netFluid Technology Ltd - http://www.nfluid.com |
4 |
|
* |
5 |
|
* Berkeley DB Wrappers |
6 |
|
* |
7 |
|
* This program is free software; you can redistribute it and/or modify |
8 |
|
* it under the terms of the GNU General Public License as published by |
9 |
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
* (at your option) any later version. |
11 |
|
* |
12 |
|
* This program is distributed in the hope that it will be useful, |
13 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
* GNU General Public License for more details. |
16 |
|
* |
17 |
|
* You should have received a copy of the GNU General Public License |
18 |
|
* along with this program; if not, write to the Free Software |
19 |
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
|
* |
21 |
|
* $Revision$ $Date$ |
22 |
|
* |
23 |
|
* -------------------------------------------------------------------------- |
24 |
|
*/ |
25 |
|
#include <errno.h> |
26 |
|
|
27 |
|
#include "dgBDB.h" |
28 |
|
|
29 |
|
static DB_ENV *dbenv = NULL; |
30 |
|
|
31 |
|
dbError dgdberrno = DBE_NOERROR; |
32 |
|
char dgdberrstr[128] = {0}; |
33 |
|
|
34 |
|
static char dbroot[256] = {0}; |
35 |
|
|
36 |
|
#define debug 0 |
37 |
|
|
38 |
|
/* ------------------------------------------------------------------------- */ |
39 |
|
|
40 |
|
int |
41 |
|
dgdb_store_row( dbRow *row, char *dbh, int insert, char *reserved ) |
42 |
|
{ |
43 |
|
DB *dbp = NULL; |
44 |
|
DBT key, data; |
45 |
|
|
46 |
|
if( dbh == NULL ) { |
47 |
|
dgdberrno = DBE_NOCONNECT; |
48 |
|
return -1; |
49 |
|
} |
50 |
|
|
51 |
|
dgdberrno = DBE_NOERROR; |
52 |
|
|
53 |
|
dbp = (DB*)dbh; |
54 |
|
|
55 |
|
/* Wrap DBT structures around key/data */ |
56 |
|
memset(&key, 0, sizeof(key)); |
57 |
|
memset(&data, 0, sizeof(data)); |
58 |
|
key.data = row->key; |
59 |
|
data.data = row->data; |
60 |
|
key.size = row->keyLen; |
61 |
|
data.size = row->dataLen; |
62 |
|
|
63 |
|
switch (errno = dbp->put(dbp, NULL, &key, &data, insert ? DB_NOOVERWRITE:0)) { |
64 |
|
case 0: |
65 |
|
if(debug) printf("db: key stored.\n" ); |
66 |
|
break; |
67 |
|
default: |
68 |
|
sprintf(dgdberrstr, "%s: %s", insert?"INSERT":"UPDATE", strerror(errno)); |
69 |
|
dgdberrno = DBE_WRITEFAIL; |
70 |
|
return -1; |
71 |
|
} |
72 |
|
|
73 |
|
dbp->sync( dbp, 0 ); |
74 |
|
|
75 |
|
return 0; |
76 |
|
} |
77 |
|
|
78 |
|
/* ------------------------------------------------------------------------- */ |
79 |
|
/* Reads row identified by the key information in 'row' and populates the |
80 |
|
* data component of 'row' with the result. |
81 |
|
*/ |
82 |
|
int |
83 |
|
dgdb_read_row( dbRow *row, char *dbh, char *reserved ) |
84 |
|
{ |
85 |
|
DB *dbp = NULL; |
86 |
|
DBT key, data; |
87 |
|
|
88 |
|
dgdberrno = DBE_NOERROR; |
89 |
|
|
90 |
|
if( dbh == NULL ) { |
91 |
|
dgdberrno = DBE_NOCONNECT; |
92 |
|
return -1; |
93 |
|
} |
94 |
|
|
95 |
|
dbp = (DB*)dbh; |
96 |
|
|
97 |
|
/* Wrap DBT structures around key/data */ |
98 |
|
memset(&key, 0, sizeof(key)); |
99 |
|
memset(&data, 0, sizeof(data)); |
100 |
|
key.data = row->key; |
101 |
|
key.size = row->keyLen; |
102 |
|
data.data = row->data; |
103 |
|
data.size = row->dataLen; |
104 |
|
data.ulen = row->dataLen; |
105 |
|
data.flags = DB_DBT_USERMEM; |
106 |
|
|
107 |
|
switch (errno = dbp->get(dbp, NULL, &key, &data, 0)) { |
108 |
|
case 0: |
109 |
|
break; |
110 |
|
case DB_NOTFOUND: |
111 |
|
if(debug) printf("db: Row not found.\n" ); |
112 |
|
dgdberrno = DBE_NOTFOUND; |
113 |
|
return -1; |
114 |
|
break; |
115 |
|
default: |
116 |
|
sprintf(dgdberrstr, "Record Get: %s", strerror(errno)); |
117 |
|
dgdberrno = DBE_DBFAIL; |
118 |
|
return -1; |
119 |
|
} |
120 |
|
|
121 |
|
return 0; |
122 |
|
} |
123 |
|
|
124 |
|
/* ------------------------------------------------------------------------- */ |
125 |
|
/* Deletes row identified by the key information in 'row' |
126 |
|
*/ |
127 |
|
int |
128 |
|
dgdb_delete_row( dbRow *row, char *dbh, char *reserved ) |
129 |
|
{ |
130 |
|
DB *dbp = NULL; |
131 |
|
DBT key, data; |
132 |
|
|
133 |
|
dgdberrno = DBE_NOERROR; |
134 |
|
|
135 |
|
if( dbh == NULL ) { |
136 |
|
dgdberrno = DBE_NOCONNECT; |
137 |
|
return -1; |
138 |
|
} |
139 |
|
|
140 |
|
dbp = (DB*)dbh; |
141 |
|
|
142 |
|
/* Wrap DBT structures around key/data */ |
143 |
|
memset(&key, 0, sizeof(key)); |
144 |
|
memset(&data, 0, sizeof(data)); |
145 |
|
key.data = row->key; |
146 |
|
key.size = row->keyLen; |
147 |
|
|
148 |
|
switch (errno = dbp->del(dbp, NULL, &key, 0)) { |
149 |
|
case 0: |
150 |
|
if(debug) printf("db: key was deleted.\n" ); |
151 |
|
break; |
152 |
|
default: |
153 |
|
sprintf(dgdberrstr, "Record Del: %s", strerror(errno)); |
154 |
|
dgdberrno = DBE_DELFAIL; |
155 |
|
return -1; |
156 |
|
} |
157 |
|
|
158 |
|
return 0; |
159 |
|
} |
160 |
|
|
161 |
|
/* ------------------------------------------------------------------------- */ |
162 |
|
|
163 |
|
void |
164 |
|
dgdb_configure( char *dbhome ) |
165 |
|
{ |
166 |
|
/* Set dbhome to the passed in path |
167 |
|
*/ |
168 |
|
strncpy( dbroot, dbhome, sizeof(dbroot) ); |
169 |
|
} |
170 |
|
|
171 |
|
/* ------------------------------------------------------------------------- */ |
172 |
|
|
173 |
|
int |
174 |
|
dgdb_init_dbengine( ) |
175 |
|
{ |
176 |
|
static int engine_init = 0; |
177 |
|
int ret = 0; |
178 |
|
|
179 |
|
if( engine_init ) return 0; |
180 |
|
|
181 |
|
if((ret = db_env_create(&dbenv, 0)) != 0 ) { |
182 |
|
printf( "dbn_env_create: %s\n", strerror(errno)); |
183 |
|
return -1; |
184 |
|
} |
185 |
|
if((ret = dbenv->set_data_dir(dbenv, "datadir" )) != 0 ) { |
186 |
|
printf( "set_data_dir: %s\n", strerror(errno)); |
187 |
|
dbenv->close(dbenv,0); |
188 |
|
return -1; |
189 |
|
} |
190 |
|
if( (ret = dbenv->open(dbenv, dbroot, DB_INIT_LOCK|DB_CREATE|DB_INIT_MPOOL, 0)) != 0 ) { |
191 |
|
printf( "dbinit (open): %s\n", strerror(errno)); |
192 |
|
dbenv->close(dbenv,0); |
193 |
|
return -1; |
194 |
|
} |
195 |
|
|
196 |
|
engine_init = 1; |
197 |
|
return 0; |
198 |
|
} |
199 |
|
|
200 |
|
/* ------------------------------------------------------------------------- */ |
201 |
|
|
202 |
|
char * |
203 |
|
dgdb_open_db( char *tableName, int unique_key ) |
204 |
|
{ |
205 |
|
DB *dbp = NULL; |
206 |
|
char dbf[256]; |
207 |
|
int ret; |
208 |
|
|
209 |
|
if ((ret = db_create(&dbp, dbenv, 0)) != 0) { |
210 |
|
sprintf(dgdberrstr, "db_create: %s\n", db_strerror(ret)); |
211 |
|
return NULL; |
212 |
|
} |
213 |
|
|
214 |
|
if( unique_key ) dbp->set_flags( dbp, DB_DUP ); |
215 |
|
|
216 |
|
dgdberrno = DBE_NOERROR; |
217 |
|
|
218 |
|
/* Path to database files is set by DB_HOME (see appinit) |
219 |
|
*/ |
220 |
|
sprintf( dbf, "DG%s.dat", tableName ); |
221 |
|
|
222 |
|
if ((ret = dbp->open(dbp, |
223 |
|
#if (defined BDBVERSION && BDBVERSION >= 401 ) |
224 |
|
NULL, /* TXID */ |
225 |
|
#endif |
226 |
|
dbf, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) { |
227 |
|
sprintf(dgdberrstr, "Open DB: %s %s", dbf, strerror(errno)); |
228 |
|
dgdberrno = DBE_DBFAIL; |
229 |
|
} |
230 |
|
return (char*)dbp; |
231 |
|
} |
232 |
|
|
233 |
|
/* ------------------------------------------------------------------------- */ |
234 |
|
|
235 |
|
int |
236 |
|
dgdb_close_db( char *dbh ) |
237 |
|
{ |
238 |
|
DB *dbp = NULL; |
239 |
|
|
240 |
|
dgdberrno = DBE_NOERROR; |
241 |
|
|
242 |
|
if( dbh == NULL ) { |
243 |
|
dgdberrno = DBE_NOCONNECT; |
244 |
|
return -1; |
245 |
|
} |
246 |
|
|
247 |
|
dbp = (DB*)dbh; |
248 |
|
|
249 |
|
switch (errno = dbp->close(dbp, 0)) { |
250 |
|
case 0: |
251 |
|
if(debug) printf("db: database closed.\n"); |
252 |
|
break; |
253 |
|
default: |
254 |
|
sprintf(dgdberrstr, "Close DB: %s", strerror(errno)); |
255 |
|
dgdberrno = DBE_CLOSEFAIL; |
256 |
|
return -1; |
257 |
|
} |
258 |
|
return 0; |
259 |
|
} |
260 |
|
|
261 |
|
/* ------------------------------------------------------------------------- */ |
262 |
|
|
263 |
|
int |
264 |
|
dgdb_get_first( dbRow *row, char *dbh, dbRowItterator *ith, char *reserved ) |
265 |
|
{ |
266 |
|
DB *dbp = NULL; |
267 |
|
DBT key, data; |
268 |
|
|
269 |
|
dgdberrno = DBE_NOERROR; |
270 |
|
|
271 |
|
if( dbh == NULL ) { |
272 |
|
dgdberrno = DBE_NOCONNECT; |
273 |
|
return -1; |
274 |
|
} |
275 |
|
|
276 |
|
dbp = (DB*)dbh; |
277 |
|
|
278 |
|
/* If we don't have an itterator (Cursor handle) then get one |
279 |
|
*/ |
280 |
|
if( ith->dbc == NULL ) { |
281 |
|
switch (errno = dbp->cursor(dbp, NULL, (DBC**)&ith->dbc, 0 )) { |
282 |
|
case 0: |
283 |
|
if(debug) printf("db: Cursor opened.\n"); |
284 |
|
break; |
285 |
|
default: |
286 |
|
sprintf(dgdberrstr, "Cursor Open: %s", strerror(errno)); |
287 |
|
dgdberrno = DBE_CURSORFAIL; |
288 |
|
return -1; |
289 |
|
} |
290 |
|
} |
291 |
|
|
292 |
|
/* Wrap DBT structures around key/data */ |
293 |
|
memset(&key, 0, sizeof(key)); |
294 |
|
memset(&data, 0, sizeof(data)); |
295 |
|
key.data = row->key; |
296 |
|
key.size = row->keyLen; |
297 |
|
key.ulen = row->dataLen; |
298 |
|
key.flags = DB_DBT_USERMEM; |
299 |
|
|
300 |
|
data.data = row->data; |
301 |
|
data.size = row->dataLen; |
302 |
|
data.ulen = row->dataLen; |
303 |
|
data.flags = DB_DBT_USERMEM; |
304 |
|
|
305 |
|
switch (errno = (ith->dbc)->c_get(ith->dbc, &key, &data, DB_FIRST)) { |
306 |
|
case 0: |
307 |
|
if(debug) printf("db: Record Fetched.\n"); |
308 |
|
break; |
309 |
|
case DB_NOTFOUND: |
310 |
|
if(debug) printf( "dn: No more results.\n" ); |
311 |
|
dgdberrno = DBE_NOMOREDATA; |
312 |
|
return -1; |
313 |
|
default: |
314 |
|
sprintf(dgdberrstr, "Cursor Get First: %s", strerror(errno)); |
315 |
|
dgdberrno = DBE_CURSORFAIL; |
316 |
|
return -1; |
317 |
|
} |
318 |
|
return 1; |
319 |
|
} |
320 |
|
|
321 |
|
/* ------------------------------------------------------------------------- */ |
322 |
|
|
323 |
|
int |
324 |
|
dgdb_get_next( dbRow *row, char *dbh, dbRowItterator *ith, char *reserved ) |
325 |
|
{ |
326 |
|
DB *dbp = NULL; |
327 |
|
DBT key, data; |
328 |
|
|
329 |
|
dgdberrno = DBE_NOERROR; |
330 |
|
|
331 |
|
if( dbh == NULL ) { |
332 |
|
dgdberrno = DBE_NOCONNECT; |
333 |
|
return -1; |
334 |
|
} |
335 |
|
|
336 |
|
dbp = (DB*)dbh; |
337 |
|
|
338 |
|
/* If we don't have an itterator (Cursor handle) then get one |
339 |
|
*/ |
340 |
|
if( ith->dbc == NULL ) { |
341 |
|
switch (errno = dbp->cursor(dbp, NULL, (DBC**)&ith->dbc, 0)) { |
342 |
|
case 0: |
343 |
|
if(debug) printf("db: Cursor opened.\n"); |
344 |
|
break; |
345 |
|
default: |
346 |
|
sprintf(dgdberrstr, "Cursor Open: %s", strerror(errno)); |
347 |
|
dgdberrno = DBE_CURSORFAIL; |
348 |
|
return -1; |
349 |
|
} |
350 |
|
} |
351 |
|
|
352 |
|
/* Wrap DBT structures around key/data */ |
353 |
|
memset(&key, 0, sizeof(key)); |
354 |
|
memset(&data, 0, sizeof(data)); |
355 |
|
key.data = row->key; |
356 |
|
key.size = row->keyLen; |
357 |
|
key.ulen = row->dataLen; |
358 |
|
key.flags = DB_DBT_USERMEM; |
359 |
|
|
360 |
|
data.data = row->data; |
361 |
|
data.size = row->dataLen; |
362 |
|
data.ulen = row->dataLen; |
363 |
|
data.flags = DB_DBT_USERMEM; |
364 |
|
|
365 |
|
switch (errno = (ith->dbc)->c_get(ith->dbc, &key, &data, DB_NEXT)) { |
366 |
|
case 0: |
367 |
|
if(debug) printf("db: Record Fetched.\n"); |
368 |
|
break; |
369 |
|
case DB_NOTFOUND: |
370 |
|
if(debug) printf( "dn: No more results.\n" ); |
371 |
|
dgdberrno = DBE_NOMOREDATA; |
372 |
|
return -1; |
373 |
|
default: |
374 |
|
sprintf(dgdberrstr, "Cursor Next: %s", strerror(errno)); |
375 |
|
dgdberrno = DBE_CURSORFAIL; |
376 |
|
return -1; |
377 |
|
} |
378 |
|
return 1; |
379 |
|
} |
380 |
|
|
381 |
|
/* ------------------------------------------------------------------------- */ |
382 |
|
|
383 |
|
int |
384 |
|
dgdb_close_itterator( dbRowItterator *ith ) |
385 |
|
{ |
386 |
|
switch (errno = (ith->dbc)->c_close(ith->dbc)) { |
387 |
|
case 0: |
388 |
|
if(debug) printf("db: Cursor Closed.\n"); |
389 |
|
break; |
390 |
|
case EAGAIN: |
391 |
|
default: |
392 |
|
sprintf(dgdberrstr, "Cursor Close: %s", strerror(errno)); |
393 |
|
dgdberrno = DBE_CURSORFAIL; |
394 |
|
return -1; |
395 |
|
} |
396 |
|
|
397 |
|
ith->dbc = NULL; |
398 |
|
return 0; |
399 |
|
} |
400 |
|
|
401 |
|
/* ------------------------------------------------------------------------- */ |
402 |
|
/* end */ |