about summary refs log tree commit diff
path: root/db2/btree/bt_put.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>1998-06-09 15:16:55 +0000
committerUlrich Drepper <drepper@redhat.com>1998-06-09 15:16:55 +0000
commitbf7997b65c7887d2acda95f5201d818a19d81711 (patch)
treeda3583de3a0b5892f90a4b1eb773a87b554ae37e /db2/btree/bt_put.c
parent7646e67e6cc4c738a7b402c60fed39d52db0433b (diff)
downloadglibc-bf7997b65c7887d2acda95f5201d818a19d81711.tar.gz
glibc-bf7997b65c7887d2acda95f5201d818a19d81711.tar.xz
glibc-bf7997b65c7887d2acda95f5201d818a19d81711.zip
Update.
1998-06-09  Ulrich Drepper  <drepper@cygnus.com>

	* sysdeps/unix/sysv/linux/netinet/ip.h (struct ip_options): Define
	__data member only for gcc.  Reported by ak@muc.de.

	* misc/mntent.h: Undo last patch.
	* sysdeps/unix/sysv/linux/fstatvfs.c (fstatvfs): Undo last patch.
	* misc/tst/mntent.c: Adjust code for this change.

	* io/fts.c: Updated from a slightly more recent BSD version.
	* io/fts.h: Likewise.

	* libc.map: Add __libc_stack_end.

	* db2/Makefile (routines): Add lock_region.
	* db2/config.h: Update from db-2.4.14.
	* db2/db.h: Likewise.
	* db2/db_185.h: Likewise.
	* db2/db_int.h: Likewise.
	* db2/bt_close.c: Likewise.
	* db2/bt_compare.c: Likewise.
	* db2/bt_conv.c: Likewise.
	* db2/bt_cursor.c: Likewise.
	* db2/bt_delete.c: Likewise.
	* db2/bt_open.c: Likewise.
	* db2/bt_page.c: Likewise.
	* db2/bt_put.c: Likewise.
	* db2/bt_rec.c: Likewise.
	* db2/bt_recno.c: Likewise.
	* db2/bt_rsearch.c: Likewise.
	* db2/bt_search.c: Likewise.
	* db2/bt_split.c: Likewise.
	* db2/bt_stat.c: Likewise.
	* db2/btree.src: Likewise.
	* db2/btree_auto.c: Likewise.
	* db2/getlong.c: Likewise.
	* db2/db_appinit.c: Likewise.
	* db2/db_apprec.c: Likewise.
	* db2/db_byteorder.c: Likewise.
	* db2/db_err.c: Likewise.
	* db2/db_log2.c: Likewise.
	* db2/db_region.c: Likewise.
	* db2/db_salloc.c: Likewise.
	* db2/db_shash.c: Likewise.
	* db2/db.c: Likewise.
	* db2/db.src: Likewise.
	* db2/db_auto.c: Likewise.
	* db2/db_conv.c: Likewise.
	* db2/db_dispatch.c: Likewise.
	* db2/db_dup.c: Likewise.
	* db2/db_overflow.c: Likewise.
	* db2/db_pr.c: Likewise.
	* db2/db_rec.c: Likewise.
	* db2/db_ret.c: Likewise.
	* db2/db_thread.c: Likewise.
	* db2/db185.c: Likewise.
	* db2/db185_int.h: Likewise.
	* db2/dbm.c: Likewise.
	* db2/hash.c: Likewise.
	* db2/hash.src: Likewise.
	* db2/hash_auto.c: Likewise.
	* db2/hash_conv.c: Likewise.
	* db2/hash_debug.c: Likewise.
	* db2/hash_dup.c: Likewise.
	* db2/hash_func.c: Likewise.
	* db2/hash_page.c: Likewise.
	* db2/hash_rec.c: Likewise.
	* db2/hash_stat.c: Likewise.
	* db2/btree.h: Likewise.
	* db2/btree_ext.h: Likewise.
	* db2/clib_ext.h: Likewise.
	* db2/common_ext.h: Likewise.
	* db2/cxx_int.h: Likewise.
	* db2/db.h.src: Likewise.
	* db2/db_185.h.src: Likewise.
	* db2/db_am.h: Likewise.
	* db2/db_auto.h: Likewise.
	* db2/db_cxx.h: Likewise.
	* db2/db_dispatch.h: Likewise.
	* db2/db_ext.h: Likewise.
	* db2/db_int.h.src: Likewise.
	* db2/db_page.h: Likewise.
	* db2/db_shash.h: Likewise.
	* db2/db_swap.h: Likewise.
	* db2/hash.h: Likewise.
	* db2/hash_ext.h: Likewise.
	* db2/lock.h: Likewise.
	* db2/lock_ext.h: Likewise.
	* db2/log.h: Likewise.
	* db2/log_ext.h: Likewise.
	* db2/mp.h: Likewise.
	* db2/mp_ext.h: Likewise.
	* db2/mutex_ext.h: Likewise.
	* db2/os_ext.h: Likewise.
	* db2/os_func.h: Likewise.
	* db2/queue.h: Likewise.
	* db2/shqueue.h: Likewise.
	* db2/txn.h: Likewise.
	* db2/lock.c: Likewise.
	* db2/lock_conflict.c: Likewise.
	* db2/lock_deadlock.c: Likewise.
	* db2/lock_region.c: Likewise.
	* db2/lock_util.c: Likewise.
	* db2/log.c: Likewise.
	* db2/log.src: Likewise.
	* db2/log_archive.c: Likewise.
	* db2/log_auto.c: Likewise.
	* db2/log_compare.c: Likewise.
	* db2/log_findckp.c: Likewise.
	* db2/log_get.c: Likewise.
	* db2/log_put.c: Likewise.
	* db2/log_rec.c: Likewise.
	* db2/log_register.c: Likewise.
	* db2/mp_bh.c: Likewise.
	* db2/mp_fget.c: Likewise.
	* db2/mp_fopen.c: Likewise.
	* db2/mp_fput.c: Likewise.
	* db2/mp_fset.c: Likewise.
	* db2/mp_open.c: Likewise.
	* db2/mp_pr.c: Likewise.
	* db2/mp_region.c: Likewise.
	* db2/mp_sync.c: Likewise.
	* db2/68020.gcc: Likewise.
	* db2/mutex.c: Likewise.
	* db2/parisc.gcc: Likewise.
	* db2/parisc.hp: Likewise.
	* db2/sco.cc: Likewise.
	* db2/os_abs.c: Likewise.
	* db2/os_alloc.c: Likewise.
	* db2/os_config.c: Likewise.
	* db2/os_dir.c: Likewise.
	* db2/os_fid.c: Likewise.
	* db2/os_fsync.c: Likewise.
	* db2/os_map.c: Likewise.
	* db2/os_oflags.c: Likewise.
	* db2/os_open.c: Likewise.
	* db2/os_rpath.c: Likewise.
	* db2/os_rw.c: Likewise.
	* db2/os_seek.c: Likewise.
	* db2/os_sleep.c: Likewise.
	* db2/os_spin.c: Likewise.
	* db2/os_stat.c: Likewise.
	* db2/os_unlink.c: Likewise.
	* db2/db_archive.c: Likewise.
	* db2/db_checkpoint.c: Likewise.
	* db2/db_deadlock.c: Likewise.
	* db2/db_dump.c: Likewise.
	* db2/db_dump185.c: Likewise.
	* db2/db_load.c: Likewise.
	* db2/db_printlog.c: Likewise.
	* db2/db_recover.c: Likewise.
	* db2/db_stat.c: Likewise.
	* db2/txn.c: Likewise.
	* db2/txn.src: Likewise.
	* db2/txn_auto.c: Likewise.
	* db2/txn_rec.c: Likewise.

	* elf/rtld.c: Move definition of __libc_stack_end to ...
	* sysdeps/generic/dl-sysdep.h: ...here.

	* sysdeps/unix/sysv/linux/fstatvfs.c: Handle nodiratime option.
	* sysdeps/unix/sysv/linux/bits/statvfs.h: Define ST_NODIRATIME.
	* sysdeps/unix/sysv/linux/sys/mount.h: Define MS_NODIRATIME.

