Skip to content

Commit 61ffecc

Browse files
feat: bulk insertion for DAOs [macata #99]
1 parent 45b3446 commit 61ffecc

File tree

4 files changed

+103
-5
lines changed

4 files changed

+103
-5
lines changed

src/main/java/com/dazednconfused/catalauncher/database/base/BaseDAO.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.sql.Statement;
88
import java.util.ArrayList;
99
import java.util.Arrays;
10+
import java.util.Collection;
1011
import java.util.List;
1112
import java.util.Objects;
1213
import java.util.Optional;
@@ -27,6 +28,11 @@ public interface BaseDAO<T extends BaseEntity> {
2728
* */
2829
T insert(T t) throws DAOException;
2930

31+
/**
32+
* Inserts the given {@link BaseEntity}(ies) into the table.
33+
* */
34+
int bulkInsert(Collection<T> t) throws DAOException;
35+
3036
/**
3137
* Updates the given {@link BaseEntity}. It must have an ID set.
3238
* */

src/main/java/com/dazednconfused/catalauncher/database/mod/dao/ModH2DAOImpl.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.sql.ResultSet;
1010
import java.sql.SQLException;
1111
import java.sql.Statement;
12+
import java.util.Arrays;
13+
import java.util.Collection;
1214
import java.util.Optional;
1315

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

46-
String sql = "INSERT INTO " + MODS_TABLE_NAME + "" +
48+
String sql = "INSERT INTO " + MODS_TABLE_NAME + " " +
4749
"(name, modinfo, created_date, updated_date) " +
4850
"VALUES " +
4951
"(?, ?, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP())";
@@ -61,6 +63,39 @@ public ModEntity insert(ModEntity entity) throws DAOException {
6163
}
6264
}
6365

66+
@Override
67+
public int bulkInsert(Collection<ModEntity> entities) throws DAOException {
68+
69+
if (entities == null || entities.isEmpty()) {
70+
LOGGER.warn("No entities provided for bulk insert. No operation shall be performed.");
71+
return 0;
72+
}
73+
74+
LOGGER.debug("Bulk inserting [{}] ModEntity(ies)...", entities.size());
75+
76+
String sql = "INSERT INTO " + MODS_TABLE_NAME + " " +
77+
"(name, modinfo, created_date, updated_date) " +
78+
"VALUES " +
79+
"(?, ?, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP())";
80+
81+
try (Connection conn = this.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
82+
for (ModEntity entity : entities) {
83+
pstmt.setString(1, entity.getName());
84+
pstmt.setString(2, entity.getModinfo());
85+
pstmt.addBatch();
86+
}
87+
88+
int[] results = pstmt.executeBatch();
89+
90+
int insertedCount = Arrays.stream(results).filter(result -> result > 0).sum();
91+
LOGGER.debug("Bulk inserted [{}] ModEntity(ies)", insertedCount);
92+
return insertedCount;
93+
} catch (SQLException e) {
94+
LOGGER.error("An error occurred during bulk insert of ModEntity(ies)", e);
95+
throw new DAOException(e);
96+
}
97+
}
98+
6499
@Override
65100
public ModEntity update(ModEntity entity) throws DAOException {
66101
Optional<ModEntity> originalEntity = this.findById(entity.getId());

src/main/java/com/dazednconfused/catalauncher/database/mod/dao/ModfileH2DAOImpl.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.sql.ResultSet;
1010
import java.sql.SQLException;
1111
import java.sql.Statement;
12+
import java.util.Arrays;
13+
import java.util.Collection;
1214
import java.util.Optional;
1315

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

44-
String sql = "INSERT INTO " + TABLE_NAME + "" +
46+
String sql = "INSERT INTO " + TABLE_NAME + " " +
4547
"(mod_id, path, hash, created_date, updated_date) " +
4648
"VALUES " +
4749
"(?, ?, ?, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP())";
@@ -60,6 +62,40 @@ public ModfileEntity insert(ModfileEntity entity) throws DAOException {
6062
}
6163
}
6264

65+
@Override
66+
public int bulkInsert(Collection<ModfileEntity> entities) throws DAOException {
67+
68+
if (entities == null || entities.isEmpty()) {
69+
LOGGER.warn("No entities provided for bulk insert. No operation shall be performed.");
70+
return 0;
71+
}
72+
73+
LOGGER.debug("Bulk inserting [{}] ModfileEntity(ies)...", entities.size());
74+
75+
String sql = "INSERT INTO " + TABLE_NAME + " " +
76+
"(mod_id, path, hash, created_date, updated_date) " +
77+
"VALUES " +
78+
"(?, ?, ?, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP())";
79+
80+
try (Connection conn = this.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
81+
for (ModfileEntity entity : entities) {
82+
pstmt.setLong(1, entity.getModId());
83+
pstmt.setString(2, entity.getPath());
84+
pstmt.setString(3, entity.getHash());
85+
pstmt.addBatch();
86+
}
87+
88+
int[] results = pstmt.executeBatch();
89+
90+
int insertedCount = Arrays.stream(results).filter(result -> result > 0).sum();
91+
LOGGER.debug("Bulk inserted [{}] ModfileEntity(ies)", insertedCount);
92+
return insertedCount;
93+
} catch (SQLException e) {
94+
LOGGER.error("An error occurred during bulk insert of ModfileEntity(ies)", e);
95+
throw new DAOException(e);
96+
}
97+
}
98+
6399
@Override
64100
public ModfileEntity update(ModfileEntity entity) throws DAOException {
65101
Optional<ModfileEntity> originalEntity = this.findById(entity.getId());

src/main/java/com/dazednconfused/catalauncher/database/mod/repository/ModH2RepositoryImpl.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.dazednconfused.catalauncher.database.mod.entity.ModfileEntity;
99

1010
import java.util.Arrays;
11+
import java.util.Collection;
1112
import java.util.List;
1213
import java.util.Objects;
1314
import java.util.Optional;
@@ -60,6 +61,25 @@ public ModEntity insert(ModEntity entity) throws DAOException {
6061
return result;
6162
}
6263

64+
@Override
65+
public int bulkInsert(Collection<ModEntity> entities) throws DAOException {
66+
LOGGER.debug("Bulk inserting [{}] ModEntity(ies)...", entities.size());
67+
68+
int insertedCount = 0;
69+
70+
for (ModEntity entity : entities) {
71+
ModEntity insertedEntity = this.insert(entity);
72+
if (insertedEntity != null) {
73+
this.insertChildEntities(insertedEntity.getId(), entity.getModfiles());
74+
insertedCount++;
75+
}
76+
}
77+
78+
LOGGER.debug("Bulk inserted [{}] ModEntity(ies)", insertedCount);
79+
80+
return insertedCount;
81+
}
82+
6383
@Override
6484
public ModEntity update(ModEntity entity) throws DAOException {
6585
LOGGER.debug("Updating ModEntity: [{}]", entity);
@@ -121,10 +141,11 @@ public List<ModEntity> findAll() throws DAOException {
121141
private List<ModfileEntity> insertChildEntities(long modId, List<ModfileEntity> entities) throws DAOException {
122142
LOGGER.debug("Inserting [{}] ModfileEntity(s) associated to modId [{}]", entities.size(), modId);
123143

124-
List<ModfileEntity> result = entities.stream()
144+
modfileDAO.bulkInsert(entities.stream()
125145
.peek(e -> e.setModId(modId)) // set/overwrite with entity ID
126-
.map(modfileDAO::insert)
127-
.collect(Collectors.toList());
146+
.collect(Collectors.toList())
147+
);
148+
List<ModfileEntity> result = modfileDAO.findAllByModId(modId);
128149

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

0 commit comments

Comments
 (0)