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