#include "db_config.h"
#include "db_int.h"
#include "dbinc/db_page.h"
#include "dbinc/btree.h"
#include "dbinc/hash.h"
#include "dbinc/lock.h"
#include "dbinc/log.h"
#include "dbinc/mp.h"
#include "dbinc/qam.h"
#include "dbinc/txn.h"
static int __db_append_primary __P((DBC *, DBT *, DBT *));
static int __db_secondary_get __P((DB *, DB_TXN *, DBT *, DBT *, u_int32_t));
static int __dbc_set_priority __P((DBC *, DB_CACHE_PRIORITY));
static int __dbc_get_priority __P((DBC *, DB_CACHE_PRIORITY* ));
int
__db_cursor_int(dbp, ip, txn, dbtype, root, flags, locker, dbcp)
DB *dbp;
DB_THREAD_INFO *ip;
DB_TXN *txn;
DBTYPE dbtype;
db_pgno_t root;
int flags;
DB_LOCKER *locker;
DBC **dbcp;
{
DBC *dbc;
DBC_INTERNAL *cp;
ENV *env;
db_threadid_t tid;
int allocated, ret;
pid_t pid;
env = dbp->env;
allocated = 0;
MUTEX_LOCK(env, dbp->mutex);
#ifndef HAVE_NO_DB_REFCOUNT
if (txn != NULL &&
!LF_ISSET(DBC_OPD|DBC_DUPLICATE) && !F_ISSET(dbp, DB_AM_RECOVER) &&
dbp->log_filename != NULL && !IS_REP_CLIENT(env) &&
(ret = __txn_record_fname(env, txn, dbp->log_filename)) != 0)
return (ret);
#endif
TAILQ_FOREACH(dbc, &dbp->free_queue, links)
if (dbtype == dbc->dbtype) {
TAILQ_REMOVE(&dbp->free_queue, dbc, links);
F_CLR(dbc, ~DBC_OWN_LID);
break;
}
MUTEX_UNLOCK(env, dbp->mutex);
if (dbc == NULL) {
if ((ret = __os_calloc(env, 1, sizeof(DBC), &dbc)) != 0)
return (ret);
allocated = 1;
dbc->flags = 0;
dbc->dbp = dbp;
dbc->dbenv = dbp->dbenv;
dbc->env = dbp->env;
if (LOCKING_ON(env)) {
if (!DB_IS_THREADED(dbp)) {
if (env->env_lref == NULL && (ret =
__lock_id(env, NULL, &env->env_lref)) != 0)
goto err;
dbc->lref = env->env_lref;
} else {
if ((ret =
__lock_id(env, NULL, &dbc->lref)) != 0)
goto err;
F_SET(dbc, DBC_OWN_LID);
}
if (CDB_LOCKING(env) &&
F_ISSET(dbp, DB_AM_SECONDARY))
memcpy(dbc->lock.fileid,
dbp->s_primary->fileid, DB_FILE_ID_LEN);
else
memcpy(dbc->lock.fileid,
dbp->fileid, DB_FILE_ID_LEN);
if (CDB_LOCKING(env)) {
if (F_ISSET(env->dbenv, DB_ENV_CDB_ALLDB)) {
DB_ASSERT(env, sizeof(db_pgno_t) ==
sizeof(u_int32_t));
dbc->lock_dbt.size = sizeof(u_int32_t);
dbc->lock_dbt.data = &dbc->lock.pgno;
dbc->lock.pgno = 0;
} else {
dbc->lock_dbt.size = DB_FILE_ID_LEN;
dbc->lock_dbt.data = dbc->lock.fileid;
}
} else {
dbc->lock.type = DB_PAGE_LOCK;
dbc->lock_dbt.size = sizeof(dbc->lock);
dbc->lock_dbt.data = &dbc->lock;
}
}
switch (dbtype) {
case DB_BTREE:
case DB_RECNO:
if ((ret = __bamc_init(dbc, dbtype)) != 0)
goto err;
break;
case DB_HASH:
if ((ret = __hamc_init(dbc)) != 0)
goto err;
break;
case DB_QUEUE:
if ((ret = __qamc_init(dbc)) != 0)
goto err;
break;
case DB_UNKNOWN:
default:
ret = __db_unknown_type(env, "DB->cursor", dbtype);
goto err;
}
cp = dbc->internal;
}
dbc->dbtype = dbtype;
RESET_RET_MEM(dbc);
dbc->set_priority = __dbc_set_priority;
dbc->get_priority = __dbc_get_priority;
dbc->priority = dbp->priority;
if ((dbc->txn = txn) != NULL)
dbc->locker = txn->locker;
else if (LOCKING_ON(env)) {
if (locker != NULL)
dbc->locker = locker;
else {
if (DB_IS_THREADED(dbp)) {
env->dbenv->thread_id(env->dbenv, &pid, &tid);
__lock_set_thread_id(dbc->lref, pid, tid);
}
dbc->locker = dbc->lref;
}
}
if (F_ISSET(dbp, DB_AM_SECONDARY))
dbc->get = dbc->c_get = __dbc_secondary_get_pp;
if (LF_ISSET(DBC_OPD))
F_SET(dbc, DBC_OPD);
if (F_ISSET(dbp, DB_AM_RECOVER))
F_SET(dbc, DBC_RECOVER);
if (F_ISSET(dbp, DB_AM_COMPENSATE))
F_SET(dbc, DBC_DONTLOCK);
cp = dbc->internal;
cp->opd = NULL;
cp->indx = 0;
cp->page = NULL;
cp->pgno = PGNO_INVALID;
cp->root = root;
switch (dbtype) {
case DB_BTREE:
case DB_RECNO:
if ((ret = __bamc_refresh(dbc)) != 0)
goto err;
break;
case DB_HASH:
case DB_QUEUE:
break;
case DB_UNKNOWN:
default:
ret = __db_unknown_type(env, "DB->cursor", dbp->type);
goto err;
}
if (txn != NULL)
++txn->cursors;
if (ip != NULL)
dbc->thread_info = ip;
else if (txn != NULL)
dbc->thread_info = txn->thread_info;
else
ENV_GET_THREAD_INFO(env, dbc->thread_info);
MUTEX_LOCK(env, dbp->mutex);
TAILQ_INSERT_TAIL(&dbp->active_queue, dbc, links);
F_SET(dbc, DBC_ACTIVE);
MUTEX_UNLOCK(env, dbp->mutex);
*dbcp = dbc;
return (0);
err: if (allocated)
__os_free(env, dbc);
return (ret);
}
int
__db_put(dbp, ip, txn, key, data, flags)
DB *dbp;
DB_THREAD_INFO *ip;
DB_TXN *txn;
DBT *key, *data;
u_int32_t flags;
{
DBC *dbc;
DBT tdata;
ENV *env;
int ret, t_ret;
env = dbp->env;
if ((ret = __db_cursor(dbp, ip, txn, &dbc, DB_WRITELOCK)) != 0)
return (ret);
DEBUG_LWRITE(dbc, txn, "DB->put", key, data, flags);
SET_RET_MEM(dbc, dbp);
F_SET(dbc, DBC_TRANSIENT);
switch (flags) {
case DB_APPEND:
tdata = *data;
switch (dbp->type) {
case DB_QUEUE:
if ((ret = __qam_append(dbc, key, &tdata)) != 0)
goto err;
break;
case DB_RECNO:
if ((ret = __ram_append(dbc, key, &tdata)) != 0)
goto err;
break;
case DB_BTREE:
case DB_HASH:
case DB_UNKNOWN:
default:
DB_ASSERT(env,
dbp->type == DB_QUEUE || dbp->type == DB_RECNO);
ret = __db_ferr(env, "DB->put", 0);
goto err;
}
DB_ASSERT(env, !F_ISSET(dbp, DB_AM_SECONDARY));
if (LIST_FIRST(&dbp->s_secondaries) != NULL &&
(ret = __dbt_usercopy(env, key)) == 0)
ret = __db_append_primary(dbc, key, &tdata);
FREE_IF_NEEDED(env, &tdata);
goto done;
default:
break;
}
if (ret == 0)
ret = __dbc_put(dbc,
key, data, flags == 0 ? DB_KEYLAST : flags);
err:
done:
if ((t_ret = __dbc_close(dbc)) != 0 && ret == 0)
ret = t_ret;
return (ret);
}
int
__db_del(dbp, ip, txn, key, flags)
DB *dbp;
DB_THREAD_INFO *ip;
DB_TXN *txn;
DBT *key;
u_int32_t flags;
{
DBC *dbc;
DBT data;
u_int32_t f_init, f_next;
int ret, t_ret;
if ((ret = __db_cursor(dbp, ip, txn, &dbc, DB_WRITELOCK)) != 0)
goto err;
DEBUG_LWRITE(dbc, txn, "DB->del", key, NULL, flags);
COMPQUIET(flags, 0);
memset(&data, 0, sizeof(data));
F_SET(&data, DB_DBT_USERMEM | DB_DBT_ISSET);
F_SET(key, DB_DBT_ISSET);
f_init = DB_SET;
f_next = DB_NEXT_DUP;
if (STD_LOCKING(dbc)) {
f_init |= DB_RMW;
f_next |= DB_RMW;
}
if (!F_ISSET(dbp, DB_AM_SECONDARY) &&
LIST_FIRST(&dbp->s_secondaries) == NULL &&
LIST_FIRST(&dbp->f_primaries) == NULL) {
#ifdef HAVE_QUEUE
if (dbp->type == DB_QUEUE) {
ret = __qam_delete(dbc, key);
F_CLR(key, DB_DBT_ISSET);
goto done;
}
#endif
if ((ret = __dbc_get(dbc, key, &data, f_init)) != 0)
goto err;
#ifdef HAVE_HASH
if (dbp->type == DB_HASH && dbc->internal->opd == NULL) {
ret = __ham_quick_delete(dbc);
goto done;
}
#endif
if ((dbp->type == DB_BTREE || dbp->type == DB_RECNO) &&
!F_ISSET(dbp, DB_AM_DUP)) {
ret = dbc->am_del(dbc);
goto done;
}
} else if ((ret = __dbc_get(dbc, key, &data, f_init)) != 0)
goto err;
for (;;) {
if ((ret = __dbc_del(dbc, 0)) != 0)
break;
F_SET(key, DB_DBT_ISSET);
F_SET(&data, DB_DBT_ISSET);
if ((ret = __dbc_get(dbc, key, &data, f_next)) != 0) {
if (ret == DB_NOTFOUND)
ret = 0;
break;
}
}
done:
err:
if ((t_ret = __dbc_close(dbc)) != 0 && ret == 0)
ret = t_ret;
return (ret);
}
int
__db_sync(dbp)
DB *dbp;
{
int ret, t_ret;
ret = 0;
if (F_ISSET(dbp, DB_AM_RDONLY))
return (0);
if (dbp->type == DB_RECNO)
ret = __ram_writeback(dbp);
if (F_ISSET(dbp, DB_AM_INMEM))
return (ret);
if (dbp->type == DB_QUEUE)
ret = __qam_sync(dbp);
else
if ((t_ret = __memp_fsync(dbp->mpf)) != 0 && ret == 0)
ret = t_ret;
return (ret);
}
int
__db_associate(dbp, ip, txn, sdbp, callback, flags)
DB *dbp, *sdbp;
DB_THREAD_INFO *ip;
DB_TXN *txn;
int (*callback) __P((DB *, const DBT *, const DBT *, DBT *));
u_int32_t flags;
{
DBC *pdbc, *sdbc;
DBT key, data, skey, *tskeyp;
ENV *env;
int build, ret, t_ret;
u_int32_t nskey;
env = dbp->env;
pdbc = sdbc = NULL;
ret = 0;
memset(&skey, 0, sizeof(DBT));
nskey = 0;
tskeyp = NULL;
build = 0;
if (LF_ISSET(DB_CREATE)) {
if ((ret = __db_cursor(sdbp, ip, txn, &sdbc, 0)) != 0)
goto err;
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
F_SET(&key, DB_DBT_PARTIAL | DB_DBT_USERMEM);
F_SET(&data, DB_DBT_PARTIAL | DB_DBT_USERMEM);
if ((ret = __dbc_get(sdbc, &key, &data,
(STD_LOCKING(sdbc) ? DB_RMW : 0) |
DB_FIRST)) == DB_NOTFOUND) {
build = 1;
ret = 0;
}
if ((t_ret = __dbc_close(sdbc)) != 0 && ret == 0)
ret = t_ret;
sdbc = NULL;
if (ret != 0)
goto err;
}
sdbp->s_callback = callback;
sdbp->s_primary = dbp;
sdbp->stored_get = sdbp->get;
sdbp->get = __db_secondary_get;
sdbp->stored_close = sdbp->close;
sdbp->close = __db_secondary_close_pp;
F_SET(sdbp, DB_AM_SECONDARY);
if (LF_ISSET(DB_IMMUTABLE_KEY))
FLD_SET(sdbp->s_assoc_flags, DB_ASSOC_IMMUTABLE_KEY);
MUTEX_LOCK(env, dbp->mutex);
DB_ASSERT(env, sdbp->s_refcnt == 0);
sdbp->s_refcnt = 1;
LIST_INSERT_HEAD(&dbp->s_secondaries, sdbp, s_links);
MUTEX_UNLOCK(env, dbp->mutex);
if (build) {
if ((ret = __db_cursor(sdbp, ip, txn, &sdbc,
CDB_LOCKING(sdbp->env) ? DB_WRITECURSOR : 0)) != 0)
goto err;
if ((ret = __db_cursor_int(dbp, ip,
txn, dbp->type, PGNO_INVALID, 0, sdbc->locker, &pdbc)) != 0)
goto err;
dbp->associate_locker = sdbc->locker;
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
while ((ret = __dbc_get(pdbc, &key, &data, DB_NEXT)) == 0) {
if ((ret = callback(sdbp, &key, &data, &skey)) != 0) {
if (ret == DB_DONOTINDEX)
continue;
goto err;
}
if (F_ISSET(&skey, DB_DBT_MULTIPLE)) {
#ifdef DIAGNOSTIC
__db_check_skeyset(sdbp, &skey);
#endif
nskey = skey.size;
tskeyp = (DBT *)skey.data;
} else {
nskey = 1;
tskeyp = &skey;
}
SWAP_IF_NEEDED(sdbp, &key);
for (; nskey > 0; nskey--, tskeyp++) {
if ((ret = __dbc_put(sdbc,
tskeyp, &key, DB_UPDATE_SECONDARY)) != 0)
goto err;
FREE_IF_NEEDED(env, tskeyp);
}
SWAP_IF_NEEDED(sdbp, &key);
FREE_IF_NEEDED(env, &skey);
}
if (ret == DB_NOTFOUND)
ret = 0;
}
err: if (sdbc != NULL && (t_ret = __dbc_close(sdbc)) != 0 && ret == 0)
ret = t_ret;
if (pdbc != NULL && (t_ret = __dbc_close(pdbc)) != 0 && ret == 0)
ret = t_ret;
dbp->associate_locker = NULL;
for (; nskey > 0; nskey--, tskeyp++)
FREE_IF_NEEDED(env, tskeyp);
FREE_IF_NEEDED(env, &skey);
return (ret);
}
static int
__db_secondary_get(sdbp, txn, skey, data, flags)
DB *sdbp;
DB_TXN *txn;
DBT *skey, *data;
u_int32_t flags;
{
DB_ASSERT(sdbp->env, F_ISSET(sdbp, DB_AM_SECONDARY));
return (__db_pget_pp(sdbp, txn, skey, NULL, data, flags));
}
int
__db_secondary_close(sdbp, flags)
DB *sdbp;
u_int32_t flags;
{
DB *primary;
ENV *env;
int doclose;
doclose = 0;
primary = sdbp->s_primary;
env = primary->env;
MUTEX_LOCK(env, primary->mutex);
DB_ASSERT(env, sdbp->s_refcnt != 0);
if (--sdbp->s_refcnt == 0) {
LIST_REMOVE(sdbp, s_links);
doclose = 1;
}
MUTEX_UNLOCK(env, primary->mutex);
return (doclose ? __db_close(sdbp, NULL, flags) : 0);
}
static int
__db_append_primary(dbc, key, data)
DBC *dbc;
DBT *key, *data;
{
DB *dbp, *sdbp;
DBC *fdbc, *sdbc, *pdbc;
DBT fdata, oldpkey, pkey, pdata, skey;
ENV *env;
int cmp, ret, t_ret;
dbp = dbc->dbp;
env = dbp->env;
sdbp = NULL;
ret = 0;
pdbc = NULL;
if (F_ISSET(data, DB_DBT_PARTIAL) || F_ISSET(key, DB_DBT_PARTIAL)) {
if ((ret = __dbc_idup(dbc, &pdbc, DB_POSITION)) != 0)
return (ret);
memset(&pkey, 0, sizeof(DBT));
memset(&pdata, 0, sizeof(DBT));
if ((ret = __dbc_get(pdbc, &pkey, &pdata, DB_CURRENT)) != 0)
goto err;
key = &pkey;
data = &pdata;
}
if ((ret = __db_s_first(dbp, &sdbp)) != 0)
goto err;
for (; sdbp != NULL && ret == 0; ret = __db_s_next(&sdbp, dbc->txn)) {
memset(&skey, 0, sizeof(DBT));
if ((ret = sdbp->s_callback(sdbp, key, data, &skey)) != 0) {
if (ret == DB_DONOTINDEX)
continue;
goto err;
}
memset(&fdata, 0, sizeof(DBT));
F_SET(&fdata, DB_DBT_PARTIAL | DB_DBT_USERMEM);
if (sdbp->s_foreign != NULL) {
if ((ret = __db_cursor_int(sdbp->s_foreign,
dbc->thread_info, dbc->txn, sdbp->s_foreign->type,
PGNO_INVALID, 0, dbc->locker, &fdbc)) != 0)
goto err;
if ((ret = __dbc_get(fdbc, &skey, &fdata,
DB_SET | (STD_LOCKING(dbc) ? DB_RMW : 0))) != 0) {
if (ret == DB_NOTFOUND || ret == DB_KEYEMPTY)
ret = DB_FOREIGN_CONFLICT;
goto err;
}
if ((ret = __dbc_close(fdbc)) != 0)
goto err;
}
if ((ret = __db_cursor_int(sdbp, dbc->thread_info, dbc->txn,
sdbp->type, PGNO_INVALID, 0, dbc->locker, &sdbc)) != 0) {
FREE_IF_NEEDED(env, &skey);
goto err;
}
if (CDB_LOCKING(env)) {
DB_ASSERT(env, sdbc->mylock.off == LOCK_INVALID);
F_SET(sdbc, DBC_WRITER);
}
if (!F_ISSET(sdbp, DB_AM_DUP)) {
memset(&oldpkey, 0, sizeof(DBT));
F_SET(&oldpkey, DB_DBT_MALLOC);
ret = __dbc_get(sdbc, &skey, &oldpkey,
DB_SET | (STD_LOCKING(dbc) ? DB_RMW : 0));
if (ret == 0) {
cmp = __bam_defcmp(sdbp, &oldpkey, key);
__os_ufree(env, oldpkey.data);
if (cmp != 0) {
__db_errx(env, "%s%s",
"Append results in a non-unique secondary key in",
" an index not configured to support duplicates");
ret = EINVAL;
goto err1;
}
} else if (ret != DB_NOTFOUND && ret != DB_KEYEMPTY)
goto err1;
}
ret = __dbc_put(sdbc, &skey, key, DB_UPDATE_SECONDARY);
err1: FREE_IF_NEEDED(env, &skey);
if ((t_ret = __dbc_close(sdbc)) != 0 && ret == 0)
ret = t_ret;
if (ret != 0)
goto err;
}
err: if (pdbc != NULL && (t_ret = __dbc_close(pdbc)) != 0 && ret == 0)
ret = t_ret;
if (sdbp != NULL &&
(t_ret = __db_s_done(sdbp, dbc->txn)) != 0 && ret == 0)
ret = t_ret;
return (ret);
}
int
__db_associate_foreign(fdbp, pdbp, callback, flags)
DB *fdbp, *pdbp;
int (*callback)(DB *, const DBT *, DBT *, const DBT *, int *);
u_int32_t flags;
{
DB_FOREIGN_INFO *f_info;
ENV *env;
int ret;
env = fdbp->env;
ret = 0;
if ((ret = __os_malloc(env, sizeof(DB_FOREIGN_INFO), &f_info)) != 0) {
return ret;
}
memset(f_info, 0, sizeof(DB_FOREIGN_INFO));
f_info->dbp = pdbp;
f_info->callback = callback;
FLD_SET(f_info->flags, flags);
MUTEX_LOCK(env, fdbp->mutex);
LIST_INSERT_HEAD(&fdbp->f_primaries, f_info, f_links);
MUTEX_UNLOCK(env, fdbp->mutex);
if (pdbp->s_foreign != NULL)
return (EINVAL);
pdbp->s_foreign = fdbp;
return (ret);
}
static int
__dbc_set_priority(dbc, priority)
DBC *dbc;
DB_CACHE_PRIORITY priority;
{
dbc->priority = priority;
return (0);
}
static int
__dbc_get_priority(dbc, priority)
DBC *dbc;
DB_CACHE_PRIORITY *priority;
{
*priority = dbc->priority;
return (0);
}