s3: reduce db_ctdb_marshall_loop_next to specialized db_ctdb_marshall_buf_parse
authorVolker Lendecke <vl@samba.org>
Sat, 10 Nov 2012 14:03:35 +0000 (15:03 +0100)
committerMichael Adam <obnox@samba.org>
Thu, 29 Nov 2012 17:00:02 +0000 (18:00 +0100)
now that the db_ctdb_marshall_loop_next_key has been factored out.

Reviewed-by: Michael Adam <obnox@samba.org>
source3/lib/dbwrap/dbwrap_ctdb.c

index dea3dc281c0b42bb7c85cb757685668f7e857f87..642053875f96d26613eb67c6db7f04bda11816f5 100644 (file)
@@ -338,43 +338,22 @@ static struct ctdb_rec_data *db_ctdb_marshall_loop_next_key(
        return r;
 }
 
-/*
-   loop over a marshalling buffer
-
-     - pass r==NULL to start
-     - loop the number of times indicated by m->count
-*/
-static struct ctdb_rec_data *db_ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
-                                                    uint32_t *reqid,
-                                                    struct ctdb_ltdb_header *header,
-                                                    TDB_DATA *key, TDB_DATA *data)
+static bool db_ctdb_marshall_buf_parse(
+       struct ctdb_rec_data *r, uint32_t *reqid,
+       struct ctdb_ltdb_header **header, TDB_DATA *data)
 {
-       r = db_ctdb_marshall_loop_next_key(m, r, key);
-       if (r == NULL) {
-               return NULL;
+       if (r->datalen < sizeof(struct ctdb_ltdb_header)) {
+               return false;
        }
 
-       if (reqid != NULL) {
-               *reqid = r->reqid;
-       }
+       *reqid = r->reqid;
 
-       if (data != NULL) {
-               data->dptr  = &r->data[r->keylen];
-               data->dsize = r->datalen;
-               if (header != NULL) {
-                       data->dptr += sizeof(*header);
-                       data->dsize -= sizeof(*header);
-               }
-       }
+       data->dptr  = &r->data[r->keylen] + sizeof(struct ctdb_ltdb_header);
+       data->dsize = r->datalen - sizeof(struct ctdb_ltdb_header);
 
-       if (header != NULL) {
-               if (r->datalen < sizeof(*header)) {
-                       return NULL;
-               }
-               *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
-       }
+       *header = (struct ctdb_ltdb_header *)&r->data[r->keylen];
 
-       return r;
+       return true;
 }
 
 /**
@@ -460,8 +439,7 @@ static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
                                             TDB_DATA *pdata)
 {
        struct ctdb_rec_data *rec = NULL;
-       struct ctdb_ltdb_header h;
-       bool found = false;
+       struct ctdb_ltdb_header *h = NULL;
        TDB_DATA data;
        int i;
 
@@ -469,9 +447,6 @@ static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
                return false;
        }
 
-       ZERO_STRUCT(h);
-       ZERO_STRUCT(data);
-
        /*
         * Walk the list of records written during this
         * transaction. If we want to read one we have already
@@ -481,26 +456,24 @@ static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
         */
 
        for (i=0; i<buf->count; i++) {
-               TDB_DATA tkey, tdata;
+               TDB_DATA tkey;
                uint32_t reqid;
-               struct ctdb_ltdb_header hdr;
-
-               ZERO_STRUCT(hdr);
 
-               rec = db_ctdb_marshall_loop_next(buf, rec, &reqid, &hdr, &tkey,
-                                                &tdata);
+               rec = db_ctdb_marshall_loop_next_key(buf, rec, &tkey);
                if (rec == NULL) {
                        return false;
                }
 
-               if (tdb_data_equal(key, tkey)) {
-                       found = true;
-                       data = tdata;
-                       h = hdr;
+               if (!tdb_data_equal(key, tkey)) {
+                       continue;
+               }
+
+               if (!db_ctdb_marshall_buf_parse(rec, &reqid, &h, &data)) {
+                       return false;
                }
        }
 
-       if (!found) {
+       if (h == NULL) {
                return false;
        }
 
@@ -514,7 +487,7 @@ static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
        }
 
        if (pheader != NULL) {
-               *pheader = h;
+               *pheader = *h;
        }
 
        return true;