Skip to content

Commit

Permalink
use long when accessing id columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokkonaut committed Aug 3, 2022
1 parent c6e8105 commit 9bfe5d0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/main/java/de/diddiz/LogBlock/BlockChange.java
Expand Up @@ -62,7 +62,7 @@ public BlockChange(long date, Location loc, Actor actor, int replaced, int repla
}

public BlockChange(ResultSet rs, QueryParams p) throws SQLException {
id = p.needId ? rs.getInt("id") : 0;
id = p.needId ? rs.getLong("id") : 0;
date = p.needDate ? rs.getTimestamp("date").getTime() : 0;
loc = p.needCoords ? new Location(p.world, rs.getInt("x"), rs.getInt("y"), rs.getInt("z")) : null;
actor = p.needPlayer ? new Actor(rs) : null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/diddiz/LogBlock/ChatMessage.java
Expand Up @@ -26,7 +26,7 @@ public ChatMessage(Actor player, String message) {
}

public ChatMessage(ResultSet rs, QueryParams p) throws SQLException {
id = p.needId ? rs.getInt("id") : 0;
id = p.needId ? rs.getLong("id") : 0;
date = p.needDate ? rs.getTimestamp("date").getTime() : 0;
player = p.needPlayer ? new Actor(rs) : null;
playerName = p.needPlayer ? rs.getString("playername") : null;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/de/diddiz/LogBlock/CommandsHandler.java
Expand Up @@ -965,14 +965,14 @@ public void run() {
StringBuilder sb = new StringBuilder();
if (params.bct == BlockChangeType.CHAT) {
sb.append("INSERT INTO `lb-chat` (`id`, `date`, `playerid`, `message`) VALUES (");
sb.append(rs.getInt("id")).append(", FROM_UNIXTIME(");
sb.append(rs.getLong("id")).append(", FROM_UNIXTIME(");
sb.append(rs.getTimestamp("date").getTime() / 1000).append("), ");
sb.append(rs.getInt("playerid")).append(", '");
sb.append(Utils.mysqlTextEscape(rs.getString("message")));
sb.append("');\n");
} else if (params.bct == BlockChangeType.KILLS) {
sb.append("INSERT INTO `").append(tableBase).append("-kills` (`id`, `date`, `killer`, `victim`, `weapon`, `x`, `y`, `z`) VALUES (");
sb.append(rs.getInt("id")).append(", FROM_UNIXTIME(");
sb.append(rs.getLong("id")).append(", FROM_UNIXTIME(");
sb.append(rs.getTimestamp("date").getTime() / 1000).append("), ");
sb.append(rs.getInt("killerid")).append(", ");
sb.append(rs.getInt("victimid")).append(", ");
Expand All @@ -985,7 +985,7 @@ public void run() {

} else {
sb.append("INSERT INTO `").append(tableBase).append("-blocks` (`id`, `date`, `playerid`, `replaced`, `replacedData`, `type`, `typeData`, `x`, `y`, `z`) VALUES (");
sb.append(rs.getInt("id")).append(", FROM_UNIXTIME(");
sb.append(rs.getLong("id")).append(", FROM_UNIXTIME(");
sb.append(rs.getTimestamp("date").getTime() / 1000).append("), ");
sb.append(rs.getInt("playerid")).append(", ");
sb.append(rs.getInt("replaced")).append(", ");
Expand All @@ -1000,15 +1000,15 @@ public void run() {
byte[] typeState = rs.getBytes("typeState");
if (replacedState != null || typeState != null) {
sb.append("INSERT INTO `").append(tableBase).append("-state` (`id`, `replacedState`, `typeState`) VALUES (");
sb.append(rs.getInt("id")).append(", ");
sb.append(rs.getLong("id")).append(", ");
sb.append(Utils.mysqlPrepareBytesForInsertAllowNull(replacedState)).append(", ");
sb.append(Utils.mysqlPrepareBytesForInsertAllowNull(typeState));
sb.append(");\n");
}
byte[] item = rs.getBytes("item");
if (item != null) {
sb.append("INSERT INTO `").append(tableBase).append("-chestdata` (`id`, `item`, `itemremove`, `itemtype`) VALUES (");
sb.append(rs.getInt("id")).append(", ");
sb.append(rs.getLong("id")).append(", ");
sb.append(Utils.mysqlPrepareBytesForInsertAllowNull(item)).append(", ");
sb.append(rs.getInt("itemremove")).append(", ");
sb.append(rs.getInt("itemtype"));
Expand Down
48 changes: 24 additions & 24 deletions src/main/java/de/diddiz/LogBlock/Consumer.java
Expand Up @@ -67,7 +67,7 @@ public class Consumer extends Thread {
private final LogBlock logblock;
private final Map<Actor, Integer> playerIds = new HashMap<>();
private final Map<Actor, Integer> uncommitedPlayerIds = new HashMap<>();
private final Map<World, Map<UUID, Integer>> uncommitedEntityIds = new HashMap<>();
private final Map<World, Map<UUID, Long>> uncommitedEntityIds = new HashMap<>();

private long addEntryCounter;
private long nextWarnCounter;
Expand Down Expand Up @@ -650,13 +650,13 @@ private boolean addPlayer(Connection conn, Actor actor) throws SQLException {
return uncommitedPlayerIds.containsKey(actor);
}

private int getEntityUUID(Connection conn, World world, UUID uuid) throws SQLException {
Map<UUID, Integer> uncommitedEntityIdsHere = uncommitedEntityIds.get(world);
private long getEntityUUID(Connection conn, World world, UUID uuid) throws SQLException {
Map<UUID, Long> uncommitedEntityIdsHere = uncommitedEntityIds.get(world);
if (uncommitedEntityIdsHere == null) {
uncommitedEntityIdsHere = new HashMap<>();
uncommitedEntityIds.put(world, uncommitedEntityIdsHere);
}
Integer existing = uncommitedEntityIdsHere.get(uuid);
Long existing = uncommitedEntityIdsHere.get(uuid);
if (existing != null) {
return existing;
}
Expand All @@ -670,15 +670,15 @@ private int getEntityUUID(Connection conn, World world, UUID uuid) throws SQLExc
int q1Result = state.executeUpdate(q1);
ResultSet rs = state.executeQuery(q2);
if (rs.next()) {
uncommitedEntityIdsHere.put(uuid, rs.getInt(1));
uncommitedEntityIdsHere.put(uuid, rs.getLong(1));
}
rs.close();
// if there was not any row in the table the query above does not work, so we need to try this one
if (!uncommitedEntityIdsHere.containsKey(uuid)) {
state.executeUpdate("INSERT IGNORE INTO `" + table + "-entityids` (entityuuid) VALUES ('" + mysqlTextEscape(uuidString) + "')");
rs = state.executeQuery(q2);
if (rs.next()) {
uncommitedEntityIdsHere.put(uuid, rs.getInt(1));
uncommitedEntityIdsHere.put(uuid, rs.getLong(1));
} else {
logblock.getLogger().warning("[Consumer] Failed to add entity uuid " + uuidString.toString());
logblock.getLogger().warning("[Consumer-Debug] World: " + world.getName());
Expand Down Expand Up @@ -881,22 +881,22 @@ public void process(Connection conn, BatchHelper batchHelper) throws SQLExceptio
smt.setInt(8, safeY(loc));
smt.setInt(9, loc.getBlockZ());
batchHelper.addUncommitedBlockActorId(loc, sourceActor);
batchHelper.addBatch(smt, new IntCallback() {
batchHelper.addBatch(smt, new LongCallback() {
@Override
public void call(int id) throws SQLException {
public void call(long id) throws SQLException {
PreparedStatement ps;
if (typeState != null || replacedState != null) {
ps = batchHelper.getOrPrepareStatement(conn, getWorldConfig(loc.getWorld()).insertBlockStateStatementString, Statement.NO_GENERATED_KEYS);
ps.setBytes(1, replacedState);
ps.setBytes(2, typeState);
ps.setInt(3, id);
ps.setLong(3, id);
batchHelper.addBatch(ps, null);
}
if (ca != null) {
ps = batchHelper.getOrPrepareStatement(conn, getWorldConfig(loc.getWorld()).insertBlockChestDataStatementString, Statement.NO_GENERATED_KEYS);
ps.setBytes(1, finalSerializedItemStack);
ps.setInt(2, ca.remove ? 1 : 0);
ps.setInt(3, id);
ps.setLong(3, id);
ps.setInt(4, ca.itemType);
batchHelper.addBatch(ps, null);
}
Expand Down Expand Up @@ -1108,7 +1108,7 @@ public void process(Connection conn, BatchHelper batchHelper) throws SQLExceptio
PreparedStatement smt = batchHelper.getOrPrepareStatement(conn, statementString, Statement.NO_GENERATED_KEYS);
smt.setLong(1, date);
smt.setInt(2, sourceActor);
smt.setInt(3, getEntityUUID(conn, loc.getWorld(), entityUUID));
smt.setLong(3, getEntityUUID(conn, loc.getWorld(), entityUUID));
smt.setInt(4, EntityTypeConverter.getOrAddEntityTypeId(type));
smt.setInt(5, loc.getBlockX());
smt.setInt(6, safeY(loc));
Expand All @@ -1121,11 +1121,11 @@ public void process(Connection conn, BatchHelper batchHelper) throws SQLExceptio

private class EntityUUIDChange implements Row {
private final World world;
private final int entityId;
private final long entityId;
private final UUID entityUUID;
final String updateEntityUUIDString;

public EntityUUIDChange(World world, int entityId, UUID entityUUID) {
public EntityUUIDChange(World world, long entityId, UUID entityUUID) {
this.world = world;
this.entityId = entityId;
this.entityUUID = entityUUID;
Expand All @@ -1150,7 +1150,7 @@ public Actor[] getActors() {
public void process(Connection conn, BatchHelper batchHelper) throws SQLException {
PreparedStatement smt = batchHelper.getOrPrepareStatement(conn, updateEntityUUIDString, Statement.NO_GENERATED_KEYS);
smt.setString(1, entityUUID.toString());
smt.setInt(2, entityId);
smt.setLong(2, entityId);
smt.executeUpdate();
}
}
Expand All @@ -1169,7 +1169,7 @@ private int safeY(Location loc) {
private class BatchHelper {
private HashMap<String, PreparedStatement> preparedStatements = new HashMap<>();
private HashSet<PreparedStatement> preparedStatementsWithGeneratedKeys = new HashSet<>();
private LinkedHashMap<PreparedStatement, ArrayList<IntCallback>> generatedKeyHandler = new LinkedHashMap<>();
private LinkedHashMap<PreparedStatement, ArrayList<LongCallback>> generatedKeyHandler = new LinkedHashMap<>();
private HashMap<Location, Integer> uncommitedBlockActors = new HashMap<>();

public void reset() {
Expand All @@ -1189,21 +1189,21 @@ public Integer getUncommitedBlockActor(Location loc) {

public void processStatements(Connection conn) throws SQLException {
while (!generatedKeyHandler.isEmpty()) {
Entry<PreparedStatement, ArrayList<IntCallback>> entry = generatedKeyHandler.entrySet().iterator().next();
Entry<PreparedStatement, ArrayList<LongCallback>> entry = generatedKeyHandler.entrySet().iterator().next();
PreparedStatement smt = entry.getKey();
ArrayList<IntCallback> callbackList = entry.getValue();
ArrayList<LongCallback> callbackList = entry.getValue();
generatedKeyHandler.remove(smt);
smt.executeBatch();
if (preparedStatementsWithGeneratedKeys.contains(smt)) {
ResultSet keys = smt.getGeneratedKeys();
int[] results = new int[callbackList.size()];
long[] results = new long[callbackList.size()];
int pos = 0;
while (keys.next() && pos < results.length) {
results[pos++] = keys.getInt(1);
results[pos++] = keys.getLong(1);
}
keys.close();
for (int i = 0; i < results.length; i++) {
IntCallback callback = callbackList.get(i);
LongCallback callback = callbackList.get(i);
if (callback != null) {
callback.call(results[i]);
}
Expand All @@ -1225,9 +1225,9 @@ public PreparedStatement getOrPrepareStatement(Connection conn, String sql, int
return smt;
}

public void addBatch(PreparedStatement smt, IntCallback generatedKeysCallback) throws SQLException {
public void addBatch(PreparedStatement smt, LongCallback generatedKeysCallback) throws SQLException {
smt.addBatch();
ArrayList<IntCallback> callbackList = generatedKeyHandler.get(smt);
ArrayList<LongCallback> callbackList = generatedKeyHandler.get(smt);
if (callbackList == null) {
callbackList = new ArrayList<>();
generatedKeyHandler.put(smt, callbackList);
Expand All @@ -1236,7 +1236,7 @@ public void addBatch(PreparedStatement smt, IntCallback generatedKeysCallback) t
}
}

protected interface IntCallback {
public void call(int value) throws SQLException;
protected interface LongCallback {
public void call(long value) throws SQLException;
}
}
2 changes: 1 addition & 1 deletion src/main/java/de/diddiz/LogBlock/EntityChange.java
Expand Up @@ -60,7 +60,7 @@ public EntityChange(long date, Location loc, Actor actor, EntityType type, UUID
}

public EntityChange(ResultSet rs, QueryParams p) throws SQLException {
id = p.needId ? rs.getInt("id") : 0;
id = p.needId ? rs.getLong("id") : 0;
date = p.needDate ? rs.getTimestamp("date").getTime() : 0;
loc = p.needCoords ? new Location(p.world, rs.getInt("x"), rs.getInt("y"), rs.getInt("z")) : null;
actor = p.needPlayer ? new Actor(rs) : null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/diddiz/LogBlock/Kill.java
Expand Up @@ -30,7 +30,7 @@ public Kill(String killerName, String victimName, int weapon, Location loc) {
}

public Kill(ResultSet rs, QueryParams p) throws SQLException {
id = p.needId ? rs.getInt("id") : 0;
id = p.needId ? rs.getLong("id") : 0;
date = p.needDate ? rs.getTimestamp("date").getTime() : 0;
loc = p.needCoords ? new Location(p.world, rs.getInt("x"), rs.getInt("y"), rs.getInt("z")) : null;
killerName = p.needKiller ? rs.getString("killer") : null;
Expand Down

0 comments on commit 9bfe5d0

Please sign in to comment.