s3:lib/memcache: make use of talloc for memcache_elements
[Samba.git] / ctdb / tests / src / ctdb_update_record_persistent.c
bloba0bb383ed52da4f08ff383994306d6f596892b03
1 /*
2 simple ctdb test tool
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/>.
21 #include "includes.h"
22 #include "lib/util/db_wrap.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "cmdline.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));
46 header.rsn++;
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");
50 exit(1);
55 main program
57 int main(int argc, const char *argv[])
59 struct ctdb_context *ctdb;
60 char *test_db = NULL;
61 char *record = NULL;
62 char *value = NULL;
63 struct ctdb_db_context *ctdb_db;
64 struct event_context *ev;
66 struct poptOption popt_options[] = {
67 POPT_AUTOHELP
68 POPT_CTDB_CMDLINE
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" },
72 POPT_TABLEEND
74 int opt;
75 const char **extra_argv;
76 int extra_argc = 0;
77 poptContext pc;
80 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
82 while ((opt = poptGetNextOpt(pc)) != -1) {
83 switch (opt) {
84 default:
85 fprintf(stderr, "Invalid option %s: %s\n",
86 poptBadOption(pc, 0), poptStrerror(opt));
87 exit(1);
91 /* setup the remaining options for the main program to use */
92 extra_argv = poptGetArgs(pc);
93 if (extra_argv) {
94 extra_argv++;
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));
101 if (ctdb == NULL) {
102 exit(1);
105 if (test_db == NULL) {
106 fprintf(stderr, "You must specify the database\n");
107 exit(10);
110 if (record == NULL) {
111 fprintf(stderr, "You must specify the record\n");
112 exit(10);
115 if (value == NULL) {
116 fprintf(stderr, "You must specify the value\n");
117 exit(10);
120 /* attach to a specific database */
121 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(5, 0), test_db, true, 0);
122 if (!ctdb_db) {
123 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
124 exit(1);
127 printf("Waiting for cluster\n");
128 while (1) {
129 uint32_t recmode=1;
130 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
131 if (recmode == 0) break;
132 event_loop_once(ev);
135 update_once(ctdb, ev, ctdb_db, record, value);
137 return 0;