ctdb-util: Rename db_wrap to tdb_wrap and make it a build subsystem
[Samba.git] / ctdb / tests / src / ctdb_store.c
blob69203434d6e0fc154e34c6a2fec720a5ecda6021
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 "system/filesys.h"
23 #include "popt.h"
24 #include "cmdline.h"
26 #include <sys/time.h>
27 #include <time.h>
29 static int num_records = 10;
30 static int base_rec;
32 static void store_records(struct ctdb_context *ctdb, struct event_context *ev)
34 TDB_DATA key, data;
35 struct ctdb_db_context *ctdb_db;
36 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
37 int ret;
38 struct ctdb_record_handle *h;
39 uint32_t i;
41 ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
43 printf("creating %d records\n", num_records);
44 for (i=0;i<num_records;i++) {
45 int r = base_rec + i;
46 key.dptr = (uint8_t *)&r;
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_pnn(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 int r = base_rec + i;
75 key.dptr = (uint8_t *)&r;
76 key.dsize = sizeof(uint32_t);
78 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
79 if (h == NULL) {
80 printf("Failed to fetch record '%s' on node %d\n",
81 (const char *)key.dptr, ctdb_get_pnn(ctdb));
82 talloc_free(tmp_ctx);
83 return;
85 talloc_free(h);
87 sleep(1);
88 printf(".");
89 fflush(stdout);
92 talloc_free(tmp_ctx);
96 main program
98 int main(int argc, const char *argv[])
100 struct ctdb_context *ctdb;
101 struct ctdb_db_context *ctdb_db;
103 struct poptOption popt_options[] = {
104 POPT_AUTOHELP
105 POPT_CTDB_CMDLINE
106 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
107 { "base-rec", 'b', POPT_ARG_INT, &base_rec, 0, "base_rec", "integer" },
108 POPT_TABLEEND
110 int opt;
111 const char **extra_argv;
112 int extra_argc = 0;
113 poptContext pc;
114 struct event_context *ev;
116 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
118 while ((opt = poptGetNextOpt(pc)) != -1) {
119 switch (opt) {
120 default:
121 fprintf(stderr, "Invalid option %s: %s\n",
122 poptBadOption(pc, 0), poptStrerror(opt));
123 exit(1);
127 /* talloc_enable_leak_report_full(); */
129 /* setup the remaining options for the main program to use */
130 extra_argv = poptGetArgs(pc);
131 if (extra_argv) {
132 extra_argv++;
133 while (extra_argv[extra_argc]) extra_argc++;
136 ev = event_context_init(NULL);
138 ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
140 if (ctdb == NULL) {
141 printf("failed to connect to ctdb daemon.\n");
142 exit(1);
145 /* attach to a specific database */
146 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb", false, 0);
147 if (!ctdb_db) {
148 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
149 exit(1);
152 printf("Waiting for cluster\n");
153 while (1) {
154 uint32_t recmode=1;
155 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
156 if (recmode == 0) break;
157 event_loop_once(ev);
160 store_records(ctdb, ev);
162 return 0;