Skip to content
Merged
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
6 changes: 6 additions & 0 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,10 @@
</module>
<module name="UnusedImports"/>
</module>

<module name="RegexpMultiline">
<property name="format" value="(\n\s*){3,}"/>
<property name="message" value="Too many consecutive blank lines."/>
<property name="severity" value="error"/>
</module>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -27,6 +28,11 @@ public interface BaseDAO<T extends BaseEntity> {
* */
T insert(T t) throws DAOException;

/**
* Inserts the given {@link BaseEntity}(ies) into the table.
* */
int bulkInsert(Collection<T> t) throws DAOException;

/**
* Updates the given {@link BaseEntity}. It must have an ID set.
* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ protected static Result<Throwable, Connection> openConnection(String database) {
).map(Result::success).recover(Result::failure).get();
}


/**
* Completely wipes this database of any and all data, <b>including</b> the underlying schema.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;

import org.slf4j.Logger;
Expand Down Expand Up @@ -43,7 +45,7 @@ public String getDatabaseName() {
public ModEntity insert(ModEntity entity) throws DAOException {
LOGGER.debug("Inserting ModEntity [{}]...", entity);

String sql = "INSERT INTO " + MODS_TABLE_NAME + "" +
String sql = "INSERT INTO " + MODS_TABLE_NAME + " " +
"(name, modinfo, created_date, updated_date) " +
"VALUES " +
"(?, ?, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP())";
Expand All @@ -61,6 +63,39 @@ public ModEntity insert(ModEntity entity) throws DAOException {
}
}

@Override
public int bulkInsert(Collection<ModEntity> entities) throws DAOException {

if (entities == null || entities.isEmpty()) {
LOGGER.warn("No entities provided for bulk insert. No operation shall be performed.");
return 0;
}

LOGGER.debug("Bulk inserting [{}] ModEntity(ies)...", entities.size());

String sql = "INSERT INTO " + MODS_TABLE_NAME + " " +
"(name, modinfo, created_date, updated_date) " +
"VALUES " +
"(?, ?, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP())";

try (Connection conn = this.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
for (ModEntity entity : entities) {
pstmt.setString(1, entity.getName());
pstmt.setString(2, entity.getModinfo());
pstmt.addBatch();
}

int[] results = pstmt.executeBatch();

int insertedCount = Arrays.stream(results).filter(result -> result > 0).sum();
LOGGER.debug("Bulk inserted [{}] ModEntity(ies)", insertedCount);
return insertedCount;
} catch (SQLException e) {
LOGGER.error("An error occurred during bulk insert of ModEntity(ies)", e);
throw new DAOException(e);
}
}

@Override
public ModEntity update(ModEntity entity) throws DAOException {
Optional<ModEntity> originalEntity = this.findById(entity.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;

import org.slf4j.Logger;
Expand Down Expand Up @@ -41,7 +43,7 @@ public String getDatabaseName() {
public ModfileEntity insert(ModfileEntity entity) throws DAOException {
LOGGER.debug("Inserting ModfileEntity [{}]...", entity);

String sql = "INSERT INTO " + TABLE_NAME + "" +
String sql = "INSERT INTO " + TABLE_NAME + " " +
"(mod_id, path, hash, created_date, updated_date) " +
"VALUES " +
"(?, ?, ?, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP())";
Expand All @@ -60,6 +62,40 @@ public ModfileEntity insert(ModfileEntity entity) throws DAOException {
}
}

@Override
public int bulkInsert(Collection<ModfileEntity> entities) throws DAOException {

if (entities == null || entities.isEmpty()) {
LOGGER.warn("No entities provided for bulk insert. No operation shall be performed.");
return 0;
}

LOGGER.debug("Bulk inserting [{}] ModfileEntity(ies)...", entities.size());

String sql = "INSERT INTO " + TABLE_NAME + " " +
"(mod_id, path, hash, created_date, updated_date) " +
"VALUES " +
"(?, ?, ?, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP())";

try (Connection conn = this.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
for (ModfileEntity entity : entities) {
pstmt.setLong(1, entity.getModId());
pstmt.setString(2, entity.getPath());
pstmt.setString(3, entity.getHash());
pstmt.addBatch();
}

int[] results = pstmt.executeBatch();

int insertedCount = Arrays.stream(results).filter(result -> result > 0).sum();
LOGGER.debug("Bulk inserted [{}] ModfileEntity(ies)", insertedCount);
return insertedCount;
} catch (SQLException e) {
LOGGER.error("An error occurred during bulk insert of ModfileEntity(ies)", e);
throw new DAOException(e);
}
}

@Override
public ModfileEntity update(ModfileEntity entity) throws DAOException {
Optional<ModfileEntity> originalEntity = this.findById(entity.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.dazednconfused.catalauncher.database.mod.entity.ModfileEntity;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -60,6 +61,20 @@ public ModEntity insert(ModEntity entity) throws DAOException {
return result;
}

@Override
public int bulkInsert(Collection<ModEntity> entities) throws DAOException {
LOGGER.debug("Bulk inserting [{}] ModEntity(ies)...", entities.size());

int insertedCount = (int) entities.stream()
.map(this::insert)
.filter(Objects::nonNull)
.count();

LOGGER.debug("Bulk inserted [{}] ModEntity(ies)", insertedCount);

return insertedCount;
}

@Override
public ModEntity update(ModEntity entity) throws DAOException {
LOGGER.debug("Updating ModEntity: [{}]", entity);
Expand Down Expand Up @@ -121,10 +136,11 @@ public List<ModEntity> findAll() throws DAOException {
private List<ModfileEntity> insertChildEntities(long modId, List<ModfileEntity> entities) throws DAOException {
LOGGER.debug("Inserting [{}] ModfileEntity(s) associated to modId [{}]", entities.size(), modId);

List<ModfileEntity> result = entities.stream()
this.modfileDAO.bulkInsert(entities.stream()
.peek(e -> e.setModId(modId)) // set/overwrite with entity ID
.map(modfileDAO::insert)
.collect(Collectors.toList());
.collect(Collectors.toList())
);
List<ModfileEntity> result = modfileDAO.findAllByModId(modId);

LOGGER.debug("Inserted [{}] ModfileEntity(s) associated to modId [{}]", result.size(), modId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,46 @@ void insert_success() {
assertThat(result.getCreatedDate()).isEqualTo(result.getUpdatedDate());
}

@Test
void bulk_insert_success() {

// prepare mock data ---
ModEntity entity1 = ModEntity.builder()
.name("testName1")
.modinfo("testModinfo1")
.build();

ModEntity entity2 = ModEntity.builder()
.name("testName2")
.modinfo("testModinfo2")
.build();

ModEntity entity3 = ModEntity.builder()
.name("testName3")
.modinfo("testModinfo3")
.build();

// execute test ---
int result = dao.bulkInsert(List.of(entity1, entity2, entity3));

// verify assertions ---
assertThat(result).isEqualTo(3);

List<ModEntity> allEntities = dao.findAll();
assertThat(allEntities).hasSize(3);

assertThat(allEntities)
.usingRecursiveFieldByFieldElementComparatorIgnoringFields("id", "createdDate", "updatedDate")
.containsExactlyInAnyOrder(entity1, entity2, entity3);

for (ModEntity entity : allEntities) {
assertThat(entity.getId()).isNotZero();
assertThat(entity.getCreatedDate()).isNotNull();
assertThat(entity.getUpdatedDate()).isNotNull();
assertThat(entity.getCreatedDate()).isEqualTo(entity.getUpdatedDate());
}
}

@Test
void find_by_id_success() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.mockito.Mockito.when;

import com.dazednconfused.catalauncher.database.base.DisposableDatabase;
import com.dazednconfused.catalauncher.database.h2.H2Database;
import com.dazednconfused.catalauncher.database.mod.entity.ModEntity;
import com.dazednconfused.catalauncher.database.mod.entity.ModfileEntity;

Expand Down Expand Up @@ -124,6 +123,49 @@ void insert_success() {
assertThat(result.getCreatedDate()).isEqualTo(result.getUpdatedDate());
}

@Test
void bulk_insert_success() {

// prepare mock data ---
ModfileEntity entity1 = ModfileEntity.builder()
.modId(parentModId)
.path("testPath1")
.hash("testHash1")
.build();

ModfileEntity entity2 = ModfileEntity.builder()
.modId(parentModId)
.path("testPath2")
.hash("testHash2")
.build();

ModfileEntity entity3 = ModfileEntity.builder()
.modId(parentModId)
.path("testPath3")
.hash("testHash3")
.build();

// execute test ---
int result = dao.bulkInsert(List.of(entity1, entity2, entity3));

// verify assertions ---
assertThat(result).isEqualTo(3);

List<ModfileEntity> allEntities = dao.findAll();
assertThat(allEntities).hasSize(3);

assertThat(allEntities)
.usingRecursiveFieldByFieldElementComparatorIgnoringFields("id", "createdDate", "updatedDate")
.containsExactlyInAnyOrder(entity1, entity2, entity3);

for (ModfileEntity entity : allEntities) {
assertThat(entity.getId()).isNotZero();
assertThat(entity.getCreatedDate()).isNotNull();
assertThat(entity.getUpdatedDate()).isNotNull();
assertThat(entity.getCreatedDate()).isEqualTo(entity.getUpdatedDate());
}
}

@Test
void find_by_id_success() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,72 @@ void insert_success() {
assertThat(resultChildEntity3.getCreatedDate()).isEqualTo(resultChildEntity3.getUpdatedDate());
}

@Test
void bulk_insert_success() {

// prepare mock data ---
ModEntity entity1 = ModEntity.builder()
.name("testName1")
.modinfo("testModinfo1")
.modfiles(Arrays.asList(
ModfileEntity.builder().path("testPath1_1").hash("testHash1_1").build(),
ModfileEntity.builder().path("testPath1_2").hash("testHash1_2").build(),
ModfileEntity.builder().path("testPath1_3").hash("testHash1_3").build()
))
.build();

ModEntity entity2 = ModEntity.builder()
.name("testName2")
.modinfo("testModinfo2")
.modfiles(Arrays.asList(
ModfileEntity.builder().path("testPath2_1").hash("testHash2_1").build(),
ModfileEntity.builder().path("testPath2_2").hash("testHash2_2").build(),
ModfileEntity.builder().path("testPath2_3").hash("testHash2_3").build()
))
.build();

ModEntity entity3 = ModEntity.builder()
.name("testName3")
.modinfo("testModinfo3")
.modfiles(Arrays.asList(
ModfileEntity.builder().path("testPath3_1").hash("testHash3_1").build(),
ModfileEntity.builder().path("testPath3_2").hash("testHash3_2").build(),
ModfileEntity.builder().path("testPath3_3").hash("testHash3_3").build()
))
.build();

// execute test ---
int result = repository.bulkInsert(Arrays.asList(entity1, entity2, entity3));

// verify assertions ---
assertThat(result).isEqualTo(3);

List<ModEntity> allEntities = repository.findAll();
assertThat(allEntities).hasSize(3);

assertThat(allEntities)
.usingRecursiveFieldByFieldElementComparatorIgnoringFields(
"id", "createdDate", "updatedDate",
"modfiles.id", "modfiles.createdDate", "modfiles.updatedDate"
)
.containsExactlyInAnyOrder(entity1, entity2, entity3);

for (ModEntity entity : allEntities) {
assertThat(entity.getId()).isNotZero();
assertThat(entity.getCreatedDate()).isNotNull();
assertThat(entity.getUpdatedDate()).isNotNull();
assertThat(entity.getCreatedDate()).isEqualTo(entity.getUpdatedDate());
assertThat(entity.getModfiles()).hasSize(3);

for (ModfileEntity modfile : entity.getModfiles()) {
assertThat(modfile.getModId()).isEqualTo(entity.getId());
assertThat(modfile.getCreatedDate()).isNotNull();
assertThat(modfile.getUpdatedDate()).isNotNull();
assertThat(modfile.getCreatedDate()).isEqualTo(modfile.getUpdatedDate());
}
}
}

@Test
void find_by_id_success() {

Expand Down
Loading