1N/A/*-
1N/A * See the file LICENSE for redistribution information.
1N/A *
1N/A * Copyright (c) 1998
1N/A * Sleepycat Software. All rights reserved.
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)db_join.c 10.10 (Sleepycat) 10/9/98";
1N/A#endif /* not lint */
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A
1N/A#include <errno.h>
1N/A#include <string.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "db_page.h"
1N/A#include "db_join.h"
1N/A#include "db_am.h"
1N/A#include "common_ext.h"
1N/A
1N/Astatic int __db_join_close __P((DBC *));
1N/Astatic int __db_join_del __P((DBC *, u_int32_t));
1N/Astatic int __db_join_get __P((DBC *, DBT *, DBT *, u_int32_t));
1N/Astatic int __db_join_put __P((DBC *, DBT *, DBT *, u_int32_t));
1N/A
1N/A/*
1N/A * This is the duplicate-assisted join functionality. Right now we're
1N/A * going to write it such that we return one item at a time, although
1N/A * I think we may need to optimize it to return them all at once.
1N/A * It should be easier to get it working this way, and I believe that
1N/A * changing it should be fairly straightforward.
1N/A *
1N/A * XXX
1N/A * Right now we do not maintain the number of duplicates so we do
1N/A * not optimize the join. If the caller does, then best performance
1N/A * will be achieved by putting the cursor with the smallest cardinality
1N/A * first.
1N/A *
1N/A * The first cursor moves sequentially through the duplicate set while
1N/A * the others search explicitly for the duplicate in question.
1N/A *
1N/A */
1N/A
1N/A/*
1N/A * __db_join --
1N/A * This is the interface to the duplicate-assisted join functionality.
1N/A * In the same way that cursors mark a position in a database, a cursor
1N/A * can mark a position in a join. While most cursors are created by the
1N/A * cursor method of a DB, join cursors are created through an explicit
1N/A * call to DB->join.
1N/A *
1N/A * The curslist is an array of existing, intialized cursors and primary
1N/A * is the DB of the primary file. The data item that joins all the
1N/A * cursors in the curslist is used as the key into the primary and that
1N/A * key and data are returned. When no more items are left in the join
1N/A * set, the c_next operation off the join cursor will return DB_NOTFOUND.
1N/A *
1N/A * PUBLIC: int __db_join __P((DB *, DBC **, u_int32_t, DBC **));
1N/A */
1N/Aint
1N/A__db_join(primary, curslist, flags, dbcp)
1N/A DB *primary;
1N/A DBC **curslist, **dbcp;
1N/A u_int32_t flags;
1N/A{
1N/A DBC *dbc;
1N/A JOIN_CURSOR *jc;
1N/A int i, ret;
1N/A
1N/A DB_PANIC_CHECK(primary);
1N/A
1N/A if ((ret = __db_joinchk(primary, flags)) != 0)
1N/A return (ret);
1N/A
1N/A if (curslist == NULL || curslist[0] == NULL)
1N/A return (EINVAL);
1N/A
1N/A dbc = NULL;
1N/A jc = NULL;
1N/A
1N/A if ((ret = __os_calloc(1, sizeof(DBC), &dbc)) != 0)
1N/A goto err;
1N/A
1N/A if ((ret = __os_calloc(1, sizeof(JOIN_CURSOR), &jc)) != 0)
1N/A goto err;
1N/A
1N/A if ((ret = __os_malloc(256, NULL, &jc->j_key.data)) != 0)
1N/A goto err;
1N/A jc->j_key.ulen = 256;
1N/A F_SET(&jc->j_key, DB_DBT_USERMEM);
1N/A
1N/A for (jc->j_curslist = curslist;
1N/A *jc->j_curslist != NULL; jc->j_curslist++)
1N/A ;
1N/A if ((ret = __os_calloc((jc->j_curslist - curslist + 1),
1N/A sizeof(DBC *), &jc->j_curslist)) != 0)
1N/A goto err;
1N/A for (i = 0; curslist[i] != NULL; i++) {
1N/A if (i != 0)
1N/A F_SET(curslist[i], DBC_KEYSET);
1N/A jc->j_curslist[i] = curslist[i];
1N/A }
1N/A
1N/A dbc->c_close = __db_join_close;
1N/A dbc->c_del = __db_join_del;
1N/A dbc->c_get = __db_join_get;
1N/A dbc->c_put = __db_join_put;
1N/A dbc->internal = jc;
1N/A dbc->dbp = primary;
1N/A jc->j_init = 1;
1N/A jc->j_primary = primary;
1N/A
1N/A *dbcp = dbc;
1N/A
1N/A return (0);
1N/A
1N/Aerr: if (jc != NULL) {
1N/A if (jc->j_curslist != NULL)
1N/A __os_free(jc->j_curslist,
1N/A (jc->j_curslist - curslist + 1) * sizeof(DBC *));
1N/A __os_free(jc, sizeof(JOIN_CURSOR));
1N/A }
1N/A if (dbc != NULL)
1N/A __os_free(dbc, sizeof(DBC));
1N/A return (ret);
1N/A}
1N/A
1N/Astatic int
1N/A__db_join_put(dbc, key, data, flags)
1N/A DBC *dbc;
1N/A DBT *key;
1N/A DBT *data;
1N/A u_int32_t flags;
1N/A{
1N/A DB_PANIC_CHECK(dbc->dbp);
1N/A
1N/A COMPQUIET(key, NULL);
1N/A COMPQUIET(data, NULL);
1N/A COMPQUIET(flags, 0);
1N/A return (EINVAL);
1N/A}
1N/A
1N/Astatic int
1N/A__db_join_del(dbc, flags)
1N/A DBC *dbc;
1N/A u_int32_t flags;
1N/A{
1N/A DB_PANIC_CHECK(dbc->dbp);
1N/A
1N/A COMPQUIET(flags, 0);
1N/A return (EINVAL);
1N/A}
1N/A
1N/Astatic int
1N/A__db_join_get(dbc, key, data, flags)
1N/A DBC *dbc;
1N/A DBT *key, *data;
1N/A u_int32_t flags;
1N/A{
1N/A DB *dbp;
1N/A DBC **cpp;
1N/A JOIN_CURSOR *jc;
1N/A int ret;
1N/A u_int32_t operation;
1N/A
1N/A dbp = dbc->dbp;
1N/A
1N/A DB_PANIC_CHECK(dbp);
1N/A
1N/A operation = LF_ISSET(DB_OPFLAGS_MASK);
1N/A if (operation != 0 && operation != DB_JOIN_ITEM)
1N/A return (__db_ferr(dbp->dbenv, "DBcursor->c_get", 0));
1N/A
1N/A LF_CLR(DB_OPFLAGS_MASK);
1N/A if ((ret =
1N/A __db_fchk(dbp->dbenv, "DBcursor->c_get", flags, DB_RMW)) != 0)
1N/A return (ret);
1N/A
1N/A jc = (JOIN_CURSOR *)dbc->internal;
1N/Aretry:
1N/A ret = jc->j_curslist[0]->c_get(jc->j_curslist[0],
1N/A &jc->j_key, key, jc->j_init ? DB_CURRENT : DB_NEXT_DUP);
1N/A
1N/A if (ret == ENOMEM) {
1N/A jc->j_key.ulen <<= 1;
1N/A if ((ret = __os_realloc(&jc->j_key.data, jc->j_key.ulen)) != 0)
1N/A return (ret);
1N/A goto retry;
1N/A }
1N/A if (ret != 0)
1N/A return (ret);
1N/A
1N/A jc->j_init = 0;
1N/A do {
1N/A /*
1N/A * We have the first element; now look for it in the
1N/A * other cursors.
1N/A */
1N/A for (cpp = jc->j_curslist + 1; *cpp != NULL; cpp++) {
1N/Aretry2: if ((ret = ((*cpp)->c_get)(*cpp,
1N/A &jc->j_key, key, DB_GET_BOTH)) == DB_NOTFOUND)
1N/A break;
1N/A if (ret == ENOMEM) {
1N/A jc->j_key.ulen <<= 1;
1N/A if ((ret = __os_realloc(&jc->j_key.data,
1N/A jc->j_key.ulen)) != 0)
1N/A return (ret);
1N/A goto retry2;
1N/A }
1N/A if (F_ISSET(*cpp, DBC_KEYSET)) {
1N/A F_CLR(*cpp, DBC_KEYSET);
1N/A F_SET(*cpp, DBC_CONTINUE);
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * If we got out of here with ret != 0, then we failed to
1N/A * find the duplicate in one of the files, so we go on to
1N/A * the next item in the outermost relation. If ret was
1N/A * equal to 0, then we've got something to return.
1N/A */
1N/A if (ret == 0)
1N/A break;
1N/A } while ((ret = jc->j_curslist[0]->c_get(jc->j_curslist[0],
1N/A &jc->j_key, key, DB_NEXT_DUP)) == 0);
1N/A
1N/A /*
1N/A * If ret != 0 here, we've exhausted the first file. Otherwise,
1N/A * key and data are set and we need to do the lookup on the
1N/A * primary.
1N/A */
1N/A if (ret != 0)
1N/A return (ret);
1N/A
1N/A if (operation == DB_JOIN_ITEM)
1N/A return (0);
1N/A else
1N/A return ((jc->j_primary->get)(jc->j_primary,
1N/A jc->j_curslist[0]->txn, key, data, 0));
1N/A}
1N/A
1N/Astatic int
1N/A__db_join_close(dbc)
1N/A DBC *dbc;
1N/A{
1N/A JOIN_CURSOR *jc;
1N/A int i;
1N/A
1N/A DB_PANIC_CHECK(dbc->dbp);
1N/A
1N/A jc = (JOIN_CURSOR *)dbc->internal;
1N/A
1N/A /*
1N/A * Clear the optimization flag in the cursors.
1N/A */
1N/A for (i = 0; jc->j_curslist[i] != NULL; i++)
1N/A F_CLR(jc->j_curslist[i], DBC_CONTINUE | DBC_KEYSET);
1N/A
1N/A __os_free(jc->j_curslist, 0);
1N/A __os_free(jc->j_key.data, jc->j_key.ulen);
1N/A __os_free(jc, sizeof(JOIN_CURSOR));
1N/A __os_free(dbc, sizeof(DBC));
1N/A
1N/A return (0);
1N/A}