add btree tests

This commit is contained in:
cgd 1993-05-27 21:19:56 +00:00
parent 7a24140432
commit 98e6ed919c
14 changed files with 3100 additions and 0 deletions

View File

@ -0,0 +1,22 @@
# from: @(#)Makefile 5.4 (Berkeley) 1/11/93
# $Id: Makefile,v 1.1 1993/05/27 21:19:56 cgd Exp $
PROG= dbtest
SRCS= dbtest.c
#SRCS+= rec_close.c rec_delete.c rec_get.c rec_open.c rec_put.c \
# rec_search.c rec_seq.c rec_utils.c
#.PATH: ${.CURDIR}/../recno
#SRCS+= bt_close.c bt_conv.c bt_debug.c bt_delete.c bt_get.c bt_open.c \
# bt_overflow.c bt_put.c bt_search.c bt_seq.c bt_split.c bt_stack.c \
# bt_utils.c
#.PATH: ${.CURDIR}/../btree
#SRCS+= hash.c hash_bigkey.c hash_buf.c hash_func.c hash_log2.c \
# hash_page.c hsearch.c ndbm.c
#.PATH: ${.CURDIR}/../hash
#LDADD= /a/users/cgd/CSRG/newdb2/src/lib/libc/obj/libc.a
CFLAGS= -g -DDEBUG -DSTATISTICS
NOMAN= noman
NOINCLUDE=1
.include <bsd.prog.mk>

View File

@ -0,0 +1,45 @@
# @(#)README 5.7 (Berkeley) 5/1/93
The script file consists of lines with a initial character which is
the "command" for that line. Legal characters are as follows:
c: compare a record
+ must be followed by [kK][dD]; the data value in the database
associated with the specified key is compared to the specified
data value.
e: echo a string
+ writes out the rest of the line into the output file; if the
last character is not a carriage-return, a newline is appended.
g: do a get command
+ must be followed by [kK]
+ writes out the retrieved data DBT.
p: do a put command
+ must be followed by [kK][dD]
r: do a del command
+ must be followed by [kK]
s: do a seq command
+ writes out the retrieved data DBT.
f: set the flags for the next command
+ no value zero's the flags
D [file]: data file
+ set the current data value to the contents of the file
d [data]:
+ set the current key value to the contents of the line.
K [file]: key file
+ set the current key value to the contents of the file
k [data]:
+ set the current key value to the contents of the line.
o [r]: dump [reverse]
+ dump the database out, if 'r' is set, in reverse order.
Options to dbtest are as follows:
-f: Use the file argument as the database file.
-i: Use the rest of the argument to set elements in the info
structure. If the type is btree, then "-i cachesize=10240"
will set BTREEINFO.cachesize to 10240.
-o: The rest of the argument is the output file instead of
using stdout.
Dbtest requires two arguments, the type of access "hash", "recno" or
"btree", and the script name.

View File

