2 simple tool to test persistent databases
4 Copyright (C) Andrew Tridgell 2006-2007
5 Copyright (c) Ronnie sahlberg 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
30 static void test_store_records(struct ctdb_context *ctdb, struct event_context *ev)
33 struct ctdb_db_context *ctdb_db;
34 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
36 struct ctdb_record_handle *h;
37 unsigned node=0, count=0;
39 ctdb_db = ctdb_db_handle(ctdb, "persistent.tdb");
41 key.dptr = discard_const("testkey");
42 key.dsize = strlen((const char *)key.dptr)+1;
45 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
47 printf("Failed to fetch record '%s' on node %d\n",
48 (const char *)key.dptr, ctdb_get_pnn(ctdb));
53 printf("Current value: %*.*s\n", (int)data.dsize, (int)data.dsize, data.dptr);
55 if (data.dsize != 0) {
56 if (sscanf((char *)data.dptr, "Node %u Count %u", &node, &count) != 2) {
57 printf("Badly formatted node data!\n");
62 node = ctdb_get_pnn(ctdb);
65 data.dptr = (uint8_t *)talloc_asprintf(h, "Node %u Count %u", node, count);
66 data.dsize = strlen((char *)data.dptr)+1;
68 ret = ctdb_record_store(h, data);
70 DEBUG(0,("Failed to store record\n"));
82 int main(int argc, const char *argv[])
84 struct ctdb_context *ctdb;
85 struct ctdb_db_context *ctdb_db;
87 struct poptOption popt_options[] = {
93 const char **extra_argv;
96 struct event_context *ev;
98 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
100 while ((opt = poptGetNextOpt(pc)) != -1) {
103 fprintf(stderr, "Invalid option %s: %s\n",
104 poptBadOption(pc, 0), poptStrerror(opt));
109 /* setup the remaining options for the main program to use */
110 extra_argv = poptGetArgs(pc);
113 while (extra_argv[extra_argc]) extra_argc++;
116 ev = event_context_init(NULL);
118 ctdb = ctdb_cmdline_client(ev);
120 /* attach to a specific database */
121 ctdb_db = ctdb_attach(ctdb, "persistent.tdb", true);
123 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
127 printf("Waiting for cluster\n");
130 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
131 if (recmode == 0) break;
135 printf("Starting test\n");
136 test_store_records(ctdb, ev);