rename ctdb.h to ctdb_protocol.h
[sahlberg/ctdb.git] / tests / src / ctdb_store.c
1 /* 
2    simple tool to create a lot of records on a tdb and to read them out
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 int num_records = 10;
32 static int base_rec;
33
34 static void store_records(struct ctdb_context *ctdb, struct event_context *ev)
35 {
36         TDB_DATA key, data;
37         struct ctdb_db_context *ctdb_db;
38         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
39         int ret;
40         struct ctdb_record_handle *h;
41         uint32_t i;
42         
43         ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
44
45         printf("creating %d records\n", num_records);
46         for (i=0;i<num_records;i++) {
47                 int r = base_rec + i;
48                 key.dptr = (uint8_t *)&r;
49                 key.dsize = sizeof(uint32_t); 
50
51                 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
52                 if (h == NULL) {
53                         printf("Failed to fetch record '%s' on node %d\n", 
54                                (const char *)key.dptr, ctdb_get_pnn(ctdb));
55                         talloc_free(tmp_ctx);
56                         return;
57                 }
58
59                 data.dptr = (uint8_t *)&i;
60                 data.dsize = sizeof(uint32_t);
61
62                 ret = ctdb_record_store(h, data);
63                 talloc_free(h);
64                 if (ret != 0) {
65                         printf("Failed to store record\n");
66                 }
67                 if (i % 1000 == 0) {
68                         printf("%u\r", i);
69                         fflush(stdout);
70                 }
71         }
72
73         printf("fetching all %d records\n", num_records);
74         while (1) {
75                 for (i=0;i<num_records;i++) {
76                         int r = base_rec + i;
77                         key.dptr = (uint8_t *)&r;
78                         key.dsize = sizeof(uint32_t); 
79
80                         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
81                         if (h == NULL) {
82                                 printf("Failed to fetch record '%s' on node %d\n", 
83                                        (const char *)key.dptr, ctdb_get_pnn(ctdb));
84                                 talloc_free(tmp_ctx);
85                                 return;
86                         }
87                         talloc_free(h);
88                 }
89                 sleep(1);
90                 printf(".");
91                 fflush(stdout);
92         }
93
94         talloc_free(tmp_ctx);
95 }
96
97 /*
98   main program
99 */
100 int main(int argc, const char *argv[])
101 {
102         struct ctdb_context *ctdb;
103         struct ctdb_db_context *ctdb_db;
104
105         struct poptOption popt_options[] = {
106                 POPT_AUTOHELP
107                 POPT_CTDB_CMDLINE
108                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
109                 { "base-rec", 'b', POPT_ARG_INT, &base_rec, 0, "base_rec", "integer" },
110                 POPT_TABLEEND
111         };
112         int opt;
113         const char **extra_argv;
114         int extra_argc = 0;
115         poptContext pc;
116         struct event_context *ev;
117
118         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
119
120         while ((opt = poptGetNextOpt(pc)) != -1) {
121                 switch (opt) {
122                 default:
123                         fprintf(stderr, "Invalid option %s: %s\n", 
124                                 poptBadOption(pc, 0), poptStrerror(opt));
125                         exit(1);
126                 }
127         }
128
129         /* talloc_enable_leak_report_full(); */
130
131         /* setup the remaining options for the main program to use */
132         extra_argv = poptGetArgs(pc);
133         if (extra_argv) {
134                 extra_argv++;
135                 while (extra_argv[extra_argc]) extra_argc++;
136         }
137
138         ev = event_context_init(NULL);
139
140         ctdb = ctdb_cmdline_client(ev);
141
142         /* attach to a specific database */
143         ctdb_db = ctdb_attach(ctdb, "test.tdb", false, 0);
144         if (!ctdb_db) {
145                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
146                 exit(1);
147         }
148
149         printf("Waiting for cluster\n");
150         while (1) {
151                 uint32_t recmode=1;
152                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
153                 if (recmode == 0) break;
154                 event_loop_once(ev);
155         }
156
157         store_records(ctdb, ev);
158
159         return 0;
160 }