4 Copyright (C) Andrew Tridgell 2006
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
27 enum my_functions {FUNC_SORT=1, FUNC_FETCH=2};
29 static int int_compare(int *i1, int *i2)
35 add an integer into a record in sorted order
37 static int sort_func(struct ctdb_call_info *call)
39 if (call->call_data == NULL ||
40 call->call_data->dsize != sizeof(int)) {
41 return CTDB_ERR_INVALID;
43 call->new_data = talloc(call, TDB_DATA);
44 if (call->new_data == NULL) {
45 return CTDB_ERR_NOMEM;
47 call->new_data->dptr = talloc_size(call,
48 call->record_data.dsize +
49 call->call_data->dsize);
50 if (call->new_data->dptr == NULL) {
51 return CTDB_ERR_NOMEM;
53 call->new_data->dsize = call->record_data.dsize + call->call_data->dsize;
54 memcpy(call->new_data->dptr,
55 call->record_data.dptr, call->record_data.dsize);
56 memcpy(call->new_data->dptr+call->record_data.dsize,
57 call->call_data->dptr, call->call_data->dsize);
59 qsort(call->new_data->dptr, call->new_data->dsize / sizeof(int),
60 sizeof(int), (comparison_fn_t)int_compare);
66 ctdb call function to fetch a record
68 static int fetch_func(struct ctdb_call_info *call)
70 call->reply_data = &call->record_data;
77 int main(int argc, const char *argv[])
79 struct ctdb_context *ctdb;
80 struct ctdb_db_context *ctdb_db;
82 struct poptOption popt_options[] = {
88 const char **extra_argv;
92 struct event_context *ev;
93 struct ctdb_call call;
95 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
97 while ((opt = poptGetNextOpt(pc)) != -1) {
100 fprintf(stderr, "Invalid option %s: %s\n",
101 poptBadOption(pc, 0), poptStrerror(opt));
106 /* setup the remaining options for the main program to use */
107 extra_argv = poptGetArgs(pc);
110 while (extra_argv[extra_argc]) extra_argc++;
113 ev = event_context_init(NULL);
115 /* initialise ctdb */
116 ctdb = ctdb_cmdline_init(ev);
118 printf("Failed to init ctdb\n");
122 /* attach to a specific database */
123 ctdb_db = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
125 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
129 /* setup a ctdb call function */
130 ret = ctdb_set_call(ctdb_db, sort_func, FUNC_SORT);
131 ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
133 /* start the protocol running */
134 ret = ctdb_start(ctdb);
136 ctdb_connect_wait(ctdb);
139 call.key.dptr = discard_const("test");
140 call.key.dsize = strlen("test")+1;
142 /* add some random data */
144 int v = random() % 1000;
146 call.call_id = FUNC_SORT;
147 call.call_data.dptr = (uint8_t *)&v;
148 call.call_data.dsize = sizeof(v);
150 ret = ctdb_call(ctdb_db, &call);
152 printf("ctdb_call FUNC_SORT failed - %s\n", ctdb_errstr(ctdb));
157 /* fetch the record */
158 call.call_id = FUNC_FETCH;
159 call.call_data.dptr = NULL;
160 call.call_data.dsize = 0;
162 ret = ctdb_call(ctdb_db, &call);
164 printf("ctdb_call FUNC_FETCH failed - %s\n", ctdb_errstr(ctdb));
168 for (i=0;i<call.reply_data.dsize/sizeof(int);i++) {
169 printf("%3d\n", ((int *)call.reply_data.dptr)[i]);
171 talloc_free(call.reply_data.dptr);
173 /* go into a wait loop to allow other nodes to complete */