blob: 8382851afb8f384edfae98da6a1cd78ad2ed084a [file] [log] [blame]
Jordan Halterman00e92da2018-05-22 23:05:52 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Thomas Vachuskab6d31672018-07-27 17:03:46 -070016package org.onosproject.store.atomix.primitives.impl;
Jordan Halterman00e92da2018-05-22 23:05:52 -070017
18import java.util.Collection;
19import java.util.concurrent.CompletableFuture;
20import java.util.concurrent.Executor;
21import java.util.function.Consumer;
22import java.util.stream.Collectors;
23
24import org.onosproject.store.service.Task;
25import org.onosproject.store.service.WorkQueue;
26import org.onosproject.store.service.WorkQueueStats;
27
28/**
29 * Atomix work queue.
30 */
31public class AtomixWorkQueue<E> implements WorkQueue<E> {
32 private final io.atomix.core.workqueue.AsyncWorkQueue<E> atomixWorkQueue;
33
34 public AtomixWorkQueue(io.atomix.core.workqueue.AsyncWorkQueue<E> atomixWorkQueue) {
35 this.atomixWorkQueue = atomixWorkQueue;
36 }
37
38 @Override
39 public String name() {
40 return atomixWorkQueue.name();
41 }
42
43 @Override
44 public CompletableFuture<Void> addMultiple(Collection<E> items) {
45 return atomixWorkQueue.addMultiple(items);
46 }
47
48 @Override
49 public CompletableFuture<Collection<Task<E>>> take(int maxItems) {
50 return atomixWorkQueue.take(maxItems)
51 .thenApply(tasks -> tasks.stream()
52 .map(task -> new Task<>(task.taskId(), task.payload()))
53 .collect(Collectors.toList()));
54 }
55
56 @Override
57 public CompletableFuture<Void> complete(Collection<String> taskIds) {
58 return atomixWorkQueue.complete(taskIds);
59 }
60
61 @Override
62 public CompletableFuture<Void> registerTaskProcessor(
63 Consumer<E> taskProcessor, int parallelism, Executor executor) {
64 return atomixWorkQueue.registerTaskProcessor(taskProcessor, parallelism, executor);
65 }
66
67 @Override
68 public CompletableFuture<Void> stopProcessing() {
69 return atomixWorkQueue.stopProcessing();
70 }
71
72 @Override
73 public CompletableFuture<WorkQueueStats> stats() {
74 return atomixWorkQueue.stats()
75 .thenApply(stats -> WorkQueueStats.builder()
76 .withTotalCompleted(stats.totalCompleted())
77 .withTotalInProgress(stats.totalInProgress())
78 .withTotalPending(stats.totalPending())
79 .build());
80 }
81}