Skip to content

Commit

Permalink
Refactor batch insert (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada authored Oct 19, 2024
1 parent aedb0ba commit 1244e24
Show file tree
Hide file tree
Showing 29 changed files with 832 additions and 194 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"java.sources.organizeImports.staticStarThreshold": 3,
"java.test.config": {
"vmArgs": [
"-Djava.util.logging.config.file=src/main/resources/logging.properties"
"-Djava.util.logging.config.file=src/test/resources/logging.properties"
]
},
"sonarlint.connectedMode.project": {
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.8.0] - unreleased

- [PR #27](https://github.com/itsallcode/simple-jdbc/pull/27): Update dependencies
- [PR #28](https://github.com/itsallcode/simple-jdbc/pull/28): Refactored batch inserts (**Breaking change**)

## [0.7.1] - 2024-09-01

Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Add dependency to your gradle project:

```groovy
dependencies {
implementation 'org.itsallcode:simple-jdbc:0.7.1'
implementation 'org.itsallcode:simple-jdbc:0.8.0'
}
```

Expand All @@ -43,8 +43,11 @@ import org.itsallcode.jdbc.resultset.SimpleResultSet;
ConnectionFactory connectionFactory = ConnectionFactory.create();
try (SimpleConnection connection = connectionFactory.create("jdbc:h2:mem:", "user", "password")) {
connection.executeScript(readResource("/schema.sql"));
connection.insert("NAMES", List.of("ID", "NAME"), Name::toRow,
Stream.of(new Name(1, "a"), new Name(2, "b"), new Name(3, "c")));
connection.batchInsert(Name.class)
.into("NAMES", List.of("ID", "NAME"))
.rows(Stream.of(new Name(1, "a"), new Name(2, "b"), new Name(3, "c")))
.mapping(Name::setPreparedStatement)
.start();
try (SimpleResultSet<Row> rs = connection.query("select * from names order by id")) {
List<Row> result = rs.stream().toList();
assertEquals(3, result.size());
Expand Down
18 changes: 13 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ plugins {
}

group 'org.itsallcode'
version = '0.7.1'

dependencies {
}
version = '0.8.0'

java {
toolchain {
Expand All @@ -37,13 +34,24 @@ tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

def getMockitoAgentPath() {
def mockitoAgentConfig = configurations.create("mockitoAgent")
dependencies {
mockitoAgent(libs.mockitoCore.get()) { transitive = false }
}
return mockitoAgentConfig.asPath
}
def mockitoAgentPath = getMockitoAgentPath()

testing {
suites {
configureEach {
useJUnitJupiter()
dependencies {
implementation project()
implementation libs.assertj
implementation libs.mockitoJunit
runtimeOnly libs.slf4jApi
runtimeOnly libs.slf4jLogger
}
targets {
Expand All @@ -53,6 +61,7 @@ testing {
testLogging.showStandardStreams = true
}
jvmArgs '-enableassertions'
jvmArgs "-javaagent:${mockitoAgentPath}"
systemProperty 'java.util.logging.config.file', file('src/test/resources/logging.properties')
}
}
Expand All @@ -61,7 +70,6 @@ testing {
test {
dependencies {
implementation libs.h2
implementation libs.mockitoJunit
implementation libs.tostringverifier
implementation libs.equalsverifier
}
Expand Down
6 changes: 4 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ dependencyResolutionManagement {
}
versionCatalogs {
libs {
version('mockito', '5.14.2')
library('assertj', 'org.assertj:assertj-core:3.26.3')
library('h2', 'com.h2database:h2:2.3.232')
library('junitPioneer', 'org.junit-pioneer:junit-pioneer:2.2.0')
library('equalsverifier', 'nl.jqno.equalsverifier:equalsverifier:3.17.1')
library('tostringverifier', 'com.jparams:to-string-verifier:1.4.8')
library('hamcrest', 'org.hamcrest:hamcrest:3.0')
library('hamcrestResultSetMatcher', 'com.exasol:hamcrest-resultset-matcher:1.6.3')
library('mockito', 'org.mockito:mockito-core:5.11.0')
library('mockitoJunit', 'org.mockito:mockito-junit-jupiter:5.14.1')
library('mockitoCore', 'org.mockito', 'mockito-core').versionRef('mockito')
library('mockitoJunit', 'org.mockito', 'mockito-junit-jupiter').versionRef('mockito')
library('slf4jApi', 'org.slf4j:slf4j-api:2.0.16')
library('slf4jLogger', 'org.slf4j:slf4j-jdk14:2.0.16')
library('exasolJdbc', 'com.exasol:exasol-jdbc:24.1.2')
library('exasolTestcontainers', 'com.exasol:exasol-testcontainers:7.1.1')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.itsallcode.jdbc;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import java.sql.PreparedStatement;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class BatchInsertPerformanceTest {
@Mock
SimpleConnection connectionMock;
@Mock
SimplePreparedStatement preparedStatementMock;

@Test
@Timeout(value = 10, unit = TimeUnit.SECONDS)
void performanceTestRowStmtSetter() {
final int rowCount = 10_000_000;
testee().into("TEST", List.of("ID", "NAME"))
.mapping((row, stmt) -> {
stmt.setInt(1, row.id);
stmt.setString(2, row.name);
}).rows(generateStream(rowCount)).start();
}

private BatchInsertBuilder<NameRow> testee() {
final PreparedStatement stmt = new NoopPreparedStatement();
when(connectionMock.prepareStatement(anyString()))
.thenReturn(new SimplePreparedStatement(null, null, stmt, "sql"));
return new BatchInsertBuilder<NameRow>(connectionMock::prepareStatement, Context.builder().build());
}

@Test
@Timeout(value = 10, unit = TimeUnit.SECONDS)
void performanceTestObjectArray() {
final int rowCount = 10_000_000;
testee().into("TEST", List.of("ID", "NAME"))
.mapping(row -> new Object[] { row.id, row.name }).rows(generateStream(rowCount)).start();
}

private Stream<NameRow> generateStream(final int rowCount) {
return IntStream.range(0, rowCount).mapToObj(id -> new NameRow(id, "Name " + id));
}

private record NameRow(int id, String name) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

class ExasolTypeTest {

@SuppressWarnings("resource") // Will be closed in @AfterAll
private static final ExasolContainer<?> container = new ExasolContainer<>("8.31.0")
.withRequiredServices(ExasolService.JDBC).withReuse(true);

Expand Down
Loading

0 comments on commit 1244e24

Please sign in to comment.