Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions PgBulkInsert/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<postgresql.version>9.4-1206-jdbc42</postgresql.version>
<postgresql.version>42.2.2</postgresql.version>
</properties>

<distributionManagement>
Expand Down Expand Up @@ -157,4 +157,4 @@

</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.postgresql.PGConnection;
import org.postgresql.copy.CopyIn;
import org.postgresql.copy.CopyManager;
import org.postgresql.copy.PGCopyOutputStream;

import java.math.BigDecimal;
import java.net.Inet4Address;
Expand All @@ -33,34 +32,44 @@
import java.util.stream.Stream;

public abstract class PgBulkInsert<TEntity> implements IPgBulkInsert<TEntity> {

private static final int DEFAULT_BUFFER_SIZE = 65536;

private IValueHandlerProvider provider;

private TableDefinition table;

private List<ColumnDefinition<TEntity>> columns;

private final int bufferSize;

public PgBulkInsert(String schemaName, String tableName)
{
this(new ValueHandlerProvider(), schemaName, tableName);
this(new ValueHandlerProvider(), schemaName, tableName, DEFAULT_BUFFER_SIZE);
}

public PgBulkInsert(String schemaName, String tableName, int bufferSize)
{
this(new ValueHandlerProvider(), schemaName, tableName, bufferSize);
}

public PgBulkInsert(IValueHandlerProvider provider, String schemaName, String tableName)
public PgBulkInsert(IValueHandlerProvider provider, String schemaName, String tableName, int bufferSize)
{
this.provider = provider;
this.table = new TableDefinition(schemaName, tableName);
this.columns = new ArrayList<>();
this.bufferSize = bufferSize;
}

public void saveAll(PGConnection connection, Stream<TEntity> entities) throws SQLException {

CopyManager cpManager = connection.getCopyAPI();
CopyIn copyIn = cpManager.copyIn(getCopyCommand());

try (PgBinaryWriter bw = new PgBinaryWriter()) {
try (PgBinaryWriter bw = new PgBinaryWriter(bufferSize)) {

// Wrap the CopyOutputStream in our own Writer:
bw.open(new PGCopyOutputStream(copyIn));
bw.open(copyIn);

// Insert Each Column:
entities.forEach(entity -> this.saveEntity(bw, entity));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;

import org.postgresql.copy.CopyIn;
import org.postgresql.copy.PGCopyOutputStream;

public class PgBinaryWriter implements AutoCloseable {

private transient DataOutputStream buffer;

private final int bufferSize;

public PgBinaryWriter() {
public PgBinaryWriter(int bufferSize) {
this.bufferSize = bufferSize;
}

public void open(final OutputStream out) {
buffer = new DataOutputStream(new BufferedOutputStream(out));
public void open(final CopyIn copyIn) {
buffer = new DataOutputStream(new BufferedOutputStream(new PGCopyOutputStream(copyIn, 1), bufferSize));

writeHeader();
}
Expand Down