-
Notifications
You must be signed in to change notification settings - Fork 184
Add COPY FROM
support
#500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Copyright 2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.r2dbc.postgresql; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.r2dbc.postgresql.api.PostgresqlConnection; | ||
import io.r2dbc.postgresql.util.PostgresqlServerExtension; | ||
import org.junit.platform.commons.annotation.Testable; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.TearDown; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
import org.postgresql.copy.CopyManager; | ||
import org.postgresql.jdbc.PgConnection; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.MappedByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* Benchmarks for Copy operation. Contains the following execution methods: | ||
*/ | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
@Testable | ||
public class CopyInBenchmarks extends BenchmarkSettings { | ||
|
||
private static PostgresqlServerExtension extension = new PostgresqlServerExtension(); | ||
|
||
@State(Scope.Benchmark) | ||
public static class ConnectionHolder { | ||
|
||
@Param({"0", "1", "100", "1000000"}) | ||
int rows; | ||
|
||
final PgConnection jdbc; | ||
|
||
final CopyManager copyManager; | ||
|
||
final PostgresqlConnection r2dbc; | ||
|
||
Path csvFile; | ||
|
||
public ConnectionHolder() { | ||
|
||
extension.initialize(); | ||
try { | ||
jdbc = extension.getDataSource().getConnection() | ||
.unwrap(PgConnection.class); | ||
copyManager = jdbc.getCopyAPI(); | ||
Statement statement = jdbc.createStatement(); | ||
|
||
try { | ||
statement.execute("DROP TABLE IF EXISTS simple_test"); | ||
} catch (SQLException e) { | ||
} | ||
|
||
statement.execute("CREATE TABLE simple_test (name VARCHAR(255), age int)"); | ||
|
||
jdbc.setAutoCommit(false); | ||
|
||
r2dbc = new PostgresqlConnectionFactory(extension.getConnectionConfiguration()).create().block(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Setup(Level.Trial) | ||
public void doSetup() throws IOException { | ||
csvFile = Files.createTempFile("jmh-input", ".csv"); | ||
|
||
try (OutputStream outputStream = new FileOutputStream(csvFile.toFile())) { | ||
IntStream.range(0, rows) | ||
.mapToObj(i -> "some-input" + i + ";" + i + "\n") | ||
.forEach(row -> { | ||
try { | ||
outputStream.write(row.getBytes(StandardCharsets.UTF_8)); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@TearDown(Level.Trial) | ||
public void doTearDown() throws IOException { | ||
Files.delete(csvFile); | ||
} | ||
|
||
} | ||
|
||
@Benchmark | ||
public void copyInR2dbc(ConnectionHolder connectionHolder, Blackhole voodoo) throws IOException { | ||
File file = connectionHolder.csvFile.toFile(); | ||
try (FileInputStream fileInputStream = new FileInputStream(file); | ||
FileChannel fileChannel = fileInputStream.getChannel()) { | ||
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); | ||
ByteBuf byteBuf = Unpooled.wrappedBuffer(mappedByteBuffer); | ||
|
||
Long rowsInserted = connectionHolder.r2dbc.copyIn("COPY simple_test (name, age) FROM STDIN DELIMITER ';'", Mono.just(byteBuf)) | ||
.block(); | ||
|
||
voodoo.consume(rowsInserted); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void copyInJdbc(ConnectionHolder connectionHolder, Blackhole voodoo) throws IOException, SQLException { | ||
try (InputStream inputStream = new FileInputStream(connectionHolder.csvFile.toFile())) { | ||
|
||
Long rowsInserted = connectionHolder.copyManager.copyIn("COPY simple_test (name, age) FROM STDIN DELIMITER ';'", inputStream); | ||
|
||
voodoo.consume(rowsInserted); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.r2dbc.postgresql; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.util.ReferenceCountUtil; | ||
import io.netty.util.ReferenceCounted; | ||
import io.r2dbc.postgresql.client.Client; | ||
import io.r2dbc.postgresql.message.backend.BackendMessage; | ||
import io.r2dbc.postgresql.message.backend.CommandComplete; | ||
import io.r2dbc.postgresql.message.backend.CopyInResponse; | ||
import io.r2dbc.postgresql.message.backend.ReadyForQuery; | ||
import io.r2dbc.postgresql.message.frontend.CopyData; | ||
import io.r2dbc.postgresql.message.frontend.CopyDone; | ||
import io.r2dbc.postgresql.message.frontend.CopyFail; | ||
import io.r2dbc.postgresql.message.frontend.Query; | ||
import io.r2dbc.postgresql.util.Assert; | ||
import io.r2dbc.postgresql.util.Operators; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import static io.r2dbc.postgresql.PostgresqlResult.toResult; | ||
|
||
/** | ||
* An implementation for {@link CopyData} PostgreSQL queries. | ||
*/ | ||
final class PostgresqlCopyIn { | ||
|
||
private final ConnectionResources context; | ||
|
||
PostgresqlCopyIn(ConnectionResources context) { | ||
this.context = Assert.requireNonNull(context, "context must not be null"); | ||
} | ||
|
||
Mono<Long> copy(String sql, Publisher<ByteBuf> stdin) { | ||
return Flux.from(stdin) | ||
.map(CopyData::new) | ||
.as(messages -> copyIn(sql, messages)); | ||
} | ||
|
||
private Mono<Long> copyIn(String sql, Flux<CopyData> copyDataMessages) { | ||
Client client = context.getClient(); | ||
|
||
Flux<BackendMessage> backendMessages = copyDataMessages | ||
.doOnNext(client::send) | ||
.doOnError((e) -> sendCopyFail(e.getMessage())) | ||
.doOnDiscard(ReferenceCounted.class, ReferenceCountUtil::release) | ||
.thenMany(client.exchange(Mono.just(CopyDone.INSTANCE))); | ||
|
||
return startCopy(sql) | ||
.concatWith(backendMessages) | ||
.doOnCancel(() -> sendCopyFail("Cancelled")) | ||
.as(Operators::discardOnCancel) | ||
.as(messages -> toResult(context, messages, ExceptionFactory.INSTANCE).getRowsUpdated()); | ||
} | ||
|
||
private Flux<BackendMessage> startCopy(String sql) { | ||
return context.getClient().exchange( | ||
// ReadyForQuery is returned when an invalid query is provided | ||
backendMessage -> backendMessage instanceof CopyInResponse || backendMessage instanceof ReadyForQuery, | ||
Mono.just(new Query(sql)) | ||
) | ||
.doOnNext(message -> { | ||
if (message instanceof CommandComplete) { | ||
throw new IllegalArgumentException("Copy from stdin query expected, sql='" + sql + "', message=" + message); | ||
} | ||
}); | ||
} | ||
|
||
private void sendCopyFail(String message) { | ||
context.getClient().exchange(Mono.just(new CopyFail("Copy operation failed: " + message))) | ||
.as(Operators::discardOnCancel) | ||
.subscribe(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PostgresqlCopyIn{" + | ||
"context=" + this.context + | ||
'}'; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the client exchange, it makes sense to start with a many-sink (
Sinks.Many.asFlux()
) where we keep a single conversation/publisher with the server open. Otherwise, we callexchange
multiple times and that generates a bit of overhead and may interfere with other connection activity causing protocol resynchronization.I can apply this change during the merge as it bears some complexity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did an attempt but it is a little bit complex since we have to wait for the CopyInResponse before sending the data frames.