@Override public <T> Observable<List<T>> query(final N1qlQuery query) { return bucket.query(query) .flatMap(aqr -> Observable.zip(aqr.rows().toList(), aqr.errors().toList(), aqr.finalSuccess().singleOrDefault(Boolean.FALSE), (rows, errors, finalSuccess) -> { if (!finalSuccess) { throw new QueryExecutionException( "execution of query resulted in exception: ", Try.apply(() -> errors.get(0)).orElse(null)); } List<T> value = new ArrayList<>(); for (AsyncN1qlQueryRow row : rows) { try { T v = converter.fromBytes(row.byteValue()); value.add(v); } catch (IOException ex) { throw new QueryExecutionException( "execution of query resulted in exception", null, ex); } } return value; })); }
@Override public void run() { Observable.<Integer>empty().singleOrDefault(10).subscribe(new Action1<Integer>() { @Override public void call(Integer integer) { log(integer); } }); } });
@Override public Single convert(Observable<?> result) throws Throwable { return result.singleOrDefault(null).toSingle(); } }
/** * Returns an Observable that emits only the very first item emitted by the source Observable, or a default * item if the source Observable completes without emitting anything. * <p> * <img width="640" height="305" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/firstOrDefault.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code firstOrDefault} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param defaultValue * the default item to emit if the source Observable doesn't emit anything * @return an Observable that emits only the very first item from the source, or a default item if the * source Observable completes without emitting any items * @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#firstordefault">RxJava wiki: firstOrDefault</a> * @see "MSDN: Observable.firstOrDefaultAsync" */ public final Observable<T> firstOrDefault(T defaultValue) { return take(1).singleOrDefault(defaultValue); }
/** * Returns an Observable that emits only the last item emitted by the source Observable, or a default item * if the source Observable completes without emitting any items. * <p> * <img width="640" height="305" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/lastOrDefault.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code lastOrDefault} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param defaultValue * the default item to emit if the source Observable is empty * @return an Observable that emits only the last item emitted by the source Observable, or a default item * if the source Observable is empty * @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#lastordefault">RxJava wiki: lastOrDefault</a> * @see "MSDN: Observable.lastOrDefaultAsync" */ public final Observable<T> lastOrDefault(T defaultValue) { return takeLast(1).singleOrDefault(defaultValue); }
/** * Returns an Observable that emits only the very first item emitted by the source Observable that satisfies * a specified condition, or a default item if the source Observable emits no such items. * <p> * <img width="640" height="310" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/firstOrDefaultN.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code firstOrDefault} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param predicate * the condition any item emitted by the source Observable has to satisfy * @param defaultValue * the default item to emit if the source Observable doesn't emit anything that satisfies the * {@code predicate} * @return an Observable that emits only the very first item emitted by the source Observable that satisfies * the {@code predicate}, or a default item if the source Observable emits no such items * @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#firstordefault">RxJava wiki: firstOrDefault</a> * @see "MSDN: Observable.firstOrDefaultAsync" */ public final Observable<T> firstOrDefault(T defaultValue, Func1<? super T, Boolean> predicate) { return takeFirst(predicate).singleOrDefault(defaultValue); }
@Override public DesignDocument getDesignDocument(final String name, final long timeout, final TimeUnit timeUnit) { return Blocking.blockForSingle( asyncBucketManager.getDesignDocument(name).singleOrDefault(null), timeout, timeUnit ); }
/** * If this {@code BlockingObservable} completes after emitting a single item that matches a predicate, * return that item; if it emits more than one such item, throw an {@code IllegalArgumentException}; if it * emits no items, return a default value. * <p> * <img width="640" height="315" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/B.singleOrDefault.p.png" alt=""> * * @param defaultValue * a default value to return if this {@code BlockingObservable} emits no matching items * @param predicate * a predicate function to evaluate items emitted by this {@code BlockingObservable} * @return the single item emitted by the {@code BlockingObservable} that matches the predicate, or the * default value if no such items are emitted * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Blocking-Observable-Operators#single-and-singleordefault">RxJava Wiki: singleOrDefault()</a> * @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.singleordefault.aspx">MSDN: Observable.SingleOrDefault</a> */ public T singleOrDefault(T defaultValue, Func1<? super T, Boolean> predicate) { return blockForSingle(o.filter(predicate).map(Functions.<T>identity()).singleOrDefault(defaultValue)); }
@Override public DesignDocument getDesignDocument(final String name, final boolean development, final long timeout, final TimeUnit timeUnit) { return Blocking.blockForSingle( asyncBucketManager.getDesignDocument(name, development).singleOrDefault(null), timeout, timeUnit ); }
@Override public BucketSettings getBucket(String name, long timeout, TimeUnit timeUnit) { return Blocking.blockForSingle(asyncClusterManager.getBucket(name).singleOrDefault(null), timeout, timeUnit); }
/** * If this {@code BlockingObservable} completes after emitting a single item, return that item; if it emits * more than one item, throw an {@code IllegalArgumentException}; if it emits no items, return a default * value. * <p> * <img width="640" height="315" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/B.singleOrDefault.png" alt=""> * * @param defaultValue * a default value to return if this {@code BlockingObservable} emits no items * @return the single item emitted by this {@code BlockingObservable}, or the default value if it emits no * items * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Blocking-Observable-Operators#single-and-singleordefault">RxJava Wiki: singleOrDefault()</a> * @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.singleordefault.aspx">MSDN: Observable.SingleOrDefault</a> */ public T singleOrDefault(T defaultValue) { return blockForSingle(o.map(Functions.<T>identity()).singleOrDefault(defaultValue)); }
@Override public JsonDocument document(long timeout, TimeUnit timeUnit) { return Blocking.blockForSingle(asyncViewRow.document().singleOrDefault(null), timeout, timeUnit); }
@Override public <D extends Document<?>> D document(Class<D> target, long timeout, TimeUnit timeUnit) { return Blocking.blockForSingle(asyncViewRow.document(target).singleOrDefault(null), timeout, timeUnit); }
@Override public JsonDocument document(long timeout, TimeUnit timeUnit) { return Blocking.blockForSingle(asyncViewRow.document().singleOrDefault(null), timeout, timeUnit); }
@Override public <D extends Document<?>> D document(Class<D> target, long timeout, TimeUnit timeUnit) { return Blocking.blockForSingle(asyncViewRow.document(target).singleOrDefault(null), timeout, timeUnit); }
/** * Maps a raw {@link ViewQueryResponse} into a {@link AsyncViewResult}. * * @param bucket reference to the bucket. * @param query the original query object. * @param response the response from the server. * @return a converted {@link AsyncViewResult}. */ public static Observable<AsyncViewResult> mapToViewResult(final AsyncBucket bucket, final ViewQuery query, final ViewQueryResponse response) { return response .info() .singleOrDefault(null) .map(new ByteBufToJsonObject()) .map(new BuildViewResult(bucket, query, response)); }
/** * Maps a raw {@link ViewQueryResponse} into a {@link AsyncSpatialViewResult}. * * @param bucket reference to the bucket. * @param query the original query object. * @param response the response from the server. * @return a converted {@link AsyncSpatialViewResult}. */ public static Observable<AsyncSpatialViewResult> mapToSpatialViewResult(final AsyncBucket bucket, final SpatialViewQuery query, final ViewQueryResponse response) { return response .info() .singleOrDefault(null) .map(new ByteBufToJsonObject()) .map(new BuildSpatialViewResult(bucket, query, response)); }
default Observable<Integer> populationOf(String query) { return search(query) .concatMapIterable(SearchResult::getGeonames) .map(Geoname::getPopulation) .filter(p -> p != null) .singleOrDefault(0) .doOnError(th -> log.warn("Falling back to 0 for {}", query, th)) .onErrorReturn(th -> 0) .subscribeOn(Schedulers.io()); }
@Override public Observable<AnalyticsQueryResult> call(final AsyncAnalyticsQueryResult aqr) { final boolean parseSuccess = aqr.parseSuccess(); final String requestId = aqr.requestId(); final String clientContextId = aqr.clientContextId(); return Observable.zip(aqr.signature().singleOrDefault(JsonObject.empty()), aqr.info().singleOrDefault(AnalyticsMetrics.EMPTY_METRICS), aqr.errors().toList(), aqr.status(), aqr.finalSuccess().singleOrDefault(Boolean.FALSE), new Func5<Object, AnalyticsMetrics, List<JsonObject>, String, Boolean, AnalyticsQueryResult>() { @Override public AnalyticsQueryResult call(Object signature, AnalyticsMetrics info, List<JsonObject> errors, String finalStatus, Boolean finalSuccess) { return new DefaultAnalyticsQueryResult(aqr.handle(), signature, info, errors, finalStatus, finalSuccess, parseSuccess, requestId, clientContextId); } }); } };
@Override public Observable<AnalyticsQueryResult> call(AsyncAnalyticsQueryResult aqr) { final boolean parseSuccess = aqr.parseSuccess(); final String requestId = aqr.requestId(); final String clientContextId = aqr.clientContextId(); return Observable.zip(aqr.rows().toList(), aqr.signature().singleOrDefault(JsonObject.empty()), aqr.info().singleOrDefault(AnalyticsMetrics.EMPTY_METRICS), aqr.errors().toList(), aqr.status(), aqr.finalSuccess().singleOrDefault(Boolean.FALSE), new Func6<List<AsyncAnalyticsQueryRow>, Object, AnalyticsMetrics, List<JsonObject>, String, Boolean, AnalyticsQueryResult>() { @Override public AnalyticsQueryResult call(List<AsyncAnalyticsQueryRow> rows, Object signature, AnalyticsMetrics info, List<JsonObject> errors, String finalStatus, Boolean finalSuccess) { return new DefaultAnalyticsQueryResult(rows, signature, info, errors, finalStatus, finalSuccess, parseSuccess, requestId, clientContextId); } }); } };