2
* See the file LICENSE for redistribution information.
5
* Sleepycat Software. All rights reserved.
11
static const char sccsid[] = "@(#)db_join.c 10.10 (Sleepycat) 10/9/98";
14
#ifndef NO_SYSTEM_INCLUDES
15
#include <sys/types.h>
25
#include "common_ext.h"
27
static int __db_join_close __P((DBC *));
28
static int __db_join_del __P((DBC *, u_int32_t));
29
static int __db_join_get __P((DBC *, DBT *, DBT *, u_int32_t));
30
static int __db_join_put __P((DBC *, DBT *, DBT *, u_int32_t));
33
* This is the duplicate-assisted join functionality. Right now we're
34
* going to write it such that we return one item at a time, although
35
* I think we may need to optimize it to return them all at once.
36
* It should be easier to get it working this way, and I believe that
37
* changing it should be fairly straightforward.
40
* Right now we do not maintain the number of duplicates so we do
41
* not optimize the join. If the caller does, then best performance
42
* will be achieved by putting the cursor with the smallest cardinality
45
* The first cursor moves sequentially through the duplicate set while
46
* the others search explicitly for the duplicate in question.
52
* This is the interface to the duplicate-assisted join functionality.
53
* In the same way that cursors mark a position in a database, a cursor
54
* can mark a position in a join. While most cursors are created by the
55
* cursor method of a DB, join cursors are created through an explicit
58
* The curslist is an array of existing, intialized cursors and primary
59
* is the DB of the primary file. The data item that joins all the
60
* cursors in the curslist is used as the key into the primary and that
61
* key and data are returned. When no more items are left in the join
62
* set, the c_next operation off the join cursor will return DB_NOTFOUND.
64
* PUBLIC: int __db_join __P((DB *, DBC **, u_int32_t, DBC **));
67
__db_join(primary, curslist, flags, dbcp)
69
DBC **curslist, **dbcp;
76
DB_PANIC_CHECK(primary);
78
if ((ret = __db_joinchk(primary, flags)) != 0)
81
if (curslist == NULL || curslist[0] == NULL)
87
if ((ret = __os_calloc(1, sizeof(DBC), &dbc)) != 0)
90
if ((ret = __os_calloc(1, sizeof(JOIN_CURSOR), &jc)) != 0)
93
if ((ret = __os_malloc(256, NULL, &jc->j_key.data)) != 0)
96
F_SET(&jc->j_key, DB_DBT_USERMEM);
98
for (jc->j_curslist = curslist;
99
*jc->j_curslist != NULL; jc->j_curslist++)
101
if ((ret = __os_calloc((jc->j_curslist - curslist + 1),
102
sizeof(DBC *), &jc->j_curslist)) != 0)
104
for (i = 0; curslist[i] != NULL; i++) {
106
F_SET(curslist[i], DBC_KEYSET);
107
jc->j_curslist[i] = curslist[i];
110
dbc->c_close = __db_join_close;
111
dbc->c_del = __db_join_del;
112
dbc->c_get = __db_join_get;
113
dbc->c_put = __db_join_put;
117
jc->j_primary = primary;
123
err: if (jc != NULL) {
124
if (jc->j_curslist != NULL)
125
__os_free(jc->j_curslist,
126
(jc->j_curslist - curslist + 1) * sizeof(DBC *));
127
__os_free(jc, sizeof(JOIN_CURSOR));
130
__os_free(dbc, sizeof(DBC));
135
__db_join_put(dbc, key, data, flags)
141
DB_PANIC_CHECK(dbc->dbp);
143
COMPQUIET(key, NULL);
144
COMPQUIET(data, NULL);
150
__db_join_del(dbc, flags)
154
DB_PANIC_CHECK(dbc->dbp);
161
__db_join_get(dbc, key, data, flags)
176
operation = LF_ISSET(DB_OPFLAGS_MASK);
177
if (operation != 0 && operation != DB_JOIN_ITEM)
178
return (__db_ferr(dbp->dbenv, "DBcursor->c_get", 0));
180
LF_CLR(DB_OPFLAGS_MASK);
182
__db_fchk(dbp->dbenv, "DBcursor->c_get", flags, DB_RMW)) != 0)
185
jc = (JOIN_CURSOR *)dbc->internal;
187
ret = jc->j_curslist[0]->c_get(jc->j_curslist[0],
188
&jc->j_key, key, jc->j_init ? DB_CURRENT : DB_NEXT_DUP);
191
jc->j_key.ulen <<= 1;
192
if ((ret = __os_realloc(&jc->j_key.data, jc->j_key.ulen)) != 0)
202
* We have the first element; now look for it in the
205
for (cpp = jc->j_curslist + 1; *cpp != NULL; cpp++) {
206
retry2: if ((ret = ((*cpp)->c_get)(*cpp,
207
&jc->j_key, key, DB_GET_BOTH)) == DB_NOTFOUND)
210
jc->j_key.ulen <<= 1;
211
if ((ret = __os_realloc(&jc->j_key.data,
212
jc->j_key.ulen)) != 0)
216
if (F_ISSET(*cpp, DBC_KEYSET)) {
217
F_CLR(*cpp, DBC_KEYSET);
218
F_SET(*cpp, DBC_CONTINUE);
223
* If we got out of here with ret != 0, then we failed to
224
* find the duplicate in one of the files, so we go on to
225
* the next item in the outermost relation. If ret was
226
* equal to 0, then we've got something to return.
230
} while ((ret = jc->j_curslist[0]->c_get(jc->j_curslist[0],
231
&jc->j_key, key, DB_NEXT_DUP)) == 0);
234
* If ret != 0 here, we've exhausted the first file. Otherwise,
235
* key and data are set and we need to do the lookup on the
241
if (operation == DB_JOIN_ITEM)
244
return ((jc->j_primary->get)(jc->j_primary,
245
jc->j_curslist[0]->txn, key, data, 0));
255
DB_PANIC_CHECK(dbc->dbp);
257
jc = (JOIN_CURSOR *)dbc->internal;
260
* Clear the optimization flag in the cursors.
262
for (i = 0; jc->j_curslist[i] != NULL; i++)
263
F_CLR(jc->j_curslist[i], DBC_CONTINUE | DBC_KEYSET);
265
__os_free(jc->j_curslist, 0);
266
__os_free(jc->j_key.data, jc->j_key.ulen);
267
__os_free(jc, sizeof(JOIN_CURSOR));
268
__os_free(dbc, sizeof(DBC));