add a function ctdb_writerecord() to write a record to the database.
[sahlberg/ctdb.git] / libctdb / tst.c
index dc030fe683e7a6015a976a016dc7a8ac89bf40f1..452ebe1a1c0c69b6c2cb7259cc2150ff50993bdc 100644 (file)
@@ -7,6 +7,8 @@
 #include "lib/tdb/include/tdb.h"
 #include "include/ctdb.h"
 
+TDB_DATA key;
+
 void msg_h(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data, void *private_data)
 {
        printf("Message received on port %d : %s\n", (int)srvid, data.dptr);
@@ -23,12 +25,46 @@ void rm_cb(int32_t status, int32_t recmaster, void *private_data)
        printf("status:%d recmaster:%d\n", status, recmaster);
 }
 
-void adb_cb(int32_t status, struct ctdb_db_context *ctdb_db, void *private_data)
+/*
+ * example on how to first read(non-existing recortds are implicitely created
+ * on demand) a record and change it in the callback.
+ * This forms the atom for the read-modify-write cycle.
+ *
+ * Pure read, or pure write are just special cases of this cycle.
+ */
+void rrl_cb(int32_t status, ctdb_handle *handle, TDB_DATA outdata, void *private_data)
 {
-       printf("status:%d db:%p\n", status, ctdb_db);
+       TDB_DATA data;
+       char tmp[256];
+
+       if (status != 0) {
+               printf("rrl_cb returned error: status %d\n", status);
+               if (handle) {
+                       ctdb_free(handle);
+               }
+               return;
+       }
+
+       printf("rrl size:%d data:%s\n", outdata.dsize, outdata.dptr);
+       if (outdata.dsize == 0) {
+               tmp[0] = 0;
+       } else {
+               strcpy(tmp, outdata.dptr);
+       }
+       strcat(tmp, "*");
+
+       data.dptr  = tmp;
+       data.dsize = strlen(tmp) + 1;
+       ctdb_writerecord(handle, key, data);
+
+       printf("Wrote new record : %s\n", tmp);
+
+       ctdb_free(handle);
 }
 
 
+
+
 int main(int argc, char *argv[])
 {
        struct ctdb_context *ctdb_context;
@@ -38,6 +74,9 @@ int main(int argc, char *argv[])
        int ret;
        TDB_DATA msg;
 
+       key.dptr  = "Test Record";
+       key.dsize = strlen(key.dptr);
+
        ctdb_context = ctdb_connect("/tmp/ctdb.socket");
 
        handle = ctdb_set_message_handler_send(ctdb_context, 55, NULL, msg_h, NULL);
@@ -60,11 +99,17 @@ int main(int argc, char *argv[])
                exit(10);
        }
 
-       handle = ctdb_attachdb_send(ctdb_context, CTDB_CURRENT_NODE, "test_test.tdb", 0, 0, adb_cb, NULL);
+       handle = ctdb_attachdb_send(ctdb_context, "test_test.tdb", 0, 0, NULL, NULL);
        if (handle == NULL) {
                printf("Failed to send attachdb control\n");
                exit(10);
        }
+       ret = ctdb_attachdb_recv(ctdb_context, handle, &ctdb_db_context);
+       if (ret != 0 ) {
+               printf("Failed to attach to database\n");
+               exit(10);
+       }
+
 
        handle = ctdb_getpnn_send(ctdb_context, CTDB_CURRENT_NODE, pnn_cb, NULL);
        if (handle == NULL) {
@@ -72,9 +117,9 @@ int main(int argc, char *argv[])
                exit(10);
        }
 
-       handle = ctdb_getrecmaster_send(ctdb_context, CTDB_CURRENT_NODE, rm_cb, NULL);
+       handle = ctdb_readrecordlock_send(ctdb_context, ctdb_db_context, key, rrl_cb, NULL);
        if (handle == NULL) {
-               printf("Failed to send get_recmaster control\n");
+               printf("Failed to send READRECORDLOCK\n");
                exit(10);
        }