1998-06-08 21:44  Ulrich Drepper  <drepper@cygnus.com>

	* sysdeps/unix/sysv/linux/fstatvfs.c: Handle constant option string
	from mntent correctly.

1998-06-06  Andreas Jaeger  <aj@arthur.rhein-neckar.de>

	* sunrpc/Makefile (generated): Correct typo.

1998-06-04  Philip Blundell  <philb@gnu.org>

	* elf/elf.h (EM_ARM, et al.): New definitions.
	* sysdeps/arm/dl-machine.h: Update for new draft ARM ELF ABI.
Diffstat (limited to 'db2/btree/bt_put.c')
-rw-r--r--db2/btree/bt_put.c176
1 files changed, 102 insertions, 74 deletions
diff --git a/db2/btree/bt_put.c b/db2/btree/bt_put.c
index 87f3fd9aff..a93faac98c 100644
--- a/db2/btree/bt_put.c
+++ b/db2/btree/bt_put.c
@@ -1,7 +1,7 @@
 /*-
  * See the file LICENSE for redistribution information.
  *
- * Copyright (c) 1996, 1997
+ * Copyright (c) 1996, 1997, 1998
  *	Sleepycat Software.  All rights reserved.
  */
 /*
@@ -47,15 +47,13 @@
 #include "config.h"
 
 #ifndef lint
-static const char sccsid[] = "@(#)bt_put.c	10.38 (Sleepycat) 1/8/98";
+static const char sccsid[] = "@(#)bt_put.c	10.45 (Sleepycat) 5/25/98";
 #endif /* not lint */
 
 #ifndef NO_SYSTEM_INCLUDES
 #include <sys/types.h>
 
 #include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 #endif
 
