|
| 1 | +/** |
| 2 | + * Copyright (c) Connexta |
| 3 | + * |
| 4 | + * <p>This is free software: you can redistribute it and/or modify it under the terms of the GNU |
| 5 | + * Lesser General Public License as published by the Free Software Foundation, either version 3 of |
| 6 | + * the License, or any later version. |
| 7 | + * |
| 8 | + * <p>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 9 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + * GNU Lesser General Public License for more details. A copy of the GNU Lesser General Public |
| 11 | + * License is distributed along with this program and can be found at |
| 12 | + * <http://www.gnu.org/licenses/lgpl.html>. |
| 13 | + */ |
| 14 | +package com.connexta.replication.api.impl.queue.memory; |
| 15 | + |
| 16 | +import com.connexta.replication.api.data.Task; |
| 17 | +import com.connexta.replication.api.queue.QueueBroker; |
| 18 | +import com.connexta.replication.api.queue.QueueException; |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | +import java.util.stream.Collectors; |
| 23 | +import java.util.stream.Stream; |
| 24 | +import javax.annotation.Nullable; |
| 25 | + |
| 26 | +/** |
| 27 | + * A composite queue is an artifact created around multiple site queues allowing a worker to |
| 28 | + * retrieve tasks from any of the queues based on task priorities. |
| 29 | + */ |
| 30 | +public class MemoryCompositeQueue implements MemoryQueue { |
| 31 | + private final MemoryQueueBroker broker; |
| 32 | + private final Set<String> sites; |
| 33 | + |
| 34 | + public MemoryCompositeQueue(MemoryQueueBroker broker, Stream<String> sites) { |
| 35 | + this.broker = broker; |
| 36 | + this.sites = sites.collect(Collectors.toSet()); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public QueueBroker getBroker() { |
| 41 | + return broker; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Gets all site queues that are compounded together. |
| 46 | + * |
| 47 | + * @return a stream of all site queues compounded together (never empty) |
| 48 | + */ |
| 49 | + public Stream<MemorySiteQueue> queues() { |
| 50 | + return sites.stream().map(broker::getQueueIfDefined).flatMap(Optional::stream); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Gets all sites that have their queues compounded together. |
| 55 | + * |
| 56 | + * @return a stream of all site ids that have their queues compounded together (never empty) |
| 57 | + */ |
| 58 | + public Stream<String> sites() { |
| 59 | + return sites.stream(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets a compounded queue for a given site. |
| 64 | + * |
| 65 | + * @param site the site for which to get a queue that was compounded |
| 66 | + * @return the site queue to use for the specified site or empty if no queue was compounded for |
| 67 | + * the specified site |
| 68 | + */ |
| 69 | + public Optional<MemorySiteQueue> getQueue(String site) { |
| 70 | + return sites.contains(site) ? broker.getQueueIfDefined(site) : Optional.empty(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public int size() { |
| 75 | + return queues().mapToInt(MemorySiteQueue::size).sum(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public int pendingSize() { |
| 80 | + return queues().mapToInt(MemorySiteQueue::pendingSize).sum(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public int activeSize() { |
| 85 | + return queues().mapToInt(MemorySiteQueue::activeSize).sum(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public int remainingCapacity() { |
| 90 | + return queues().mapToInt(MemorySiteQueue::remainingCapacity).sum(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Task take() throws InterruptedException { |
| 95 | + throw new QueueException("not yet supported"); |
| 96 | + } |
| 97 | + |
| 98 | + @Nullable |
| 99 | + @Override |
| 100 | + public Task poll(long timeout, TimeUnit unit) throws InterruptedException { |
| 101 | + throw new QueueException("not yet supported"); |
| 102 | + } |
| 103 | +} |
0 commit comments