CompactionIDFactory.java 3.8 KB

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