-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathFlowableCache.java
447 lines (409 loc) · 15.5 KB
/
FlowableCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* Copyright 2016 Netflix, Inc.
*
* 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 io.reactivex.internal.operators.flowable;
import java.util.concurrent.atomic.*;
import org.reactivestreams.*;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.internal.subscriptions.SubscriptionHelper;
import io.reactivex.internal.util.*;
import io.reactivex.plugins.RxJavaPlugins;
/**
* An observable which auto-connects to another observable, caches the elements
* from that observable but allows terminating the connection and completing the cache.
*
* @param <T> the source element type
*/
public final class FlowableCache<T> extends AbstractFlowableWithUpstream<T, T> {
/** The cache and replay state. */
final CacheState<T> state;
final AtomicBoolean once;
/**
* Creates a cached Flowable with a default capacity hint of 16.
* @param <T> the value type
* @param source the source Observable to cache
* @return the CachedObservable instance
*/
public static <T> Flowable<T> from(Flowable<T> source) {
return from(source, 16);
}
/**
* Creates a cached Flowable with the given capacity hint.
* @param <T> the value type
* @param source the source Observable to cache
* @param capacityHint the hint for the internal buffer size
* @return the CachedObservable instance
*/
public static <T> Flowable<T> from(Flowable<T> source, int capacityHint) {
if (capacityHint < 1) {
throw new IllegalArgumentException("capacityHint > 0 required");
}
CacheState<T> state = new CacheState<T>(source, capacityHint);
return RxJavaPlugins.onAssembly(new FlowableCache<T>(source, state));
}
/**
* Private constructor because state needs to be shared between the Observable body and
* the onSubscribe function.
* @param source the upstream source whose signals to cache
* @param state the cache state object performing the caching and replaying
*/
private FlowableCache(Flowable<T> source, CacheState<T> state) {
super(source);
this.state = state;
this.once = new AtomicBoolean();
}
@Override
protected void subscribeActual(Subscriber<? super T> t) {
// we can connect first because we replay everything anyway
ReplaySubscription<T> rp = new ReplaySubscription<T>(t, state);
state.addProducer(rp);
t.onSubscribe(rp);
// we ensure a single connection here to save an instance field of AtomicBoolean in state.
if (!once.get() && once.compareAndSet(false, true)) {
state.connect();
}
// no need to call rp.replay() here because the very first request will trigger it anyway
}
/**
* Check if this cached observable is connected to its source.
* @return true if already connected
*/
/* public */boolean isConnected() {
return state.isConnected;
}
/**
* Returns true if there are observers subscribed to this observable.
* @return true if the cache has Subscribers
*/
/* public */ boolean hasSubscribers() {
return state.producers.length != 0;
}
/**
* Returns the number of events currently cached.
* @return the number of currently cached event count
*/
/* public */ int cachedEventCount() {
return state.size();
}
/**
* Contains the active child producers and the values to replay.
*
* @param <T> the value type of the cached items
*/
static final class CacheState<T> extends LinkedArrayList implements Subscriber<T> {
/** The source observable to connect to. */
final Flowable<? extends T> source;
/** Holds onto the subscriber connected to source. */
final AtomicReference<Subscription> connection = new AtomicReference<Subscription>();
/** Guarded by connection (not this). */
volatile ReplaySubscription<?>[] producers;
/** The default empty array of producers. */
static final ReplaySubscription<?>[] EMPTY = new ReplaySubscription<?>[0];
/** Set to true after connection. */
volatile boolean isConnected;
/**
* Indicates that the source has completed emitting values or the
* Observable was forcefully terminated.
*/
boolean sourceDone;
public CacheState(Flowable<? extends T> source, int capacityHint) {
super(capacityHint);
this.source = source;
this.producers = EMPTY;
}
/**
* Adds a ReplayProducer to the producers array atomically.
* @param p the target ReplaySubscription wrapping a downstream Subscriber with state
*/
public void addProducer(ReplaySubscription<T> p) {
// guarding by connection to save on allocating another object
// thus there are two distinct locks guarding the value-addition and child come-and-go
synchronized (connection) {
ReplaySubscription<?>[] a = producers;
int n = a.length;
ReplaySubscription<?>[] b = new ReplaySubscription<?>[n + 1];
System.arraycopy(a, 0, b, 0, n);
b[n] = p;
producers = b;
}
}
/**
* Removes the ReplayProducer (if present) from the producers array atomically.
* @param p the target ReplaySubscription wrapping a downstream Subscriber with state
*/
public void removeProducer(ReplaySubscription<T> p) {
synchronized (connection) {
ReplaySubscription<?>[] a = producers;
int n = a.length;
int j = -1;
for (int i = 0; i < n; i++) {
if (a[i].equals(p)) {
j = i;
break;
}
}
if (j < 0) {
return;
}
if (n == 1) {
producers = EMPTY;
return;
}
ReplaySubscription<?>[] b = new ReplaySubscription<?>[n - 1];
System.arraycopy(a, 0, b, 0, j);
System.arraycopy(a, j + 1, b, j, n - j - 1);
producers = b;
}
}
@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(connection, s)) {
s.request(Long.MAX_VALUE);
}
}
/**
* Connects the cache to the source.
* Make sure this is called only once.
*/
public void connect() {
source.subscribe(this);
isConnected = true;
}
@Override
public void onNext(T t) {
if (!sourceDone) {
Object o = NotificationLite.next(t);
add(o);
dispatch();
}
}
@Override
public void onError(Throwable e) {
if (!sourceDone) {
sourceDone = true;
Object o = NotificationLite.error(e);
add(o);
SubscriptionHelper.cancel(connection);
dispatch();
}
}
@Override
public void onComplete() {
if (!sourceDone) {
sourceDone = true;
Object o = NotificationLite.complete();
add(o);
SubscriptionHelper.cancel(connection);
dispatch();
}
}
/**
* Signals all known children there is work to do.
*/
void dispatch() {
ReplaySubscription<?>[] a = producers;
for (ReplaySubscription<?> rp : a) {
rp.replay();
}
}
}
/**
* Keeps track of the current request amount and the replay position for a child Subscriber.
*
* @param <T>
*/
static final class ReplaySubscription<T> extends AtomicLong implements Subscription, Disposable {
/** */
private static final long serialVersionUID = -2557562030197141021L;
private static final long CANCELLED = -1;
/** The actual child subscriber. */
final Subscriber<? super T> child;
/** The cache state object. */
final CacheState<T> state;
/**
* Contains the reference to the buffer segment in replay.
* Accessed after reading state.size() and when emitting == true.
*/
Object[] currentBuffer;
/**
* Contains the index into the currentBuffer where the next value is expected.
* Accessed after reading state.size() and when emitting == true.
*/
int currentIndexInBuffer;
/**
* Contains the absolute index up until the values have been replayed so far.
*/
int index;
/** Indicates there is a replay going on; guarded by this. */
boolean emitting;
/** Indicates there were some state changes/replay attempts; guarded by this. */
boolean missed;
public ReplaySubscription(Subscriber<? super T> child, CacheState<T> state) {
this.child = child;
this.state = state;
}
@Override
public void request(long n) {
if (!SubscriptionHelper.validate(n)) {
return;
}
for (;;) {
long r = get();
if (r == CANCELLED) {
return;
}
long u = BackpressureHelper.addCap(r, n);
if (compareAndSet(r, u)) {
replay();
return;
}
}
}
/**
* Updates the request count to reflect values have been produced.
* @param n the produced amount
* @return the current requested amount
*/
public long produced(long n) {
return addAndGet(-n);
}
@Override
public boolean isDisposed() {
return get() == CANCELLED;
}
@Override
public void dispose() {
long r = get();
if (r != CANCELLED) {
r = getAndSet(CANCELLED);
if (r != CANCELLED) {
state.removeProducer(this);
}
}
}
@Override
public void cancel() {
dispose();
}
/**
* Continue replaying available values if there are requests for them.
*/
public void replay() {
// make sure there is only a single thread emitting
synchronized (this) {
if (emitting) {
missed = true;
return;
}
emitting = true;
}
boolean skipFinal = false;
try {
final Subscriber<? super T> child = this.child;
for (;;) {
long r = get();
if (r < 0L) {
skipFinal = true;
return;
}
// read the size, if it is non-zero, we can safely read the head and
// read values up to the given absolute index
int s = state.size();
if (s != 0) {
Object[] b = currentBuffer;
// latch onto the very first buffer now that it is available.
if (b == null) {
b = state.head();
currentBuffer = b;
}
final int n = b.length - 1;
int j = index;
int k = currentIndexInBuffer;
// eagerly emit any terminal event
if (r == 0) {
Object o = b[k];
if (NotificationLite.isComplete(o)) {
child.onComplete();
skipFinal = true;
dispose();
return;
} else
if (NotificationLite.isError(o)) {
child.onError(NotificationLite.getError(o));
skipFinal = true;
dispose();
return;
}
} else
if (r > 0) {
int valuesProduced = 0;
while (j < s && r > 0) {
if (get() == CANCELLED) {
skipFinal = true;
return;
}
if (k == n) {
b = (Object[])b[n];
k = 0;
}
Object o = b[k];
try {
if (NotificationLite.accept(o, child)) {
skipFinal = true;
dispose();
return;
}
} catch (Throwable err) {
Exceptions.throwIfFatal(err);
skipFinal = true;
dispose();
if (!NotificationLite.isError(o) && !NotificationLite.isComplete(o)) {
child.onError(err);
}
return;
}
k++;
j++;
r--;
valuesProduced++;
}
if (get() == CANCELLED) {
skipFinal = true;
return;
}
index = j;
currentIndexInBuffer = k;
currentBuffer = b;
produced(valuesProduced);
}
}
synchronized (this) {
if (!missed) {
emitting = false;
skipFinal = true;
return;
}
missed = false;
}
}
} finally {
if (!skipFinal) {
synchronized (this) {
emitting = false;
}
}
}
}
}
}