Dropped IdFactory temporary object SQL table.

This commit is contained in:
MobiusDevelopment 2020-02-03 20:58:13 +00:00
parent ef0273c5e8
commit 05283c9afa
2 changed files with 43 additions and 40 deletions

View File

@ -20,6 +20,9 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -57,6 +60,16 @@ public abstract class IdFactory
"SELECT object_id FROM itemsonground WHERE object_id >= ? AND object_id < ?"
};
//@formatter:off
private static final String[][] ID_EXTRACTS =
{
{"characters", "obj_Id"},
{"items", "object_id"},
{"clan_data", "clan_id"},
{"itemsonground", "object_id"}
};
//@formatter:on
protected boolean _initialized;
public static final int FIRST_OID = 0x10000000;
@ -166,47 +179,35 @@ public abstract class IdFactory
}
}
protected int[] extractUsedObjectIDTable() throws SQLException
/**
* @return
* @throws Exception
* @throws SQLException
*/
protected final Integer[] extractUsedObjectIDTable() throws Exception
{
try (Connection con = DatabaseFactory.getConnection())
final List<Integer> temp = new ArrayList<>();
try (Connection con = DatabaseFactory.getConnection();
Statement s = con.createStatement())
{
// create a temporary table
final Statement s = con.createStatement();
try
String extractUsedObjectIdsQuery = "";
for (String[] tblClmn : ID_EXTRACTS)
{
s.executeUpdate("drop table temporaryObjectTable");
}
catch (SQLException e)
{
}
s.executeUpdate("delete from itemsonground where object_id in (select object_id from items)");
s.executeUpdate("create table temporaryObjectTable (object_id int NOT NULL PRIMARY KEY)");
s.executeUpdate("insert into temporaryObjectTable (object_id) select obj_id from characters");
s.executeUpdate("insert into temporaryObjectTable (object_id) select object_id from items");
s.executeUpdate("insert into temporaryObjectTable (object_id) select clan_id from clan_data");
s.executeUpdate("insert into temporaryObjectTable (object_id) select object_id from itemsonground");
ResultSet result = s.executeQuery("select count(object_id) from temporaryObjectTable");
result.next();
final int size = result.getInt(1);
final int[] tmp_obj_ids = new int[size];
result.close();
result = s.executeQuery("select object_id from temporaryObjectTable ORDER BY object_id");
int idx = 0;
while (result.next())
{
tmp_obj_ids[idx++] = result.getInt(1);
extractUsedObjectIdsQuery += "SELECT " + tblClmn[1] + " FROM " + tblClmn[0] + " UNION ";
}
result.close();
s.close();
return tmp_obj_ids;
extractUsedObjectIdsQuery = extractUsedObjectIdsQuery.substring(0, extractUsedObjectIdsQuery.length() - 7); // Remove the last " UNION "
try (ResultSet rs = s.executeQuery(extractUsedObjectIdsQuery))
{
while (rs.next())
{
temp.add(rs.getInt(1));
}
}
}
Collections.sort(temp);
return temp.toArray(new Integer[temp.size()]);
}
public boolean isInitialized()

View File

@ -43,17 +43,19 @@ public class StackIDFactory extends IdFactory
try (Connection con = DatabaseFactory.getConnection())
{
final int[] tmpObjIds = extractUsedObjectIDTable();
// con.createStatement().execute("drop table if exists tmp_obj_id");
final Integer[] tmpObjIds = extractUsedObjectIDTable();
if (tmpObjIds.length > 0)
{
_curOID = tmpObjIds[tmpObjIds.length - 1];
}
LOGGER.info("Max Id = " + _curOID);
int N = tmpObjIds.length;
for (int idx = 0; idx < N; idx++)
int n = tmpObjIds.length;
for (int idx = 0; idx < n; idx++)
{
N = insertUntil(tmpObjIds, idx, N, con);
n = insertUntil(tmpObjIds, idx, n, con);
}
_curOID++;
@ -66,7 +68,7 @@ public class StackIDFactory extends IdFactory
}
}
private int insertUntil(int[] tmpObjIds, int idx, int n, Connection con) throws SQLException
private int insertUntil(Integer[] tmpObjIds, int idx, int n, Connection con) throws SQLException
{
final int id = tmpObjIds[idx];
if (id == _tempOID)