2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)main.c 8.1 (Berkeley) 6/4/93
37 * $DragonFly: src/lib/libc/db/test/btree.tests/main.c,v 1.3 2003/11/12 18:07:00 eirikn Exp $
40 #include <sys/param.h>
50 typedef struct cmd_table {
54 void (*func) __P((DB *, char **));
55 char *usage, *descrip;
61 void append __P((DB *, char **));
62 void bstat __P((DB *, char **));
63 void cursor __P((DB *, char **));
64 void delcur __P((DB *, char **));
65 void delete __P((DB *, char **));
66 void dump __P((DB *, char **));
67 void first __P((DB *, char **));
68 void get __P((DB *, char **));
69 void help __P((DB *, char **));
70 void iafter __P((DB *, char **));
71 void ibefore __P((DB *, char **));
72 void icursor __P((DB *, char **));
73 void insert __P((DB *, char **));
74 void keydata __P((DBT *, DBT *));
75 void last __P((DB *, char **));
76 void list __P((DB *, char **));
77 void load __P((DB *, char **));
78 void mstat __P((DB *, char **));
79 void next __P((DB *, char **));
80 int parse __P((char *, char **, int));
81 void previous __P((DB *, char **));
82 void show __P((DB *, char **));
83 void usage __P((void));
84 void user __P((DB *));
86 cmd_table commands[] = {
87 "?", 0, 0, help, "help", NULL,
88 "a", 2, 1, append, "append key def", "append key with data def",
89 "b", 0, 0, bstat, "bstat", "stat btree",
90 "c", 1, 1, cursor, "cursor word", "move cursor to word",
91 "delc", 0, 0, delcur, "delcur", "delete key the cursor references",
92 "dele", 1, 1, delete, "delete word", "delete word",
93 "d", 0, 0, dump, "dump", "dump database",
94 "f", 0, 0, first, "first", "move cursor to first record",
95 "g", 1, 1, get, "get key", "locate key",
96 "h", 0, 0, help, "help", "print command summary",
97 "ia", 2, 1, iafter, "iafter key data", "insert data after key",
98 "ib", 2, 1, ibefore, "ibefore key data", "insert data before key",
99 "ic", 2, 1, icursor, "icursor key data", "replace cursor",
100 "in", 2, 1, insert, "insert key def", "insert key with data def",
101 "la", 0, 0, last, "last", "move cursor to last record",
102 "li", 1, 1, list, "list file", "list to a file",
103 "loa", 1, 0, load, "load file", NULL,
104 "loc", 1, 1, get, "get key", NULL,
105 "m", 0, 0, mstat, "mstat", "stat memory pool",
106 "n", 0, 0, next, "next", "move cursor forward one record",
107 "p", 0, 0, previous, "previous", "move cursor back one record",
108 "q", 0, 0, NULL, "quit", "quit",
109 "sh", 1, 0, show, "show page", "dump a page",
113 int recno; /* use record numbers */
114 char *dict = "words"; /* default dictionary */
137 while ((c = getopt(argc, argv, "bc:di:lp:ru")) != EOF) {
140 b.lorder = BIG_ENDIAN;
143 b.cachesize = atoi(optarg);
152 b.lorder = LITTLE_ENDIAN;
155 b.psize = atoi(optarg);
171 db = dbopen(*argv == NULL ? NULL : *argv, O_RDWR,
174 db = dbopen(*argv == NULL ? NULL : *argv, O_CREAT|O_RDWR,
178 (void)fprintf(stderr, "dbopen: %s\n", strerror(errno));
193 char *lbuf, *argv[4], buf[512];
195 if ((ifp = fopen("/dev/tty", "r")) == NULL) {
196 (void)fprintf(stderr,
197 "/dev/tty: %s\n", strerror(errno));
202 (void)fflush(stdout);
203 if ((lbuf = fgets(&buf[0], 512, ifp)) == NULL)
205 if (lbuf[0] == '\n') {
209 lbuf[strlen(lbuf) - 1] = '\0';
214 argc = parse(lbuf, &argv[0], 3);
218 for (i = 0; commands[i].cmd != NULL; i++)
219 if (strncmp(commands[i].cmd, argv[0],
220 strlen(commands[i].cmd)) == 0)
223 if (commands[i].cmd == NULL) {
224 (void)fprintf(stderr,
225 "%s: command unknown ('help' for help)\n", lbuf);
229 if (commands[i].nargs != argc - 1) {
230 (void)fprintf(stderr, "usage: %s\n", commands[i].usage);
234 if (recno && commands[i].rconv) {
235 static recno_t nlong;
236 nlong = atoi(argv[1]);
237 argv[1] = (char *)&nlong;
240 (*commands[i].func)(db, argv);
242 if ((db->sync)(db) == RET_ERROR)
244 else if ((db->close)(db) == RET_ERROR)
249 parse(lbuf, argv, maxargc)
259 while (*c != '\0' && argc < maxargc) {
262 while (!isspace(*c) && *c != '\0') {
280 (void)fprintf(stderr,
281 "append only available for recno db's.\n");
285 key.size = sizeof(recno_t);
287 data.size = strlen(data.data);
288 status = (db->put)(db, &key, &data, R_APPEND);
291 perror("append/put");
294 (void)printf("%s (duplicate key)\n", argv[1]);
311 key.size = sizeof(recno_t);
313 key.size = strlen(argv[1]) + 1;
314 status = (*db->seq)(db, &key, &data, R_CURSOR);
317 perror("cursor/seq");
320 (void)printf("key not found\n");
323 keydata(&key, &data);
335 status = (*db->del)(db, NULL, R_CURSOR);
337 if (status == RET_ERROR)
338 perror("delcur/del");
351 key.size = sizeof(recno_t);
353 key.size = strlen(argv[1]) + 1;
355 status = (*db->del)(db, &key, 0);
358 perror("delete/del");
361 (void)printf("key not found\n");
384 status = (*db->seq)(db, &key, &data, R_FIRST);
391 (void)printf("no more keys\n");
394 keydata(&key, &data);
409 key.size = sizeof(recno_t);
411 key.size = strlen(argv[1]) + 1;
413 status = (*db->get)(db, &key, &data, 0);
420 (void)printf("key not found\n");
423 keydata(&key, &data);
435 for (i = 0; commands[i].cmd; i++)
436 if (commands[i].descrip)
437 (void)printf("%s: %s\n",
438 commands[i].usage, commands[i].descrip);
450 (void)fprintf(stderr,
451 "iafter only available for recno db's.\n");
455 key.size = sizeof(recno_t);
457 data.size = strlen(data.data);
458 status = (db->put)(db, &key, &data, R_IAFTER);
461 perror("iafter/put");
464 (void)printf("%s (duplicate key)\n", argv[1]);
480 (void)fprintf(stderr,
481 "ibefore only available for recno db's.\n");
485 key.size = sizeof(recno_t);
487 data.size = strlen(data.data);
488 status = (db->put)(db, &key, &data, R_IBEFORE);
491 perror("ibefore/put");
494 (void)printf("%s (duplicate key)\n", argv[1]);
511 key.size = sizeof(recno_t);
513 key.size = strlen(argv[1]) + 1;
515 data.size = strlen(argv[2]) + 1;
517 status = (*db->put)(db, &key, &data, R_CURSOR);
520 perror("icursor/put");
523 (void)printf("%s (duplicate key)\n", argv[1]);
540 key.size = sizeof(recno_t);
542 key.size = strlen(argv[1]) + 1;
544 data.size = strlen(argv[2]) + 1;
546 status = (*db->put)(db, &key, &data, R_NOOVERWRITE);
549 perror("insert/put");
552 (void)printf("%s (duplicate key)\n", argv[1]);
567 status = (*db->seq)(db, &key, &data, R_LAST);
574 (void)printf("no more keys\n");
577 keydata(&key, &data);
591 if ((fp = fopen(argv[1], "w")) == NULL) {
592 (void)fprintf(stderr, "%s: %s\n", argv[1], strerror(errno));
595 status = (*db->seq)(db, &key, &data, R_FIRST);
596 while (status == RET_SUCCESS) {
597 (void)fprintf(fp, "%s\n", key.data);
598 status = (*db->seq)(db, &key, &data, R_NEXT);
600 if (status == RET_ERROR)
610 register char *p, *t;
616 char *lp, buf[16 * 1024];
619 if ((fp = fopen(argv[1], "r")) == NULL) {
620 (void)fprintf(stderr, "%s: %s\n", argv[1], strerror(errno));
623 (void)printf("loading %s...\n", argv[1]);
625 for (cnt = 1; (lp = fgetline(fp, &len)) != NULL; ++cnt) {
628 key.size = sizeof(recno_t);
634 for (p = lp + len - 1, t = buf; p >= lp; *t++ = *p--);
640 status = (*db->put)(db, &key, &data, R_NOOVERWRITE);
647 (void)fprintf(stderr,
648 "duplicate: %ld {%s}\n", cnt, data.data);
650 (void)fprintf(stderr,
651 "duplicate: %ld {%s}\n", cnt, key.data);
668 status = (*db->seq)(db, &key, &data, R_NEXT);
675 (void)printf("no more keys\n");
678 keydata(&key, &data);
691 status = (*db->seq)(db, &key, &data, R_PREV);
695 perror("previous/seq");
698 (void)printf("no more keys\n");
701 keydata(&key, &data);
717 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) {
718 (void)printf("getpage of %ld failed\n", pg);
725 mpool_put(t->bt_mp, h, 0);
733 (void)printf("BTREE\n");
742 (void)printf("MPOOL\n");
743 mpool_stat(((BTREE *)db->internal)->bt_mp);
750 if (!recno && key->size > 0)
751 (void)printf("%s/", key->data);
753 (void)printf("%s", data->data);
760 (void)fprintf(stderr,
761 "usage: %s [-bdlu] [-c cache] [-i file] [-p page] [file]\n",