client: add req timeout argument to ctdb_cmdline_client
[sahlberg/ctdb.git] / tests / src / ctdb_traverse.c
1 /* 
2    simple tool to traverse a ctdb database over and over and over
3
4    Copyright (C) Andrew Tridgell  2006
5         Ronnie sahlberg 2007
6
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.
11    
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.
16    
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/>.
19 */
20
21 #include "includes.h"
22 #include "lib/tevent/tevent.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "cmdline.h"
26
27 #include <sys/time.h>
28 #include <time.h>
29
30 static const char *dbname = "test.tdb";
31
32 static int traverse_callback(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA data, void *private_data)
33 {
34         uint32_t *count = private_data;
35         
36         (*count)++;
37         return 0;
38 }
39
40 static void traverse_loop(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, struct event_context *ev)
41 {
42         uint32_t count;
43
44         printf("traversing database\n");
45         count = 0;
46         ctdb_traverse(ctdb_db, traverse_callback, &count);
47         printf("traversed %d records\n", count);
48 }
49
50 /*
51   main program
52 */
53 int main(int argc, const char *argv[])
54 {
55         struct ctdb_context *ctdb;
56         struct ctdb_db_context *ctdb_db;
57
58         struct poptOption popt_options[] = {
59                 POPT_AUTOHELP
60                 POPT_CTDB_CMDLINE
61                 { "database", 0, POPT_ARG_STRING, &dbname, 0, "database to traverse", "name" },
62                 POPT_TABLEEND
63         };
64         int opt;
65         const char **extra_argv;
66         int extra_argc = 0;
67         poptContext pc;
68         struct event_context *ev;
69
70         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
71
72         while ((opt = poptGetNextOpt(pc)) != -1) {
73                 switch (opt) {
74                 default:
75                         fprintf(stderr, "Invalid option %s: %s\n", 
76                                 poptBadOption(pc, 0), poptStrerror(opt));
77                         exit(1);
78                 }
79         }
80
81         /* talloc_enable_leak_report_full(); */
82
83         /* setup the remaining options for the main program to use */
84         extra_argv = poptGetArgs(pc);
85         if (extra_argv) {
86                 extra_argv++;
87                 while (extra_argv[extra_argc]) extra_argc++;
88         }
89
90         ev = event_context_init(NULL);
91
92         ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
93
94         /* attach to a specific database */
95         ctdb_db = ctdb_attach(ctdb, dbname, false, 0);
96         if (!ctdb_db) {
97                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
98                 exit(1);
99         }
100
101         printf("Waiting for cluster\n");
102         while (1) {
103                 uint32_t recmode=1;
104                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
105                 if (recmode == 0) break;
106                 event_loop_once(ev);
107         }
108
109         while (1) {
110                 traverse_loop(ctdb, ctdb_db, ev);
111         }
112
113         return 0;
114 }