summaryrefslogtreecommitdiffstats
path: root/lib/libc/db/btree
diff options
context:
space:
mode:
authorpst <pst@FreeBSD.org>1996-02-27 19:42:00 +0000
committerpst <pst@FreeBSD.org>1996-02-27 19:42:00 +0000
commit912836fc11fb14d3f93b1bbd3d937daf0364a21f (patch)
tree282ca344758456835717d95b22f07b9d665d2134 /lib/libc/db/btree
parent221f5fcda8cae5ef7821d99fddd45a0d52a012f2 (diff)
downloadFreeBSD-src-912836fc11fb14d3f93b1bbd3d937daf0364a21f.zip
FreeBSD-src-912836fc11fb14d3f93b1bbd3d937daf0364a21f.tar.gz
Fix conflicts and merge into mainline
Diffstat (limited to 'lib/libc/db/btree')
-rw-r--r--lib/libc/db/btree/bt_close.c82
-rw-r--r--lib/libc/db/btree/bt_open.c76
-rw-r--r--lib/libc/db/btree/bt_stack.c92
3 files changed, 70 insertions, 180 deletions
diff --git a/lib/libc/db/btree/bt_close.c b/lib/libc/db/btree/bt_close.c
index 6269bd5..27f9ab6 100644
--- a/lib/libc/db/btree/bt_close.c
+++ b/lib/libc/db/btree/bt_close.c
@@ -1,5 +1,5 @@
/*-
- * Copyright (c) 1990, 1993
+ * 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
@@ -35,7 +35,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)bt_close.c 8.3 (Berkeley) 2/21/94";
+static char sccsid[] = "@(#)bt_close.c 8.7 (Berkeley) 8/17/94";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
@@ -75,25 +75,30 @@ __bt_close(dbp)
t->bt_pinned = NULL;
}
- /*
- * Delete any already deleted record that we've been saving
- * because the cursor pointed to it.
- */
- if (ISSET(t, B_DELCRSR) && __bt_crsrdel(t, &t->bt_bcursor))
- return (RET_ERROR);
-
+ /* Sync the tree. */
if (__bt_sync(dbp, 0) == RET_ERROR)
return (RET_ERROR);
+ /* Close the memory pool. */
if (mpool_close(t->bt_mp) == RET_ERROR)
return (RET_ERROR);
- if (t->bt_stack)
- free(t->bt_stack);
- if (t->bt_kbuf)
- free(t->bt_kbuf);
- if (t->bt_dbuf)
- free(t->bt_dbuf);
+ /* Free random memory. */
+ if (t->bt_cursor.key.data != NULL) {
+ free(t->bt_cursor.key.data);
+ t->bt_cursor.key.size = 0;
+ t->bt_cursor.key.data = NULL;
+ }
+ if (t->bt_rkey.data) {
+ free(t->bt_rkey.data);
+ t->bt_rkey.size = 0;
+ t->bt_rkey.data = NULL;
+ }
+ if (t->bt_rdata.data) {
+ free(t->bt_rdata.data);
+ t->bt_rdata.size = 0;
+ t->bt_rdata.data = NULL;
+ }
fd = t->bt_fd;
free(t);
@@ -117,8 +122,6 @@ __bt_sync(dbp, flags)
{
BTREE *t;
int status;
- PAGE *h;
- void *p;
t = dbp->internal;
@@ -134,40 +137,15 @@ __bt_sync(dbp, flags)
return (RET_ERROR);
}
- if (ISSET(t, B_INMEM | B_RDONLY) || !ISSET(t, B_MODIFIED))
+ if (F_ISSET(t, B_INMEM | B_RDONLY) || !F_ISSET(t, B_MODIFIED))
return (RET_SUCCESS);
- if (ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)
+ if (F_ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)
return (RET_ERROR);
- /*
- * Nastiness. If the cursor has been marked for deletion, but not
- * actually deleted, we have to make a copy of the page, delete the
- * key/data item, sync the file, and then restore the original page
- * contents.
- */
- if (ISSET(t, B_DELCRSR)) {
- if ((p = (void *)malloc(t->bt_psize)) == NULL)
- return (RET_ERROR);
- if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL)
- return (RET_ERROR);
- memmove(p, h, t->bt_psize);
- if ((status =
- __bt_dleaf(t, h, t->bt_bcursor.index)) == RET_ERROR)
- goto ecrsr;
- mpool_put(t->bt_mp, h, MPOOL_DIRTY);
- }
-
if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS)
- CLR(t, B_MODIFIED);
-
-ecrsr: if (ISSET(t, B_DELCRSR)) {
- if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL)
- return (RET_ERROR);
- memmove(h, p, t->bt_psize);
- free(p);
- mpool_put(t->bt_mp, h, MPOOL_DIRTY);
- }
+ F_CLR(t, B_MODIFIED);
+
return (status);
}
@@ -191,12 +169,12 @@ bt_meta(t)
return (RET_ERROR);
/* Fill in metadata. */
- m.m_magic = BTREEMAGIC;
- m.m_version = BTREEVERSION;
- m.m_psize = t->bt_psize;
- m.m_free = t->bt_free;
- m.m_nrecs = t->bt_nrecs;
- m.m_flags = t->bt_flags & SAVEMETA;
+ m.magic = BTREEMAGIC;
+ m.version = BTREEVERSION;
+ m.psize = t->bt_psize;
+ m.free = t->bt_free;
+ m.nrecs = t->bt_nrecs;
+ m.flags = F_ISSET(t, SAVEMETA);
memmove(p, &m, sizeof(BTMETA));
mpool_put(t->bt_mp, p, MPOOL_DIRTY);
diff --git a/lib/libc/db/btree/bt_open.c b/lib/libc/db/btree/bt_open.c
index f156745..f052249 100644
--- a/lib/libc/db/btree/bt_open.c
+++ b/lib/libc/db/btree/bt_open.c
@@ -1,5 +1,5 @@
/*-
- * Copyright (c) 1990, 1993
+ * 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
@@ -35,7 +35,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)bt_open.c 8.5 (Berkeley) 2/21/94";
+static char sccsid[] = "@(#)bt_open.c 8.10 (Berkeley) 8/17/94";
#endif /* LIBC_SCCS and not lint */
/*
@@ -61,6 +61,11 @@ static char sccsid[] = "@(#)bt_open.c 8.5 (Berkeley) 2/21/94";
#include <db.h>
#include "btree.h"
+#ifdef DEBUG
+#undef MINPSIZE
+#define MINPSIZE 128
+#endif
+
static int byteorder __P((void));
static int nroot __P((BTREE *));
static int tmp __P((void));
@@ -157,7 +162,6 @@ __bt_open(fname, flags, mode, openinfo, dflags)
if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL)
goto err;
memset(t, 0, sizeof(BTREE));
- t->bt_bcursor.pgno = P_INVALID;
t->bt_fd = -1; /* Don't close unopened fd on error. */
t->bt_lorder = b.lorder;
t->bt_order = NOT;
@@ -167,9 +171,9 @@ __bt_open(fname, flags, mode, openinfo, dflags)
if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL)
goto err;
- t->bt_flags = 0;
+ memset(t->bt_dbp, 0, sizeof(DB));
if (t->bt_lorder != machine_lorder)
- SET(t, B_NEEDSWAP);
+ F_SET(t, B_NEEDSWAP);
dbp->type = DB_BTREE;
dbp->internal = t;
@@ -186,9 +190,9 @@ __bt_open(fname, flags, mode, openinfo, dflags)
* open a backing temporary file. Otherwise, it's a disk-based tree.
*/
if (fname) {
- switch(flags & O_ACCMODE) {
+ switch (flags & O_ACCMODE) {
case O_RDONLY:
- SET(t, B_RDONLY);
+ F_SET(t, B_RDONLY);
break;
case O_RDWR:
break;
@@ -196,7 +200,7 @@ __bt_open(fname, flags, mode, openinfo, dflags)
default:
goto einval;
}
-
+
if ((t->bt_fd = open(fname, flags, mode)) < 0)
goto err;
@@ -205,7 +209,7 @@ __bt_open(fname, flags, mode, openinfo, dflags)
goto einval;
if ((t->bt_fd = tmp()) == -1)
goto err;
- SET(t, B_INMEM);
+ F_SET(t, B_INMEM);
}
if (fcntl(t->bt_fd, F_SETFD, 1) == -1)
@@ -214,8 +218,7 @@ __bt_open(fname, flags, mode, openinfo, dflags)
if (fstat(t->bt_fd, &sb))
goto err;
if (sb.st_size) {
- nr = read(t->bt_fd, &m, sizeof(BTMETA));
- if (nr < 0)
+ if ((nr = read(t->bt_fd, &m, sizeof(BTMETA))) < 0)
goto err;
if (nr != sizeof(BTMETA))
goto eftype;
@@ -228,28 +231,28 @@ __bt_open(fname, flags, mode, openinfo, dflags)
* don't bother to return an error, we just clear the NEEDSWAP
* bit.
*/
- if (m.m_magic == BTREEMAGIC)
- CLR(t, B_NEEDSWAP);
+ if (m.magic == BTREEMAGIC)
+ F_CLR(t, B_NEEDSWAP);
else {
- SET(t, B_NEEDSWAP);
- M_32_SWAP(m.m_magic);
- M_32_SWAP(m.m_version);
- M_32_SWAP(m.m_psize);
- M_32_SWAP(m.m_free);
- M_32_SWAP(m.m_nrecs);
- M_32_SWAP(m.m_flags);
+ F_SET(t, B_NEEDSWAP);
+ M_32_SWAP(m.magic);
+ M_32_SWAP(m.version);
+ M_32_SWAP(m.psize);
+ M_32_SWAP(m.free);
+ M_32_SWAP(m.nrecs);
+ M_32_SWAP(m.flags);
}
- if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION)
+ if (m.magic != BTREEMAGIC || m.version != BTREEVERSION)
goto eftype;
- if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET + 1 ||
- m.m_psize & sizeof(indx_t) - 1)
+ if (m.psize < MINPSIZE || m.psize > MAX_PAGE_OFFSET + 1 ||
+ m.psize & sizeof(indx_t) - 1)
goto eftype;
- if (m.m_flags & ~SAVEMETA)
+ if (m.flags & ~SAVEMETA)
goto eftype;
- b.psize = m.m_psize;
- t->bt_flags |= m.m_flags;
- t->bt_free = m.m_free;
- t->bt_nrecs = m.m_nrecs;
+ b.psize = m.psize;
+ F_SET(t, m.flags);
+ t->bt_free = m.free;
+ t->bt_nrecs = m.nrecs;
} else {
/*
* Set the page size to the best value for I/O to this file.
@@ -265,11 +268,11 @@ __bt_open(fname, flags, mode, openinfo, dflags)
/* Set flag if duplicates permitted. */
if (!(b.flags & R_DUP))
- SET(t, B_NODUPS);
+ F_SET(t, B_NODUPS);
t->bt_free = P_INVALID;
t->bt_nrecs = 0;
- SET(t, B_METADIRTY);
+ F_SET(t, B_METADIRTY);
}
t->bt_psize = b.psize;
@@ -304,7 +307,7 @@ __bt_open(fname, flags, mode, openinfo, dflags)
if ((t->bt_mp =
mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
goto err;
- if (!ISSET(t, B_INMEM))
+ if (!F_ISSET(t, B_INMEM))
mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
/* Create a root page if new tree. */
@@ -313,11 +316,11 @@ __bt_open(fname, flags, mode, openinfo, dflags)
/* Global flags. */
if (dflags & DB_LOCK)
- SET(t, B_DB_LOCK);
+ F_SET(t, B_DB_LOCK);
if (dflags & DB_SHMEM)
- SET(t, B_DB_SHMEM);
+ F_SET(t, B_DB_SHMEM);
if (dflags & DB_TXN)
- SET(t, B_DB_TXN);
+ F_SET(t, B_DB_TXN);
return (dbp);
@@ -357,8 +360,9 @@ nroot(t)
mpool_put(t->bt_mp, meta, 0);
return (RET_SUCCESS);
}
- if (errno != EINVAL)
+ if (errno != EINVAL) /* It's OK to not exist. */
return (RET_ERROR);
+ errno = 0;
if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
return (RET_ERROR);
@@ -432,7 +436,7 @@ __bt_fd(dbp)
}
/* In-memory database can't have a file descriptor. */
- if (ISSET(t, B_INMEM)) {
+ if (F_ISSET(t, B_INMEM)) {
errno = ENOENT;
return (-1);
}
diff --git a/lib/libc/db/btree/bt_stack.c b/lib/libc/db/btree/bt_stack.c
deleted file mode 100644
index 5c7fab9..0000000
--- a/lib/libc/db/btree/bt_stack.c
+++ /dev/null
@@ -1,92 +0,0 @@
-/*-
- * Copyright (c) 1990, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Mike Olson.
- *
- * 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
- * documentation and/or other materials provided with the distribution.
- * 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.
- */
-
-#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)bt_stack.c 8.3 (Berkeley) 2/21/94";
-#endif /* LIBC_SCCS and not lint */
-
-#include <sys/types.h>
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include <db.h>
-#include "btree.h"
-
-/*
- * When a page splits, a new record has to be inserted into its parent page.
- * This page may have to split as well, all the way up to the root. Since
- * parent pointers in each page would be expensive, we maintain a stack of
- * parent pages as we descend the tree.
- *
- * XXX
- * This is a concurrency problem -- if user a builds a stack, then user b
- * splits the tree, then user a tries to split the tree, there's a new level
- * in the tree that user a doesn't know about.
- */
-
-/*
- * __BT_PUSH -- Push parent page info onto the stack (LIFO).
- *
- * Parameters:
- * t: tree
- * pgno: page
- * index: page index
- *
- * Returns:
- * RET_ERROR, RET_SUCCESS
- */
-int
-__bt_push(t, pgno, index)
- BTREE *t;
- pgno_t pgno;
- indx_t index;
-{
- if (t->bt_sp == t->bt_maxstack) {
- t->bt_maxstack += 50;
- if ((t->bt_stack = (EPGNO *)realloc(t->bt_stack,
- t->bt_maxstack * sizeof(EPGNO))) == NULL) {
- t->bt_maxstack -= 50;
- return (RET_ERROR);
- }
- }
-
- t->bt_stack[t->bt_sp].pgno = pgno;
- t->bt_stack[t->bt_sp].index = index;
- ++t->bt_sp;
- return (RET_SUCCESS);
-}
OpenPOWER on IntegriCloud