IdFactory cleanup.

This commit is contained in:
MobiusDev
2018-04-06 23:55:40 +00:00
parent f013b5d027
commit 7fcc48a051
5 changed files with 23 additions and 224 deletions

View File

@ -910,9 +910,8 @@ public final class Config
public static int PVP_NORMAL_TIME;
public static int PVP_PVP_TIME;
public static enum IdFactoryType
public enum IdFactoryType
{
Compaction,
BitSet,
Stack
}

View File

@ -56,7 +56,7 @@ public class BitSetIDFactory extends IdFactory
ThreadPool.scheduleAtFixedRate(new BitSetCapacityCheck(), 30000, 30000);
initialize();
}
_log.info(getClass().getSimpleName() + ": " + _freeIds.size() + " id's available.");
LOGGER.info(getClass().getSimpleName() + ": " + _freeIds.size() + " id's available.");
}
public void initialize()
@ -72,7 +72,7 @@ public class BitSetIDFactory extends IdFactory
final int objectID = usedObjectId - FIRST_OID;
if (objectID < 0)
{
_log.warning(getClass().getSimpleName() + ": Object ID " + usedObjectId + " in DB is less than minimum ID of " + FIRST_OID);
LOGGER.warning(getClass().getSimpleName() + ": Object ID " + usedObjectId + " in DB is less than minimum ID of " + FIRST_OID);
continue;
}
_freeIds.set(usedObjectId - FIRST_OID);
@ -85,7 +85,7 @@ public class BitSetIDFactory extends IdFactory
catch (Exception e)
{
_initialized = false;
_log.severe(getClass().getSimpleName() + ": Could not be initialized properly: " + e.getMessage());
LOGGER.severe(getClass().getSimpleName() + ": Could not be initialized properly: " + e.getMessage());
}
}
@ -99,7 +99,7 @@ public class BitSetIDFactory extends IdFactory
}
else
{
_log.warning(getClass().getSimpleName() + ": Release objectID " + objectID + " failed (< " + FIRST_OID + ")");
LOGGER.warning(getClass().getSimpleName() + ": Release objectID " + objectID + " failed (< " + FIRST_OID + ")");
}
}

View File

@ -1,151 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.idfactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.l2jmobius.Config;
import com.l2jmobius.commons.database.DatabaseFactory;
/**
* 18.8.2010 - JIV: Disabling until someone update it
*/
@Deprecated
public class CompactionIDFactory extends IdFactory
{
private int _curOID;
private final int _freeSize;
protected CompactionIDFactory()
{
super();
_curOID = FIRST_OID;
_freeSize = 0;
try (Connection con = DatabaseFactory.getInstance().getConnection())
{
final Integer[] tmp_obj_ids = extractUsedObjectIDTable();
int N = tmp_obj_ids.length;
for (int idx = 0; idx < N; idx++)
{
N = insertUntil(tmp_obj_ids, idx, N, con);
}
_curOID++;
_log.info(getClass().getSimpleName() + ": Next usable Object ID is: " + _curOID);
_initialized = true;
}
catch (Exception e)
{
_log.severe(getClass().getSimpleName() + ": Could not initialize properly: " + e.getMessage());
}
}
private int insertUntil(Integer[] tmp_obj_ids, int idx, int N, Connection con) throws SQLException
{
int id = tmp_obj_ids[idx];
if (id == _curOID)
{
_curOID++;
return N;
}
// check these IDs not present in DB
if (Config.BAD_ID_CHECKING)
{
for (String check : ID_CHECKS)
{
try (PreparedStatement ps = con.prepareStatement(check))
{
ps.setInt(1, _curOID);
ps.setInt(2, id);
try (ResultSet rs = ps.executeQuery())
{
while (rs.next())
{
final int badId = rs.getInt(1);
_log.severe(getClass().getSimpleName() + ": Bad ID " + badId + " in DB found by: " + check);
throw new RuntimeException();
}
}
}
}
}
final int hole = (id - _curOID) > (N - idx) ? N - idx : id - _curOID;
for (int i = 1; i <= hole; i++)
{
id = tmp_obj_ids[N - i];
_log.info(getClass().getSimpleName() + ": Compacting DB object ID=" + id + " into " + _curOID);
for (String update : ID_UPDATES)
{
try (PreparedStatement ps = con.prepareStatement(update))
{
ps.setInt(1, _curOID);
ps.setInt(2, id);
ps.execute();
}
}
_curOID++;
}
if (hole < (N - idx))
{
_curOID++;
}
return N - hole;
}
@Override
public synchronized int getNextId()
{
// @formatter:off
/*
* if (_freeSize == 0)
*/
return _curOID++;
/*
* else
* return _freeOIDs[--_freeSize];
*/
// @formatter:on
}
@Override
public synchronized void releaseId(int id)
{
// Don't release Ids until we are sure it isn't messing up
// @formatter:off
/*
if (_freeSize >= _freeOIDs.length)
{
int[] tmp = new int[_freeSize + STACK_SIZE_INCREMENT];
System.arraycopy(_freeOIDs, 0, tmp, 0, _freeSize);
_freeOIDs = tmp;
}
_freeOIDs[_freeSize++] = id;
*/
// @formatter:on
}
@Override
public int size()
{
return (_freeSize + LAST_OID) - FIRST_OID;
}
}

