123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package com.l2jserver.gameserver.idfactory;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import com.l2jserver.Config;
- import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
- @Deprecated
- public class CompactionIDFactory extends IdFactory
- {
- private int _curOID;
- private final int _freeSize;
-
- protected CompactionIDFactory()
- {
- super();
- _curOID = FIRST_OID;
- _freeSize = 0;
-
- try (Connection con = ConnectionFactory.getInstance().getConnection())
- {
- 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;
- }
-
- 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())
- {
- int badId = rs.getInt(1);
- _log.severe(getClass().getSimpleName() + ": Bad ID " + badId + " in DB found by: " + check);
- throw new RuntimeException();
- }
- }
- }
- }
- }
-
- int hole = id - _curOID;
- if (hole > (N - idx))
- {
- hole = N - idx;
- }
- 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()
- {
-
-
- return _curOID++;
-
-
- }
-
- @Override
- public synchronized void releaseId(int id)
- {
-
-
-
-
- }
-
- @Override
- public int size()
- {
- return (_freeSize + LAST_OID) - FIRST_OID;
- }
- }
|