@@ -75,21 +73,22 @@ static u_int32_t __bam_partsize __P((DBT *, PAGE *, u_int32_t));
  * __bam_put --
  *	Add a new key/data pair or replace an existing pair (btree).
  *
- * PUBLIC: int __bam_put __P((DB *, DB_TXN *, DBT *, DBT *, int));
+ * PUBLIC: int __bam_put __P((DB *, DB_TXN *, DBT *, DBT *, u_int32_t));
  */
 int
 __bam_put(argdbp, txn, key, data, flags)
 	DB *argdbp;
 	DB_TXN *txn;
 	DBT *key, *data;
-	int flags;
+	u_int32_t flags;
 {
 	BTREE *t;
 	CURSOR c;
 	DB *dbp;
 	PAGE *h;
 	db_indx_t indx;
-	int exact, iflags, isdeleted, newkey, replace, ret, stack;
+	u_int32_t iitem_flags, insert_flags;
+	int exact, isdeleted, newkey, ret, stack;
 
 	DEBUG_LWRITE(argdbp, txn, "bam_put", key, data, flags);
 
@@ -121,14 +120,13 @@ retry:	/*
 	 * been marked for deletion, we do a replace, otherwise, it has to be
 	 * a set of duplicates, and we simply append a new one to the set.
 	 */
-	isdeleted = replace = 0;
+	isdeleted = 0;
 	if (exact) {
 		if ((ret = __bam_isdeleted(dbp, h, indx, &isdeleted)) != 0)
 			goto err;
-		if (isdeleted) {
-			replace = 1;
+		if (isdeleted)
 			__bam_ca_replace(dbp, h->pgno, indx, REPLACE_SETUP);
-		} else
+		else
 			if (flags == DB_NOOVERWRITE) {
 				ret = DB_KEYEXIST;
 				goto err;
@@ -179,42 +177,38 @@ retry:	/*
 				t->bt_csp->page = h = c.page;
 				indx = c.dindx;
 			}
-			iflags = DB_AFTER;
+			insert_flags = DB_AFTER;
 		} else
-			iflags = DB_CURRENT;
+			insert_flags = DB_CURRENT;
 	} else
-		iflags = DB_BEFORE;
+		insert_flags = DB_BEFORE;
 
 	/*
 	 * The pages we're using may be modified by __bam_iitem(), so make
 	 * sure we reset the stack.
 	 */
-	ret = __bam_iitem(dbp,
-	    &h, &indx, key, data, iflags, newkey ? BI_NEWKEY : 0);
+	iitem_flags = 0;
+	if (newkey)
+		iitem_flags |= BI_NEWKEY;
+	if (isdeleted)
+		iitem_flags |= BI_DOINCR;
+	ret = __bam_iitem(dbp, &h, &indx, key, data, insert_flags, iitem_flags);
 	t->bt_csp->page = h;
 	t->bt_csp->indx = indx;
 
 	switch (ret) {
 	case 0:
-		/*
-		 * Done.  Clean up the cursor, and, if we're doing record
-		 * numbers, adjust the internal page counts.
-		 */
-		if (replace)
+		/* Done.  Clean up the cursor. */
+		if (isdeleted)
 			__bam_ca_replace(dbp, h->pgno, indx, REPLACE_SUCCESS);
-
-		if (!replace && F_ISSET(dbp, DB_BT_RECNUM))
-			ret = __bam_adjust(dbp, t, 1);
 		break;
 	case DB_NEEDSPLIT:
 		/*
 		 * We have to split the page.  Back out the cursor setup,
 		 * discard the stack of pages, and do the split.
 		 */
-		if (replace) {
-			replace = 0;
+		if (isdeleted)
 			__bam_ca_replace(dbp, h->pgno, indx, REPLACE_FAILED);
-		}
 
 		(void)__bam_stkrel(dbp);
 		stack = 0;
@@ -225,7 +219,7 @@ retry:	/*
 		goto retry;
 		/* NOTREACHED */
 	default:
-		if (replace)
+		if (isdeleted)
 			__bam_ca_replace(dbp, h->pgno, indx, REPLACE_FAILED);
 		break;
 	}
@@ -393,7 +387,8 @@ __bam_lookup(dbp, key, exactp)
 				for (indx = 0;
 				    indx < (db_indx_t)(NUM_ENT(h) - P_INDX) &&
 				    h->inp[indx] == h->inp[indx + P_INDX];
-				    indx += P_INDX);
+				    indx += P_INDX)
+					;
 				e.indx = indx;
 			}
 			goto fast;
@@ -427,7 +422,7 @@ slow:	return (__bam_search(dbp, key, S_INSERT, 1, NULL, exactp));
  *	Insert an item into the tree.
  *
  * PUBLIC: int __bam_iitem __P((DB *,
- * PUBLIC:    PAGE **, db_indx_t *, DBT *, DBT *, int, int));
+ * PUBLIC:    PAGE **, db_indx_t *, DBT *, DBT *, u_int32_t, u_int32_t));
  */
 int
 __bam_iitem(dbp, hp, indxp, key, data, op, flags)