View File

@ -36,48 +36,7 @@ import com.l2jmobius.commons.database.DatabaseFactory;
*/
public abstract class IdFactory
{
protected final Logger _log = Logger.getLogger(getClass().getName());
@Deprecated
protected static final String[] ID_UPDATES =
{
"UPDATE items SET owner_id = ? WHERE owner_id = ?",
"UPDATE items SET object_id = ? WHERE object_id = ?",
"UPDATE character_quests SET charId = ? WHERE charId = ?",
"UPDATE character_contacts SET charId = ? WHERE charId = ?",
"UPDATE character_contacts SET friendId = ? WHERE contactId = ?",
"UPDATE character_friends SET charId = ? WHERE charId = ?",
"UPDATE character_friends SET friendId = ? WHERE friendId = ?",
"UPDATE character_hennas SET charId = ? WHERE charId = ?",
"UPDATE character_recipebook SET charId = ? WHERE charId = ?",
"UPDATE character_recipeshoplist SET charId = ? WHERE charId = ?",
"UPDATE character_shortcuts SET charId = ? WHERE charId = ?",
"UPDATE character_shortcuts SET shortcut_id = ? WHERE shortcut_id = ? AND type = 1", // items
"UPDATE character_macroses SET charId = ? WHERE charId = ?",
"UPDATE character_skills SET charId = ? WHERE charId = ?",
"UPDATE character_skills_save SET charId = ? WHERE charId = ?",
"UPDATE character_subclasses SET charId = ? WHERE charId = ?",
"UPDATE character_ui_actions SET charId = ? WHERE charId = ?",
"UPDATE character_ui_categories SET charId = ? WHERE charId = ?",
"UPDATE characters SET charId = ? WHERE charId = ?",
"UPDATE characters SET clanid = ? WHERE clanid = ?",
"UPDATE clan_data SET clan_id = ? WHERE clan_id = ?",
"UPDATE siege_clans SET clan_id = ? WHERE clan_id = ?",
"UPDATE clan_data SET ally_id = ? WHERE ally_id = ?",
"UPDATE clan_data SET leader_id = ? WHERE leader_id = ?",
"UPDATE pets SET item_obj_id = ? WHERE item_obj_id = ?",
"UPDATE character_hennas SET charId = ? WHERE charId = ?",
"UPDATE itemsonground SET object_id = ? WHERE object_id = ?",
"UPDATE auction_bid SET bidderId = ? WHERE bidderId = ?",
"UPDATE auction_watch SET charObjId = ? WHERE charObjId = ?",
"UPDATE olympiad_fights SET charOneId = ? WHERE charOneId = ?",
"UPDATE olympiad_fights SET charTwoId = ? WHERE charTwoId = ?",
"UPDATE heroes_diary SET charId = ? WHERE charId = ?",
"UPDATE olympiad_nobles SET charId = ? WHERE charId = ?",
"UPDATE character_offline_trade SET charId = ? WHERE charId = ?",
"UPDATE character_offline_trade_items SET charId = ? WHERE charId = ?",
"UPDATE clanhall SET ownerId = ? WHERE ownerId = ?"
};
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
protected static final String[] ID_CHECKS =
{
@ -151,12 +110,6 @@ public abstract class IdFactory
{
switch (Config.IDFACTORY_TYPE)
{
case Compaction:
{
throw new UnsupportedOperationException("Compaction IdFactory is disabled.");
// _instance = new CompactionIDFactory();
// break;
}
case BitSet:
{
_instance = new BitSetIDFactory();
@ -184,11 +137,11 @@ public abstract class IdFactory
Statement s = con.createStatement())
{
s.executeUpdate("UPDATE characters SET online = 0");
_log.info("Updated characters online status.");
LOGGER.info("Updated characters online status.");
}
catch (SQLException e)
{
_log.log(Level.WARNING, "Could not update characters online status: " + e.getMessage(), e);
LOGGER.log(Level.WARNING, "Could not update characters online status: " + e.getMessage(), e);
}
}
@ -226,6 +179,7 @@ public abstract class IdFactory
// stmt.executeUpdate("DELETE FROM characters WHERE characters.account_name NOT IN (SELECT login FROM accounts);");
// If the character does not exist...
// Characters
cleanCount += stmt.executeUpdate("DELETE FROM account_gsdata WHERE account_gsdata.account_name NOT IN (SELECT account_name FROM characters);");
cleanCount += stmt.executeUpdate("DELETE FROM character_contacts WHERE character_contacts.charId NOT IN (SELECT charId FROM characters);");
cleanCount += stmt.executeUpdate("DELETE FROM character_contacts WHERE character_contacts.contactId NOT IN (SELECT charId FROM characters);");
@ -244,11 +198,15 @@ public abstract class IdFactory
cleanCount += stmt.executeUpdate("DELETE FROM character_instance_time WHERE character_instance_time.charId NOT IN (SELECT charId FROM characters);");
cleanCount += stmt.executeUpdate("DELETE FROM character_ui_actions WHERE character_ui_actions.charId NOT IN (SELECT charId FROM characters);");
cleanCount += stmt.executeUpdate("DELETE FROM character_ui_categories WHERE character_ui_categories.charId NOT IN (SELECT charId FROM characters);");
// Items
cleanCount += stmt.executeUpdate("DELETE FROM items WHERE items.owner_id NOT IN (SELECT charId FROM characters) AND items.owner_id NOT IN (SELECT clan_id FROM clan_data) AND items.owner_id != -1;");
cleanCount += stmt.executeUpdate("DELETE FROM items WHERE items.owner_id = -1 AND loc LIKE 'MAIL' AND loc_data NOT IN (SELECT messageId FROM messages WHERE senderId = -1);");
cleanCount += stmt.executeUpdate("DELETE FROM item_auction_bid WHERE item_auction_bid.playerObjId NOT IN (SELECT charId FROM characters);");
cleanCount += stmt.executeUpdate("DELETE FROM item_attributes WHERE item_attributes.itemId NOT IN (SELECT object_id FROM items);");
cleanCount += stmt.executeUpdate("DELETE FROM item_elementals WHERE item_elementals.itemId NOT IN (SELECT object_id FROM items);");
// Misc
cleanCount += stmt.executeUpdate("DELETE FROM cursed_weapons WHERE cursed_weapons.charId NOT IN (SELECT charId FROM characters);");
cleanCount += stmt.executeUpdate("DELETE FROM heroes WHERE heroes.charId NOT IN (SELECT charId FROM characters);");
cleanCount += stmt.executeUpdate("DELETE FROM olympiad_nobles WHERE olympiad_nobles.charId NOT IN (SELECT charId FROM characters);");
@ -277,13 +235,6 @@ public abstract class IdFactory
cleanCount += stmt.executeUpdate("DELETE FROM siege_clans WHERE siege_clans.clan_id NOT IN (SELECT clan_id FROM clan_data);");
cleanCount += stmt.executeUpdate("DELETE FROM clan_notices WHERE clan_notices.clan_id NOT IN (SELECT clan_id FROM clan_data);");
cleanCount += stmt.executeUpdate("DELETE FROM auction_bid WHERE auction_bid.bidderId NOT IN (SELECT clan_id FROM clan_data);");
// Untested, leaving commented out until confirmation that it's safe/works properly. Was
// initially removed because of a bug. Search for idfactory.java changes in the trac for
// further info.
// cleanCount +=
// stmt.executeUpdate("DELETE FROM auction WHERE auction.id IN (SELECT id FROM clanhall WHERE ownerId <> 0) AND auction.sellerId=0;");
// cleanCount +=
// stmt.executeUpdate("DELETE FROM auction_bid WHERE auctionId NOT IN (SELECT id FROM auction)");
// Forum Related
cleanCount += stmt.executeUpdate("DELETE FROM forums WHERE forums.forum_owner_id NOT IN (SELECT clan_id FROM clan_data) AND forums.forum_parent=2;");
@ -300,11 +251,11 @@ public abstract class IdFactory
stmt.executeUpdate("UPDATE clanhall SET ownerId=0, paidUntil=0, paid=0 WHERE clanhall.ownerId NOT IN (SELECT clan_id FROM clan_data);");
stmt.executeUpdate("UPDATE fort SET owner=0 WHERE owner NOT IN (SELECT clan_id FROM clan_data);");
_log.info("Cleaned " + cleanCount + " elements from database in " + ((System.currentTimeMillis() - cleanupStart) / 1000) + " s");
LOGGER.info("Cleaned " + cleanCount + " elements from database in " + ((System.currentTimeMillis() - cleanupStart) / 1000) + " s");
}
catch (SQLException e)
{
_log.log(Level.WARNING, "Could not clean up database: " + e.getMessage(), e);
LOGGER.log(Level.WARNING, "Could not clean up database: " + e.getMessage(), e);
}
}
@ -315,11 +266,11 @@ public abstract class IdFactory
{
s.executeUpdate("DELETE FROM mods_wedding WHERE player1Id NOT IN (SELECT charId FROM characters)");
s.executeUpdate("DELETE FROM mods_wedding WHERE player2Id NOT IN (SELECT charId FROM characters)");
_log.info("Cleaned up invalid Weddings.");
LOGGER.info("Cleaned up invalid Weddings.");
}
catch (SQLException e)
{
_log.log(Level.WARNING, "Could not clean up invalid Weddings: " + e.getMessage(), e);
LOGGER.log(Level.WARNING, "Could not clean up invalid Weddings: " + e.getMessage(), e);
}
}
@ -336,7 +287,7 @@ public abstract class IdFactory
cleanCount += stmt.executeUpdate();
}
}
_log.info("Cleaned " + cleanCount + " expired timestamps from database.");
LOGGER.info("Cleaned " + cleanCount + " expired timestamps from database.");
}
catch (SQLException e)
{

View File

@ -51,7 +51,7 @@ public class StackIDFactory extends IdFactory
{
_curOID = tmp_obj_ids[tmp_obj_ids.length - 1];
}
_log.info("Max Id = " + _curOID);
LOGGER.info("Max Id = " + _curOID);
int N = tmp_obj_ids.length;
for (int idx = 0; idx < N; idx++)
@ -60,12 +60,12 @@ public class StackIDFactory extends IdFactory
}
_curOID++;
_log.info("IdFactory: Next usable Object ID is: " + _curOID);
LOGGER.info("IdFactory: Next usable Object ID is: " + _curOID);
_initialized = true;
}
catch (Exception e)
{
_log.severe(getClass().getSimpleName() + ": Could not be initialized properly:" + e.getMessage());
LOGGER.severe(getClass().getSimpleName() + ": Could not be initialized properly:" + e.getMessage());
}
}
@ -89,10 +89,10 @@ public class StackIDFactory extends IdFactory
ps.setInt(2, id);
try (ResultSet rs = ps.executeQuery())
{
while (rs.next())
if (rs.next())
{
final int badId = rs.getInt(1);
_log.severe("Bad ID " + badId + " in DB found by: " + check);
LOGGER.severe("Bad ID " + badId + " in DB found by: " + check);
throw new RuntimeException();
}
}