StackIDFactory.java 3.8 KB

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