@@ -435,13 +430,13 @@ __bam_iitem(dbp, hp, indxp, key, data, op, flags)
 	PAGE **hp;
 	db_indx_t *indxp;
 	DBT *key, *data;
-	int op, flags;
+	u_int32_t op, flags;
 {
 	BTREE *t;
 	BKEYDATA *bk;
 	DBT tdbt;
 	PAGE *h;
-	db_indx_t indx;
+	db_indx_t indx, nbytes;
 	u_int32_t data_size, have_bytes, need_bytes, needed;
 	int bigkey, bigdata, dupadjust, replace, ret;
 
@@ -466,12 +461,27 @@ __bam_iitem(dbp, hp, indxp, key, data, op, flags)
 			++*indxp;
 
 		/* Remove the current item if it's a DB_CURRENT op. */
-		if (op == DB_CURRENT && (ret = __db_ditem(dbp, *hp, *indxp,
-		    BKEYDATA_SIZE(GET_BKEYDATA(*hp, *indxp)->len))) != 0)
-			return (ret);
+		if (op == DB_CURRENT) {
+			bk = GET_BKEYDATA(*hp, *indxp);
+			switch (B_TYPE(bk->type)) {
+			case B_KEYDATA:
+				nbytes = BKEYDATA_SIZE(bk->len);
+				break;
+			case B_OVERFLOW:
+				nbytes = BOVERFLOW_SIZE;
+				break;
+			default:
+				return (__db_pgfmt(dbp, h->pgno));
+			}
+			if ((ret = __db_ditem(dbp, *hp, *indxp, nbytes)) != 0)
+				return (ret);
+		}
 
 		/* Put the new/replacement item onto the page. */
-		return (__db_dput(dbp, data, hp, indxp, __bam_new));
+		if ((ret = __db_dput(dbp, data, hp, indxp, __bam_new)) != 0)
+			return (ret);
+
+		goto done;
 	}
 
 	/* Handle fixed-length records: build the real record. */
