StackIDFactory.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.Logger;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.L2DatabaseFactory;
  24. /**
  25. * This class ...
  26. *
  27. * @version $Revision: 1.3.2.1.2.7 $ $Date: 2005/04/11 10:06:12 $
  28. */
  29. public class StackIDFactory extends IdFactory
  30. {
  31. private static Logger _log = Logger.getLogger(IdFactory.class.getName());
  32. private int _curOID;
  33. private int _tempOID;
  34. private Stack<Integer> _freeOIDStack = new Stack<Integer>();
  35. protected StackIDFactory()
  36. {
  37. super();
  38. _curOID = FIRST_OID;
  39. _tempOID = FIRST_OID;
  40. Connection con = null;
  41. try
  42. {
  43. con = L2DatabaseFactory.getInstance().getConnection();
  44. //con.createStatement().execute("drop table if exists tmp_obj_id");
  45. int[] tmp_obj_ids = extractUsedObjectIDTable();
  46. if (tmp_obj_ids.length > 0)
  47. {
  48. _curOID = tmp_obj_ids[tmp_obj_ids.length - 1];
  49. }
  50. _log.info("Max Id = " + _curOID);
  51. int N = tmp_obj_ids.length;
  52. for (int idx = 0; idx < N; idx++)
  53. {
  54. N = insertUntil(tmp_obj_ids, idx, N, con);
  55. }
  56. _curOID++;
  57. _log.config("IdFactory: Next usable Object ID is: " + _curOID);
  58. _initialized = true;
  59. }
  60. catch (Exception e1)
  61. {
  62. e1.printStackTrace();
  63. _log.severe("ID Factory could not be initialized correctly:" + e1);
  64. }
  65. finally
  66. {
  67. try
  68. {
  69. con.close();
  70. }
  71. catch (Exception e)
  72. {
  73. }
  74. }
  75. }
  76. private int insertUntil(int[] tmp_obj_ids, int idx, int N, Connection con) throws SQLException
  77. {
  78. int id = tmp_obj_ids[idx];
  79. if (id == _tempOID)
  80. {
  81. _tempOID++;
  82. return N;
  83. }
  84. // check these IDs not present in DB
  85. if (Config.BAD_ID_CHECKING)
  86. {
  87. for (String check : ID_CHECKS)
  88. {
  89. PreparedStatement ps = con.prepareStatement(check);
  90. ps.setInt(1, _tempOID);
  91. //ps.setInt(1, _curOID);
  92. ps.setInt(2, id);
  93. ResultSet rs = ps.executeQuery();
  94. while (rs.next())
  95. {
  96. int badId = rs.getInt(1);
  97. _log.severe("Bad ID " + badId + " in DB found by: " + check);
  98. throw new RuntimeException();
  99. }
  100. rs.close();
  101. ps.close();
  102. }
  103. }
  104. //int hole = id - _curOID;
  105. int hole = id - _tempOID;
  106. if (hole > N - idx)
  107. hole = N - idx;
  108. for (int i = 1; i <= hole; i++)
  109. {
  110. //log.info("Free ID added " + (_tempOID));
  111. _freeOIDStack.push(_tempOID);
  112. _tempOID++;
  113. //_curOID++;
  114. }
  115. if (hole < N - idx)
  116. _tempOID++;
  117. return N - hole;
  118. }
  119. public static IdFactory getInstance()
  120. {
  121. return _instance;
  122. }
  123. @Override
  124. public synchronized int getNextId()
  125. {
  126. int id;
  127. if (!_freeOIDStack.empty())
  128. id = _freeOIDStack.pop();
  129. else
  130. {
  131. id = _curOID;
  132. _curOID = _curOID + 1;
  133. }
  134. return id;
  135. }
  136. /**
  137. * return a used Object ID back to the pool
  138. * @param object ID
  139. */
  140. @Override
  141. public synchronized void releaseId(int id)
  142. {
  143. _freeOIDStack.push(id);
  144. }
  145. @Override
  146. public int size()
  147. {
  148. return FREE_OBJECT_ID_SIZE - _curOID + FIRST_OID + _freeOIDStack.size();
  149. }
  150. }