|
| 1 | +package com.clickhouse.r2dbc; |
| 2 | + |
| 3 | +import com.clickhouse.client.ClickHouseResponse; |
| 4 | +import com.clickhouse.client.ClickHouseResponseSummary; |
| 5 | +import com.clickhouse.client.logging.Logger; |
| 6 | +import com.clickhouse.client.logging.LoggerFactory; |
| 7 | +import io.r2dbc.spi.Result; |
| 8 | +import io.r2dbc.spi.Row; |
| 9 | +import io.r2dbc.spi.RowMetadata; |
| 10 | +import org.apache.commons.lang3.tuple.Pair; |
| 11 | +import org.reactivestreams.Publisher; |
| 12 | +import reactor.core.publisher.Flux; |
| 13 | +import reactor.core.publisher.Mono; |
| 14 | + |
| 15 | +import java.util.function.BiFunction; |
| 16 | +import java.util.function.Function; |
| 17 | +import java.util.function.Predicate; |
| 18 | +import java.util.stream.StreamSupport; |
| 19 | + |
| 20 | +public class ClickHouseResult implements Result { |
| 21 | + |
| 22 | + private static final Logger log = LoggerFactory.getLogger(ClickHouseResult.class); |
| 23 | + |
| 24 | + private final Flux<? extends Result.Segment> rowSegments; |
| 25 | + private final Mono<? extends Result.Segment> updatedCount; |
| 26 | + private final Flux<? extends Result.Segment> segments; |
| 27 | + |
| 28 | + ClickHouseResult(Mono<ClickHouseResponse> response) { |
| 29 | + this.rowSegments = response.flux() |
| 30 | + .flatMap(resp -> Flux |
| 31 | + .fromStream(StreamSupport.stream(resp.records().spliterator(), false) |
| 32 | + .map(rec -> Pair.of(resp.getColumns(), rec)))) |
| 33 | + .map(pair -> new ClickHouseRow(pair.getRight(), pair.getLeft())) |
| 34 | + .map(RowSegment::new); |
| 35 | + this.updatedCount = response.map(clickHouseResponse -> clickHouseResponse.getSummary()) |
| 36 | + .map(ClickHouseResponseSummary::getProgress) |
| 37 | + .map(ClickHouseResponseSummary.Progress::getWrittenRows) |
| 38 | + .map(UpdateCount::new); |
| 39 | + this.segments = Flux.concat(this.updatedCount, this.rowSegments); |
| 40 | + } |
| 41 | + |
| 42 | + ClickHouseResult(Flux<? extends Result.Segment> rowSegments, Mono<? extends Result.Segment> updatedCount) { |
| 43 | + this.rowSegments = rowSegments; |
| 44 | + this.updatedCount = updatedCount; |
| 45 | + this.segments = Flux.concat(this.updatedCount, this.rowSegments); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns updated count(written rows from summary of {@link ClickHouseResponse}).Important! if writtenRows is greater than MAX_INT then it will return MAX_INT. |
| 50 | + * @return |
| 51 | + */ |
| 52 | + @Override |
| 53 | + public Mono<Integer> getRowsUpdated() { |
| 54 | + |
| 55 | + return updatedCount.map(val -> { |
| 56 | + UpdateCount updateCount = (UpdateCount) val; |
| 57 | + if (updateCount.value() > Integer.MAX_VALUE) |
| 58 | + return Integer.MAX_VALUE; |
| 59 | + else return Long.valueOf(updateCount.value()).intValue(); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public <T> Publisher<T> map(BiFunction<Row, RowMetadata, ? extends T> biFunction) { |
| 65 | + return rowSegments.cast(RowSegment.class) |
| 66 | + .map(RowSegment::row).handle((row, sink) -> { |
| 67 | + try { |
| 68 | + sink.next(biFunction.apply(row, row.getMetadata())); |
| 69 | + } catch (Exception e) { |
| 70 | + log.error("Provided function caused exception:", e); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Result filter(Predicate<Segment> predicate) { |
| 77 | + return new ClickHouseResult(segments.filter(predicate), updatedCount.filter(predicate)); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public <T> Publisher<T> flatMap(Function<Segment, ? extends Publisher<? extends T>> function) { |
| 82 | + return segments.flatMap(segment -> { |
| 83 | + try { |
| 84 | + Publisher<? extends T> retValue = function.apply(segment); |
| 85 | + if (retValue == null) { |
| 86 | + return Mono.error(new IllegalStateException("flatmap function returned null value")); |
| 87 | + } |
| 88 | + return retValue; |
| 89 | + } catch (Exception e) { |
| 90 | + log.error("Provided function caused exception:", e); |
| 91 | + return Mono.error(e); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + class RowSegment implements Result.RowSegment { |
| 98 | + |
| 99 | + final ClickHouseRow row; |
| 100 | + |
| 101 | + RowSegment(ClickHouseRow row) { |
| 102 | + this.row = row; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public Row row() { |
| 107 | + return row; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + class UpdateCount implements Result.UpdateCount { |
| 112 | + |
| 113 | + final long updateCount; |
| 114 | + |
| 115 | + UpdateCount(long updateCount) { |
| 116 | + this.updateCount = updateCount; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public long value() { |
| 121 | + return updateCount; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments