2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
11
static const char copyright[] =
12
"Copyright (c) 1996-2002\nSleepycat Software Inc. All rights reserved.\n";
13
static const char revid[] =
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: #ifdef _DB185_INT_H_
43
* EXTERN: DB185 *__db185_open
44
* EXTERN: __P((const char *, int, int, DBTYPE, const void *));
46
* EXTERN: DB *__db185_open
47
* EXTERN: __P((const char *, int, int, DBTYPE, const void *));
51
__db185_open(file, oflags, mode, type, openinfo)
69
if ((ret = db_create(&dbp, NULL, 0)) != 0)
72
if ((ret = __os_calloc(NULL, 1, sizeof(DB185), &db185p)) != 0)
77
* The DBTYPE enum wasn't initialized in DB 185, so it's off-by-one
81
case 0: /* DB_BTREE */
83
if ((bi = openinfo) != NULL) {
84
if (bi->flags & ~R_DUP)
86
if (bi->flags & R_DUP)
87
(void)dbp->set_flags(dbp, DB_DUP);
88
if (bi->cachesize != 0)
89
(void)dbp->set_cachesize
90
(dbp, 0, bi->cachesize, 0);
91
if (bi->minkeypage != 0)
92
(void)dbp->set_bt_minkey(dbp, bi->minkeypage);
94
(void)dbp->set_pagesize(dbp, bi->psize);
97
* Comparisons and prefix calls work because the DBT
98
* structures in 1.85 and 2.0 have the same initial
101
if (bi->prefix != NULL) {
102
db185p->prefix = bi->prefix;
103
dbp->set_bt_prefix(dbp, db185_prefix);
105
if (bi->compare != NULL) {
106
db185p->compare = bi->compare;
107
dbp->set_bt_compare(dbp, db185_compare);
110
dbp->set_lorder(dbp, bi->lorder);
113
case 1: /* DB_HASH */
115
if ((hi = openinfo) != NULL) {
117
(void)dbp->set_pagesize(dbp, hi->bsize);
118
if (hi->ffactor != 0)
119
(void)dbp->set_h_ffactor(dbp, hi->ffactor);
121
(void)dbp->set_h_nelem(dbp, hi->nelem);
122
if (hi->cachesize != 0)
123
(void)dbp->set_cachesize
124
(dbp, 0, hi->cachesize, 0);
125
if (hi->hash != NULL) {
126
db185p->hash = hi->hash;
127
(void)dbp->set_h_hash(dbp, db185_hash);
130
dbp->set_lorder(dbp, hi->lorder);
134
case 2: /* DB_RECNO */
137
/* DB 1.85 did renumbering by default. */
138
(void)dbp->set_flags(dbp, DB_RENUMBER);
142
* The file name given to DB 1.85 recno is the name of the DB
143
* 2.0 backing file. If the file doesn't exist, create it if
144
* the user has the O_CREAT flag set, DB 1.85 did it for you,
145
* and DB 2.0 doesn't.
148
* Setting the file name to NULL specifies that we're creating
149
* a temporary backing file, in DB 2.X. If we're opening the
150
* DB file read-only, change the flags to read-write, because
151
* temporary backing files cannot be opened read-only, and DB
152
* 2.X will return an error. We are cheating here -- if the
153
* application does a put on the database, it will succeed --
154
* although that would be a stupid thing for the application
158
* Note, the file name in DB 1.85 was a const -- we don't do
159
* that in DB 2.0, so do that cast.
162
if (oflags & O_CREAT && __os_exists(file, NULL) != 0)
163
if (__os_openhandle(NULL, file,
164
oflags, mode, &fh) == 0)
165
(void)__os_closehandle(NULL, &fh);
166
(void)dbp->set_re_source(dbp, file);
174
if ((ri = openinfo) != NULL) {
177
* We can't support the bfname field.
179
#define BFMSG "DB: DB 1.85's recno bfname field is not supported.\n"
180
if (ri->bfname != NULL) {
181
db185_openstderr(&fh);
182
(void)__os_write(NULL, &fh,
183
BFMSG, sizeof(BFMSG) - 1, &nw);
187
if (ri->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
189
if (ri->flags & R_FIXEDLEN) {
191
(void)dbp->set_re_pad(dbp, ri->bval);
193
(void)dbp->set_re_len(dbp, ri->reclen);
196
(void)dbp->set_re_delim(dbp, ri->bval);
200
* We ignore the R_NOKEY flag, but that's okay, it was
201
* only an optimization that was never implemented.
203
if (ri->flags & R_SNAPSHOT)
204
(void)dbp->set_flags(dbp, DB_SNAPSHOT);
206
if (ri->cachesize != 0)
207
(void)dbp->set_cachesize
208
(dbp, 0, ri->cachesize, 0);
210
(void)dbp->set_pagesize(dbp, ri->psize);
212
dbp->set_lorder(dbp, ri->lorder);
219
db185p->close = db185_close;
220
db185p->del = db185_del;
221
db185p->fd = db185_fd;
222
db185p->get = db185_get;
223
db185p->put = db185_put;
224
db185p->seq = db185_seq;
225
db185p->sync = db185_sync;
228
* Store a reference so we can indirect from the DB 1.85 structure
229
* to the underlying DB structure, and vice-versa. This has to be
230
* done BEFORE the DB::open method call because the hash callback
231
* is exercised as part of hash database initialiation.
234
dbp->api_internal = db185p;
236
/* Open the database. */
237
if ((ret = dbp->open(dbp, NULL,
238
file, NULL, type, __db_oflags(oflags), mode)) != 0)
241
/* Create the cursor used for sequential ops. */
242
if ((ret = dbp->cursor(dbp, NULL, &((DB185 *)db185p)->dbc, 0)) != 0)
247
err: if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
248
einval: ret = EINVAL;
250
__os_free(NULL, db185p);
252
(void)dbp->close(dbp, 0);
267
ret = dbp->close(dbp, 0);
269
__os_free(NULL, db185p);
274
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
281
db185_del(db185p, key185, flags)
283
const DBT185 *key185;
292
memset(&key, 0, sizeof(key));
293
key.data = key185->data;
294
key.size = key185->size;
296
if (flags & ~R_CURSOR)
298
if (flags & R_CURSOR)
299
ret = db185p->dbc->c_del(db185p->dbc, 0);
301
ret = dbp->del(dbp, NULL, &key, 0);
310
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
311
einval: ret = EINVAL;
325
if ((ret = dbp->fd(dbp, &fd)) == 0)
328
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
335
db185_get(db185p, key185, data185, flags)
337
const DBT185 *key185;
347
memset(&key, 0, sizeof(key));
348
key.data = key185->data;
349
key.size = key185->size;
350
memset(&data, 0, sizeof(data));
351
data.data = data185->data;
352
data.size = data185->size;
357
switch (ret = dbp->get(dbp, NULL, &key, &data, 0)) {
359
data185->data = data.data;
360
data185->size = data.size;
366
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
367
einval: ret = EINVAL;
373
db185_put(db185p, key185, data185, flags)
376
const DBT185 *data185;
386
memset(&key, 0, sizeof(key));
387
key.data = key185->data;
388
key.size = key185->size;
389
memset(&data, 0, sizeof(data));
390
data.data = data185->data;
391
data.size = data185->size;
395
ret = dbp->put(dbp, NULL, &key, &data, 0);
398
ret = db185p->dbc->c_put(db185p->dbc, &key, &data, DB_CURRENT);
402
if (dbp->type != DB_RECNO)
405
if ((ret = dbp->cursor(dbp, NULL, &dbcp_put, 0)) != 0)
408
dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) == 0) {
409
memset(&data, 0, sizeof(data));
410
data.data = data185->data;
411
data.size = data185->size;
412
ret = dbcp_put->c_put(dbcp_put, &key, &data,
413
flags == R_IAFTER ? DB_AFTER : DB_BEFORE);
415
if ((t_ret = dbcp_put->c_close(dbcp_put)) != 0 && ret == 0)
419
ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE);
422
if (dbp->type != DB_BTREE && dbp->type != DB_RECNO)
425
if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) != 0)
428
db185p->dbc->c_get(db185p->dbc, &key, &data, DB_SET_RANGE);
436
key185->data = key.data;
437
key185->size = key.size;
443
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
444
einval: ret = EINVAL;
450
db185_seq(db185p, key185, data185, flags)
452
DBT185 *key185, *data185;
461
memset(&key, 0, sizeof(key));
462
key.data = key185->data;
463
key.size = key185->size;
464
memset(&data, 0, sizeof(data));
465
data.data = data185->data;
466
data.size = data185->size;
470
flags = DB_SET_RANGE;
476
if (dbp->type != DB_BTREE && dbp->type != DB_RECNO)
484
if (dbp->type != DB_BTREE && dbp->type != DB_RECNO)
491
switch (ret = db185p->dbc->c_get(db185p->dbc, &key, &data, flags)) {
493
key185->data = key.data;
494
key185->size = key.size;
495
data185->data = data.data;
496
data185->size = data.size;
502
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
503
einval: ret = EINVAL;
509
db185_sync(db185p, flags)
526
* We can't support the R_RECNOSYNC flag.
528
#define RSMSG "DB: DB 1.85's R_RECNOSYNC sync flag is not supported.\n"
529
db185_openstderr(&fh);
530
(void)__os_write(NULL, &fh, RSMSG, sizeof(RSMSG) - 1, &nw);
536
if ((ret = dbp->sync(dbp, 0)) == 0)
539
if (ret < 0) /* DB 1.85 can't handle DB 2.0's errors. */
540
einval: ret = EINVAL;
546
db185_openstderr(fhp)
549
/* Dummy up the results of an __os_openhandle() on stderr. */
550
memset(fhp, 0, sizeof(*fhp));
551
F_SET(fhp, DB_FH_VALID);
553
#ifndef STDERR_FILENO
554
#define STDERR_FILENO 2
556
fhp->fd = STDERR_FILENO;
561
* Cutout routine to call the user's Btree comparison function.
564
db185_compare(dbp, a, b)
568
return (((DB185 *)dbp->api_internal)->compare(a, b));
573
* Cutout routine to call the user's Btree prefix function.
576
db185_prefix(dbp, a, b)
580
return (((DB185 *)dbp->api_internal)->prefix(a, b));
585
* Cutout routine to call the user's hash function.
588
db185_hash(dbp, key, len)
593
return (((DB185 *)dbp->api_internal)->hash(key, (size_t)len));