dbwrap: Use messaging_filtered_read
authorVolker Lendecke <vl@samba.org>
Thu, 24 Apr 2014 09:23:48 +0000 (09:23 +0000)
committerVolker Lendecke <vl@samba.org>
Thu, 8 May 2014 07:10:12 +0000 (09:10 +0200)
This does not really save any code lines, but IMHO the code is simpler
this way. Also, in case we have lots of watchers this will be slightly
cheaper, because we don't have to re-establish a tevent_req.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/lib/dbwrap/dbwrap_watch.c

index 4f3a2b3f059592c870e5a028bdf3f134cd6d195d..a5f1ebd5bffef2556104d006e19642627fb4d8bc 100644 (file)
@@ -235,6 +235,8 @@ struct dbwrap_record_watch_state {
        TDB_DATA w_key;
 };
 
+static bool dbwrap_record_watch_filter(struct messaging_rec *rec,
+                                      void *private_data);
 static void dbwrap_record_watch_done(struct tevent_req *subreq);
 static int dbwrap_record_watch_state_destructor(
        struct dbwrap_record_watch_state *state);
@@ -271,8 +273,8 @@ struct tevent_req *dbwrap_record_watch_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
 
-       subreq = messaging_read_send(state, ev, state->msg,
-                                    MSG_DBWRAP_MODIFIED);
+       subreq = messaging_filtered_read_send(
+               state, ev, state->msg, dbwrap_record_watch_filter, state);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -288,6 +290,21 @@ struct tevent_req *dbwrap_record_watch_send(TALLOC_CTX *mem_ctx,
        return req;
 }
 
+static bool dbwrap_record_watch_filter(struct messaging_rec *rec,
+                                      void *private_data)
+{
+       struct dbwrap_record_watch_state *state = talloc_get_type_abort(
+               private_data, struct dbwrap_record_watch_state);
+
+       if (rec->msg_type != MSG_DBWRAP_MODIFIED) {
+               return false;
+       }
+       if (rec->buf.length != state->w_key.dsize) {
+               return false;
+       }
+       return memcmp(rec->buf.data, state->w_key.dptr, rec->buf.length) == 0;
+}
+
 static int dbwrap_record_watch_state_destructor(
        struct dbwrap_record_watch_state *s)
 {
@@ -351,33 +368,16 @@ static void dbwrap_record_watch_done(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(
                subreq, struct tevent_req);
-       struct dbwrap_record_watch_state *state = tevent_req_data(
-               req, struct dbwrap_record_watch_state);
        struct messaging_rec *rec;
        int ret;
 
-       ret = messaging_read_recv(subreq, talloc_tos(), &rec);
+       ret = messaging_filtered_read_recv(subreq, talloc_tos(), &rec);
        TALLOC_FREE(subreq);
        if (ret != 0) {
                tevent_req_nterror(req, map_nt_error_from_unix(ret));
                return;
        }
-
-       if ((rec->buf.length == state->w_key.dsize) &&
-           (memcmp(rec->buf.data, state->w_key.dptr, rec->buf.length) == 0)) {
-               tevent_req_done(req);
-               return;
-       }
-
-       /*
-        * Not our record, wait for the next one
-        */
-       subreq = messaging_read_send(state, state->ev, state->msg,
-                                    MSG_DBWRAP_MODIFIED);
-       if (tevent_req_nomem(subreq, req)) {
-               return;
-       }
-       tevent_req_set_callback(subreq, dbwrap_record_watch_done, req);
+       tevent_req_done(req);
 }
 
 NTSTATUS dbwrap_record_watch_recv(struct tevent_req *req,