blob: 8382851afb8f384edfae98da6a1cd78ad2ed084a [file] [log] [blame]
/*
* Copyright 2018-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.atomix.primitives.impl;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.onosproject.store.service.Task;
import org.onosproject.store.service.WorkQueue;
import org.onosproject.store.service.WorkQueueStats;
/**
* Atomix work queue.
*/
public class AtomixWorkQueue<E> implements WorkQueue<E> {
private final io.atomix.core.workqueue.AsyncWorkQueue<E> atomixWorkQueue;
public AtomixWorkQueue(io.atomix.core.workqueue.AsyncWorkQueue<E> atomixWorkQueue) {
this.atomixWorkQueue = atomixWorkQueue;
}
@Override
public String name() {
return atomixWorkQueue.name();
}
@Override
public CompletableFuture<Void> addMultiple(Collection<E> items) {
return atomixWorkQueue.addMultiple(items);
}
@Override
public CompletableFuture<Collection<Task<E>>> take(int maxItems) {
return atomixWorkQueue.take(maxItems)
.thenApply(tasks -> tasks.stream()
.map(task -> new Task<>(task.taskId(), task.payload()))
.collect(Collectors.toList()));
}
@Override
public CompletableFuture<Void> complete(Collection<String> taskIds) {
return atomixWorkQueue.complete(taskIds);
}
@Override
public CompletableFuture<Void> registerTaskProcessor(
Consumer<E> taskProcessor, int parallelism, Executor executor) {
return atomixWorkQueue.registerTaskProcessor(taskProcessor, parallelism, executor);
}
@Override
public CompletableFuture<Void> stopProcessing() {
return atomixWorkQueue.stopProcessing();
}
@Override
public CompletableFuture<WorkQueueStats> stats() {
return atomixWorkQueue.stats()
.thenApply(stats -> WorkQueueStats.builder()
.withTotalCompleted(stats.totalCompleted())
.withTotalInProgress(stats.totalInProgress())
.withTotalPending(stats.totalPending())
.build());
}
}