Reduce amount of string allocations

This commit is contained in:
Brokkonaut
2018-08-21 06:29:44 +02:00
parent 56404533db
commit 681c4a2033
2 changed files with 13 additions and 6 deletions

View File

@@ -710,9 +710,8 @@ public class Consumer extends Thread {
public BlockRow(Location loc, Actor actor, int replaced, int replacedData, byte[] replacedState, int type, int typeData, byte[] typeState, ChestAccess ca) {
super(System.currentTimeMillis() / 1000, loc, actor, replaced, replacedData, replacedState, type, typeData, typeState, ca);
final String table = getWorldConfig(loc.getWorld()).table;
statementString = "INSERT INTO `" + table + "-blocks` (date, playerid, replaced, replaceddata, type, typedata, x, y, z) VALUES (FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ?, ?)";
selectActorIdStatementString = "SELECT playerid FROM `" + table + "-blocks` WHERE x = ? AND y = ? AND z = ? ORDER BY date DESC LIMIT 1";
statementString = getWorldConfig(loc.getWorld()).insertBlockStatementString;
selectActorIdStatementString = getWorldConfig(loc.getWorld()).selectBlockActorIdStatementString;
}
@Override
@@ -769,17 +768,16 @@ public class Consumer extends Thread {
batchHelper.addBatch(smt, new IntCallback() {
@Override
public void call(int id) throws SQLException {
final String table = getWorldConfig(loc.getWorld()).table;
PreparedStatement ps;
if (typeState != null || replacedState != null) {
ps = batchHelper.getOrPrepareStatement(conn, "INSERT INTO `" + table + "-state` (replacedState, typeState, id) VALUES(?, ?, ?)", Statement.NO_GENERATED_KEYS);
ps = batchHelper.getOrPrepareStatement(conn, getWorldConfig(loc.getWorld()).insertBlockStateStatementString, Statement.NO_GENERATED_KEYS);
ps.setBytes(1, replacedState);
ps.setBytes(2, typeState);
ps.setInt(3, id);
batchHelper.addBatch(ps, null);
}
if (ca != null) {
ps = batchHelper.getOrPrepareStatement(conn, "INSERT INTO `" + table + "-chestdata` (item, itemremove, id, itemtype) values (?, ?, ?, ?)", Statement.NO_GENERATED_KEYS);
ps = batchHelper.getOrPrepareStatement(conn, getWorldConfig(loc.getWorld()).insertBlockChestDataStatementString, Statement.NO_GENERATED_KEYS);
ps.setBytes(1, Utils.saveItemStack(ca.itemStack));
ps.setInt(2, ca.remove ? 1 : 0);
ps.setInt(3, id);

View File

@@ -12,6 +12,10 @@ import java.util.Map.Entry;
public class WorldConfig extends LoggingEnabledMapping {
public final String world;
public final String table;
public final String insertBlockStatementString;
public final String selectBlockActorIdStatementString;
public final String insertBlockStateStatementString;
public final String insertBlockChestDataStatementString;
public WorldConfig(String world, File file) throws IOException {
this.world = world;
@@ -33,5 +37,10 @@ public class WorldConfig extends LoggingEnabledMapping {
for (final Logging l : Logging.values()) {
setLogging(l, config.getBoolean("logging." + l.toString()));
}
insertBlockStatementString = "INSERT INTO `" + table + "-blocks` (date, playerid, replaced, replaceddata, type, typedata, x, y, z) VALUES (FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ?, ?)";
selectBlockActorIdStatementString = "SELECT playerid FROM `" + table + "-blocks` WHERE x = ? AND y = ? AND z = ? ORDER BY date DESC LIMIT 1";
insertBlockStateStatementString = "INSERT INTO `" + table + "-state` (replacedState, typeState, id) VALUES(?, ?, ?)";
insertBlockChestDataStatementString = "INSERT INTO `" + table + "-chestdata` (item, itemremove, id, itemtype) values (?, ?, ?, ?)";
}
}