reactor.core.publisher.FluxWindowWhen Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson-all Show documentation
Show all versions of redisson-all Show documentation
Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC
/*
* Copyright (c) 2016-2023 VMware Inc. or its affiliates, All Rights Reserved.
*
* 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
*
* https://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 reactor.core.publisher;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.function.Function;
import java.util.function.Supplier;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.core.Disposable;
import reactor.core.Disposables;
import reactor.core.Exceptions;
import reactor.util.annotation.Nullable;
import reactor.util.concurrent.Queues;
import static reactor.core.Exceptions.wrapSource;
/**
* Splits the source sequence into potentially overlapping windowEnds controlled by items
* of a start Publisher and end Publishers derived from the start values.
*
* @param the source value type
* @param the window starter value type
* @param the window end value type (irrelevant)
*
* @see Reactive-Streams-Commons
*/
final class FluxWindowWhen extends InternalFluxOperator> {
final Publisher start;
final Function super U, ? extends Publisher> end;
final Supplier extends Queue> processorQueueSupplier;
FluxWindowWhen(Flux extends T> source,
Publisher start,
Function super U, ? extends Publisher> end,
Supplier extends Queue> processorQueueSupplier) {
super(source);
this.start = Operators.toFluxOrMono(Objects.requireNonNull(start, "start"));
this.end = Objects.requireNonNull(end, "end");
this.processorQueueSupplier =
Objects.requireNonNull(processorQueueSupplier, "processorQueueSupplier");
}
@Override
public int getPrefetch() {
return Integer.MAX_VALUE;
}
@Override
@Nullable
public CoreSubscriber super T> subscribeOrReturn(CoreSubscriber super Flux> actual) {
WindowWhenMainSubscriber main = new WindowWhenMainSubscriber<>(actual,
start, end, processorQueueSupplier);
actual.onSubscribe(main);
if (main.cancelled) {
return null;
}
WindowWhenOpenSubscriber os = new WindowWhenOpenSubscriber<>(main);
if (WindowWhenMainSubscriber.BOUNDARY.compareAndSet(main,null, os)) {
WindowWhenMainSubscriber.OPEN_WINDOW_COUNT.incrementAndGet(main);
start.subscribe(os);
return main;
}
else {
return null;
}
}
@Override
public Object scanUnsafe(Attr key) {
if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;
return super.scanUnsafe(key);
}
static final class WindowWhenMainSubscriber
extends QueueDrainSubscriber> {
final Publisher open;
final Function super U, ? extends Publisher> close;
final Supplier extends Queue> processorQueueSupplier;
final Disposable.Composite resources;
Subscription s;
volatile Disposable boundary;
static final AtomicReferenceFieldUpdater BOUNDARY =
AtomicReferenceFieldUpdater.newUpdater(WindowWhenMainSubscriber.class, Disposable.class, "boundary");
final List> windows;
volatile long openWindowCount;
static final AtomicLongFieldUpdater OPEN_WINDOW_COUNT =
AtomicLongFieldUpdater.newUpdater(WindowWhenMainSubscriber.class, "openWindowCount");
WindowWhenMainSubscriber(CoreSubscriber super Flux> actual,
Publisher open, Function super U, ? extends Publisher> close,
Supplier extends Queue> processorQueueSupplier) {
super(actual, Queues.unboundedMultiproducer().get());
this.open = open;
this.close = close;
this.processorQueueSupplier = processorQueueSupplier;
this.resources = Disposables.composite();
this.windows = new ArrayList<>();
OPEN_WINDOW_COUNT.lazySet(this, 1);
}
@Override
public void onSubscribe(Subscription s) {
if (Operators.validate(this.s, s)) {
this.s = s;
s.request(Long.MAX_VALUE);
}
}
@Override
public void onNext(T t) {
if (done) {
Operators.onNextDropped(t, actual.currentContext());
return;
}
if (fastEnter()) {
for (Sinks.Many w : windows) {
w.emitNext(t, Sinks.EmitFailureHandler.FAIL_FAST);
}
if (leave(-1) == 0) {
return;
}
} else {
queue.offer(t);
if (!enter()) {
return;
}
}
drainLoop();
}
@Override
public void onError(Throwable t) {
if (done) {
Operators.onErrorDropped(t, actual.currentContext());
return;
}
error = t;
done = true;
if (enter()) {
drainLoop();
}
if (OPEN_WINDOW_COUNT.decrementAndGet(this) == 0) {
resources.dispose();
}
}
@Override
public void onComplete() {
if (done) {
return;
}
done = true;
if (enter()) {
drainLoop();
}
if (OPEN_WINDOW_COUNT.decrementAndGet(this) == 0) {
resources.dispose();
}
}
void error(Throwable t) {
s.cancel();
resources.dispose();
OperatorDisposables.dispose(BOUNDARY, this);
actual.onError(t);
}
@Override
public void request(long n) {
requested(n);
}
@Override
public void cancel() {
cancelled = true;
}
void dispose() {
resources.dispose();
OperatorDisposables.dispose(BOUNDARY, this);
}
void drainLoop() {
final Queue
© 2015 - 2024 Weber Informatics LLC | Privacy Policy