Cache MakerRecipe, move stimulant to MakerInfoProvider
This commit is contained in:
19
src/main/java/database/JdbiConfig.java
Normal file
19
src/main/java/database/JdbiConfig.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package database;
|
||||
|
||||
import database.maker.MakerReagentRowMapper;
|
||||
import database.maker.MakerRecipeRowMapper;
|
||||
import database.note.NoteRowMapper;
|
||||
import org.jdbi.v3.core.Jdbi;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
public final class JdbiConfig {
|
||||
private JdbiConfig() {}
|
||||
|
||||
public static Jdbi createConfigured(DataSource dataSource) {
|
||||
return Jdbi.create(dataSource)
|
||||
.registerRowMapper(new NoteRowMapper())
|
||||
.registerRowMapper(new MakerReagentRowMapper())
|
||||
.registerRowMapper(new MakerRecipeRowMapper());
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package database;
|
||||
|
||||
import database.maker.MakerReagentRowMapper;
|
||||
import database.note.NoteRowMapper;
|
||||
import org.jdbi.v3.core.Handle;
|
||||
import org.jdbi.v3.core.Jdbi;
|
||||
|
||||
@@ -15,14 +13,7 @@ public class PgDatabaseConnection {
|
||||
|
||||
public PgDatabaseConnection(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
this.jdbi = Jdbi.create(dataSource);
|
||||
registerRowMappers();
|
||||
// TODO: configure jdbi elsewhere
|
||||
}
|
||||
|
||||
private void registerRowMappers() {
|
||||
jdbi.registerRowMapper(new NoteRowMapper())
|
||||
.registerRowMapper(new MakerReagentRowMapper());
|
||||
this.jdbi = JdbiConfig.createConfigured(dataSource);
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
|
||||
@@ -27,4 +27,18 @@ public class MakerDao {
|
||||
throw new DaoException("Failed to get maker reagent with item id: %d".formatted(itemId), e);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<MakerRecipe> getRecipe(int itemId) {
|
||||
try (Handle handle = connection.getHandle()) {
|
||||
return handle.createQuery("""
|
||||
SELECT *
|
||||
FROM maker_recipe
|
||||
WHERE item_id = ?;""")
|
||||
.bind(0, itemId)
|
||||
.mapTo(MakerRecipe.class)
|
||||
.findOne();
|
||||
} catch (JdbiException e) {
|
||||
throw new DaoException("Failed to get maker recipe with item id: %d".formatted(itemId), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,23 @@
|
||||
package database.maker;
|
||||
|
||||
import net.jcip.annotations.ThreadSafe;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ThreadSafe
|
||||
public class MakerInfoProvider {
|
||||
private final MakerDao makerDao;
|
||||
private final Map<Integer, MakerReagent> reagents = new ConcurrentHashMap<>();
|
||||
private final Map<Integer, MakerReagent> reagentCache = new ConcurrentHashMap<>();
|
||||
private final Map<Integer, MakerRecipe> recipeCache = new ConcurrentHashMap<>();
|
||||
|
||||
public MakerInfoProvider(MakerDao makerDao) {
|
||||
this.makerDao = makerDao;
|
||||
}
|
||||
|
||||
public Optional<MakerReagent> getMakerReagent(int itemId) {
|
||||
final MakerReagent cachedReagent = reagents.get(itemId);
|
||||
final MakerReagent cachedReagent = reagentCache.get(itemId);
|
||||
if (cachedReagent != null) {
|
||||
return Optional.of(cachedReagent);
|
||||
}
|
||||
@@ -22,7 +26,25 @@ public class MakerInfoProvider {
|
||||
if (reagentFromDb.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
reagents.put(itemId, reagentFromDb.get());
|
||||
reagentCache.put(itemId, reagentFromDb.get());
|
||||
return reagentFromDb;
|
||||
}
|
||||
|
||||
public Optional<MakerRecipe> getMakerRecipe(int itemId) {
|
||||
final MakerRecipe cachedRecipe = recipeCache.get(itemId);
|
||||
if (cachedRecipe != null) {
|
||||
return Optional.of(cachedRecipe);
|
||||
}
|
||||
|
||||
final Optional<MakerRecipe> recipeFromDb = makerDao.getRecipe(itemId);
|
||||
if (recipeFromDb.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
recipeCache.put(itemId, recipeFromDb.get());
|
||||
return recipeFromDb;
|
||||
}
|
||||
|
||||
public Optional<Integer> getStimulant(int itemId) {
|
||||
return getMakerRecipe(itemId).map(MakerRecipe::catalyst);
|
||||
}
|
||||
}
|
||||
|
||||
5
src/main/java/database/maker/MakerRecipe.java
Normal file
5
src/main/java/database/maker/MakerRecipe.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package database.maker;
|
||||
|
||||
public record MakerRecipe(int itemId, short jobCategory, short requiredLevel, short requiredMakerLevel, int mesoCost,
|
||||
Integer requiredItem, Integer requiredEquip, Integer catalyst, short quantity,
|
||||
short reagentSlots) {}
|
||||
26
src/main/java/database/maker/MakerRecipeRowMapper.java
Normal file
26
src/main/java/database/maker/MakerRecipeRowMapper.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package database.maker;
|
||||
|
||||
import org.jdbi.v3.core.mapper.RowMapper;
|
||||
import org.jdbi.v3.core.statement.StatementContext;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class MakerRecipeRowMapper implements RowMapper<MakerRecipe> {
|
||||
|
||||
@Override
|
||||
public MakerRecipe map(ResultSet rs, StatementContext ctx) throws SQLException {
|
||||
int itemId = rs.getInt("item_id");
|
||||
short jobCategory = rs.getShort("job_category");
|
||||
short requiredLevel = rs.getShort("required_level");
|
||||
short requiredMakerLevel = rs.getShort("required_maker_level");
|
||||
int mesoCost = rs.getInt("meso_cost");
|
||||
Integer requiredItem = rs.getObject("required_item", Integer.class);
|
||||
Integer requiredEquip = rs.getObject("required_equip", Integer.class);
|
||||
Integer catalyst = rs.getObject("catalyst", Integer.class);
|
||||
short quantity = rs.getShort("quantity");
|
||||
short reagentSlots = rs.getShort("reagent_slots");
|
||||
return new MakerRecipe(itemId, jobCategory, requiredLevel, requiredMakerLevel, mesoCost, requiredItem,
|
||||
requiredEquip, catalyst, quantity, reagentSlots);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user