|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 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 | + */ |
| 16 | + |
| 17 | +package rx.internal.operators; |
| 18 | + |
| 19 | +import java.util.Queue; |
| 20 | +import java.util.concurrent.atomic.*; |
| 21 | +import java.util.regex.Pattern; |
| 22 | + |
| 23 | +import rx.*; |
| 24 | +import rx.Observable.OnSubscribe; |
| 25 | +import rx.exceptions.Exceptions; |
| 26 | +import rx.internal.operators.BackpressureUtils; |
| 27 | +import rx.internal.util.atomic.SpscAtomicArrayQueue; |
| 28 | +import rx.internal.util.unsafe.*; |
| 29 | +import rx.plugins.RxJavaHooks; |
| 30 | + |
| 31 | +/** |
| 32 | + * Split a sequence of strings based on a Rexexp pattern spanning subsequent |
| 33 | + * items if necessary. |
| 34 | + */ |
| 35 | +public final class ObservableSplit implements OnSubscribe<String> { |
| 36 | + |
| 37 | + final Observable<String> source; |
| 38 | + |
| 39 | + final Pattern pattern; |
| 40 | + |
| 41 | + final int bufferSize; |
| 42 | + |
| 43 | + public ObservableSplit(Observable<String> source, Pattern pattern, int bufferSize) { |
| 44 | + this.source = source; |
| 45 | + this.pattern = pattern; |
| 46 | + this.bufferSize = bufferSize; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void call(Subscriber<? super String> t) { |
| 51 | + SplitSubscriber parent = new SplitSubscriber(t, pattern, bufferSize); |
| 52 | + t.add(parent.requested); |
| 53 | + t.setProducer(parent.requested); |
| 54 | + |
| 55 | + source.unsafeSubscribe(parent); |
| 56 | + } |
| 57 | + |
| 58 | + static final class SplitSubscriber extends Subscriber<String> { |
| 59 | + |
| 60 | + final Subscriber<? super String> actual; |
| 61 | + |
| 62 | + final Pattern pattern; |
| 63 | + |
| 64 | + final Requested requested; |
| 65 | + |
| 66 | + final AtomicInteger wip; |
| 67 | + |
| 68 | + final int limit; |
| 69 | + |
| 70 | + final Queue<String[]> queue; |
| 71 | + |
| 72 | + String[] current; |
| 73 | + |
| 74 | + String leftOver; |
| 75 | + |
| 76 | + int index; |
| 77 | + |
| 78 | + int produced; |
| 79 | + |
| 80 | + int empty; |
| 81 | + |
| 82 | + volatile boolean done; |
| 83 | + Throwable error; |
| 84 | + |
| 85 | + volatile boolean cancelled; |
| 86 | + |
| 87 | + SplitSubscriber(Subscriber<? super String> actual, Pattern pattern, int bufferSize) { |
| 88 | + this.actual = actual; |
| 89 | + this.pattern = pattern; |
| 90 | + this.limit = bufferSize - (bufferSize >> 2); |
| 91 | + this.requested = new Requested(); |
| 92 | + this.wip = new AtomicInteger(); |
| 93 | + if (UnsafeAccess.isUnsafeAvailable()) { |
| 94 | + this.queue = new SpscArrayQueue<String[]>(bufferSize); |
| 95 | + } else { |
| 96 | + this.queue = new SpscAtomicArrayQueue<String[]>(bufferSize); |
| 97 | + } |
| 98 | + request(bufferSize); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onNext(String t) { |
| 103 | + String lo = leftOver; |
| 104 | + String[] a; |
| 105 | + try { |
| 106 | + if (lo == null || lo.isEmpty()) { |
| 107 | + a = pattern.split(t, -1); |
| 108 | + } else { |
| 109 | + a = pattern.split(lo + t, -1); |
| 110 | + } |
| 111 | + } catch (Throwable ex) { |
| 112 | + Exceptions.throwIfFatal(ex); |
| 113 | + unsubscribe(); |
| 114 | + onError(ex); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if (a.length == 0) { |
| 119 | + leftOver = null; |
| 120 | + request(1); |
| 121 | + return; |
| 122 | + } else |
| 123 | + if (a.length == 1) { |
| 124 | + leftOver = a[0]; |
| 125 | + request(1); |
| 126 | + return; |
| 127 | + } |
| 128 | + leftOver = a[a.length - 1]; |
| 129 | + queue.offer(a); |
| 130 | + drain(); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void onError(Throwable e) { |
| 135 | + if (done) { |
| 136 | + RxJavaHooks.onError(e); |
| 137 | + return; |
| 138 | + } |
| 139 | + String lo = leftOver; |
| 140 | + if (lo != null && !lo.isEmpty()) { |
| 141 | + leftOver = null; |
| 142 | + queue.offer(new String[] { lo, null }); |
| 143 | + } |
| 144 | + error = e; |
| 145 | + done = true; |
| 146 | + drain(); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void onCompleted() { |
| 151 | + if (!done) { |
| 152 | + done = true; |
| 153 | + String lo = leftOver; |
| 154 | + if (lo != null && !lo.isEmpty()) { |
| 155 | + leftOver = null; |
| 156 | + queue.offer(new String[] { lo, null }); |
| 157 | + } |
| 158 | + drain(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + void drain() { |
| 163 | + if (wip.getAndIncrement() != 0) { |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + Queue<String[]> q = queue; |
| 168 | + |
| 169 | + int missed = 1; |
| 170 | + int consumed = produced; |
| 171 | + String[] array = current; |
| 172 | + int idx = index; |
| 173 | + int emptyCount = empty; |
| 174 | + |
| 175 | + Subscriber<? super String> a = actual; |
| 176 | + |
| 177 | + for (;;) { |
| 178 | + long r = requested.get(); |
| 179 | + long e = 0; |
| 180 | + |
| 181 | + while (e != r) { |
| 182 | + if (cancelled) { |
| 183 | + current = null; |
| 184 | + q.clear(); |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + boolean d = done; |
| 189 | + |
| 190 | + if (array == null) { |
| 191 | + array = q.poll(); |
| 192 | + if (array != null) { |
| 193 | + current = array; |
| 194 | + if (++consumed == limit) { |
| 195 | + consumed = 0; |
| 196 | + request(limit); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + boolean empty = array == null; |
| 202 | + |
| 203 | + if (d && empty) { |
| 204 | + current = null; |
| 205 | + Throwable ex = error; |
| 206 | + if (ex != null) { |
| 207 | + a.onError(ex); |
| 208 | + } else { |
| 209 | + a.onCompleted(); |
| 210 | + } |
| 211 | + return; |
| 212 | + } |
| 213 | + |
| 214 | + if (empty) { |
| 215 | + break; |
| 216 | + } |
| 217 | + |
| 218 | + if (array.length == idx + 1) { |
| 219 | + array = null; |
| 220 | + current = null; |
| 221 | + idx = 0; |
| 222 | + continue; |
| 223 | + } |
| 224 | + |
| 225 | + String v = array[idx]; |
| 226 | + |
| 227 | + if (v.isEmpty()) { |
| 228 | + emptyCount++; |
| 229 | + idx++; |
| 230 | + } else { |
| 231 | + while (emptyCount != 0 && e != r) { |
| 232 | + if (cancelled) { |
| 233 | + current = null; |
| 234 | + q.clear(); |
| 235 | + return; |
| 236 | + } |
| 237 | + a.onNext(""); |
| 238 | + e++; |
| 239 | + emptyCount--; |
| 240 | + } |
| 241 | + |
| 242 | + if (e != r && emptyCount == 0) { |
| 243 | + a.onNext(v); |
| 244 | + |
| 245 | + e++; |
| 246 | + idx++; |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + if (e == r) { |
| 252 | + if (cancelled) { |
| 253 | + current = null; |
| 254 | + q.clear(); |
| 255 | + return; |
| 256 | + } |
| 257 | + |
| 258 | + boolean d = done; |
| 259 | + |
| 260 | + if (array == null) { |
| 261 | + array = q.poll(); |
| 262 | + if (array != null) { |
| 263 | + current = array; |
| 264 | + if (++consumed == limit) { |
| 265 | + consumed = 0; |
| 266 | + request(limit); |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + boolean empty = array == null; |
| 272 | + |
| 273 | + if (d && empty) { |
| 274 | + current = null; |
| 275 | + Throwable ex = error; |
| 276 | + if (ex != null) { |
| 277 | + a.onError(ex); |
| 278 | + } else { |
| 279 | + a.onCompleted(); |
| 280 | + } |
| 281 | + return; |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + if (e != 0L) { |
| 286 | + BackpressureUtils.produced(requested, e); |
| 287 | + } |
| 288 | + |
| 289 | + empty = emptyCount; |
| 290 | + produced = consumed; |
| 291 | + missed = wip.addAndGet(-missed); |
| 292 | + if (missed == 0) { |
| 293 | + break; |
| 294 | + } |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + void cancel() { |
| 299 | + cancelled = true; |
| 300 | + unsubscribe(); |
| 301 | + if (wip.getAndIncrement() == 0) { |
| 302 | + current = null; |
| 303 | + queue.clear(); |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + boolean isCancelled() { |
| 308 | + return isUnsubscribed(); |
| 309 | + } |
| 310 | + |
| 311 | + final class Requested extends AtomicLong implements Producer, Subscription { |
| 312 | + |
| 313 | + private static final long serialVersionUID = 3399839515828647345L; |
| 314 | + |
| 315 | + @Override |
| 316 | + public void request(long n) { |
| 317 | + if (n > 0) { |
| 318 | + BackpressureUtils.getAndAddRequest(this, n); |
| 319 | + drain(); |
| 320 | + } |
| 321 | + else if (n < 0) { |
| 322 | + throw new IllegalArgumentException("n >= 0 required but it was " + n); |
| 323 | + } |
| 324 | + } |
| 325 | + |
| 326 | + @Override |
| 327 | + public boolean isUnsubscribed() { |
| 328 | + return isCancelled(); |
| 329 | + } |
| 330 | + |
| 331 | + @Override |
| 332 | + public void unsubscribe() { |
| 333 | + cancel(); |
| 334 | + } |
| 335 | + } |
| 336 | + } |
| 337 | +} |
0 commit comments