Use a custom init function for samba4 that sets a samba4
[Samba.git] / source4 / cluster / ctdb / tests / ctdb_store.c
blob8b44276d21cb4d5f83f5bd8c17d0cb5e49a8f46e
1 /*
2 simple tool to create a lot of records on a tdb and to read them out
4 Copyright (C) Andrew Tridgell 2006
5 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/>.
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "cmdline.h"
27 #include <sys/time.h>
28 #include <time.h>
30 static int num_records = 10;
33 static void store_records(struct ctdb_context *ctdb, struct event_context *ev)
35 TDB_DATA key, data;
36 struct ctdb_db_context *ctdb_db;
37 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
38 int ret;
39 struct ctdb_record_handle *h;
40 uint32_t i;
42 ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
44 printf("creating %d records\n", num_records);
45 for (i=0;i<num_records;i++) {
46 key.dptr = (uint8_t *)&i;
47 key.dsize = sizeof(uint32_t);
49 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
50 if (h == NULL) {
51 printf("Failed to fetch record '%s' on node %d\n",
52 (const char *)key.dptr, ctdb_get_vnn(ctdb));
53 talloc_free(tmp_ctx);
54 return;
57 data.dptr = (uint8_t *)&i;
58 data.dsize = sizeof(uint32_t);
60 ret = ctdb_record_store(h, data);
61 talloc_free(h);
62 if (ret != 0) {
63 printf("Failed to store record\n");
65 if (i % 1000 == 0) {
66 printf("%u\r", i);
67 fflush(stdout);
71 printf("fetching all %d records\n", num_records);
72 while (1) {
73 for (i=0;i<num_records;i++) {
74 key.dptr = (uint8_t *)&i;
75 key.dsize = sizeof(uint32_t);
77 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
78 if (h == NULL) {
79 printf("Failed to fetch record '%s' on node %d\n",
80 (const char *)key.dptr, ctdb_get_vnn(ctdb));
81 talloc_free(tmp_ctx);
82 return;
84 talloc_free(h);
86 sleep(1);
87 printf(".");
88 fflush(stdout);
91 talloc_free(tmp_ctx);
95 main program
97 int main(int argc, const char *argv[])
99 struct ctdb_context *ctdb;
100 struct ctdb_db_context *ctdb_db;
102 struct poptOption popt_options[] = {
103 POPT_AUTOHELP
104 POPT_CTDB_CMDLINE
105 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
106 POPT_TABLEEND
108 int opt;
109 const char **extra_argv;
110 int extra_argc = 0;
111 poptContext pc;
112 struct event_context *ev;
114 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
116 while ((opt = poptGetNextOpt(pc)) != -1) {
117 switch (opt) {
118 default:
119 fprintf(stderr, "Invalid option %s: %s\n",
120 poptBadOption(pc, 0), poptStrerror(opt));
121 exit(1);
125 /* talloc_enable_leak_report_full(); */
127 /* setup the remaining options for the main program to use */
128 extra_argv = poptGetArgs(pc);
129 if (extra_argv) {
130 extra_argv++;
131 while (extra_argv[extra_argc]) extra_argc++;
134 ev = s4_event_context_init(NULL);
136 ctdb = ctdb_cmdline_client(ev);
138 /* attach to a specific database */
139 ctdb_db = ctdb_attach(ctdb, "test.tdb");
140 if (!ctdb_db) {
141 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
142 exit(1);
145 printf("Waiting for cluster\n");
146 while (1) {
147 uint32_t recmode=1;
148 ctdb_ctrl_getrecmode(ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
149 if (recmode == 0) break;
150 event_loop_once(ev);
153 store_records(ctdb, ev);
155 return 0;