@@ -568,7 +578,7 @@ __bam_iitem(dbp, hp, indxp, key, data, op, flags)
 		case DB_BEFORE:		/* 2. Insert a new key/data pair. */
 			break;
 		default:
-			abort();
+			return (EINVAL);
 		}
 
 		/* Add the key. */
@@ -638,7 +648,7 @@ __bam_iitem(dbp, hp, indxp, key, data, op, flags)
 			replace = 1;
 			break;
 		default:
-			abort();
+			return (EINVAL);
 		}
 	}
 
@@ -666,9 +676,8 @@ __bam_iitem(dbp, hp, indxp, key, data, op, flags)
 			return (ret);
 	}
 
-	++t->lstat.bt_added;
-
-	ret = memp_fset(dbp->mpf, h, DB_MPOOL_DIRTY);
+	if ((ret = memp_fset(dbp->mpf, h, DB_MPOOL_DIRTY)) != 0)
+		return (ret);
 
 	/*
 	 * If the page is at least 50% full, and we added a duplicate, see if
@@ -681,9 +690,25 @@ __bam_iitem(dbp, hp, indxp, key, data, op, flags)
 			return (ret);
 	}
 
+	/*
+	 * If we've changed the record count, update the tree.  Record counts
+	 * need to be updated in recno databases and in btree databases where
+	 * we are supporting records.  In both cases, adjust the count if the
+	 * operation wasn't performed on the current record or when the caller
+	 * overrides and wants the adjustment made regardless.
+	 */
+done:	if (LF_ISSET(BI_DOINCR) ||
+	    (op != DB_CURRENT &&
+	    (F_ISSET(dbp, DB_BT_RECNUM) || dbp->type == DB_RECNO)))
+		if ((ret = __bam_adjust(dbp, t, 1)) != 0)
+			return (ret);
+
+	/* If we've modified a recno file, set the flag */
 	if (t->bt_recno != NULL)
 		F_SET(t->bt_recno, RECNO_MODIFIED);
 
+	++t->lstat.bt_added;
+
 	return (ret);
 }
 
@@ -1036,8 +1061,8 @@ __bam_partial(dbp, dbt, h, indx, nbytes)
 	BOVERFLOW *bo;
 	DBT copy;
 	u_int32_t len, tlen;
-	int ret;
 	u_int8_t *p;
+	int ret;
 
 	COMPQUIET(bo, NULL);
 
@@ -1065,59 +1090,62 @@ __bam_partial(dbp, dbt, h, indx, nbytes)
 		bk->len = 0;
 	}
 
