2
0

CompactionIDFactory.java 4.0 KB

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