2
0

CompactionIDFactory.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * $Header: CompactionIDFactory.java, 24/08/2005 22:32:43 luisantonioa Exp $
  3. *
  4. * $Author: luisantonioa $
  5. * $Date: 24/08/2005 22:32:43 $
  6. * $Revision: 1 $
  7. * $Log: CompactionIDFactory.java,v $
  8. * Revision 1 24/08/2005 22:32:43 luisantonioa
  9. * Added copyright notice
  10. *
  11. *
  12. * This program is free software: you can redistribute it and/or modify it under
  13. * the terms of the GNU General Public License as published by the Free Software
  14. * Foundation, either version 3 of the License, or (at your option) any later
  15. * version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License along with
  23. * this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. package com.l2jserver.gameserver.idfactory;
  26. import java.sql.Connection;
  27. import java.sql.PreparedStatement;
  28. import java.sql.ResultSet;
  29. import java.sql.SQLException;
  30. import java.util.logging.Level;
  31. import java.util.logging.Logger;
  32. import com.l2jserver.Config;
  33. import com.l2jserver.L2DatabaseFactory;
  34. /**
  35. * This class ...
  36. * 18.8.2010 - JIV - Disabling until someone update it
  37. *
  38. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  39. */
  40. @Deprecated
  41. public class CompactionIDFactory extends IdFactory
  42. {
  43. private static Logger _log = Logger.getLogger(CompactionIDFactory.class.getName());
  44. private int _curOID;
  45. private int _freeSize;
  46. protected CompactionIDFactory()
  47. {
  48. super();
  49. _curOID = FIRST_OID;
  50. _freeSize = 0;
  51. Connection con = null;
  52. try
  53. {
  54. con = L2DatabaseFactory.getInstance().getConnection();
  55. //con.createStatement().execute("drop table if exists tmp_obj_id");
  56. int[] tmp_obj_ids = extractUsedObjectIDTable();
  57. int N = tmp_obj_ids.length;
  58. for (int idx = 0; idx < N; idx++)
  59. {
  60. N = insertUntil(tmp_obj_ids, idx, N, con);
  61. }
  62. _curOID++;
  63. _log.info("IdFactory: Next usable Object ID is: " + _curOID);
  64. _initialized = true;
  65. }
  66. catch (Exception e)
  67. {
  68. _log.log(Level.SEVERE, "ID Factory could not be initialized correctly: " + e.getMessage(), e);
  69. }
  70. finally
  71. {
  72. L2DatabaseFactory.close(con);
  73. }
  74. }
  75. private int insertUntil(int[] tmp_obj_ids, int idx, int N, Connection con) throws SQLException
  76. {
  77. int id = tmp_obj_ids[idx];
  78. if (id == _curOID)
  79. {
  80. _curOID++;
  81. return N;
  82. }
  83. // check these IDs not present in DB
  84. if (Config.BAD_ID_CHECKING)
  85. {
  86. for (String check : ID_CHECKS)
  87. {
  88. PreparedStatement ps = con.prepareStatement(check);
  89. ps.setInt(1, _curOID);
  90. ps.setInt(2, id);
  91. ResultSet rs = ps.executeQuery();
  92. while (rs.next())
  93. {
  94. int badId = rs.getInt(1);
  95. _log.severe("Bad ID " + badId + " in DB found by: " + check);
  96. throw new RuntimeException();
  97. }
  98. rs.close();
  99. ps.close();
  100. }
  101. }
  102. int hole = id - _curOID;
  103. if (hole > N - idx)
  104. hole = N - idx;
  105. for (int i = 1; i <= hole; i++)
  106. {
  107. id = tmp_obj_ids[N - i];
  108. _log.info("Compacting DB object ID=" + id + " into " + (_curOID));
  109. for (String update : ID_UPDATES)
  110. {
  111. PreparedStatement ps = con.prepareStatement(update);
  112. ps.setInt(1, _curOID);
  113. ps.setInt(2, id);
  114. ps.execute();
  115. ps.close();
  116. }
  117. _curOID++;
  118. }
  119. if (hole < N - idx)
  120. _curOID++;
  121. return N - hole;
  122. }
  123. @Override
  124. public synchronized int getNextId()
  125. {
  126. /*if (_freeSize == 0)*/return _curOID++;
  127. /* else
  128. return _freeOIDs[--_freeSize];*/
  129. }
  130. @Override
  131. public synchronized void releaseId(int id)
  132. {
  133. //dont release ids until we are sure it isnt messing up
  134. /* if (_freeSize >= _freeOIDs.length)
  135. {
  136. int[] tmp = new int[_freeSize + STACK_SIZE_INCREMENT];
  137. System.arraycopy(_freeOIDs, 0, tmp, 0, _freeSize);
  138. _freeOIDs = tmp;
  139. }
  140. _freeOIDs[_freeSize++] = id;*/
  141. }
  142. @Override
  143. public int size()
  144. {
  145. return _freeSize + LAST_OID - FIRST_OID;
  146. }
  147. }