|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 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 | + * https://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 io.r2dbc.postgresql; |
| 18 | + |
| 19 | +import io.r2dbc.postgresql.api.PostgresqlConnectionMetadata; |
| 20 | +import io.r2dbc.postgresql.client.Client; |
| 21 | +import io.r2dbc.postgresql.message.backend.BackendMessage; |
| 22 | +import io.r2dbc.postgresql.message.backend.EmptyQueryResponse; |
| 23 | +import io.r2dbc.postgresql.message.backend.ErrorResponse; |
| 24 | +import io.r2dbc.postgresql.message.backend.ReadyForQuery; |
| 25 | +import io.r2dbc.postgresql.message.frontend.FrontendMessage; |
| 26 | +import io.r2dbc.postgresql.message.frontend.Query; |
| 27 | +import io.r2dbc.postgresql.replication.LogSequenceNumber; |
| 28 | +import io.r2dbc.postgresql.replication.ReplicationRequest; |
| 29 | +import io.r2dbc.postgresql.replication.ReplicationSlot; |
| 30 | +import io.r2dbc.postgresql.replication.ReplicationSlotRequest; |
| 31 | +import io.r2dbc.postgresql.replication.ReplicationStream; |
| 32 | +import io.r2dbc.postgresql.util.Assert; |
| 33 | +import io.r2dbc.spi.Row; |
| 34 | +import reactor.core.publisher.EmitterProcessor; |
| 35 | +import reactor.core.publisher.Mono; |
| 36 | + |
| 37 | +import java.util.function.Predicate; |
| 38 | + |
| 39 | +import static io.r2dbc.postgresql.util.PredicateUtils.or; |
| 40 | + |
| 41 | +/** |
| 42 | + * Postgres replication connection. |
| 43 | + */ |
| 44 | +final class DefaultPostgresqlReplicationConnection implements io.r2dbc.postgresql.api.PostgresqlReplicationConnection { |
| 45 | + |
| 46 | + private static final Predicate<BackendMessage> WINDOW_UNTIL = or(ReadyForQuery.class::isInstance, EmptyQueryResponse.class::isInstance, ErrorResponse.class::isInstance); |
| 47 | + |
| 48 | + private final PostgresqlConnection delegate; |
| 49 | + |
| 50 | + private final Client client; |
| 51 | + |
| 52 | + DefaultPostgresqlReplicationConnection(PostgresqlConnection delegate) { |
| 53 | + this.delegate = delegate; |
| 54 | + this.client = delegate.getClient(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Mono<Void> close() { |
| 59 | + return this.delegate.close(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Mono<ReplicationSlot> createSlot(ReplicationSlotRequest request) { |
| 64 | + |
| 65 | + Assert.requireNonNull(request, "request must not be null"); |
| 66 | + |
| 67 | + return this.delegate.createStatement(request.asSQL()).execute().flatMap(it -> { |
| 68 | + |
| 69 | + return it.map((row, rowMetadata) -> getReplicationSlot(request, row)); |
| 70 | + }).last(); |
| 71 | + } |
| 72 | + |
| 73 | + private static ReplicationSlot getReplicationSlot(ReplicationSlotRequest request, Row row) { |
| 74 | + return new ReplicationSlot( |
| 75 | + getString(row, "slot_name"), |
| 76 | + request.getReplicationType(), |
| 77 | + LogSequenceNumber.valueOf(getString(row, "consistent_point")), |
| 78 | + row.get("snapshot_name", String.class), |
| 79 | + row.get("output_plugin", String.class)); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public Mono<ReplicationStream> startReplication(ReplicationRequest request) { |
| 84 | + |
| 85 | + Assert.requireNonNull(request, "request must not be null"); |
| 86 | + |
| 87 | + String sql = request.asSQL(); |
| 88 | + ExceptionFactory exceptionFactory = ExceptionFactory.withSql(sql); |
| 89 | + |
| 90 | + EmitterProcessor<FrontendMessage> requestProcessor = EmitterProcessor.create(); |
| 91 | + |
| 92 | + return Mono.fromDirect(this.client.exchange(requestProcessor.startWith(new Query(sql))) |
| 93 | + .handle(exceptionFactory::handleErrorResponse) |
| 94 | + .windowUntil(WINDOW_UNTIL) |
| 95 | + .map(messages -> { |
| 96 | + return new PostgresReplicationStream(this.client.getByteBufAllocator(), request, requestProcessor, messages); |
| 97 | + })); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public PostgresqlConnectionMetadata getMetadata() { |
| 102 | + return this.delegate.getMetadata(); |
| 103 | + } |
| 104 | + |
| 105 | + private static String getString(Row row, String column) { |
| 106 | + String value = row.get(column, String.class); |
| 107 | + if (value == null) { |
| 108 | + throw new IllegalStateException(String.format("No value found for column %s", column)); |
| 109 | + } |
| 110 | + return value; |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments