2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2001
5
* Sleepycat Software. All rights reserved.
11
static const char copyright[] =
12
"Copyright (c) 1996-2001\nSleepycat Software Inc. All rights reserved.\n";
13
static const char revid[] =
14
"$Id: db185.c,v 11.18 2001/04/10 20:43:53 bostic Exp $";
17
#ifndef NO_SYSTEM_INCLUDES
18
#include <sys/types.h>
26
#include "db185_int.h"
28
static int db185_close __P((DB185 *));
29
static int db185_compare __P((DB *, const DBT *, const DBT *));
30
static int db185_del __P((const DB185 *, const DBT185 *, u_int));
31
static int db185_fd __P((const DB185 *));
32
static int db185_get __P((const DB185 *, const DBT185 *, DBT185 *, u_int));
34
db185_hash __P((DB *, const void *, u_int32_t));
35
static void db185_openstderr __P((DB_FH *));
36
static size_t db185_prefix __P((DB *, const DBT *, const DBT *));
37
static int db185_put __P((const DB185 *, DBT185 *, const DBT185 *, u_int));
38
static int db185_seq __P((const DB185 *, DBT185 *, DBT185 *, u_int));
39
static int db185_sync __P((const DB185 *, u_int));
42
* EXTERN: #define dbopen __db185_open
43
* EXTERN: DB185 *__db185_open
44
* EXTERN: __P((const char *, int, int, DBTYPE, const void *));
47
__db185_open(file, oflags, mode, type, openinfo)
65
if ((ret = db_create(&dbp, NULL, 0)) != 0)
68
if ((ret = __os_calloc(NULL, 1, sizeof(DB185), &db185p)) != 0)
73
* The DBTYPE enum wasn't initialized in DB 185, so it's off-by-one
77
case 0: /* DB_BTREE */
79
if ((bi = openinfo) != NULL) {
80
if (bi->flags & ~R_DUP)
82
if (bi->flags & R_DUP)
83
(void)dbp->set_flags(dbp, DB_DUP);
84
if (bi->cachesize != 0)
85
(void)dbp->set_cachesize
86
(dbp, 0, bi->cachesize, 0);
87
if (bi->minkeypage != 0)
88
(void)dbp->set_bt_minkey(dbp, bi->minkeypage);
90
(void)dbp->set_pagesize(dbp, bi->psize);
93
* Comparisons and prefix calls work because the DBT
94
* structures in 1.85 and 2.0 have the same initial
97
if (bi->prefix != NULL) {
98
db185p->prefix = bi->prefix;
99
dbp->set_bt_prefix(dbp, db185_prefix);
101
if (bi->compare != NULL) {
102
db185p->compare = bi->compare;
103
dbp->set_bt_compare(dbp, db185_compare);
106
dbp->set_lorder(dbp, bi->lorder);
109
case 1: /* DB_HASH */
111
if ((hi = openinfo) != NULL) {
113
(void)dbp->set_pagesize(dbp, hi->bsize);
114
if (hi->ffactor != 0)
115
(void)dbp->set_h_ffactor(dbp, hi->ffactor);
117
(void)dbp->set_h_nelem(dbp, hi->nelem);
118
if (hi->cachesize != 0)
119
(void)dbp->set_cachesize
120
(dbp, 0, hi->cachesize, 0);
121
if (hi->hash != NULL) {
122
db185p->hash = hi->hash;
123
(void)dbp->set_h_hash(dbp, db185_hash);
126
dbp->set_lorder(dbp, hi->lorder);
130
case 2: /* DB_RECNO */
133
/* DB 1.85 did renumbering by default. */
134
(void)dbp->set_flags(dbp, DB_RENUMBER);
138
* The file name given to DB 1.85 recno is the name of the DB
139
* 2.0 backing file. If the file doesn't exist, create it if
140
* the user has the O_CREAT flag set, DB 1.85 did it for you,
141
* and DB 2.0 doesn't.
144
* Setting the file name to NULL specifies that we're creating
145
* a temporary backing file, in DB 2.X. If we're opening the
146
* DB file read-only, change the flags to read-write, because
147
* temporary backing files cannot be opened read-only, and DB
148
* 2.X will return an error. We are cheating here -- if the
149
* application does a put on the database, it will succeed --
150
* although that would be a stupid thing for the application
154
* Note, the file name in DB 1.85 was a const -- we don't do
155
* that in DB 2.0, so do that cast.
158
if (oflags & O_CREAT && __os_exists(file, NULL) != 0)
159
if (__os_openhandle(NULL, file,
160
oflags, mode, &fh) == 0)
161
(void)__os_closehandle(&fh);
162
(void)dbp->set_re_source(dbp, file);
170
if ((ri = openinfo) != NULL) {
173
* We can't support the bfname field.
175
#define BFMSG "DB: DB 1.85's recno bfname field is not supported.\n"
176
if (ri->bfname != NULL) {
177
db185_openstderr(&fh);
178
(void)__os_write(NULL, &fh,
179
BFMSG, sizeof(BFMSG) - 1, &nw);
183
if (ri->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
185
if (ri->flags & R_FIXEDLEN) {
187
(void)dbp->set_re_pad(dbp, ri->bval);
189
(void)dbp->set_re_len(dbp, ri->reclen);
192
(void)dbp->set_re_delim(dbp, ri->bval);
196
* We ignore the R_NOKEY flag, but that's okay, it was
197
* only an optimization that was never implemented.
199
if (ri->flags & R_SNAPSHOT)
200
(void)dbp->set_flags(dbp, DB_SNAPSHOT);
202
if (ri->cachesize != 0)
203
(void)dbp->set_cachesize
204
(dbp, 0, ri->cachesize, 0);
206
(void)dbp->set_pagesize(dbp, ri->psize);
208
dbp->set_lorder(dbp, ri->lorder);
215
db185p->close = db185_close;
216
db185p->del = db185_del;
217
db185p->fd = db185_fd;
218
db185p->get = db185_get;
219
db185p->put = db185_put;
220
db185p->seq = db185_seq;
221
db185p->sync = db185_sync;
224
* Store a reference so we can indirect from the DB 1.85 structure
225
* to the underlying DB structure, and vice-versa. This has to be
226
* done BEFORE the DB::open method call because the hash callback
227
* is exercised as part of hash database initialiation.
230
* Overload the cj_internal field for this purpose.
233
dbp->cj_internal = db185p;
235
/* Open the database. */
236
if ((ret = dbp->open(dbp,
237
file, NULL, type, __db_oflags(oflags), mode)) != 0)
240
/* Create the cursor used for sequential ops. */
241
if ((ret = dbp->cursor(dbp, NULL, &((DB185 *)db185p)->dbc, 0)) != 0)
246
einval: ret = EINVAL;
248
err: if (db185p != NULL)
249
__os_free(NULL, db185p, sizeof(DB185));
251
(void)dbp->close(dbp, 0);
266
ret = dbp->close(dbp, 0);
268
__os_free(NULL, db185p, sizeof(DB185));
278
db185_del(db185p, key185, flags)
280
const DBT185 *key185;
289
memset(&key, 0, sizeof(key));
290
key.data = key185->data;
291
key.size = key185->size;
293
if (flags & ~R_CURSOR)
295
if (flags & R_CURSOR)
296
ret = db185p->dbc->c_del(db185p->dbc, 0);
298
ret = dbp->del(dbp, NULL, &key, 0);
310
einval: __os_set_errno(EINVAL);
323
if ((ret = dbp->fd(dbp, &fd)) == 0)
331
db185_get(db185p, key185, data185, flags)
333
const DBT185 *key185;
343
memset(&key, 0, sizeof(key));
344
key.data = key185->data;
345
key.size = key185->size;
346
memset(&data, 0, sizeof(data));
347
data.data = data185->data;
348
data.size = data185->size;
353
switch (ret = dbp->get(dbp, NULL, &key, &data, 0)) {
355
data185->data = data.data;
356
data185->size = data.size;
365
einval: __os_set_errno(EINVAL);
370
db185_put(db185p, key185, data185, flags)
373
const DBT185 *data185;
383
memset(&key, 0, sizeof(key));
384
key.data = key185->data;
385
key.size = key185->size;
386
memset(&data, 0, sizeof(data));
387
data.data = data185->data;
388
data.size = data185->size;
392
ret = dbp->put(dbp, NULL, &key, &data, 0);
395
ret = db185p->dbc->c_put(db185p->dbc, &key, &data, DB_CURRENT);
399
if (dbp->type != DB_RECNO)
402
if ((ret = dbp->cursor(dbp, NULL, &dbcp_put, 0)) != 0) {
407
dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) != 0) {
408
(void)dbcp_put->c_close(dbcp_put);
412
memset(&data, 0, sizeof(data));
413
data.data = data185->data;
414
data.size = data185->size;
415
ret = dbcp_put->c_put(dbcp_put,
416
&key, &data, flags == R_IAFTER ? DB_AFTER : DB_BEFORE);
417
(void)dbcp_put->c_close(dbcp_put);
421
ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE);
424
if (dbp->type != DB_BTREE && dbp->type != DB_RECNO)
427
if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) != 0)
430
db185p->dbc->c_get(db185p->dbc, &key, &data, DB_SET_RANGE);
438
key185->data = key.data;
439
key185->size = key.size;
447
einval: __os_set_errno(EINVAL);
452
db185_seq(db185p, key185, data185, flags)
454
DBT185 *key185, *data185;
463
memset(&key, 0, sizeof(key));
464
key.data = key185->data;
465
key.size = key185->size;
466
memset(&data, 0, sizeof(data));
467
data.data = data185->data;
468
data.size = data185->size;
472
flags = DB_SET_RANGE;
478
if (dbp->type != DB_BTREE && dbp->type != DB_RECNO)
486
if (dbp->type != DB_BTREE && dbp->type != DB_RECNO)
493
switch (ret = db185p->dbc->c_get(db185p->dbc, &key, &data, flags)) {
495
key185->data = key.data;
496
key185->size = key.size;
497
data185->data = data.data;
498
data185->size = data.size;
507
einval: __os_set_errno(EINVAL);
512
db185_sync(db185p, flags)
529
* We can't support the R_RECNOSYNC flag.
531
#define RSMSG "DB: DB 1.85's R_RECNOSYNC sync flag is not supported.\n"
532
db185_openstderr(&fh);
533
(void)__os_write(NULL, &fh, RSMSG, sizeof(RSMSG) - 1, &nw);
539
if ((ret = dbp->sync(dbp, 0)) == 0)
545
einval: __os_set_errno(EINVAL);
550
db185_openstderr(fhp)
553
/* Dummy up the results of an __os_openhandle() on stderr. */
554
memset(fhp, 0, sizeof(*fhp));
555
F_SET(fhp, DB_FH_VALID);
557
#ifndef STDERR_FILENO
558
#define STDERR_FILENO 2
560
fhp->fd = STDERR_FILENO;
565
* Cutout routine to call the user's Btree comparison function.
568
db185_compare(dbp, a, b)
572
return (((DB185 *)dbp->cj_internal)->compare(a, b));
577
* Cutout routine to call the user's Btree prefix function.
580
db185_prefix(dbp, a, b)
584
return (((DB185 *)dbp->cj_internal)->prefix(a, b));
589
* Cutout routine to call the user's hash function.
592
db185_hash(dbp, key, len)
597
return (((DB185 *)dbp->cj_internal)->hash(key, (size_t)len));