xref: /openbsd/usr.sbin/ldapd/btest.c (revision 73471bf0)
1 /*	$OpenBSD: btest.c,v 1.3 2017/02/22 14:40:46 gsoares Exp $ */
2 
3 /* Simple test program for the btree database. */
4 /*
5  * Copyright (c) 2009 Martin Hedenfalk <martin@bzero.se>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 
22 #include <err.h>
23 #include <getopt.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "btree.h"
28 
29 int
30 main(int argc, char **argv)
31 {
32 	int		 c, rc = BT_FAIL;
33 	unsigned int	 flags = 0;
34 	struct btree	*bt;
35 	struct cursor	*cursor;
36 	const char	*filename = "test.db";
37 	struct btval	 key, data, maxkey;
38 
39 	while ((c = getopt(argc, argv, "rf:")) != -1) {
40 		switch (c) {
41 		case 'r':
42 			flags |= BT_REVERSEKEY;
43 			break;
44 		case 'f':
45 			filename = optarg;
46 			break;
47 		}
48 	}
49 
50 	argc -= optind;
51 	argv += optind;
52 
53 	if (argc == 0)
54 		errx(1, "missing command");
55 
56 	bt = btree_open(filename, flags | BT_NOSYNC, 0644);
57 	if (bt == NULL)
58 		err(1, filename);
59 
60 	memset(&key, 0, sizeof(key));
61 	memset(&data, 0, sizeof(data));
62 	memset(&maxkey, 0, sizeof(maxkey));
63 
64 	if (strcmp(argv[0], "put") == 0) {
65 		if (argc < 3)
66 			errx(1, "missing arguments");
67 		key.data = argv[1];
68 		key.size = strlen(key.data);
69 		data.data = argv[2];
70 		data.size = strlen(data.data);
71 		rc = btree_put(bt, &key, &data, 0);
72 		if (rc == BT_SUCCESS)
73 			printf("OK\n");
74 		else
75 			printf("FAIL\n");
76 	} else if (strcmp(argv[0], "del") == 0) {
77 		if (argc < 2)
78 			errx(1, "missing argument");
79 		key.data = argv[1];
80 		key.size = strlen(key.data);
81 		rc = btree_del(bt, &key, NULL);
82 		if (rc == BT_SUCCESS)
83 			printf("OK\n");
84 		else
85 			printf("FAIL\n");
86 	} else if (strcmp(argv[0], "get") == 0) {
87 		if (argc < 2)
88 			errx(1, "missing arguments");
89 		key.data = argv[1];
90 		key.size = strlen(key.data);
91 		rc = btree_get(bt, &key, &data);
92 		if (rc == BT_SUCCESS) {
93 			printf("OK %.*s\n", (int)data.size, (char *)data.data);
94 		} else {
95 			printf("FAIL\n");
96 		}
97 	} else if (strcmp(argv[0], "scan") == 0) {
98 		if (argc > 1) {
99 			key.data = argv[1];
100 			key.size = strlen(key.data);
101 			flags = BT_CURSOR;
102 		}
103 		else
104 			flags = BT_FIRST;
105 		if (argc > 2) {
106 			maxkey.data = argv[2];
107 			maxkey.size = strlen(key.data);
108 		}
109 
110 		cursor = btree_cursor_open(bt);
111 		while ((rc = btree_cursor_get(cursor, &key, &data,
112 		    flags)) == BT_SUCCESS) {
113 			if (argc > 2 && btree_cmp(bt, &key, &maxkey) > 0)
114 				break;
115 			printf("OK %zi %.*s\n",
116 			    key.size, (int)key.size, (char *)key.data);
117 			flags = BT_NEXT;
118 		}
119 		btree_cursor_close(cursor);
120 	} else if (strcmp(argv[0], "compact") == 0) {
121 		if ((rc = btree_compact(bt)) != BT_SUCCESS)
122 			warn("compact");
123 	} else if (strcmp(argv[0], "revert") == 0) {
124 		if ((rc = btree_revert(bt)) != BT_SUCCESS)
125 			warn("revert");
126 	} else
127 		errx(1, "%s: invalid command", argv[0]);
128 
129 	btree_close(bt);
130 
131 	return rc;
132 }
133 
134