CompactionIDFactory.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.Logger;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.L2DatabaseFactory;
  33. /**
  34. * This class ...
  35. *
  36. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  37. */
  38. public class CompactionIDFactory extends IdFactory
  39. {
  40. private static Logger _log = Logger.getLogger(CompactionIDFactory.class.getName());
  41. private int _curOID;
  42. private int _freeSize;
  43. protected CompactionIDFactory()
  44. {
  45. super();
  46. _curOID = FIRST_OID;
  47. _freeSize = 0;
  48. Connection con = null;
  49. try
  50. {
  51. con = L2DatabaseFactory.getInstance().getConnection();
  52. //con.createStatement().execute("drop table if exists tmp_obj_id");
  53. int[] tmp_obj_ids = extractUsedObjectIDTable();
  54. int N = tmp_obj_ids.length;
  55. for (int idx = 0; idx < N; idx++)
  56. {
  57. N = insertUntil(tmp_obj_ids, idx, N, con);
  58. }
  59. _curOID++;
  60. _log.config("IdFactory: Next usable Object ID is: " + _curOID);
  61. _initialized = true;
  62. }
  63. catch (Exception e1)
  64. {
  65. e1.printStackTrace();
  66. _log.severe("ID Factory could not be initialized correctly:" + e1);
  67. }
  68. finally
  69. {
  70. try
  71. {
  72. con.close();
  73. }
  74. catch (Exception e)
  75. {
  76. }
  77. }
  78. }
  79. private int insertUntil(int[] tmp_obj_ids, int idx, int N, Connection con) throws SQLException
  80. {
  81. int id = tmp_obj_ids[idx];
  82. if (id == _curOID)
  83. {
  84. _curOID++;
  85. return N;
  86. }
  87. // check these IDs not present in DB
  88. if (Config.BAD_ID_CHECKING)
  89. {
  90. for (String check : ID_CHECKS)
  91. {
  92. PreparedStatement ps = con.prepareStatement(check);
  93. ps.setInt(1, _curOID);
  94. ps.setInt(2, id);
  95. ResultSet rs = ps.executeQuery();
  96. while (rs.next())
  97. {
  98. int badId = rs.getInt(1);
  99. _log.severe("Bad ID " + badId + " in DB found by: " + check);
  100. throw new RuntimeException();
  101. }
  102. rs.close();
  103. ps.close();
  104. }
  105. }
  106. int hole = id - _curOID;
  107. if (hole > N - idx)
  108. hole = N - idx;
  109. for (int i = 1; i <= hole; i++)
  110. {
  111. id = tmp_obj_ids[N - i];
  112. _log.info("Compacting DB object ID=" + id + " into " + (_curOID));
  113. for (String update : ID_UPDATES)
  114. {
  115. PreparedStatement ps = con.prepareStatement(update);
  116. ps.setInt(1, _curOID);
  117. ps.setInt(2, id);
  118. ps.execute();
  119. ps.close();
  120. }
  121. _curOID++;
  122. }
  123. if (hole < N - idx)
  124. _curOID++;
  125. return N - hole;
  126. }
  127. @Override
  128. public synchronized int getNextId()
  129. {
  130. /*if (_freeSize == 0)*/return _curOID++;
  131. /* else
  132. return _freeOIDs[--_freeSize];*/
  133. }
  134. @Override
  135. public synchronized void releaseId(int id)
  136. {
  137. //dont release ids until we are sure it isnt messing up
  138. /* if (_freeSize >= _freeOIDs.length)
  139. {
  140. int[] tmp = new int[_freeSize + STACK_SIZE_INCREMENT];
  141. System.arraycopy(_freeOIDs, 0, tmp, 0, _freeSize);
  142. _freeOIDs = tmp;
  143. }
  144. _freeOIDs[_freeSize++] = id;*/
  145. }
  146. @Override
  147. public int size()
  148. {
  149. return _freeSize + LAST_OID - FIRST_OID;
  150. }
  151. }