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 2 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 "system/filesys.h"
28 enum my_functions {FUNC_SORT=1, FUNC_FETCH=2};
30 static int int_compare(int *i1, int *i2)
36 add an integer into a record in sorted order
38 static int sort_func(struct ctdb_call *call)
40 if (call->call_data == NULL ||
41 call->call_data->dsize != sizeof(int)) {
42 return CTDB_ERR_INVALID;
44 call->new_data = talloc(call, TDB_DATA);
45 if (call->new_data == NULL) {
46 return CTDB_ERR_NOMEM;
48 call->new_data->dptr = talloc_size(call,
49 call->record_data.dsize +
50 call->call_data->dsize);
51 if (call->new_data->dptr == NULL) {
52 return CTDB_ERR_NOMEM;
54 call->new_data->dsize = call->record_data.dsize + call->call_data->dsize;
55 memcpy(call->new_data->dptr+call->record_data.dsize,
56 call->call_data->dptr, call->call_data->dsize);
57 qsort(call->new_data->dptr, call->new_data->dsize / sizeof(int),
58 sizeof(int), (comparison_fn_t)int_compare);
63 ctdb call function to fetch a record
65 static int fetch_func(struct ctdb_call *call)
67 call->reply_data = &call->record_data;
74 int main(int argc, const char *argv[])
76 struct ctdb_context *ctdb;
77 const char *nlist = NULL;
78 const char *myaddress = NULL;
80 struct poptOption popt_options[] = {
82 { "nlist", 0, POPT_ARG_STRING, &nlist, 0, "node list file", "filename" },
83 { "listen", 0, POPT_ARG_STRING, &myaddress, 0, "address to listen on", "address" },
86 const char **extra_argv;
92 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
94 while ((opt = poptGetNextOpt(pc)) != -1) {
97 fprintf(stderr, "Invalid option %s: %s\n",
98 poptBadOption(pc, 0), poptStrerror(opt));
103 /* setup the remaining options for the main program to use */
104 extra_argv = poptGetArgs(pc);
107 while (extra_argv[extra_argc]) extra_argc++;
110 if (nlist == NULL || myaddress == NULL) {
111 printf("You must provide a node list with --nlist and an address with --listen\n");
115 /* initialise ctdb */
116 ctdb = ctdb_init(NULL);
118 printf("Failed to init ctdb\n");
122 /* tell ctdb what address to listen on */
123 ret = ctdb_set_address(ctdb, myaddress);
125 printf("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb));
129 /* tell ctdb what nodes are available */
130 ret = ctdb_set_nlist(ctdb, nlist);
132 printf("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb));
136 /* setup a ctdb call function */
137 ret = ctdb_set_call(ctdb, sort_func, FUNC_SORT);
138 ret = ctdb_set_call(ctdb, fetch_func, FUNC_FETCH);
140 /* attach to a specific database */
141 ret = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
143 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
147 /* start the protocol running */
148 ret = ctdb_start(ctdb);
151 key.dsize = strlen("test")+1;
153 /* add some random data */
154 for (i=0;i<100;i++) {
156 data.dptr = (uint8_t *)&v;
157 data.dsize = sizeof(v);
158 ret = ctdb_call(ctdb, key, FUNC_SORT, &data, NULL);
160 printf("ctdb_call FUNC_SORT failed - %s\n", ctdb_errstr(ctdb));
165 /* fetch the record */
166 ret = ctdb_call(ctdb, key, FUNC_FETCH, NULL, &data);
168 printf("ctdb_call FUNC_FETCH failed - %s\n", ctdb_errstr(ctdb));
172 for (i=0;i<data.dsize/sizeof(int);i++) {
173 printf("%3d\n", ((int *)data.dptr)[i]);