-	/* We use nul bytes for extending the record, get it over with. */
+	/*
+	 * We use nul bytes for any part of the record that isn't specified,
+	 * get it over with.
+	 */
 	memset(t->bt_rdata.data, 0, nbytes);
 
-	tlen = 0;
 	if (B_TYPE(bk->type) == B_OVERFLOW) {
-		/* Take up to doff bytes from the record. */
+		/*
+		 * In the case of an overflow record, we shift things around
+		 * in the current record rather than allocate a separate copy.
+		 */
 		memset(&copy, 0, sizeof(copy));
 		if ((ret = __db_goff(dbp, &copy, bo->tlen,
 		    bo->pgno, &t->bt_rdata.data, &t->bt_rdata.ulen)) != 0)
 			return (ret);
-		tlen += dbt->doff;
+
+		/* Skip any leading data from the original record. */
+		tlen = dbt->doff;
+		p = (u_int8_t *)t->bt_rdata.data + dbt->doff;
 
 		/*
-		 * If the original record was larger than the offset:
-		 *	If dlen > size, shift the remaining data down.
-		 *	If dlen < size, shift the remaining data up.
+		 * Copy in any trailing data from the original record.
+		 *
+		 * If the original record was larger than the original offset
+		 * plus the bytes being deleted, there is trailing data in the
+		 * original record we need to preserve.  If we aren't deleting
+		 * the same number of bytes as we're inserting, copy it up or
+		 * down, into place.
+		 *
 		 * Use memmove(), the regions may overlap.
 		 */
-		p = t->bt_rdata.data;
-		if (bo->tlen > dbt->doff)
-			if (dbt->dlen > dbt->size) {
-				tlen += len = bo->tlen -
-				    dbt->doff - (dbt->dlen - dbt->size);
-				memmove(p + dbt->doff + dbt->size,
-				    p + dbt->doff + dbt->dlen, len);
-			} else if (dbt->dlen < dbt->size) {
-				tlen += len = bo->tlen -
-				    dbt->doff - (dbt->size - dbt->dlen);
-				memmove(p + dbt->doff + dbt->dlen,
-				    p + dbt->doff + dbt->size, len);
-			} else
-				tlen += bo->tlen - dbt->doff;
+		if (bo->tlen > dbt->doff + dbt->dlen) {
+			len = bo->tlen - (dbt->doff + dbt->dlen);
+			if (dbt->dlen != dbt->size)
+				memmove(p + dbt->size, p + dbt->dlen, len);
+			tlen += len;
+		}
 
-		/* Copy in the user's data. */
-		memcpy((u_int8_t *)t->bt_rdata.data + dbt->doff,
-		    dbt->data, dbt->size);
+		/* Copy in the application provided data. */
+		memcpy(p, dbt->data, dbt->size);
 		tlen += dbt->size;
 	} else {
-		/* Take up to doff bytes from the record. */
+		/* Copy in any leading data from the original record. */
 		memcpy(t->bt_rdata.data,
 		    bk->data, dbt->doff > bk->len ? bk->len : dbt->doff);
-		tlen += dbt->doff;
+		tlen = dbt->doff;
+		p = (u_int8_t *)t->bt_rdata.data + dbt->doff;
 
-		/* Copy in the user's data. */
-		memcpy((u_int8_t *)t->bt_rdata.data +
-		    dbt->doff, dbt->data, dbt->size);
+		/* Copy in the application provided data. */
+		memcpy(p, dbt->data, dbt->size);
 		tlen += dbt->size;
 
-		/* Copy in any remaining data. */
+		/* Copy in any trailing data from the original record. */
 		len = dbt->doff + dbt->dlen;
 		if (bk->len > len) {
-			memcpy((u_int8_t *)t->bt_rdata.data + dbt->doff +
-			    dbt->size, bk->data + len, bk->len - len);
+			memcpy(p + dbt->size, bk->data + len, bk->len - len);
 			tlen += bk->len - len;
 		}
 	}