3 This test just creates/updates a record in a persistent database
5 Copyright (C) Ronnie Sahlberg 2012
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/>.
22 #include "lib/util/db_wrap.h"
23 #include "system/filesys.h"
26 #include "ctdb_private.h"
29 static void update_once(struct ctdb_context
*ctdb
, struct event_context
*ev
, struct ctdb_db_context
*ctdb_db
, char *record
, char *value
)
31 TDB_DATA key
, data
, olddata
;
32 struct ctdb_ltdb_header header
;
34 memset(&header
, 0, sizeof(header
));
36 key
.dptr
= (uint8_t *)record
;
37 key
.dsize
= strlen(record
);
39 data
.dptr
= (uint8_t *)value
;
40 data
.dsize
= strlen(value
);
42 olddata
= tdb_fetch(ctdb_db
->ltdb
->tdb
, key
);
43 if (olddata
.dsize
!= 0) {
44 memcpy(&header
, olddata
.dptr
, sizeof(header
));
48 if (ctdb_ctrl_updaterecord(ctdb
, ctdb
, timeval_zero(), CTDB_CURRENT_NODE
, ctdb_db
, key
, &header
, data
) != 0) {
49 printf("Failed to update record\n");
57 int main(int argc
, const char *argv
[])
59 struct ctdb_context
*ctdb
;
63 struct ctdb_db_context
*ctdb_db
;
64 struct event_context
*ev
;
66 struct poptOption popt_options
[] = {
69 { "database", 'D', POPT_ARG_STRING
, &test_db
, 0, "database", "string" },
70 { "record", 'R', POPT_ARG_STRING
, &record
, 0, "record", "string" },
71 { "value", 'V', POPT_ARG_STRING
, &value
, 0, "value", "string" },
75 const char **extra_argv
;
80 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
82 while ((opt
= poptGetNextOpt(pc
)) != -1) {
85 fprintf(stderr
, "Invalid option %s: %s\n",
86 poptBadOption(pc
, 0), poptStrerror(opt
));
91 /* setup the remaining options for the main program to use */
92 extra_argv
= poptGetArgs(pc
);
95 while (extra_argv
[extra_argc
]) extra_argc
++;
98 ev
= event_context_init(NULL
);
100 ctdb
= ctdb_cmdline_client(ev
, timeval_current_ofs(5, 0));
105 if (test_db
== NULL
) {
106 fprintf(stderr
, "You must specify the database\n");
110 if (record
== NULL
) {
111 fprintf(stderr
, "You must specify the record\n");
116 fprintf(stderr
, "You must specify the value\n");
120 /* attach to a specific database */
121 ctdb_db
= ctdb_attach(ctdb
, timeval_current_ofs(5, 0), test_db
, true, 0);
123 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb
));
127 printf("Waiting for cluster\n");
130 ctdb_ctrl_getrecmode(ctdb
, ctdb
, timeval_zero(), CTDB_CURRENT_NODE
, &recmode
);
131 if (recmode
== 0) break;
135 update_once(ctdb
, ev
, ctdb_db
, record
, value
);