]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
replicator: Add replicator_queue_count() and replicator_queue_peek()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Fri, 28 Oct 2022 14:57:03 +0000 (17:57 +0300)
committerMartti Rannanjärvi <martti.rannanjarvi@open-xchange.com>
Wed, 2 Nov 2022 13:53:17 +0000 (15:53 +0200)
These will be useful for the following unit test.

src/replication/replicator/replicator-queue.c
src/replication/replicator/replicator-queue.h

index 69aef700bcac9f738889f1c82ca3e1119e5e89cd..c5abbf6a8557462bc68a3f67ce660fc2cdd35489 100644 (file)
@@ -257,19 +257,26 @@ void replicator_queue_remove(struct replicator_queue *queue,
                queue->change_callback(queue->change_context);
 }
 
+unsigned int replicator_queue_count(struct replicator_queue *queue)
+{
+       return priorityq_count(queue->user_queue);
+}
+
 bool replicator_queue_want_sync_now(struct replicator_user *user,
                                    unsigned int *next_secs_r)
 {
        time_t next_sync = replicator_user_next_sync_time(user);
-       if (next_sync <= ioloop_time)
+       if (next_sync <= ioloop_time) {
+               *next_secs_r = 0;
                return TRUE;
+       }
        *next_secs_r = next_sync - ioloop_time;
        return FALSE;
 }
 
 struct replicator_user *
-replicator_queue_pop(struct replicator_queue *queue,
-                    unsigned int *next_secs_r)
+replicator_queue_peek(struct replicator_queue *queue,
+                     unsigned int *next_secs_r)
 {
        struct priorityq_item *item;
        struct replicator_user *user;
@@ -281,12 +288,25 @@ replicator_queue_pop(struct replicator_queue *queue,
                return NULL;
        }
        user = (struct replicator_user *)item;
-       if (!replicator_queue_want_sync_now(user, next_secs_r)) {
+       (void)replicator_queue_want_sync_now(user, next_secs_r);
+       return user;
+}
+
+struct replicator_user *
+replicator_queue_pop(struct replicator_queue *queue,
+                    unsigned int *next_secs_r)
+{
+       struct replicator_user *user;
+
+       user = replicator_queue_peek(queue, next_secs_r);
+       if (*next_secs_r > 0) {
                /* we don't want to sync the user yet */
                return NULL;
        }
-       priorityq_remove(queue->user_queue, &user->item);
-       user->popped = TRUE;
+       if (user != NULL) {
+               priorityq_remove(queue->user_queue, &user->item);
+               user->popped = TRUE;
+       }
        return user;
 }
 
index 8a8a05ab76dc34c9bdfe79e22956b310b2a80eb7..4e021e20bd8a6a3afe2d0c7e4a2d67011fa88f80 100644 (file)
@@ -64,7 +64,15 @@ void replicator_queue_add_sync_callback(struct replicator_queue *queue,
 /* Remove user from replication queue and free it. */
 void replicator_queue_remove(struct replicator_queue *queue,
                             struct replicator_user **user);
+/* Return the number of users in the queue. */
+unsigned int replicator_queue_count(struct replicator_queue *queue);
 
+/* Return the next user from replication queue and how many seconds from now
+   the returned user should be synced (0 = immediately). Returns NULL only if
+   there are no users in the queue. */
+struct replicator_user *
+replicator_queue_peek(struct replicator_queue *queue,
+                     unsigned int *next_secs_r);
 /* Return the next user from replication queue, and remove it from the queue.
    If there's nothing to be replicated currently, returns NULL and sets
    next_secs_r to when there should be more work to do. */