private static int comparatorResult(Long result) { checkCondition( (result != null) && ((result == -1) || (result == 0) || (result == 1)), INVALID_FUNCTION_ARGUMENT, "Lambda comparator must return either -1, 0, or 1"); return result.intValue(); }
@ScalarOperator(OperatorType.CAST) @SqlType("varchar(y)") @LiteralParameters({"x", "y"}) public static Slice charToVarcharCast(@LiteralParameter("x") Long x, @LiteralParameter("y") Long y, @SqlType("char(x)") Slice slice) { if (x.intValue() <= y.intValue()) { return padSpaces(slice, x.intValue()); } return padSpaces(truncateToLength(slice, y.intValue()), y.intValue()); }
@Override protected Object getGreaterValue(Object value) { int bits = ((Long) value).intValue(); float greaterValue = intBitsToFloat(bits) + 0.1f; return Long.valueOf(floatToRawIntBits(greaterValue)); }
@ScalarOperator(OperatorType.CAST) @SqlType("char(y)") @LiteralParameters({"x", "y"}) public static Slice varcharToCharCast(@LiteralParameter("y") Long y, @SqlType("varchar(x)") Slice slice) { return truncateToLengthAndTrimSpaces(slice, y.intValue()); }
@GetMapping(value = "/message-stream", produces = "application/x-protobuf;delimited=true") Flux<Msg> messageStream() { return testInterval(Duration.ofMillis(50), 5).map(l -> Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(l.intValue()).build()).build()); }
@ScalarOperator(OperatorType.CAST) @SqlType("varchar(y)") @LiteralParameters({"x", "y"}) public static Slice varcharToVarcharCast(@LiteralParameter("x") Long x, @LiteralParameter("y") Long y, @SqlType("varchar(x)") Slice slice) { if (x > y) { return truncateToLength(slice, y.intValue()); } else { return slice; } }
@ScalarOperator(OperatorType.CAST) @SqlType("char(y)") @LiteralParameters({"x", "y"}) public static Slice charToCharCast(@LiteralParameter("x") Long x, @LiteralParameter("y") Long y, @SqlType("char(x)") Slice slice) { if (x > y) { return truncateToLength(slice, y.intValue()); } else { return slice; } }
@ScalarOperator(OperatorType.CAST) @LiteralParameters("x") @SqlType(LikePatternType.NAME) public static Regex castCharToLikePattern(@LiteralParameter("x") Long charLength, @SqlType("char(x)") Slice pattern) { return likePattern(padSpaces(pattern, charLength.intValue())); }
@ScalarOperator(OperatorType.CAST) @SqlType(CodePointsType.NAME) @LiteralParameters("x") public static int[] castCharToCodePoints(@LiteralParameter("x") Long charLength, @SqlType("char(x)") Slice slice) { return castToCodePoints(padSpaces(slice, charLength.intValue())); }
@ScalarOperator(OperatorType.CAST) @LiteralParameters("x") @SqlType(JoniRegexpType.NAME) public static Regex castCharToJoniRegexp(@LiteralParameter("x") Long charLength, @SqlType("char(x)") Slice pattern) { return joniRegexp(padSpaces(pattern, charLength.intValue())); }
@Test public void exactSequence() { Flowable.range(1, 5) .doOnRequest(this) .doOnCancel(this) .limit(5) .test() .assertResult(1, 2, 3, 4, 5); assertEquals(2, requests.size()); assertEquals(5, requests.get(0).intValue()); assertEquals(CANCELLED, requests.get(1)); }
@Test(timeout = 2000) public void backpressureWithBufferDropLatest() throws InterruptedException { int bufferSize = 3; final AtomicInteger droppedCount = new AtomicInteger(0); Action incrementOnDrop = new Action() { @Override public void run() throws Exception { droppedCount.incrementAndGet(); } }; TestSubscriber<Long> ts = createTestSubscriber(); Flowable.fromPublisher(send500ValuesAndComplete.onBackpressureBuffer(bufferSize, incrementOnDrop, DROP_LATEST)) .subscribe(ts); // we request 10 but only 3 should come from the buffer ts.request(10); ts.awaitTerminalEvent(); assertEquals(bufferSize, ts.values().size()); ts.assertNoErrors(); assertEquals(0, ts.values().get(0).intValue()); assertEquals(1, ts.values().get(1).intValue()); assertEquals(499, ts.values().get(2).intValue()); assertEquals(droppedCount.get(), 500 - bufferSize); }
@Test(timeout = 2000) public void backpressureWithBufferDropOldest() throws InterruptedException { int bufferSize = 3; final AtomicInteger droppedCount = new AtomicInteger(0); Action incrementOnDrop = new Action() { @Override public void run() throws Exception { droppedCount.incrementAndGet(); } }; TestSubscriber<Long> ts = createTestSubscriber(); Flowable.fromPublisher(send500ValuesAndComplete.onBackpressureBuffer(bufferSize, incrementOnDrop, DROP_OLDEST)) .subscribe(ts); // we request 10 but only 3 should come from the buffer ts.request(10); ts.awaitTerminalEvent(); assertEquals(bufferSize, ts.values().size()); ts.assertNoErrors(); assertEquals(497, ts.values().get(0).intValue()); assertEquals(498, ts.values().get(1).intValue()); assertEquals(499, ts.values().get(2).intValue()); assertEquals(droppedCount.get(), 500 - bufferSize); }
@Test public void shorterSequence() { Flowable.range(1, 5) .doOnRequest(this) .limit(6) .test() .assertResult(1, 2, 3, 4, 5); assertEquals(6, requests.get(0).intValue()); }
@Test public void longerSequence() { Flowable.range(1, 6) .doOnRequest(this) .limit(5) .test() .assertResult(1, 2, 3, 4, 5); assertEquals(5, requests.get(0).intValue()); }
@ScalarFunction(value = "like", hidden = true) @LiteralParameters("x") @SqlType(StandardTypes.BOOLEAN) public static boolean likeChar(@LiteralParameter("x") Long x, @SqlType("char(x)") Slice value, @SqlType(LikePatternType.NAME) Regex pattern) { return likeVarchar(padSpaces(value, x.intValue()), pattern); }
@ScalarOperator(OperatorType.CAST) @LiteralParameters("x") @SqlType(JsonPathType.NAME) public static JsonPath castCharToJsonPath(@LiteralParameter("x") Long charLength, @SqlType("char(x)") Slice pattern) { return new JsonPath(padSpaces(pattern, charLength.intValue()).toStringUtf8()); }
@Test public void simpleFlowable() { Assert.assertEquals(0, Flowable.empty().count().toFlowable().blockingLast().intValue()); Assert.assertEquals(1, Flowable.just(1).count().toFlowable().blockingLast().intValue()); Assert.assertEquals(10, Flowable.range(1, 10).count().toFlowable().blockingLast().intValue()); }
@Test public void testSwitchWithProducer() throws Exception { final AtomicBoolean emitted = new AtomicBoolean(false); Flowable<Long> withProducer = Flowable.unsafeCreate(new Publisher<Long>() { @Override public void subscribe(final Subscriber<? super Long> subscriber) { subscriber.onSubscribe(new Subscription() { @Override public void request(long n) { if (n > 0 && emitted.compareAndSet(false, true)) { emitted.set(true); subscriber.onNext(42L); subscriber.onComplete(); } } @Override public void cancel() { } }); } }); final Flowable<Long> flowable = Flowable.<Long>empty().switchIfEmpty(withProducer); assertEquals(42, flowable.blockingSingle().intValue()); }