/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1996, 1997, 1998
* Sleepycat Software. All rights reserved.
*/
/*
* Copyright (c) 1990, 1993, 1994
* Margo Seltzer. All rights reserved.
*/
/*
* Copyright (c) 1990, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Margo Seltzer.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#ifndef lint
#endif /* not lint */
#ifndef NO_SYSTEM_INCLUDES
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#endif
#include "db_int.h"
#include "shqueue.h"
#include "db_page.h"
#include "db_am.h"
#include "db_ext.h"
#include "hash.h"
#include "btree.h"
#include "log.h"
#include "db_shash.h"
#include "lock.h"
#include "lock_ext.h"
/************************** INTERFACE ROUTINES ***************************/
/*
* __ham_open --
*
* PUBLIC: int __ham_open __P((DB *, DB_INFO *));
*/
int
{
/* Set the hash function if specified by the user. */
/*
* Initialize the remaining fields of the dbp. The only function
* that differs from the default set is __ham_stat().
*/
/* Get a cursor we can use for the rest of this function. */
goto out;
if (ret != 0)
goto out;
/*
* If this is a new file, initialize it, and put it back dirty.
*/
/* Initialize the hdr structure */
file_existed = 1;
/* File exists, verify the data in the header. */
goto out;
}
} else {
/*
* File does not exist, we must initialize the header. If
* locking is enabled that means getting a write lock first.
*/
file_existed = 0;
if (ret < 0)
goto out;
}
goto out;
}
/* Release the meta data page */
goto out;
/* Sync the file so that we know that the meta data goes to disk. */
goto out;
return (0);
return (ret);
}
/*
* PUBLIC: int __ham_close __P((DB *));
*/
int
{
return (0);
}
/************************** LOCAL CREATION ROUTINES **********************/
/*
* Returns 0 on No Error
*/
static void
{
} else
l2 = 2;
}
static int
{
if ((ret =
return (ret);
return (ret);
if (ret != 0)
goto out;
else
ret = DB_NOTFOUND;
return (ret);
}
/* ****************** CURSORS ********************************** */
/*
* __ham_c_init --
* Initialize the hash-specific portion of a cursor.
*
* PUBLIC: int __ham_c_init __P((DBC *));
*/
int
{
int ret;
return (ret);
if ((ret =
return (ret);
}
return (0);
}
/*
* __ham_c_close --
* Close down the cursor from a single use.
*/
static int
{
int ret;
return (ret);
return (0);
}
/*
* __ham_c_destroy --
* Cleanup the access method private part of a cursor.
*/
static int
{
return (0);
}
static int
{
return (ret);
return (DB_NOTFOUND);
/*
* If we are in the concurrent DB product and this cursor
* is not a write cursor, then this request is invalid.
* If it is a simple write cursor, then we need to upgrade its
* lock.
*/
/* Make sure it's a valid update cursor. */
return (EINVAL);
return (EAGAIN);
}
if (ret != 0)
return (ret);
goto out;
/*
* We are about to remove a duplicate from offpage.
*
* There are 4 cases.
* 1. We will remove an item on a page, but there are more
* items on that page.
* 2. We will remove the last item on a page, but there is a
* following page of duplicates.
* 3. We will remove the last item on a page, this page was the
* last page in a duplicate set, but there were dups before
* it.
* 4. We will remove the last item on a page, removing the last
* duplicate.
* In case 1 hcp->dpagep is unchanged.
* In case 2 hcp->dpagep comes back pointing to the next dup
* page.
* In case 3 hcp->dpagep comes back NULL.
* In case 4 hcp->dpagep comes back NULL.
*
* Case 4 results in deleting the pair off the master page.
* The normal code for doing this knows how to delete the
* duplicates, so we will handle this case in the normal code.
*/
if (ppgno == PGNO_INVALID &&
goto normal;
/* Remove item from duplicate page. */
goto out;
DB_LOCK_READ)) != 0)
goto out;
} else { /* Case 4 */
/*
* Delpair updated the cursor queue, so we
* don't have to do that here.
*/
}
if (ppgno == PGNO_INVALID)
/*
* We need to put the master page here, because
* although we have a duplicate page, the master
* page is dirty, and ham_item_done assumes that
* if you have a duplicate page, it's the only one
* that can be dirty.
*/
} else /* Case 1 */
if (chg_pgno != PGNO_INVALID)
else {
}
} else
/* Not a duplicate */
DB_LOCK_IWRITE, 0);
return (ret);
}
static int
{
if ((ret =
return (ret);
/* Clear OR'd in additional bits so we can check for flag equality. */
} else
if (ret != 0)
return (ret);
ret = 0;
get_key = 1;
switch (flags) {
case DB_PREV:
break;
}
/* FALLTHROUGH */
case DB_LAST:
break;
case DB_FIRST:
break;
case DB_NEXT_DUP:
else {
}
break;
case DB_NEXT:
break;
case DB_SET:
case DB_SET_RANGE:
case DB_GET_BOTH:
else
get_key = 0;
break;
case DB_CURRENT:
ret = DB_KEYEMPTY;
goto out;
}
break;
}
/*
* Must always enter this loop to do error handling and
*/
while (1) {
goto out1;
/* Get the key. */
goto out1;
break;
abort();
break;
}
/*
* Ran out of entries in a bucket; change buckets.
*/
switch (flags) {
case DB_LAST:
case DB_PREV:
ret = DB_NOTFOUND;
goto out1;
}
if (ret == 0)
break;
case DB_FIRST:
case DB_NEXT:
ret = DB_NOTFOUND;
goto out1;
}
if (ret == 0)
break;
case DB_GET_BOTH:
case DB_NEXT_DUP:
case DB_SET:
case DB_SET_RANGE:
/* Key not found. */
ret = DB_NOTFOUND;
goto out1;
}
}
return (ret);
}
static int
{
return (ret);
return (DB_NOTFOUND);
/*
* If we are in the concurrent DB product and this cursor
* is not a write cursor, then this request is invalid.
* If it is a simple write cursor, then we need to upgrade its
* lock.
*/
/* Make sure it's a valid update cursor. */
return (EINVAL);
return (EAGAIN);
}
if (ret != 0)
return (ret);
switch (flags) {
case DB_KEYLAST:
case DB_KEYFIRST:
ret = 0;
goto out;
}
/*
* A partial put, but the key does not exist
* and we are not beginning the write at 0.
* We must create a data item padded up to doff
* and then write the new bytes represented by
* val.
*/
}
} else
if (ret == 0)
goto done;
}
break;
case DB_BEFORE:
case DB_AFTER:
case DB_CURRENT:
break;
}
if (ret == 0) {
else
}
}
DB_LOCK_IWRITE, 0);
return (ret);
}
/********************************* UTILITIES ************************/
/*
* __ham_expand_table --
*/
static int
{
int ret;
ret = 0;
if (ret)
return (ret);
/*
* If the split point is about to increase, make sure that we
* have enough extra pages. The calculation here is weird.
* We'd like to do this after we've upped max_bucket, but it's
* too late then because we've logged the meta-data split. What
* we'll do between then and now is increment max bucket and then
* see what the log of one greater than that is; here we have to
* look at the log of max + 2. VERY NASTY STUFF.
*/
/*
* We are about to shift the split point. Make sure that
* if the next doubling is going to be big (more than 8
* pages), we have some extra pages around.
*/
}
/* Now we can log the meta-data split. */
if (DB_LOGGING(dbc)) {
return (ret);
}
/*
* If the split point is increasing, copy the current contents
* of the spare split bucket to the next bucket.
*/
}
/* Starting a new doubling */
}
"hash: Cannot allocate new bucket. Pages exhausted.");
return (ENOSPC);
}
/* Relocate records to the new bucket */
}
/*
* PUBLIC: u_int32_t __ham_call_hash __P((HASH_CURSOR *, u_int8_t *, int32_t));
*/
u_int8_t *k;
{
return (bucket);
}
/*
* Check for duplicates, and call __db_ret appropriately. Release
* everything held by the cursor.
*/
static int
{
/* Check for duplicate and return the first one. */
/*
* There are 4 cases:
* 1. We are not in duplicate, simply call db_ret.
* 2. We are looking at keys and stumbled onto a duplicate.
* 3. We are in the middle of a duplicate set. (ISDUP set)
* 4. This is a duplicate and we need to return a specific item.
*/
/*
* Here we check for the case where we just stumbled onto a
* duplicate. In this case, we do initialization and then
* let the normal duplicate code handle it.
*/
if (type == H_DUPLICATE) {
do {
sizeof(db_indx_t));
} else {
}
sizeof(db_pgno_t));
return (ret);
return (ret);
}
/*
* may need to adjust the cursor before returning data.
*/
if (flags == DB_GET_BOTH) {
!= 0)
return (ret);
if (cmp == 0)
} else {
}
} else {
return (ret);
} else {
/*
* We do not zero tmp_val since the comparison
* routines may only look at data and size.
*/
}
}
if (cmp != 0)
return (DB_NOTFOUND);
}
/*
* Now, everything is initialized, grab a duplicate if
* necessary.
*/
} else {
/*
* Copy the DBT in case we are retrieving into user
* memory and we need the parameters for it. If the
* user requested a partial, then we need to adjust
* the user's parameters to get the partial of the
* duplicate which is itself a partial.
*/
/*
* Take the user's length unless it would go
* beyond the end of the duplicate.
*/
/*
* Calculate the new offset.
*/
} else {
}
}
/*
* Finally, if we had a duplicate, pp, ndx, and myval should be
* set appropriately.
*/
return (ret);
/*
* In case we sent a temporary off to db_ret, set the real
* return values.
*/
return (0);
}
static int
{
else
} else /* Regular partial put */
}
/*
* the key resides. If the key is found, the cursor H_OK flag is set
* and the pagep, bndx, pgno (dpagep, dndx, dpgno) fields are set.
* If the key is not found, the H_OK flag is not set. If the sought
* field is non-0, the pagep, bndx, pgno (dpagep, dndx, dpgno) fields
* are set indicating where an add might take place. If it is 0,
* non of the cursor pointer field are valid.
*/
static int
{
/*
* Set up cursor so that we're looking for space to add an item
* as we cycle through the pages looking for the key.
*/
return (ret);
while (1) {
return (ret);
break;
switch (HPAGE_PTYPE(hk)) {
case H_OFFPAGE:
return (ret);
if (match == 0) {
return (0);
}
}
break;
case H_KEYDATA:
return (0);
}
break;
case H_DUPLICATE:
case H_OFFDUP:
/*
* These are errors because keys are never
* duplicated, only data items are.
*/
}
}
/*
* Item was not found, adjust cursor properly.
*/
if (sought != 0)
return (ret);
return (ret);
}
/*
* Initialize a dbt using some possibly already allocated storage
* for items.
* PUBLIC: int __ham_init_dbt __P((DBT *, u_int32_t, void **, u_int32_t *));
*/
int
void **bufp;
{
int ret;
*sizep = 0;
return (ret);
}
}
return (0);
}
/*
* Adjust the cursor after an insert or delete. The cursor passed is
* the one that was operated upon; we just need to check any of the
* others.
*
* add indicates if the item indicated by the cursor has just been
* added (add == 1) or deleted (add == 0).
* dup indicates if the addition occurred into a duplicate set.
*
* PUBLIC: void __ham_c_update
* PUBLIC: __P((HASH_CURSOR *, db_pgno_t, u_int32_t, int, int));
*/
void
{
int page_deleted;
/*
* Regular adds are always at the end of a given page, so we never
* have to adjust anyone's cursor after a regular add.
*/
return;
/*
* Determine if a page was deleted. If this is a regular update
* (i.e., not is_dup) then the deleted page's number will be that in
* chg_pgno, and the pgno in the cursor will be different. If this
* was an onpage-duplicate, then the same conditions apply. If this
* was an off-page duplicate, then we need to verify if hcp->dpgno
* is the same (no delete) or different (delete) than chg_pgno.
*/
else
continue;
continue;
if (is_dup) {
continue;
continue;
}
if (page_deleted) {
if (is_dup) {
} else {
}
continue;
}
/* Assign dpgno in case there was page conversion. */
/* Now adjust on-page information. */
if (add) {
} else {
}
}
}
}