common/messaging: Abstract db related operations inside db functions
authorAmitay Isaacs <amitay@gmail.com>
Wed, 3 Apr 2013 04:08:14 +0000 (15:08 +1100)
committerAmitay Isaacs <amitay@gmail.com>
Fri, 5 Apr 2013 04:19:53 +0000 (15:19 +1100)
This simplifies the use of message indexdb API and abstracts tdb related code
inside the API.

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
(cherry picked from commit bf7296ce9b98563bcb8426cd035dbeab6d884f59)

common/ctdb_message.c

index e7b00e6c2747a67612b6b6d1bbe06042861d1b73..d164837e5c10c489c886423f45042209f8f387c0 100644 (file)
@@ -42,9 +42,11 @@ static int message_list_db_init(struct ctdb_context *ctdb)
        return 0;
 }
 
-static int message_list_db_add(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA data)
+static int message_list_db_add(struct ctdb_context *ctdb, uint64_t srvid,
+                              struct ctdb_message_list_header *h)
 {
        int ret;
+       TDB_DATA key, data;
 
        if (ctdb->message_list_indexdb == NULL) {
                ret = message_list_db_init(ctdb);
@@ -53,6 +55,12 @@ static int message_list_db_add(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA
                }
        }
 
+       key.dptr = (uint8_t *)&srvid;
+       key.dsize = sizeof(uint64_t);
+
+       data.dptr = (uint8_t *)&h;
+       data.dsize = sizeof(struct ctdb_message_list_header *);
+
        ret = tdb_store(ctdb->message_list_indexdb, key, data, TDB_INSERT);
        if (ret < 0) {
                DEBUG(DEBUG_ERR, ("Failed to add message list handler (%s)\n",
@@ -63,14 +71,18 @@ static int message_list_db_add(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA
        return 0;
 }
 
-static int message_list_db_delete(struct ctdb_context *ctdb, TDB_DATA key)
+static int message_list_db_delete(struct ctdb_context *ctdb, uint64_t srvid)
 {
        int ret;
+       TDB_DATA key;
 
        if (ctdb->message_list_indexdb == NULL) {
                return -1;
        }
 
+       key.dptr = (uint8_t *)&srvid;
+       key.dsize = sizeof(uint64_t);
+
        ret = tdb_delete(ctdb->message_list_indexdb, key);
        if (ret < 0) {
                DEBUG(DEBUG_ERR, ("Failed to delete message list handler (%s)\n",
@@ -81,16 +93,27 @@ static int message_list_db_delete(struct ctdb_context *ctdb, TDB_DATA key)
        return 0;
 }
 
-static int message_list_db_fetch(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA *data)
+static int message_list_db_fetch(struct ctdb_context *ctdb, uint64_t srvid,
+                                struct ctdb_message_list_header **h)
 {
+       TDB_DATA key, data;
+
        if (ctdb->message_list_indexdb == NULL) {
                return -1;
        }
 
-       *data = tdb_fetch(ctdb->message_list_indexdb, key);
-       if (data->dsize == 0) {
+       key.dptr = (uint8_t *)&srvid;
+       key.dsize = sizeof(uint64_t);
+
+       data = tdb_fetch(ctdb->message_list_indexdb, key);
+       if (data.dsize != sizeof(struct ctdb_message_list_header *)) {
+               talloc_free(data.dptr);
                return -1;
        }
+
+       *h = *(struct ctdb_message_list_header **)data.dptr;
+       talloc_free(data.dptr);
+
        return 0;
 }
 
@@ -101,31 +124,18 @@ int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA da
 {
        struct ctdb_message_list_header *h;
        struct ctdb_message_list *m;
-       TDB_DATA key, hdata;
        uint64_t srvid_all = CTDB_SRVID_ALL;
        int ret;
 
-       key.dptr = (uint8_t *)&srvid;
-       key.dsize = sizeof(uint64_t);
-
-       ret = message_list_db_fetch(ctdb, key, &hdata);
+       ret = message_list_db_fetch(ctdb, srvid, &h);
        if (ret == 0) {
-               h = *(struct ctdb_message_list_header **)hdata.dptr;
-               free(hdata.dptr);
-
                for (m=h->m; m; m=m->next) {
                        m->message_handler(ctdb, srvid, data, m->message_private);
                }
        }
 
-       key.dptr = (uint8_t *)&srvid_all;
-       key.dsize = sizeof(uint64_t);
-
-       ret = message_list_db_fetch(ctdb, key, &hdata);
+       ret = message_list_db_fetch(ctdb, srvid_all, &h);
        if (ret == 0) {
-               h = *(struct ctdb_message_list_header **)hdata.dptr;
-               free(hdata.dptr);
-
                for(m=h->m; m; m=m->next) {
                        m->message_handler(ctdb, srvid, data, m->message_private);
                }
@@ -154,7 +164,6 @@ void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr
 static int message_header_destructor(struct ctdb_message_list_header *h)
 {
        struct ctdb_message_list *m;
-       TDB_DATA key;
 
        while (h->m != NULL) {
                m = h->m;
@@ -162,10 +171,7 @@ static int message_header_destructor(struct ctdb_message_list_header *h)
                TALLOC_FREE(m);
        }
 
-       key.dptr = (uint8_t *)&h->srvid;
-       key.dsize = sizeof(uint64_t);
-
-       message_list_db_delete(h->ctdb, key);
+       message_list_db_delete(h->ctdb, h->srvid);
        DLIST_REMOVE(h->ctdb->message_list_header, h);
 
        return 0;
@@ -196,7 +202,6 @@ int ctdb_register_message_handler(struct ctdb_context *ctdb,
 {
        struct ctdb_message_list_header *h;
        struct ctdb_message_list *m;
-       TDB_DATA key, data;
        int ret;
 
        m = talloc_zero(mem_ctx, struct ctdb_message_list);
@@ -205,11 +210,8 @@ int ctdb_register_message_handler(struct ctdb_context *ctdb,
        m->message_handler = handler;
        m->message_private = private_data;
 
-       key.dptr = (uint8_t *)&srvid;
-       key.dsize = sizeof(uint64_t);
-
-       ret = message_list_db_fetch(ctdb, key, &data);
-       if (ret < 0) {
+       ret = message_list_db_fetch(ctdb, srvid, &h);
+       if (ret != 0) {
                /* srvid not registered yet */
                h = talloc_zero(ctdb, struct ctdb_message_list_header);
                CTDB_NO_MEMORY(ctdb, h);
@@ -217,9 +219,7 @@ int ctdb_register_message_handler(struct ctdb_context *ctdb,
                h->ctdb = ctdb;
                h->srvid = srvid;
 
-               data.dptr = (uint8_t *)&h;
-               data.dsize = sizeof(struct ctdb_message_list_header *);
-               ret = message_list_db_add(ctdb, key, data);
+               ret = message_list_db_add(ctdb, srvid, h);
                if (ret < 0) {
                        talloc_free(m);
                        talloc_free(h);
@@ -228,9 +228,6 @@ int ctdb_register_message_handler(struct ctdb_context *ctdb,
 
                DLIST_ADD(ctdb->message_list_header, h);
                talloc_set_destructor(h, message_header_destructor);
-       } else {
-               h = *(struct ctdb_message_list_header **)data.dptr;
-               free(data.dptr);
        }
 
        m->h = h;
@@ -247,20 +244,13 @@ int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, v
 {
        struct ctdb_message_list_header *h;
        struct ctdb_message_list *m;
-       TDB_DATA key, data;
        int ret;
 
-       key.dptr = (uint8_t *)&srvid;
-       key.dsize = sizeof(uint64_t);
-
-       ret = message_list_db_fetch(ctdb, key, &data);
-       if (ret < 0) {
+       ret = message_list_db_fetch(ctdb, srvid, &h);
+       if (ret != 0) {
                return -1;
        }
 
-       h = *(struct ctdb_message_list_header **)data.dptr;
-       free(data.dptr);
-
        for (m=h->m; m; m=m->next) {
                if (m->message_private == private_data) {
                        talloc_free(m);
@@ -278,18 +268,10 @@ int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, v
 bool ctdb_check_message_handler(struct ctdb_context *ctdb, uint64_t srvid)
 {
        struct ctdb_message_list_header *h;
-       TDB_DATA key, data;
-
-       key.dptr = (uint8_t *)&srvid;
-       key.dsize = sizeof(uint64_t);
-
-       if (message_list_db_fetch(ctdb, key, &data) < 0) {
-               return false;
-       }
+       int ret;
 
-       h = *(struct ctdb_message_list_header **)data.dptr;
-       free(data.dptr);
-       if (h->m == NULL) {
+       ret = message_list_db_fetch(ctdb, srvid, &h);
+       if (ret != 0 || h->m == NULL) {
                return false;
        }