@ -0,0 +1,765 @@
/*-
* Copyright (c) 1990 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[] = "@(#)main.c 5.4 (Berkeley) 6/23/92";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <fcntl.h>
#include <db.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "btree.h"
typedef struct cmd_table {
char *cmd;
int nargs;
int rconv;
void (*func) __P((DB *, char **));
char *usage, *descrip;
} cmd_table;
int stopstop;
DB *globaldb;
void append __P((DB *, char **));
void bstat __P((DB *, char **));
void cursor __P((DB *, char **));
void delcur __P((DB *, char **));
void delete __P((DB *, char **));
void dump __P((DB *, char **));
void first __P((DB *, char **));
void get __P((DB *, char **));
void help __P((DB *, char **));
void iafter __P((DB *, char **));
void ibefore __P((DB *, char **));
void icursor __P((DB *, char **));
void insert __P((DB *, char **));
void keydata __P((DBT *, DBT *));
void last __P((DB *, char **));
void list __P((DB *, char **));
void load __P((DB *, char **));
void mstat __P((DB *, char **));
void next __P((DB *, char **));
int parse __P((char *, char **, int));
void previous __P((DB *, char **));
void show __P((DB *, char **));
void usage __P((void));
void user __P((DB *));
cmd_table commands[] = {
"?", 0, 0, help, "help", NULL,
"a", 2, 1, append, "append key def", "append key with data def",
"b", 0, 0, bstat, "bstat", "stat btree",
"c", 1, 1, cursor, "cursor word", "move cursor to word",
"delc", 0, 0, delcur, "delcur", "delete key the cursor references",
"dele", 1, 1, delete, "delete word", "delete word",
"d", 0, 0, dump, "dump", "dump database",
"f", 0, 0, first, "first", "move cursor to first record",
"g", 1, 1, get, "get key", "locate key",
"h", 0, 0, help, "help", "print command summary",
"ia", 2, 1, iafter, "iafter key data", "insert data after key",
"ib", 2, 1, ibefore, "ibefore key data", "insert data before key",
"ic", 2, 1, icursor, "icursor key data", "replace cursor",
"in", 2, 1, insert, "insert key def", "insert key with data def",
"la", 0, 0, last, "last", "move cursor to last record",
"li", 1, 1, list, "list file", "list to a file",
"loa", 1, 0, load, "load file", NULL,
"loc", 1, 1, get, "get key", NULL,
"m", 0, 0, mstat, "mstat", "stat memory pool",
"n", 0, 0, next, "next", "move cursor forward one record",
"p", 0, 0, previous, "previous", "move cursor back one record",
"q", 0, 0, NULL, "quit", "quit",
"sh", 1, 0, show, "show page", "dump a page",
{ NULL },
};
int recno; /* use record numbers */
char *dict = "words"; /* default dictionary */
char *progname;
int
main(argc, argv)
int argc;
char **argv;
{
int c;
DB *db;
BTREEINFO b;
progname = *argv;
b.flags = 0;
b.cachesize = 0;
b.maxkeypage = 0;
b.minkeypage = 0;
b.psize = 0;
b.compare = NULL;
b.prefix = NULL;
b.lorder = 0;
while ((c = getopt(argc, argv, "bc:di:lp:ru")) != EOF) {
switch (c) {
case 'b':
b.lorder = BIG_ENDIAN;
break;
case 'c':
b.cachesize = atoi(optarg);
break;
case 'd':
b.flags |= R_DUP;
break;
case 'i':
dict = optarg;
break;
case 'l':
b.lorder = LITTLE_ENDIAN;
break;
case 'p':
b.psize = atoi(optarg);
break;
case 'r':
recno = 1;
break;
case 'u':
b.flags = 0;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (recno)
db = dbopen(*argv == NULL ? NULL : *argv, O_RDWR,
0, DB_RECNO, NULL);
else
db = dbopen(*argv == NULL ? NULL : *argv, O_CREAT|O_RDWR,
0600, DB_BTREE, &b);
if (db == NULL) {
(void)fprintf(stderr, "dbopen: %s\n", strerror(errno));
exit(1);
}
globaldb = db;
user(db);
exit(0);
/* NOTREACHED */
}
void
user(db)
DB *db;
{
FILE *ifp;
int argc, i, last;
char *lbuf, *argv[4], buf[512];
if ((ifp = fopen("/dev/tty", "r")) == NULL) {
(void)fprintf(stderr,
"/dev/tty: %s\n", strerror(errno));
exit(1);
}
for (last = 0;;) {
(void)printf("> ");
(void)fflush(stdout);
if ((lbuf = fgets(&buf[0], 512, ifp)) == NULL)
break;
if (lbuf[0] == '\n') {
i = last;
goto uselast;
}
lbuf[strlen(lbuf) - 1] = '\0';
if (lbuf[0] == 'q')
break;
argc = parse(lbuf, &argv[0], 3);
if (argc == 0)
continue;
for (i = 0; commands[i].cmd != NULL; i++)
if (strncmp(commands[i].cmd, argv[0],
strlen(commands[i].cmd)) == 0)
break;
if (commands[i].cmd == NULL) {
(void)fprintf(stderr,
"%s: command unknown ('help' for help)\n", lbuf);
continue;
}
if (commands[i].nargs != argc - 1) {
(void)fprintf(stderr, "usage: %s\n", commands[i].usage);
continue;
}
if (recno && commands[i].rconv) {
static recno_t nlong;
nlong = atoi(argv[1]);
argv[1] = (char *)&nlong;
}
uselast: last = i;
(*commands[i].func)(db, argv);
}
if ((db->sync)(db) == RET_ERROR)
perror("dbsync");
else if ((db->close)(db) == RET_ERROR)
perror("dbclose");
}
int
parse(lbuf, argv, maxargc)
char *lbuf, **argv;
int maxargc;
{
int argc = 0;
char *c;
c = lbuf;
while (isspace(*c))
c++;
while (*c != '\0' && argc < maxargc) {
*argv++ = c;
argc++;
while (!isspace(*c) && *c != '\0') {
c++;
}
while (isspace(*c))
*c++ = '\0';
}
return (argc);
}
void
append(db, argv)
DB *db;
char **argv;
{
DBT key, data;
int status;
if (!recno) {
(void)fprintf(stderr,
"append only available for recno db's.\n");
return;
}
key.data = argv[1];
key.size = sizeof(recno_t);
data.data = argv[2];
data.size = strlen(data.data);
status = (db->put)(db, &key, &data, R_APPEND);
switch (status) {
case RET_ERROR:
perror("append/put");
break;
case RET_SPECIAL:
(void)printf("%s (duplicate key)\n", argv[1]);
break;
case RET_SUCCESS:
break;
}
}
void
cursor(db, argv)
DB *db;
char **argv;
{
DBT data, key;
int status;
key.data = argv[1];
if (recno)
key.size = sizeof(recno_t);
else
key.size = strlen(argv[1]) + 1;
status = (*db->seq)(db, &key, &data, R_CURSOR);
switch (status) {
case RET_ERROR:
perror("cursor/seq");
break;
case RET_SPECIAL:
(void)printf("key not found\n");
break;
case RET_SUCCESS:
keydata(&key, &data);
break;
}
}
void
delcur(db, argv)
DB *db;
char **argv;
{
int status;
status = (*db->del)(db, NULL, R_CURSOR);
if (status == RET_ERROR)
perror("delcur/del");
}
void
delete(db, argv)
DB *db;
char **argv;
{
DBT key;
int status;
key.data = argv[1];
if (recno)
key.size = sizeof(recno_t);
else
key.size = strlen(argv[1]) + 1;
status = (*db->del)(db, &key, 0);
switch (status) {
case RET_ERROR:
perror("delete/del");
break;
case RET_SPECIAL:
(void)printf("key not found\n");
break;
case RET_SUCCESS:
break;
}
}
void
dump(db, argv)
DB *db;
char **argv;
{
__bt_dump(db);
}
void
first(db, argv)
DB *db;
char **argv;
{
DBT data, key;
int status;
status = (*db->seq)(db, &key, &data, R_FIRST);
switch (status) {
case RET_ERROR:
perror("first/seq");
break;
case RET_SPECIAL:
(void)printf("no more keys\n");
break;
case RET_SUCCESS:
keydata(&key, &data);
break;
}
}
void
get(db, argv)
DB *db;
char **argv;
{
DBT data, key;
int status;
key.data = argv[1];
if (recno)
key.size = sizeof(recno_t);
else
key.size = strlen(argv[1]) + 1;
status = (*db->get)(db, &key, &data, 0);
switch (status) {
case RET_ERROR:
perror("get/get");
break;
case RET_SPECIAL:
(void)printf("key not found\n");
break;
case RET_SUCCESS:
keydata(&key, &data);
break;
}
}
void
help(db, argv)
DB *db;
char **argv;
{
int i;
for (i = 0; commands[i].cmd; i++)
if (commands[i].descrip)
(void)printf("%s: %s\n",
commands[i].usage, commands[i].descrip);
}
void
iafter(db, argv)
DB *db;
char **argv;
{
DBT key, data;
int status;
if (!recno) {
(void)fprintf(stderr,
"iafter only available for recno db's.\n");
return;
}
key.data = argv[1];
key.size = sizeof(recno_t);
data.data = argv[2];
data.size = strlen(data.data);
status = (db->put)(db, &key, &data, R_IAFTER);
switch (status) {
case RET_ERROR:
perror("iafter/put");
break;
case RET_SPECIAL:
(void)printf("%s (duplicate key)\n", argv[1]);
break;
case RET_SUCCESS:
break;
}
}
void
ibefore(db, argv)
DB *db;
char **argv;
{
DBT key, data;
int status;
if (!recno) {
(void)fprintf(stderr,
"ibefore only available for recno db's.\n");
return;
}
key.data = argv[1];
key.size = sizeof(recno_t);
data.data = argv[2];
data.size = strlen(data.data);
status = (db->put)(db, &key, &data, R_IBEFORE);
switch (status) {
case RET_ERROR:
perror("ibefore/put");
break;
case RET_SPECIAL:
(void)printf("%s (duplicate key)\n", argv[1]);
break;
case RET_SUCCESS:
break;
}
}
void
icursor(db, argv)
DB *db;
char **argv;
{
int status;
DBT data, key;
key.data = argv[1];
if (recno)
key.size = sizeof(recno_t);
else
key.size = strlen(argv[1]) + 1;
data.data = argv[2];
data.size = strlen(argv[2]) + 1;
status = (*db->put)(db, &key, &data, R_CURSOR);
switch (status) {
case RET_ERROR:
perror("icursor/put");
break;
case RET_SPECIAL:
(void)printf("%s (duplicate key)\n", argv[1]);
break;
case RET_SUCCESS:
break;
}
}
void
insert(db, argv)
DB *db;
char **argv;
{
int status;
DBT data, key;
key.data = argv[1];
if (recno)
key.size = sizeof(recno_t);
else
key.size = strlen(argv[1]) + 1;
data.data = argv[2];
data.size = strlen(argv[2]) + 1;
status = (*db->put)(db, &key, &data, R_NOOVERWRITE);
switch (status) {
case RET_ERROR:
perror("insert/put");
break;
case RET_SPECIAL:
(void)printf("%s (duplicate key)\n", argv[1]);
break;
case RET_SUCCESS:
break;
}
}
void
last(db, argv)
DB *db;
char **argv;
{
DBT data, key;
int status;
status = (*db->seq)(db, &key, &data, R_LAST);
switch (status) {
case RET_ERROR:
perror("last/seq");
break;
case RET_SPECIAL:
(void)printf("no more keys\n");
break;
case RET_SUCCESS:
keydata(&key, &data);
break;
}
}
void
list(db, argv)
DB *db;
char **argv;
{
DBT data, key;
FILE *fp;
int status;
if ((fp = fopen(argv[1], "w")) == NULL) {
(void)fprintf(stderr, "%s: %s\n", argv[1], strerror(errno));
return;
}
status = (*db->seq)(db, &key, &data, R_FIRST);
while (status == RET_SUCCESS) {
(void)fprintf(fp, "%s\n", key.data);
status = (*db->seq)(db, &key, &data, R_NEXT);
}
if (status == RET_ERROR)
perror("list/seq");
}
DB *BUGdb;
void
load(db, argv)
DB *db;
char **argv;
{
register char *p, *t;
FILE *fp;
DBT data, key;
recno_t cnt;
size_t len;
int status;
char *lp, buf[16 * 1024];
BUGdb = db;
if ((fp = fopen(argv[1], "r")) == NULL) {
(void)fprintf(stderr, "%s: %s\n", argv[1], strerror(errno));
return;
}
(void)printf("loading %s...\n", argv[1]);
for (cnt = 1; (lp = fgetline(fp, &len)) != NULL; ++cnt) {
if (recno) {
key.data = &cnt;
key.size = sizeof(recno_t);
data.data = lp;
data.size = len + 1;
} else {
key.data = lp;
key.size = len + 1;
for (p = lp + len - 1, t = buf; p >= lp; *t++ = *p--);
*t = '\0';
data.data = buf;
data.size = len + 1;
}
status = (*db->put)(db, &key, &data, R_NOOVERWRITE);
switch (status) {
case RET_ERROR:
perror("load/put");
exit(1);
case RET_SPECIAL:
if (recno)
(void)fprintf(stderr,
"duplicate: %ld {%s}\n", cnt, data.data);
else
(void)fprintf(stderr,
"duplicate: %ld {%s}\n", cnt, key.data);
exit(1);
case RET_SUCCESS:
break;
}
}
(void)fclose(fp);
}
void
next(db, argv)
DB *db;
char **argv;
{
DBT data, key;
int status;
status = (*db->seq)(db, &key, &data, R_NEXT);
switch (status) {
case RET_ERROR:
perror("next/seq");
break;
case RET_SPECIAL:
(void)printf("no more keys\n");
break;
case RET_SUCCESS:
keydata(&key, &data);
break;
}
}
void
previous(db, argv)
DB *db;
char **argv;
{
DBT data, key;
int status;
status = (*db->seq)(db, &key, &data, R_PREV);
switch (status) {
case RET_ERROR:
perror("previous/seq");
break;
case RET_SPECIAL:
(void)printf("no more keys\n");
break;
case RET_SUCCESS:
keydata(&key, &data);
break;
}
}
void
show(db, argv)
DB *db;
char **argv;
{
BTREE *t;
PAGE *h;
pgno_t pg;
pg = atoi(argv[1]);
t = db->internal;
if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) {
(void)printf("getpage of %ld failed\n", pg);
return;
}
if (pg == 0)
__bt_dmpage(h);
else
__bt_dpage(h);
mpool_put(t->bt_mp, h, 0);
}
void
bstat(db, argv)
DB *db;
char **argv;
{
(void)printf("BTREE\n");
__bt_stat(db);
}
void
mstat(db, argv)
DB *db;
char **argv;
{
(void)printf("MPOOL\n");
mpool_stat(((BTREE *)db->internal)->bt_mp);
}
void
keydata(key, data)
DBT *key, *data;
{
if (!recno && key->size > 0)
(void)printf("%s/", key->data);
if (data->size > 0)
(void)printf("%s", data->data);
(void)printf("\n");
}
void
usage()
{
(void)fprintf(stderr,
"usage: %s [-bdlu] [-c cache] [-i file] [-p page] [file]\n",
progname);
exit (1);
}

View File

@ -0,0 +1,656 @@
/*-
* Copyright (c) 1992 The Regents of the University of California.
* All rights reserved.
*
* 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.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1992 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)dbtest.c 5.18 (Berkeley) 5/22/93";
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <db.h>
enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA };
void compare __P((DBT *, DBT *));
DBTYPE dbtype __P((char *));
void dump __P((DB *, int));
void err __P((const char *, ...));
void get __P((DB *, DBT *));
void getdata __P((DB *, DBT *, DBT *));
void put __P((DB *, DBT *, DBT *));
void rem __P((DB *, DBT *));
void *rfile __P((char *, size_t *));
void seq __P((DB *, DBT *));
u_int setflags __P((char *));
void *setinfo __P((DBTYPE, char *));
void usage __P((void));
void *xmalloc __P((char *, size_t));
DBTYPE type;
void *infop;
u_long lineno;
u_int flags;
int ofd = STDOUT_FILENO;
DB *XXdbp; /* Global for gdb. */
int
main(argc, argv)
int argc;
char *argv[];
{
extern int optind;
extern char *optarg;
enum S command, state;
DB *dbp;
DBT data, key, keydata;
size_t len;
int ch;
char *fname, *infoarg, *p, buf[8 * 1024];
infoarg = NULL;
fname = NULL;
while ((ch = getopt(argc, argv, "f:i:o:")) != EOF)
switch(ch) {
case 'f':
fname = optarg;
break;
case 'i':
infoarg = optarg;
break;
case 'o':
if ((ofd = open(optarg,
O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
err("%s: %s", optarg, strerror(errno));
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argc != 2)
usage();
/* Set the type. */
type = dbtype(*argv++);
/* Open the descriptor file. */
if (freopen(*argv, "r", stdin) == NULL)
err("%s: %s", *argv, strerror(errno));
/* Set up the db structure as necessary. */
if (infoarg == NULL)
infop = NULL;
else
for (p = strtok(infoarg, ",\t "); p != NULL;
p = strtok(0, ",\t "))
if (*p != '\0')
infop = setinfo(type, p);
/* Open the DB. */
if (fname == NULL) {
p = getenv("TMPDIR");
if (p == NULL)
p = "/var/tmp";
(void)sprintf(buf, "%s/__dbtest", p);
fname = buf;
(void)unlink(buf);
}
if ((dbp = dbopen(fname,
O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, type, infop)) == NULL)
err("dbopen: %s", strerror(errno));
XXdbp = dbp;
state = COMMAND;
for (lineno = 1;
(p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
len = strlen(buf);
switch(*p) {
case 'c': /* compare */
if (state != COMMAND)
err("line %lu: not expecting command", lineno);
state = KEY;
command = COMPARE;
break;
case 'e': /* echo */
if (state != COMMAND)
err("line %lu: not expecting command", lineno);
/* Don't display the newline, if CR at EOL. */
if (p[len - 2] == '\r')
--len;
if (write(ofd, p + 1, len - 1) != len - 1)
err("write: %s", strerror(errno));
break;
case 'g': /* get */
if (state != COMMAND)
err("line %lu: not expecting command", lineno);
state = KEY;
command = GET;
break;
case 'p': /* put */
if (state != COMMAND)
err("line %lu: not expecting command", lineno);
state = KEY;
command = PUT;
break;
case 'r': /* remove */
if (state != COMMAND)
err("line %lu: not expecting command", lineno);
state = KEY;
command = REMOVE;
break;
case 's': /* seq */
if (state != COMMAND)
err("line %lu: not expecting command", lineno);
if (flags == R_CURSOR) {
state = KEY;
command = SEQ;
} else
seq(dbp, &key);
break;
case 'f':
flags = setflags(p + 1);
break;
case 'D': /* data file */
if (state != DATA)
err("line %lu: not expecting data", lineno);
data.data = rfile(p + 1, &data.size);
goto ldata;
case 'd': /* data */
if (state != DATA)
err("line %lu: not expecting data", lineno);
data.data = xmalloc(p + 1, len - 1);
data.size = len - 1;
ldata: switch(command) {
case COMPARE:
compare(&keydata, &data);
break;
case PUT:
put(dbp, &key, &data);
break;
default:
err("line %lu: command doesn't take data",
lineno);
}
if (type != DB_RECNO)
free(key.data);
free(data.data);
state = COMMAND;
break;
case 'K': /* key file */
if (state != KEY)
err("line %lu: not expecting a key", lineno);
if (type == DB_RECNO)
err("line %lu: 'K' not available for recno",
lineno);
key.data = rfile(p + 1, &key.size);
goto lkey;
case 'k': /* key */
if (state != KEY)
err("line %lu: not expecting a key", lineno);
if (type == DB_RECNO) {
static recno_t recno;
recno = strtol(p + 1, NULL, 0);
key.data = &recno;
key.size = sizeof(recno);
} else {
key.data = xmalloc(p + 1, len - 1);
key.size = len - 1;
}
lkey: switch(command) {
case COMPARE:
getdata(dbp, &key, &keydata);
state = DATA;
break;
case GET:
get(dbp, &key);
if (type != DB_RECNO)
free(key.data);
state = COMMAND;
break;
case PUT:
state = DATA;
break;
case REMOVE:
rem(dbp, &key);
if (type != DB_RECNO)
free(key.data);
state = COMMAND;
break;
case SEQ:
seq(dbp, &key);
if (type != DB_RECNO)
free(key.data);
state = COMMAND;
break;
default:
err("line %lu: command doesn't take a key",
lineno);
}
break;
case 'o':
dump(dbp, p[1] == 'r');
break;
default:
err("line %lu: %s: unknown command character",
p, lineno);
}
}
if (dbp->close(dbp))
err("db->close: %s", strerror(errno));
(void)close(ofd);
exit(0);
}
#define NOOVERWRITE "put failed, would overwrite key\n"
#define NOSUCHKEY "get failed, no such key\n"
void
compare(db1, db2)
DBT *db1, *db2;
{
register size_t len;
register u_char *p1, *p2;
if (db1->size != db2->size)
printf("compare failed: key->data len %lu != data len %lu\n",
db1->size, db2->size);
len = MIN(db1->size, db2->size);
for (p1 = db1->data, p2 = db2->data; len--;)
if (*p1++ != *p2++) {
printf("compare failed at offset %d\n",
p1 - (u_char *)db1->data);
break;
}
}
void
get(dbp, kp)
DB *dbp;
DBT *kp;
{
DBT data;
switch(dbp->get(dbp, kp, &data, flags)) {
case 0:
(void)write(ofd, data.data, data.size);
break;
case -1:
err("line %lu: get: %s", lineno, strerror(errno));
/* NOTREACHED */
case 1:
(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
(void)fprintf(stderr, "%d: %.*s: %s\n",
lineno, kp->size, kp->data, NOSUCHKEY);
break;
}
}
void
getdata(dbp, kp, dp)
DB *dbp;
DBT *kp, *dp;
{
switch(dbp->get(dbp, kp, dp, flags)) {
case 0:
return;
case -1:
err("line %lu: getdata: %s", lineno, strerror(errno));
/* NOTREACHED */
case 1:
err("line %lu: get failed, no such key", lineno);
/* NOTREACHED */
}
}
void
put(dbp, kp, dp)
DB *dbp;
DBT *kp, *dp;
{
switch(dbp->put(dbp, kp, dp, flags)) {
case 0:
break;
case -1:
err("line %lu: put: %s", lineno, strerror(errno));
/* NOTREACHED */
case 1:
(void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
break;
}
}
void
rem(dbp, kp)
DB *dbp;
DBT *kp;
{
switch(dbp->del(dbp, kp, flags)) {
case 0:
break;
case -1:
err("line %lu: get: %s", lineno, strerror(errno));
/* NOTREACHED */
case 1:
(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
break;
}
}
void
seq(dbp, kp)
DB *dbp;
DBT *kp;
{
DBT data;
switch(dbp->seq(dbp, kp, &data, flags)) {
case 0:
(void)write(ofd, data.data, data.size);
break;
case -1:
err("line %lu: seq: %s", lineno, strerror(errno));
/* NOTREACHED */
case 1:
(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
break;
}
}
void
dump(dbp, rev)
DB *dbp;
int rev;
{
DBT key, data;
int flags, nflags;
if (rev) {
flags = R_LAST;
nflags = R_PREV;
} else {
flags = R_FIRST;
nflags = R_NEXT;
}
for (;; flags = nflags)
switch(dbp->seq(dbp, &key, &data, flags)) {
case 0:
(void)write(ofd, data.data, data.size);
break;
case 1:
goto done;
case -1:
err("line %lu: (dump) seq: %s",
lineno, strerror(errno));
/* NOTREACHED */
}
done: return;
}
u_int
setflags(s)
char *s;
{
char *p;
for (; isspace(*s); ++s);
if (*s == '\n')
return (0);
if ((p = index(s, '\n')) != NULL)
*p = '\0';
if (!strcmp(s, "R_CURSOR"))
return (R_CURSOR);
if (!strcmp(s, "R_FIRST"))
return (R_FIRST);
if (!strcmp(s, "R_IAFTER"))
return (R_IAFTER);
if (!strcmp(s, "R_IBEFORE"))
return (R_IBEFORE);
if (!strcmp(s, "R_LAST"))
return (R_LAST);
if (!strcmp(s, "R_NEXT"))
return (R_NEXT);
if (!strcmp(s, "R_NOOVERWRITE"))
return (R_NOOVERWRITE);
if (!strcmp(s, "R_PREV"))
return (R_PREV);
if (!strcmp(s, "R_SETCURSOR"))
return (R_SETCURSOR);
err("line %lu: %s: unknown flag", lineno, s);
/* NOTREACHED */
}
DBTYPE
dbtype(s)
char *s;
{
if (!strcmp(s, "btree"))
return (DB_BTREE);
if (!strcmp(s, "hash"))
return (DB_HASH);
if (!strcmp(s, "recno"))
return (DB_RECNO);
err("%s: unknown type (use btree, hash or recno)", s);
/* NOTREACHED */
}
void *
setinfo(type, s)
DBTYPE type;
char *s;
{
static BTREEINFO ib;
static HASHINFO ih;
static RECNOINFO rh;
char *eq;
if ((eq = index(s, '=')) == NULL)
err("%s: illegal structure set statement", s);
*eq++ = '\0';
if (!isdigit(*eq))
err("%s: structure set statement must be a number", s);
switch(type) {
case DB_BTREE:
if (!strcmp("flags", s)) {
ib.flags = strtoul(eq, NULL, 0);
return (&ib);
}
if (!strcmp("cachesize", s)) {
ib.cachesize = strtoul(eq, NULL, 0);
return (&ib);
}
if (!strcmp("maxkeypage", s)) {
ib.maxkeypage = strtoul(eq, NULL, 0);
return (&ib);
}
if (!strcmp("minkeypage", s)) {
ib.minkeypage = strtoul(eq, NULL, 0);
return (&ib);
}
if (!strcmp("lorder", s)) {
ib.lorder = strtoul(eq, NULL, 0);
return (&ib);
}
if (!strcmp("psize", s)) {
ib.psize = strtoul(eq, NULL, 0);
return (&ib);
}
break;
case DB_HASH:
if (!strcmp("bsize", s)) {
ih.bsize = strtoul(eq, NULL, 0);
return (&ih);
}
if (!strcmp("ffactor", s)) {
ih.ffactor = strtoul(eq, NULL, 0);
return (&ih);
}
if (!strcmp("nelem", s)) {
ih.nelem = strtoul(eq, NULL, 0);
return (&ih);
}
if (!strcmp("cachesize", s)) {
ih.cachesize = strtoul(eq, NULL, 0);
return (&ih);
}
if (!strcmp("lorder", s)) {
ih.lorder = strtoul(eq, NULL, 0);
return (&ih);
}
break;
case DB_RECNO:
if (!strcmp("flags", s)) {
rh.flags = strtoul(eq, NULL, 0);
return (&rh);
}
if (!strcmp("cachesize", s)) {
rh.cachesize = strtoul(eq, NULL, 0);
return (&rh);
}
if (!strcmp("lorder", s)) {
rh.lorder = strtoul(eq, NULL, 0);
return (&rh);
}
if (!strcmp("reclen", s)) {
rh.reclen = strtoul(eq, NULL, 0);
return (&rh);
}
if (!strcmp("bval", s)) {
rh.bval = strtoul(eq, NULL, 0);
return (&rh);
}
if (!strcmp("psize", s)) {
rh.psize = strtoul(eq, NULL, 0);
return (&rh);
}
break;
}
err("%s: unknown structure value", s);
/* NOTREACHED */
}
void *
rfile(name, lenp)
char *name;
size_t *lenp;
{
struct stat sb;
void *p;
int fd;
char *np;
for (; isspace(*name); ++name);
if ((np = index(name, '\n')) != NULL)
*np = '\0';
if ((fd = open(name, O_RDONLY, 0)) < 0 ||
fstat(fd, &sb))
err("%s: %s\n", name, strerror(errno));
if (sb.st_size > (off_t)INT_MAX)
err("%s: %s\n", name, strerror(E2BIG));
if ((p = malloc((u_int)sb.st_size)) == NULL)
err("%s", strerror(errno));
(void)read(fd, p, (int)sb.st_size);
*lenp = sb.st_size;
(void)close(fd);
return (p);
}
void *
xmalloc(text, len)
char *text;
size_t len;
{
void *p;
if ((p = malloc(len)) == NULL)
err("%s", strerror(errno));
memmove(p, text, len);
return (p);
}
void
usage()
{
(void)fprintf(stderr,
"usage: dbtest [-f file] [-i info] [-o file] type script\n");
exit(1);
}
#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
void
#if __STDC__
err(const char *fmt, ...)
#else
err(fmt, va_alist)
char *fmt;
va_dcl
#endif
{
va_list ap;
#if __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
(void)fprintf(stderr, "dbtest: ");
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
exit(1);
/* NOTREACHED */
}

View File

@ -0,0 +1,114 @@
/*-
* Copyright (c) 1991 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
* 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.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)driver2.c 5.2 (Berkeley) 9/11/91";
#endif /* not lint */
/*
* Test driver, to try to tackle the large ugly-split problem.
*/
#include <sys/file.h>
#include <stdio.h>
#include "ndbm.h"
int my_hash(key, len)
char *key;
int len;
{
return(17); /* So I'm cruel... */
}
main(argc, argv)
int argc;
{
DB *db;
DBT key, content;
char keybuf[2049];
char contentbuf[2049];
char buf[256];
int i;
HASHINFO info;
info.bsize = 1024;
info.ffactor = 5;
info.nelem = 1;
info.cachesize = NULL;
#ifdef HASH_ID_PROGRAM_SPECIFIED
info.hash_id = HASH_ID_PROGRAM_SPECIFIED;
info.hash_func = my_hash;
#else
info.hash = my_hash;
#endif
info.lorder = 0;
if (!(db = dbopen("bigtest", O_RDWR | O_CREAT, 0644, DB_HASH, &info))) {
sprintf(buf, "dbopen: failed on file bigtest");
perror(buf);
exit(1);
}
srandom(17);
key.data = keybuf;
content.data = contentbuf;
bzero(keybuf, sizeof(keybuf));
bzero(contentbuf, sizeof(contentbuf));
for (i=1; i <= 500; i++) {
key.size = 128 + (random()&1023);
content.size = 128 + (random()&1023);
/* printf("%d: Key size %d, data size %d\n", i, key.size,
content.size); */
sprintf(keybuf, "Key #%d", i);
sprintf(contentbuf, "Contents #%d", i);
if ((db->put)(db, &key, &content, R_NOOVERWRITE)) {
sprintf(buf, "dbm_store #%d", i);
perror(buf);
}
}
if ((db->close)(db)) {
perror("closing hash file");
exit(1);
}
exit(0);
}

View File

@ -0,0 +1,11 @@
#!/bin/sh
awk '{i++; print $0; print i;}' /usr/share/dict/words > WORDS
ls /bin /usr/bin /usr/ucb /etc | egrep '^(...|....|.....|......)$' | \
sort | uniq | \
awk '{
printf "%s\n", $0
for (i = 0; i < 1000; i++)
printf "%s+", $0
printf "\n"
}' > LONG.DATA

View File

@ -0,0 +1,105 @@
/*-
* Copyright (c) 1991 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
* 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.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)tcreat3.c 5.4 (Berkeley) 9/11/91";
#endif /* not lint */
#include <sys/types.h>
#include <sys/file.h>
#include <stdio.h>
#include <db.h>
#define INITIAL 25000
#define MAXWORDS 25000 /* # of elements in search table */
char wp1[8192];
char wp2[8192];
main(argc, argv)
char **argv;
{
DBT item, key;
DB *dbp;
HASHINFO ctl;
FILE *fp;
int trash;
int i = 0;
argv++;
ctl.hash = NULL;
ctl.bsize = atoi(*argv++);
ctl.ffactor = atoi(*argv++);
ctl.nelem = atoi(*argv++);
ctl.lorder = 0;
if (!(dbp = dbopen( "hashtest",
O_CREAT|O_TRUNC|O_RDWR, 0600, DB_HASH, &ctl))){
/* create table */
fprintf(stderr, "cannot create: hash table (size %d)\n",
INITIAL);
exit(1);
}
key.data = wp1;
item.data = wp2;
while ( fgets(wp1, 8192, stdin) &&
fgets(wp2, 8192, stdin) &&
i++ < MAXWORDS) {
/*
* put info in structure, and structure in the item
*/
key.size = strlen(wp1);
item.size = strlen(wp2);
/*
* enter key/data pair into the table
*/
if ((dbp->put)(dbp, &key, &item, R_NOOVERWRITE) != NULL) {
fprintf(stderr, "cannot enter: key %s\n",
item.data);
exit(1);
}
}
(dbp->close)(dbp);
exit(0);
}

View File

@ -0,0 +1,122 @@
/*-
* Copyright (c) 1991 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
* 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.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)tdel.c 5.5 (Berkeley) 9/11/91";
#endif /* not lint */
#include <sys/types.h>
#include <sys/file.h>
#include <db.h>
#include <stdio.h>
#define INITIAL 25000
#define MAXWORDS 25000 /* # of elements in search table */
/* Usage: thash pagesize fillfactor file */
char wp1[8192];
char wp2[8192];
main(argc, argv)
char **argv;
{
DBT item, key;
DB *dbp;
HASHINFO ctl;
FILE *fp;
int stat;
int i = 0;
argv++;
ctl.nelem = INITIAL;
ctl.hash = NULL;
ctl.bsize = atoi(*argv++);
ctl.ffactor = atoi(*argv++);
ctl.cachesize = 1024 * 1024; /* 1 MEG */
ctl.lorder = 0;
argc -= 2;
if (!(dbp = dbopen( NULL, O_CREAT|O_RDWR, 0400, DB_HASH, &ctl))) {
/* create table */
fprintf(stderr, "cannot create: hash table size %d)\n",
INITIAL);
exit(1);
}
key.data = wp1;
item.data = wp2;
while ( fgets(wp1, 8192, stdin) &&
fgets(wp2, 8192, stdin) &&
i++ < MAXWORDS) {
/*
* put info in structure, and structure in the item
*/
key.size = strlen(wp1);
item.size = strlen(wp2);
/*
* enter key/data pair into the table
*/
if ((dbp->put)(dbp, &key, &item, R_NOOVERWRITE) != NULL) {
fprintf(stderr, "cannot enter: key %s\n",
item.data);
exit(1);
}
}
if ( --argc ) {
fp = fopen ( argv[0], "r");
i = 0;
while ( fgets(wp1, 8192, fp) &&
fgets(wp2, 8192, fp) &&
i++ < MAXWORDS) {
key.size = strlen(wp1);
stat = (dbp->del)(dbp, &key, 0);
if (stat) {
fprintf ( stderr, "Error retrieving %s\n", key.data );
exit(1);
}
}
fclose(fp);
}
(dbp->close)(dbp);
exit(0);
}

View File

@ -0,0 +1,181 @@
#!/bin/csh -f
#
# Copyright (c) 1991 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
# 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.
#
# @(#)testit 5.1 (Berkeley) 9/4/91
#
echo ""
echo "PAGE FILL "
set name=WORDS
set i = 256
foreach j ( 11 14 21 )
./thash4 $i $j 25000 65536 $name < $name
end
set i = 512
foreach j ( 21 28 43 )
./thash4 $i $j 25000 65536 $name < $name
end
set i = 1024
foreach j ( 43 57 85 )
./thash4 $i $j 25000 65536 $name < $name
end
set i = 2048
foreach j ( 85 114 171 )
./thash4 $i $j 25000 65536 $name < $name
end
set i = 4096
foreach j ( 171 228 341 )
./thash4 $i $j 25000 65536 $name < $name
end
set i = 8192
foreach j ( 341 455 683 )
./thash4 $i $j 25000 65536 $name < $name
end
echo "PAGE FILL "
set i = 256
foreach j ( 11 14 21 )
echo "$i"_"$j"
./tcreat3 $i $j 25000 $name < $name
./tread2 65536 < $name
./tverify $name < $name
./tseq > /dev/null
./tdel $i $j $name < $name
end
set i = 512
foreach j ( 21 28 43 )
echo "$i"_"$j"
./tcreat3 $i $j 25000 $name < $name
./tread2 65536 < $name
./tverify $name < $name
./tseq > /dev/null
./tdel $i $j $name < $name
end
set i = 1024
foreach j ( 43 57 85 )
echo "$i"_"$j"
./tcreat3 $i $j 25000 $name < $name
./tread2 65536 < $name
./tverify $name < $name
./tseq > /dev/null
./tdel $i $j $name < $name
end
set i = 2048
foreach j ( 85 114 171 )
echo "$i"_"$j"
./tcreat3 $i $j 25000 $name < $name
./tread2 65536 < $name
./tverify $name < $name
./tseq > /dev/null
./tdel $i $j $name < $name
end
set i = 4096
foreach j ( 171 228 341 )
echo "$i"_"$j"
./tcreat3 $i $j 25000 $name < $name
./tread2 65536 < $name
./tverify $name < $name
./tseq > /dev/null
./tdel $i $j $name < $name
end
set i = 8192
foreach j ( 341 455 683 )
echo "$i"_"$j"
./tcreat3 $i $j 25000 $name < $name
./tread2 65536 < $name
./tverify $name < $name
./tseq > /dev/null
./tdel $i $j $name < $name
end
set name=LONG.DATA
set i = 1024
foreach j ( 1 2 4 )
echo thash4 $i $j 600 65536 $name
./thash4 $i $j 600 65536 $name < $name
end
set i = 2048
foreach j ( 1 2 4 )
echo thash4 $i $j 600 65536 $name
./thash4 $i $j 600 65536 $name < $name
end
set i = 4096
foreach j ( 1 2 4 )
echo thash4 $i $j 600 65536 $name
./thash4 $i $j 600 65536 $name < $name
end
set i = 8192
foreach j ( 2 4 8 )
echo thash4 $i $j 600 65536 $name
./thash4 $i $j 600 65536 $name < $name
end
echo "PAGE FILL "
set i = 1024
foreach j ( 1 2 4 )
echo "$i"_"$j"
tcreat3 $i $j 600 $name < $name
tread2 65536 < $name
tverify $name < $name
tseq > /dev/null
tdel $i $j $name < $name
end
set i = 2048
foreach j ( 1 2 4 )
echo "$i"_"$j"
./tcreat3 $i $j 600 $name < $name
./tread2 65536 < $name
./tverify $name < $name
./tseq > /dev/null
./tdel $i $j $name < $name
end
set i = 4096
foreach j ( 1 2 4 )
echo "$i"_"$j"
./tcreat3 $i $j 600 $name < $name
./tread2 65536 < $name
./tverify $name < $name
./tseq > /dev/null
./tdel $i $j $name < $name
end
set i = 8192
foreach j ( 2 4 8 )
echo "$i"_"$j"
./tcreat3 $i $j 600 $name < $name
./tread2 65536 < $name
./tverify $name < $name
./tseq > /dev/null
./tdel $i $j $name < $name
end
./driver2

View File

@ -0,0 +1,132 @@
/*-
* Copyright (c) 1991 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
* 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.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)thash4.c 5.5 (Berkeley) 9/11/91";
#endif /* not lint */
#include <sys/types.h>
#include <sys/file.h>
#include <sys/timeb.h>
#include <stdio.h>
#include <errno.h>
#include <db.h>
#define INITIAL 25000
#define MAXWORDS 25000 /* # of elements in search table */
/* Usage: thash pagesize fillfactor file */
char wp1[8192];
char wp2[8192];
main(argc, argv)
char **argv;
{
DBT item, key, res;
DB *dbp;
HASHINFO ctl;
FILE *fp;
int stat;
time_t t;
int i = 0;
argv++;
ctl.hash = NULL;
ctl.bsize = atoi(*argv++);
ctl.ffactor = atoi(*argv++);
ctl.nelem = atoi(*argv++);
ctl.cachesize = atoi(*argv++);
ctl.lorder = 0;
if (!(dbp = dbopen( NULL, O_CREAT|O_RDWR, 0400, DB_HASH, &ctl))) {
/* create table */
fprintf(stderr, "cannot create: hash table size %d)\n",
INITIAL);
fprintf(stderr, "\terrno: %d\n", errno);
exit(1);
}
key.data = wp1;
item.data = wp2;
while ( fgets(wp1, 8192, stdin) &&
fgets(wp2, 8192, stdin) &&
i++ < MAXWORDS) {
/*
* put info in structure, and structure in the item
*/
key.size = strlen(wp1);
item.size = strlen(wp2);
/*
* enter key/data pair into the table
*/
if ((dbp->put)(dbp, &key, &item, R_NOOVERWRITE) != NULL) {
fprintf(stderr, "cannot enter: key %s\n",
item.data);
fprintf(stderr, "\terrno: %d\n", errno);
exit(1);
}
}
if ( --argc ) {
fp = fopen ( argv[0], "r");
i = 0;
while ( fgets(wp1, 256, fp) &&
fgets(wp2, 8192, fp) &&
i++ < MAXWORDS) {
key.size = strlen(wp1);
stat = (dbp->get)(dbp, &key, &res, 0);
if (stat < 0 ) {
fprintf ( stderr, "Error retrieving %s\n", key.data );
fprintf(stderr, "\terrno: %d\n", errno);
exit(1);
} else if ( stat > 0 ) {
fprintf ( stderr, "%s not found\n", key.data );
fprintf(stderr, "\terrno: %d\n", errno);
exit(1);
}
}
fclose(fp);
}
dbp->close(dbp);
exit(0);
}

View File

@ -0,0 +1,105 @@
/*-
* Copyright (c) 1991 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
* 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.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)tread2.c 5.5 (Berkeley) 9/11/91";
#endif /* not lint */
#include <sys/types.h>
#include <sys/file.h>
#include <stdio.h>
#include <db.h>
#define INITIAL 25000
#define MAXWORDS 25000 /* # of elements in search table */
typedef struct { /* info to be stored */
int num, siz;
} info;
char wp1[8192];
char wp2[8192];
main(argc, argv)
char **argv;
{
DBT item, key, res;
DB *dbp;
HASHINFO ctl;
int stat;
int i = 0;
ctl.nelem = INITIAL;
ctl.hash = NULL;
ctl.bsize = 64;
ctl.ffactor = 1;
ctl.cachesize = atoi(*argv++);
ctl.lorder = 0;
if (!(dbp = dbopen( "hashtest", O_RDONLY, 0400, DB_HASH, &ctl))) {
/* create table */
fprintf(stderr, "cannot open: hash table\n" );
exit(1);
}
key.data = wp1;
item.data = wp2;
while ( fgets(wp1, 8192, stdin) &&
fgets(wp2, 8192, stdin) &&
i++ < MAXWORDS) {
/*
* put info in structure, and structure in the item
*/
key.size = strlen(wp1);
item.size = strlen(wp2);
stat = (dbp->get)(dbp, &key, &res,0);
if (stat < 0) {
fprintf ( stderr, "Error retrieving %s\n", key.data );
exit(1);
} else if ( stat > 0 ) {
fprintf ( stderr, "%s not found\n", key.data );
exit(1);
}
}
(dbp->close)(dbp);
exit(0);
}

View File

@ -0,0 +1,88 @@
/*-
* Copyright (c) 1991 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
* 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.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)tseq.c 5.4 (Berkeley) 9/11/91";
#endif /* not lint */
#include <sys/types.h>
#include <sys/file.h>
#include <stdio.h>
#include <db.h>
#define INITIAL 25000
#define MAXWORDS 25000 /* # of elements in search table */
char wp[8192];
char cp[8192];
main(argc, argv)
char **argv;
{
DBT item, key, res;
DB *dbp;
FILE *fp;
int stat;
if (!(dbp = dbopen( "hashtest", O_RDONLY, 0400, DB_HASH, NULL))) {
/* create table */
fprintf(stderr, "cannot open: hash table\n" );
exit(1);
}
/*
* put info in structure, and structure in the item
*/
for ( stat = (dbp->seq) (dbp, &res, &item, 1 );
stat == 0;
stat = (dbp->seq) (dbp, &res, &item, 0 ) ) {
bcopy ( res.data, wp, res.size );
wp[res.size] = 0;
bcopy ( item.data, cp, item.size );
cp[item.size] = 0;
printf ( "%s %s\n", wp, cp );
}
(dbp->close)(dbp);
exit(0);
}

View File

@ -0,0 +1,107 @@
/*-
* Copyright (c) 1991 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
* 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.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)tverify.c 5.5 (Berkeley) 9/11/91";
#endif /* not lint */
#include <sys/types.h>
#include <sys/file.h>
#include <stdio.h>
#include <db.h>
#define INITIAL 25000
#define MAXWORDS 25000 /* # of elements in search table */
typedef struct { /* info to be stored */
int num, siz;
} info;
char wp1[8192];
char wp2[8192];
main(argc, argv)
char **argv;
{
DBT key, res;
DB *dbp;
HASHINFO ctl;
int trash;
int stat;
int i = 0;
ctl.nelem = INITIAL;
ctl.hash = NULL;
ctl.bsize = 64;
ctl.ffactor = 1;
ctl.cachesize = 1024 * 1024; /* 1 MEG */
ctl.lorder = 0;
if (!(dbp = dbopen( "hashtest", O_RDONLY, 0400, DB_HASH, &ctl))) {
/* create table */
fprintf(stderr, "cannot open: hash table\n" );
exit(1);
}
key.data = wp1;
while ( fgets(wp1, 8192, stdin) &&
fgets(wp2, 8192, stdin) &&
i++ < MAXWORDS) {
/*
* put info in structure, and structure in the item
*/
key.size = strlen(wp1);
stat = (dbp->get)(dbp, &key, &res,0);
if (stat < 0) {
fprintf ( stderr, "Error retrieving %s\n", key.data );
exit(1);
} else if ( stat > 0 ) {
fprintf ( stderr, "%s not found\n", key.data );
exit(1);
}
if ( memcmp ( res.data, wp2, res.size ) ) {
fprintf ( stderr, "data for %s is incorrect. Data was %s. Should have been %s\n", key.data, res.data, wp2 );
}
}
(dbp->close)(dbp);
exit(0);
}

View File

@ -0,0 +1,647 @@
#!/bin/sh -
#
# Copyright (c) 1992 The Regents of the University of California.
# All rights reserved.
#
# 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.
#
# @(#)run.test 5.17 (Berkeley) 5/22/93
#
# db regression tests
main()
{
DICT=/usr/share/dict/words
PROG=obj/dbtest
TMP1=t1
TMP2=t2
TMP3=t3
test1
test2
test3
test4
test5
test6
test7
test8
test9
test10
test11
test12
test13
test20
rm -f $TMP1 $TMP2 $TMP3
exit 0
}
# Take the first hundred entries in the dictionary, and make them
# be key/data pairs.
test1()
{
printf "Test 1: btree, hash: small key, small data pairs\n"
sed 200q $DICT > $TMP1
for type in btree hash; do
rm -f $TMP2 $TMP3
for i in `sed 200q $DICT`; do
printf "p\nk%s\nd%s\ng\nk%s\n" $i $i $i
done > $TMP2
$PROG -o $TMP3 $type $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test1: type %s: failed\n" $type
exit 1
fi
done
printf "Test 1: recno: small key, small data pairs\n"
rm -f $TMP2 $TMP3
sed 200q $DICT |
awk '{
++i;
printf("p\nk%d\nd%s\ng\nk%d\n", i, $0, i);
}' > $TMP2
$PROG -o $TMP3 recno $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test1: type recno: failed\n"
exit 1
fi
}
# Take the first 200 entries in the dictionary, and give them
# each a medium size data entry.
test2()
{
printf "Test 2: btree, hash: small key, medium data pairs\n"
mdata=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
echo $mdata |
awk '{ for (i = 1; i < 201; ++i) print $0 }' > $TMP1
for type in hash btree; do
rm -f $TMP2 $TMP3
for i in `sed 200q $DICT`; do
printf "p\nk%s\nd%s\ng\nk%s\n" $i $mdata $i
done > $TMP2
$PROG -o $TMP3 $type $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test2: type %s: failed\n" $type
exit 1
fi
done
printf "Test 2: recno: small key, medium data pairs\n"
rm -f $TMP2 $TMP3
echo $mdata |
awk '{ for (i = 1; i < 201; ++i)
printf("p\nk%d\nd%s\ng\nk%d\n", i, $0, i);
}' > $TMP2
$PROG -o $TMP3 recno $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test2: type recno: failed\n"
exit 1
fi
}
# Insert the programs in /bin with their paths as their keys.
test3()
{
printf "Test 3: hash: small key, big data pairs\n"
rm -f $TMP1
(find /bin -type f -print | xargs cat) > $TMP1
for type in hash; do
rm -f $TMP2 $TMP3
for i in `find /bin -type f -print`; do
printf "p\nk%s\nD%s\ng\nk%s\n" $i $i $i
done > $TMP2
$PROG -o $TMP3 $type $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test3: %s: page size %d: failed\n" \
$type $psize
exit 1
fi
done
printf "Test 3: btree: small key, big data pairs\n"
for psize in 512 16384 65536; do
printf "\tpage size %d\n" $psize
for type in btree; do
rm -f $TMP2 $TMP3
for i in `find /bin -type f -print`; do
printf "p\nk%s\nD%s\ng\nk%s\n" $i $i $i
done > $TMP2
$PROG -i psize=$psize -o $TMP3 $type $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test3: %s: page size %d: failed\n" \
$type $psize
exit 1
fi
done
done
printf "Test 3: recno: big data pairs\n"
rm -f $TMP2 $TMP3
find /bin -type f -print |
awk '{
++i;
printf("p\nk%d\nD%s\ng\nk%d\n", i, $0, i);
}' > $TMP2
for psize in 512 16384 65536; do
printf "\tpage size %d\n" $psize
$PROG -i psize=$psize -o $TMP3 recno $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test3: recno: page size %d: failed\n" $psize
exit 1
fi
done
}
# Do random recno entries.
test4()
{
printf "Test 4: recno: random entries\n"
echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
awk '{
for (i = 37; i <= 37 + 88 * 17; i += 17)
printf("input key %d: %.*s\n", i, i % 41, $0);
for (i = 1; i <= 15; ++i)
printf("input key %d: %.*s\n", i, i % 41, $0);
for (i = 19234; i <= 19234 + 61 * 27; i += 27)
printf("input key %d: %.*s\n", i, i % 41, $0);
exit
}' > $TMP1
rm -f TMP2 $TMP3
cat $TMP1 |
awk 'BEGIN {
i = 37;
incr = 17;
}
{
printf("p\nk%d\nd%s\n", i, $0);
if (i == 19234 + 61 * 27)
exit;
if (i == 37 + 88 * 17) {
i = 1;
incr = 1;
} else if (i == 15) {
i = 19234;
incr = 27;
} else
i += incr;
}
END {
for (i = 37; i <= 37 + 88 * 17; i += 17)
printf("g\nk%d\n", i);
for (i = 1; i <= 15; ++i)
printf("g\nk%d\n", i);
for (i = 19234; i <= 19234 + 61 * 27; i += 27)
printf("g\nk%d\n", i);
}' > $TMP2
$PROG -o $TMP3 recno $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test4: type recno: failed\n"
exit 1
fi
}
# Do reverse order recno entries.
test5()
{
printf "Test 5: recno: reverse order entries\n"
echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
awk ' {
for (i = 1500; i; --i)
printf("input key %d: %.*s\n", i, i % 34, $0);
exit;
}' > $TMP1
rm -f TMP2 $TMP3
cat $TMP1 |
awk 'BEGIN {
i = 1500;
}
{
printf("p\nk%d\nd%s\n", i, $0);
--i;
}
END {
for (i = 1500; i; --i)
printf("g\nk%d\n", i);
}' > $TMP2
$PROG -o $TMP3 recno $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test5: type recno: failed\n"
exit 1
fi
}
# Do alternating order recno entries.
test6()
{
printf "Test 6: recno: alternating order entries\n"
echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
awk ' {
for (i = 1; i < 1200; i += 2)
printf("input key %d: %.*s\n", i, i % 34, $0);
for (i = 2; i < 1200; i += 2)
printf("input key %d: %.*s\n", i, i % 34, $0);
exit;
}' > $TMP1
rm -f TMP2 $TMP3
cat $TMP1 |
awk 'BEGIN {
i = 1;
even = 0;
}
{
printf("p\nk%d\nd%s\n", i, $0);
i += 2;
if (i >= 1200) {
if (even == 1)
exit;
even = 1;
i = 2;
}
}
END {
for (i = 1; i < 1200; ++i)
printf("g\nk%d\n", i);
}' > $TMP2
$PROG -o $TMP3 recno $TMP2
sort -o $TMP1 $TMP1
sort -o $TMP3 $TMP3
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test6: type recno: failed\n"
exit 1
fi
}
# Delete cursor record
test7()
{
printf "Test 7: btree, recno: delete cursor record\n"
echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
awk '{
for (i = 1; i <= 120; ++i)
printf("%05d: input key %d: %s\n", i, i, $0);
printf("%05d: input key %d: %s\n", 120, 120, $0);
printf("get failed, no such key\n");
printf("%05d: input key %d: %s\n", 1, 1, $0);
printf("%05d: input key %d: %s\n", 2, 2, $0);
exit;
}' > $TMP1
rm -f TMP2 $TMP3
for type in btree recno; do
cat $TMP1 |
awk '{
if (i == 120)
exit;
printf("p\nk%d\nd%s\n", ++i, $0);
}
END {
printf("fR_NEXT\n");
for (i = 1; i <= 120; ++i)
printf("s\n");
printf("fR_CURSOR\ns\nk120\n");
printf("r\nk120\n");
printf("fR_NEXT\ns\n");
printf("fR_CURSOR\ns\nk1\n");
printf("r\nk1\n");
printf("fR_FIRST\ns\n");
}' > $TMP2
$PROG -o $TMP3 recno $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test7: type $type: failed\n"
exit 1
fi
done
}
# Make sure that overflow pages are reused.
test8()
{
printf "Test 8: btree, hash: repeated small key, big data pairs\n"
rm -f $TMP1
awk 'BEGIN {
for (i = 1; i <= 10; ++i) {
printf("p\nkkey1\nD/bin/sh\n");
printf("p\nkkey2\nD/bin/csh\n");
if (i % 8 == 0) {
printf("c\nkkey2\nD/bin/csh\n");
printf("c\nkkey1\nD/bin/sh\n");
printf("e\t%d of 10 (comparison)\r\n", i);
} else
printf("e\t%d of 10 \r\n", i);
printf("r\nkkey1\nr\nkkey2\n");
}
printf("e\n");
printf("eend of test8 run\n");
}' > $TMP1
$PROG btree $TMP1
$PROG hash $TMP1
# No explicit test for success.
}
# Test btree duplicate keys
test9()
{
printf "Test 9: btree: duplicate keys\n"
echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
awk '{
for (i = 1; i <= 543; ++i)
printf("%05d: input key %d: %s\n", i, i, $0);
exit;
}' > $TMP1
rm -f TMP2 $TMP3
for type in btree; do
cat $TMP1 |
awk '{
if (i++ % 2)
printf("p\nkduplicatekey\nd%s\n", $0);
else
printf("p\nkunique%dkey\nd%s\n", i, $0);
}
END {
printf("o\n");
}' > $TMP2
$PROG -iflags=1 -o $TMP3 $type $TMP2
sort -o $TMP3 $TMP3
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test9: type $type: failed\n"
exit 1
fi
done
}
# Test use of cursor flags without initialization
test10()
{
printf "Test 10: btree, recno: test cursor flag use\n"
echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
awk '{
for (i = 1; i <= 20; ++i)
printf("%05d: input key %d: %s\n", i, i, $0);
exit;
}' > $TMP1
rm -f TMP2 $TMP3
# Test that R_CURSOR doesn't succeed before cursor initialized
for type in btree recno; do
cat $TMP1 |
awk '{
if (i == 10)
exit;
printf("p\nk%d\nd%s\n", ++i, $0);
}
END {
printf("fR_CURSOR\nr\nk1\n");
printf("eR_CURSOR SHOULD HAVE FAILED\n");
}' > $TMP2
$PROG -o $TMP3 $type $TMP2 > /dev/null 2>&1
if [ -s $TMP3 ] ; then
printf "Test 10: delete: R_CURSOR SHOULD HAVE FAILED\n"
exit 1
fi
done
for type in btree recno; do
cat $TMP1 |
awk '{
if (i == 10)
exit;
printf("p\nk%d\nd%s\n", ++i, $0);
}
END {
printf("fR_CURSOR\np\nk1\ndsome data\n");
printf("eR_CURSOR SHOULD HAVE FAILED\n");
}' > $TMP2
$PROG -o $TMP3 $type $TMP2 > /dev/null 2>&1
if [ -s $TMP3 ] ; then
printf "Test 10: put: R_CURSOR SHOULD HAVE FAILED\n"
exit 1
fi
done
}
# Test insert in reverse order.
test11()
{
printf "Test 11: recno: reverse order insert\n"
echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
awk '{
for (i = 1; i <= 779; ++i)
printf("%05d: input key %d: %s\n", i, i, $0);
exit;
}' > $TMP1
rm -f TMP2 $TMP3
for type in recno; do
cat $TMP1 |
awk '{
if (i == 0) {
i = 1;
printf("p\nk1\nd%s\n", $0);
printf("%s\n", "fR_IBEFORE");
} else
printf("p\nk1\nd%s\n", $0);
}
END {
printf("or\n");
}' > $TMP2
$PROG -o $TMP3 $type $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test11: type $type: failed\n"
exit 1
fi
done
}
# Take the first 20000 entries in the dictionary, reverse them, and give
# them each a small size data entry. Use a small page size to make sure
# the btree split code gets hammered.
test12()
{
printf "Test 12: btree: lots of keys, small page size\n"
mdata=abcdefghijklmnopqrstuvwxy
echo $mdata |
awk '{ for (i = 1; i < 20001; ++i) print $0 }' > $TMP1
for type in btree; do
rm -f $TMP2 $TMP3
for i in `sed 20000q $DICT | rev`; do
printf "p\nk%s\nd%s\ng\nk%s\n" $i $mdata $i
done > $TMP2
$PROG -i psize=512 -o $TMP3 $type $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test12: type %s: failed\n" $type
exit 1
fi
done
}
# Test different byte orders.
test13()
{
printf "Test 13: btree, hash: differing byte orders\n"
sed 50q $DICT > $TMP1
for order in 1234 4321; do
for type in btree hash; do
rm -f byte.file $TMP2 $TMP3
for i in `sed 50q $DICT`; do
printf "p\nk%s\nd%s\ng\nk%s\n" $i $i $i
done > $TMP2
$PROG -ilorder=$order -f byte.file -o $TMP3 $type $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test13: %s/%s put failed\n" $type $order
exit 1
fi
for i in `sed 50q $DICT`; do
printf "g\nk%s\n" $i
done > $TMP2
$PROG -ilorder=$order -f byte.file -o $TMP3 $type $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test13: %s/%s get failed\n" $type $order
exit 1
fi
done
done
rm -f byte.file
}
# Try a variety of bucketsizes and fill factors for hashing
test20()
{
printf\
"Test 20: hash: bucketsize, fill factor; nelem 25000 cachesize 65536\n"
awk 'BEGIN {
for (i = 1; i <= 10000; ++i)
printf("%.*s\n", i % 34,
"abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg");
}' > $TMP1
sed 10000q $DICT |
awk '{
++i;
printf("p\nk%s\nd%.*s\n", $0, i % 34,
"abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg");
}' > $TMP2
sed 10000q $DICT |
awk '{
++i;
printf("g\nk%s\n", $0);
}' >> $TMP2
bsize=256
for ffactor in 11 14 21; do
printf "\tbucketsize %d, fill factor %d\n" $bsize $ffactor
$PROG -o$TMP3 \
-ibsize=$bsize,ffactor=$ffactor,nelem=25000,cachesize=65536\
hash $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test20: type hash:\
bsize=$bsize ffactor=$ffactor nelem=25000 cachesize=65536 failed\n"
exit 1
fi
done
bsize=512
for ffactor in 21 28 43; do
printf "\tbucketsize %d, fill factor %d\n" $bsize $ffactor
$PROG -o$TMP3 \
-ibsize=$bsize,ffactor=$ffactor,nelem=25000,cachesize=65536\
hash $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test20: type hash:\
bsize=$bsize ffactor=$ffactor nelem=25000 cachesize=65536 failed\n"
exit 1
fi
done
bsize=1024
for ffactor in 43 57 85; do
printf "\tbucketsize %d, fill factor %d\n" $bsize $ffactor
$PROG -o$TMP3 \
-ibsize=$bsize,ffactor=$ffactor,nelem=25000,cachesize=65536\
hash $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test20: type hash:\
bsize=$bsize ffactor=$ffactor nelem=25000 cachesize=65536 failed\n"
exit 1
fi
done
bsize=2048
for ffactor in 85 114 171; do
printf "\tbucketsize %d, fill factor %d\n" $bsize $ffactor
$PROG -o$TMP3 \
-ibsize=$bsize,ffactor=$ffactor,nelem=25000,cachesize=65536\
hash $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test20: type hash:\
bsize=$bsize ffactor=$ffactor nelem=25000 cachesize=65536 failed\n"
exit 1
fi
done
bsize=4096
for ffactor in 171 228 341; do
printf "\tbucketsize %d, fill factor %d\n" $bsize $ffactor
$PROG -o$TMP3 \
-ibsize=$bsize,ffactor=$ffactor,nelem=25000,cachesize=65536\
hash $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test20: type hash:\
bsize=$bsize ffactor=$ffactor nelem=25000 cachesize=65536 failed\n"
exit 1
fi
done
bsize=8192
for ffactor in 341 455 683; do
printf "\tbucketsize %d, fill factor %d\n" $bsize $ffactor
$PROG -o$TMP3 \
-ibsize=$bsize,ffactor=$ffactor,nelem=25000,cachesize=65536\
hash $TMP2
if (cmp -s $TMP1 $TMP3) ; then :
else
printf "test20: type hash:\
bsize=$bsize ffactor=$ffactor nelem=25000 cachesize=65536 failed\n"
exit 1
fi
done
}
main