ctdb-vacuum: use tdb_parse_record instead of tdb_fetch in delete_queue_traverse()
authorMichael Adam <obnox@samba.org>
Fri, 14 Feb 2014 14:28:22 +0000 (15:28 +0100)
committerAmitay Isaacs <amitay@gmail.com>
Thu, 6 Mar 2014 00:31:11 +0000 (11:31 +1100)
this spares malloc and free

Signed-off-by: Michael Adam <obnox@samba.org>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/server/ctdb_vacuum.c

index 41fd5a3af63ca8900019f31b30e3d191f5cd98c5..a3396f21932c6d39e90fe76eb9e27739e3a3588f 100644 (file)
@@ -209,6 +209,19 @@ static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
 static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
                              struct timeval t, void *private_data);
 
+static int vacuum_record_parser(TDB_DATA key, TDB_DATA data, void *private_data)
+{
+       struct ctdb_ltdb_header *header =
+               (struct ctdb_ltdb_header *)private_data;
+
+       if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
+               return -1;
+       }
+
+       *header = *(struct ctdb_ltdb_header *)data.dptr;
+
+       return 0;
+}
 
 /*
  * traverse function for gathering the records that can be deleted
@@ -466,8 +479,7 @@ static int delete_queue_traverse(void *param, void *data)
        struct ctdb_db_context *ctdb_db = dd->ctdb_db;
        struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
        int res;
-       struct ctdb_ltdb_header *header;
-       TDB_DATA tdb_data;
+       struct ctdb_ltdb_header header;
        uint32_t lmaster;
        uint32_t hash = ctdb_hash(&(dd->key));
 
@@ -483,25 +495,18 @@ static int delete_queue_traverse(void *param, void *data)
                return 0;
        }
 
-       tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
-       if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
-               /* Does not exist or not a ctdb record. Skip. */
-               goto skipped;
-       }
-
-       if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
-               /* The record has been recycled (filled with data). Skip. */
+       res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
+                              vacuum_record_parser, &header);
+       if (res != 0) {
                goto skipped;
        }
 
-       header = (struct ctdb_ltdb_header *)tdb_data.dptr;
-
-       if (header->dmaster != ctdb->pnn) {
+       if (header.dmaster != ctdb->pnn) {
                /* The record has been migrated off the node. Skip. */
                goto skipped;
        }
 
-       if (header->rsn != dd->hdr.rsn) {
+       if (header.rsn != dd->hdr.rsn) {
                /*
                 * The record has been migrated off the node and back again.
                 * But not requeued for deletion. Skip it.
@@ -571,8 +576,6 @@ skipped:
        vdata->fast_skipped++;
 
 done:
-       free(tdb_data.dptr);
-
        tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
 
        return 0;