IdFactory.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under the terms of the
  3. * GNU General Public License as published by the Free Software Foundation, either version 3 of the
  4. * License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  7. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8. * General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with this program. If
  11. * not, see <http://www.gnu.org/licenses/>.
  12. */
  13. package com.l2jserver.gameserver.idfactory;
  14. import gnu.trove.TIntArrayList;
  15. import java.sql.Connection;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.sql.Statement;
  20. import java.util.logging.Logger;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.L2DatabaseFactory;
  23. /**
  24. * This class ...
  25. *
  26. * @version $Revision: 1.3.2.1.2.7 $ $Date: 2005/04/11 10:06:12 $
  27. */
  28. public abstract class IdFactory
  29. {
  30. private static Logger _log = Logger.getLogger(IdFactory.class.getName());
  31. protected static final String[] ID_UPDATES = {
  32. "UPDATE items SET owner_id = ? WHERE owner_id = ?",
  33. "UPDATE items SET object_id = ? WHERE object_id = ?",
  34. "UPDATE character_quests SET charId = ? WHERE charId = ?",
  35. "UPDATE character_friends SET charId = ? WHERE charId = ?",
  36. "UPDATE character_friends SET friendId = ? WHERE friendId = ?",
  37. "UPDATE character_hennas SET charId = ? WHERE charId = ?",
  38. "UPDATE character_recipebook SET charId = ? WHERE charId = ?",
  39. "UPDATE character_recipeshoplist SET charId = ? WHERE charId = ?",
  40. "UPDATE character_shortcuts SET charId = ? WHERE charId = ?",
  41. "UPDATE character_shortcuts SET shortcut_id = ? WHERE shortcut_id = ? AND type = 1", // items
  42. "UPDATE character_macroses SET charId = ? WHERE charId = ?",
  43. "UPDATE character_skills SET charId = ? WHERE charId = ?",
  44. "UPDATE character_skills_save SET charId = ? WHERE charId = ?",
  45. "UPDATE character_subclasses SET charId = ? WHERE charId = ?",
  46. "UPDATE character_ui_actions SET charId = ? WHERE charId = ?",
  47. "UPDATE character_ui_categories SET charId = ? WHERE charId = ?",
  48. "UPDATE characters SET charId = ? WHERE charId = ?",
  49. "UPDATE characters SET clanid = ? WHERE clanid = ?",
  50. "UPDATE clan_data SET clan_id = ? WHERE clan_id = ?",
  51. "UPDATE siege_clans SET clan_id = ? WHERE clan_id = ?",
  52. "UPDATE clan_data SET ally_id = ? WHERE ally_id = ?",
  53. "UPDATE clan_data SET leader_id = ? WHERE leader_id = ?",
  54. "UPDATE pets SET item_obj_id = ? WHERE item_obj_id = ?",
  55. "UPDATE character_hennas SET charId = ? WHERE charId = ?",
  56. "UPDATE itemsonground SET object_id = ? WHERE object_id = ?",
  57. "UPDATE auction_bid SET bidderId = ? WHERE bidderId = ?",
  58. "UPDATE auction_watch SET charObjId = ? WHERE charObjId = ?",
  59. "UPDATE olympiad_fights SET charOneId = ? WHERE charOneId = ?",
  60. "UPDATE olympiad_fights SET charTwoId = ? WHERE charTwoId = ?",
  61. "UPDATE heroes_diary SET charId = ? WHERE charId = ?",
  62. "UPDATE olympiad_nobles SET charId = ? WHERE charId = ?",
  63. "UPDATE character_offline_trade SET charId = ? WHERE charId = ?",
  64. "UPDATE character_offline_trade_items SET charId = ? WHERE charId = ?",
  65. "UPDATE clanhall SET ownerId = ? WHERE ownerId = ?" };
  66. protected static final String[] ID_CHECKS = {
  67. "SELECT owner_id FROM items WHERE object_id >= ? AND object_id < ?",
  68. "SELECT object_id FROM items WHERE object_id >= ? AND object_id < ?",
  69. "SELECT charId FROM character_quests WHERE charId >= ? AND charId < ?",
  70. "SELECT charId FROM character_friends WHERE charId >= ? AND charId < ?",
  71. "SELECT charId FROM character_friends WHERE friendId >= ? AND friendId < ?",
  72. "SELECT charId FROM character_hennas WHERE charId >= ? AND charId < ?",
  73. "SELECT charId FROM character_recipebook WHERE charId >= ? AND charId < ?",
  74. "SELECT charId FROM character_recipeshoplist WHERE charId >= ? AND charId < ?",
  75. "SELECT charId FROM character_shortcuts WHERE charId >= ? AND charId < ?",
  76. "SELECT charId FROM character_macroses WHERE charId >= ? AND charId < ?",
  77. "SELECT charId FROM character_skills WHERE charId >= ? AND charId < ?",
  78. "SELECT charId FROM character_skills_save WHERE charId >= ? AND charId < ?",
  79. "SELECT charId FROM character_subclasses WHERE charId >= ? AND charId < ?",
  80. "SELECT charId FROM character_ui_actions WHERE charId >= ? AND charId < ?",
  81. "SELECT charId FROM character_ui_categories WHERE charId >= ? AND charId < ?",
  82. "SELECT charId FROM characters WHERE charId >= ? AND charId < ?",
  83. "SELECT clanid FROM characters WHERE clanid >= ? AND clanid < ?",
  84. "SELECT clan_id FROM clan_data WHERE clan_id >= ? AND clan_id < ?",
  85. "SELECT clan_id FROM siege_clans WHERE clan_id >= ? AND clan_id < ?",
  86. "SELECT ally_id FROM clan_data WHERE ally_id >= ? AND ally_id < ?",
  87. "SELECT leader_id FROM clan_data WHERE leader_id >= ? AND leader_id < ?",
  88. "SELECT item_obj_id FROM pets WHERE item_obj_id >= ? AND item_obj_id < ?",
  89. "SELECT object_id FROM itemsonground WHERE object_id >= ? AND object_id < ?" };
  90. private static final String[] TIMESTAMPS_CLEAN = {
  91. "DELETE FROM character_instance_time WHERE time <= ?",
  92. "DELETE FROM character_skills_save WHERE restore_type = 1 AND systime <= ?" };
  93. protected boolean _initialized;
  94. public static final int FIRST_OID = 0x10000000;
  95. public static final int LAST_OID = 0x7FFFFFFF;
  96. public static final int FREE_OBJECT_ID_SIZE = LAST_OID - FIRST_OID;
  97. protected static final IdFactory _instance;
  98. protected IdFactory()
  99. {
  100. setAllCharacterOffline();
  101. cleanUpDB();
  102. cleanUpTimeStamps();
  103. }
  104. static
  105. {
  106. switch (Config.IDFACTORY_TYPE)
  107. {
  108. case Compaction:
  109. _instance = new CompactionIDFactory();
  110. break;
  111. case BitSet:
  112. _instance = new BitSetIDFactory();
  113. break;
  114. case Stack:
  115. _instance = new StackIDFactory();
  116. break;
  117. default:
  118. _instance = null;
  119. break;
  120. }
  121. }
  122. /**
  123. * Sets all character offline
  124. */
  125. private void setAllCharacterOffline()
  126. {
  127. Connection con = null;
  128. try
  129. {
  130. con = L2DatabaseFactory.getInstance().getConnection();
  131. Statement statement = con.createStatement();
  132. statement.executeUpdate("UPDATE characters SET online = 0");
  133. statement.close();
  134. _log.info("Updated characters online status.");
  135. }
  136. catch (SQLException e)
  137. {
  138. }
  139. finally
  140. {
  141. L2DatabaseFactory.close(con);
  142. }
  143. }
  144. /**
  145. * Cleans up Database
  146. */
  147. private void cleanUpDB()
  148. {
  149. Connection con = null;
  150. Statement stmt = null;
  151. try
  152. {
  153. long cleanupStart = System.currentTimeMillis();
  154. int cleanCount = 0;
  155. con = L2DatabaseFactory.getInstance().getConnection();
  156. stmt = con.createStatement();
  157. // Misc/Account Related
  158. // Please read the descriptions above each before uncommenting them. If you are still
  159. // unsure of what exactly it does, leave it commented out. This is for those who know
  160. // what they are doing. :)
  161. // Deletes only accounts that HAVE been logged into and have no characters associated
  162. // with the account.
  163. // cleanCount +=
  164. // stmt.executeUpdate("DELETE FROM accounts WHERE accounts.lastactive > 0 AND accounts.login NOT IN (SELECT account_name FROM characters);");
  165. // Deletes any accounts that don't have characters. Whether or not the player has ever
  166. // logged into the account.
  167. // cleanCount +=
  168. // stmt.executeUpdate("DELETE FROM accounts WHERE accounts.login NOT IN (SELECT account_name FROM characters);");
  169. // Deletes banned accounts that have not been logged into for xx amount of days
  170. // (specified at the end of the script, default is set to 90 days). This prevents
  171. // accounts from being deleted that were accidentally or temporarily banned.
  172. // cleanCount +=
  173. // stmt.executeUpdate("DELETE FROM accounts WHERE accounts.accessLevel < 0 AND DATEDIFF(CURRENT_DATE( ) , FROM_UNIXTIME(`lastactive`/1000)) > 90;");
  174. // cleanCount +=
  175. // stmt.executeUpdate("DELETE FROM characters WHERE characters.account_name NOT IN (SELECT login FROM accounts);");
  176. // If the character does not exist...
  177. cleanCount += stmt.executeUpdate("DELETE FROM character_friends WHERE character_friends.charId NOT IN (SELECT charId FROM characters);");
  178. cleanCount += stmt.executeUpdate("DELETE FROM character_friends WHERE character_friends.friendId NOT IN (SELECT charId FROM characters);");
  179. cleanCount += stmt.executeUpdate("DELETE FROM character_hennas WHERE character_hennas.charId NOT IN (SELECT charId FROM characters);");
  180. cleanCount += stmt.executeUpdate("DELETE FROM character_macroses WHERE character_macroses.charId NOT IN (SELECT charId FROM characters);");
  181. cleanCount += stmt.executeUpdate("DELETE FROM character_quests WHERE character_quests.charId NOT IN (SELECT charId FROM characters);");
  182. cleanCount += stmt.executeUpdate("DELETE FROM character_recipebook WHERE character_recipebook.charId NOT IN (SELECT charId FROM characters);");
  183. cleanCount += stmt.executeUpdate("DELETE FROM character_recipeshoplist WHERE character_recipeshoplist.charId NOT IN (SELECT charId FROM characters);");
  184. cleanCount += stmt.executeUpdate("DELETE FROM character_shortcuts WHERE character_shortcuts.charId NOT IN (SELECT charId FROM characters);");
  185. cleanCount += stmt.executeUpdate("DELETE FROM character_skills WHERE character_skills.charId NOT IN (SELECT charId FROM characters);");
  186. cleanCount += stmt.executeUpdate("DELETE FROM character_skills_save WHERE character_skills_save.charId NOT IN (SELECT charId FROM characters);");
  187. cleanCount += stmt.executeUpdate("DELETE FROM character_subclasses WHERE character_subclasses.charId NOT IN (SELECT charId FROM characters);");
  188. cleanCount += stmt.executeUpdate("DELETE FROM character_raid_points WHERE character_raid_points.charId NOT IN (SELECT charId FROM characters);");
  189. cleanCount += stmt.executeUpdate("DELETE FROM character_instance_time WHERE character_instance_time.charId NOT IN (SELECT charId FROM characters);");
  190. cleanCount += stmt.executeUpdate("DELETE FROM character_ui_actions WHERE character_ui_actions.charId NOT IN (SELECT charId FROM characters);");
  191. cleanCount += stmt.executeUpdate("DELETE FROM character_ui_categories WHERE character_ui_categories.charId NOT IN (SELECT charId FROM characters);");
  192. cleanCount += stmt.executeUpdate("DELETE FROM items WHERE items.owner_id NOT IN (SELECT charId FROM characters) AND items.owner_id NOT IN (SELECT clan_id FROM clan_data);");
  193. cleanCount += stmt.executeUpdate("DELETE FROM item_attributes WHERE item_attributes.itemId NOT IN (SELECT object_id FROM items);");
  194. cleanCount += stmt.executeUpdate("DELETE FROM cursed_weapons WHERE cursed_weapons.charId NOT IN (SELECT charId FROM characters);");
  195. cleanCount += stmt.executeUpdate("DELETE FROM heroes WHERE heroes.charId NOT IN (SELECT charId FROM characters);");
  196. cleanCount += stmt.executeUpdate("DELETE FROM olympiad_nobles WHERE olympiad_nobles.charId NOT IN (SELECT charId FROM characters);");
  197. cleanCount += stmt.executeUpdate("DELETE FROM olympiad_nobles_eom WHERE olympiad_nobles_eom.charId NOT IN (SELECT charId FROM characters);");
  198. cleanCount += stmt.executeUpdate("DELETE FROM pets WHERE pets.item_obj_id NOT IN (SELECT object_id FROM items);");
  199. cleanCount += stmt.executeUpdate("DELETE FROM seven_signs WHERE seven_signs.charId NOT IN (SELECT charId FROM characters);");
  200. cleanCount += stmt.executeUpdate("DELETE FROM merchant_lease WHERE merchant_lease.player_id NOT IN (SELECT charId FROM characters);");
  201. cleanCount += stmt.executeUpdate("DELETE FROM character_recommends WHERE character_recommends.charId NOT IN (SELECT charId FROM characters);");
  202. cleanCount += stmt.executeUpdate("DELETE FROM character_recommends WHERE character_recommends.target_id NOT IN (SELECT charId FROM characters);");
  203. cleanCount += stmt.executeUpdate("DELETE FROM clan_data WHERE clan_data.leader_id NOT IN (SELECT charId FROM characters);");
  204. cleanCount += stmt.executeUpdate("DELETE FROM clan_data WHERE clan_data.clan_id NOT IN (SELECT clanid FROM characters);");
  205. cleanCount += stmt.executeUpdate("DELETE FROM olympiad_fights WHERE olympiad_fights.charOneId NOT IN (SELECT charId FROM characters);");
  206. cleanCount += stmt.executeUpdate("DELETE FROM olympiad_fights WHERE olympiad_fights.charTwoId NOT IN (SELECT charId FROM characters);");
  207. cleanCount += stmt.executeUpdate("DELETE FROM heroes_diary WHERE heroes_diary.charId NOT IN (SELECT charId FROM characters);");
  208. cleanCount += stmt.executeUpdate("DELETE FROM character_offline_trade WHERE character_offline_trade.charId NOT IN (SELECT charId FROM characters);");
  209. cleanCount += stmt.executeUpdate("DELETE FROM character_offline_trade_items WHERE character_offline_trade_items.charId NOT IN (SELECT charId FROM characters);");
  210. cleanCount += stmt.executeUpdate("DELETE FROM character_quest_global_data WHERE character_quest_global_data.charId NOT IN (SELECT charId FROM characters);");
  211. cleanCount += stmt.executeUpdate("DELETE FROM character_tpbookmark WHERE character_tpbookmark.charId NOT IN (SELECT charId FROM characters);");
  212. // If the clan does not exist...
  213. cleanCount += stmt.executeUpdate("DELETE FROM clan_privs WHERE clan_privs.clan_id NOT IN (SELECT clan_id FROM clan_data);");
  214. cleanCount += stmt.executeUpdate("DELETE FROM clan_skills WHERE clan_skills.clan_id NOT IN (SELECT clan_id FROM clan_data);");
  215. cleanCount += stmt.executeUpdate("DELETE FROM clan_subpledges WHERE clan_subpledges.clan_id NOT IN (SELECT clan_id FROM clan_data);");
  216. cleanCount += stmt.executeUpdate("DELETE FROM clan_wars WHERE clan_wars.clan1 NOT IN (SELECT clan_id FROM clan_data);");
  217. cleanCount += stmt.executeUpdate("DELETE FROM clan_wars WHERE clan_wars.clan2 NOT IN (SELECT clan_id FROM clan_data);");
  218. cleanCount += stmt.executeUpdate("DELETE FROM clanhall_functions WHERE clanhall_functions.hall_id NOT IN (SELECT id FROM clanhall WHERE ownerId <> 0);");
  219. cleanCount += stmt.executeUpdate("DELETE FROM siege_clans WHERE siege_clans.clan_id NOT IN (SELECT clan_id FROM clan_data);");
  220. cleanCount += stmt.executeUpdate("DELETE FROM clan_notices WHERE clan_notices.clan_id NOT IN (SELECT clan_id FROM clan_data);");
  221. cleanCount += stmt.executeUpdate("DELETE FROM auction_bid WHERE auction_bid.bidderId NOT IN (SELECT clan_id FROM clan_data);");
  222. // Untested, leaving commented out until confirmation that it's safe/works properly. Was
  223. // initially removed because of a bug. Search for idfactory.java changes in the trac for
  224. // further info.
  225. // cleanCount +=
  226. // stmt.executeUpdate("DELETE FROM auction WHERE auction.id IN (SELECT id FROM clanhall WHERE ownerId <> 0) AND auction.sellerId=0;");
  227. // cleanCount +=
  228. // stmt.executeUpdate("DELETE FROM auction_bid WHERE auctionId NOT IN (SELECT id FROM auction)");
  229. // Forum Related
  230. cleanCount += stmt.executeUpdate("DELETE FROM forums WHERE forums.forum_owner_id NOT IN (SELECT clan_id FROM clan_data) AND forums.forum_parent=2;");
  231. cleanCount += stmt.executeUpdate("DELETE FROM forums WHERE forums.forum_owner_id NOT IN (SELECT charId FROM characters) AND forums.forum_parent=3;");
  232. cleanCount += stmt.executeUpdate("DELETE FROM posts WHERE posts.post_forum_id NOT IN (SELECT forum_id FROM forums);");
  233. cleanCount += stmt.executeUpdate("DELETE FROM topic WHERE topic.topic_forum_id NOT IN (SELECT forum_id FROM forums);");
  234. // Update needed items after cleaning has taken place.
  235. stmt.executeUpdate("UPDATE clan_data SET auction_bid_at = 0 WHERE auction_bid_at NOT IN (SELECT auctionId FROM auction_bid);");
  236. stmt.executeUpdate("UPDATE clan_subpledges SET leader_id=0 WHERE clan_subpledges.leader_id NOT IN (SELECT charId FROM characters) AND leader_id > 0;");
  237. stmt.executeUpdate("UPDATE castle SET taxpercent=0 WHERE castle.id NOT IN (SELECT hasCastle FROM clan_data);");
  238. stmt.executeUpdate("UPDATE characters SET clanid=0, clan_privs=0, wantspeace=0, subpledge=0, lvl_joined_academy=0, apprentice=0, sponsor=0, clan_join_expiry_time=0, clan_create_expiry_time=0 WHERE characters.clanid > 0 AND characters.clanid NOT IN (SELECT clan_id FROM clan_data);");
  239. stmt.executeUpdate("UPDATE clanhall SET ownerId=0, paidUntil=0, paid=0 WHERE clanhall.ownerId NOT IN (SELECT clan_id FROM clan_data);");
  240. stmt.executeUpdate("UPDATE fort SET owner=0 WHERE owner NOT IN (SELECT clan_id FROM clan_data);");
  241. _log.info("Cleaned " + cleanCount + " elements from database in "+((System.currentTimeMillis()-cleanupStart)/1000)+" s");
  242. stmt.close();
  243. }
  244. catch (SQLException e)
  245. {
  246. }
  247. finally
  248. {
  249. L2DatabaseFactory.close(con);
  250. }
  251. }
  252. private void cleanUpTimeStamps()
  253. {
  254. Connection con = null;
  255. PreparedStatement stmt = null;
  256. try
  257. {
  258. int cleanCount = 0;
  259. con = L2DatabaseFactory.getInstance().getConnection();
  260. for (String line : TIMESTAMPS_CLEAN)
  261. {
  262. stmt = con.prepareStatement(line);
  263. stmt.setLong(1, System.currentTimeMillis());
  264. cleanCount += stmt.executeUpdate();
  265. stmt.close();
  266. }
  267. _log.info("Cleaned " + cleanCount + " expired timestamps from database.");
  268. }
  269. catch (SQLException e)
  270. {
  271. }
  272. finally
  273. {
  274. L2DatabaseFactory.close(con);
  275. }
  276. }
  277. /**
  278. * @param con
  279. * @return
  280. * @throws SQLException
  281. */
  282. protected final int[] extractUsedObjectIDTable() throws Exception
  283. {
  284. Connection con = null;
  285. try
  286. {
  287. con = L2DatabaseFactory.getInstance().getConnection();
  288. Statement statement = null;
  289. ResultSet rset = null;
  290. statement = con.createStatement();
  291. final TIntArrayList temp = new TIntArrayList();
  292. rset = statement.executeQuery("SELECT COUNT(*) FROM characters");
  293. rset.next();
  294. temp.ensureCapacity(rset.getInt(1));
  295. rset = statement.executeQuery("SELECT charId FROM characters");
  296. while (rset.next())
  297. {
  298. temp.add(rset.getInt(1));
  299. }
  300. rset = statement.executeQuery("SELECT COUNT(*) FROM items");
  301. rset.next();
  302. temp.ensureCapacity(temp.size() + rset.getInt(1));
  303. rset = statement.executeQuery("SELECT object_id FROM items");
  304. while (rset.next())
  305. {
  306. temp.add(rset.getInt(1));
  307. }
  308. rset = statement.executeQuery("SELECT COUNT(*) FROM clan_data");
  309. rset.next();
  310. temp.ensureCapacity(temp.size() + rset.getInt(1));
  311. rset = statement.executeQuery("SELECT clan_id FROM clan_data");
  312. while (rset.next())
  313. {
  314. temp.add(rset.getInt(1));
  315. }
  316. rset = statement.executeQuery("SELECT COUNT(*) FROM itemsonground");
  317. rset.next();
  318. temp.ensureCapacity(temp.size() + rset.getInt(1));
  319. rset = statement.executeQuery("SELECT object_id FROM itemsonground");
  320. while (rset.next())
  321. {
  322. temp.add(rset.getInt(1));
  323. }
  324. rset = statement.executeQuery("SELECT COUNT(*) FROM messages");
  325. rset.next();
  326. temp.ensureCapacity(temp.size() + rset.getInt(1));
  327. rset = statement.executeQuery("SELECT messageId FROM messages");
  328. while (rset.next())
  329. {
  330. temp.add(rset.getInt(1));
  331. }
  332. temp.sort();
  333. return temp.toNativeArray();
  334. }
  335. finally
  336. {
  337. L2DatabaseFactory.close(con);
  338. }
  339. }
  340. public boolean isInitialized()
  341. {
  342. return _initialized;
  343. }
  344. public static IdFactory getInstance()
  345. {
  346. return _instance;
  347. }
  348. public abstract int getNextId();
  349. /**
  350. * return a used Object ID back to the pool
  351. *
  352. * @param object
  353. * ID
  354. */
  355. public abstract void releaseId(int id);
  356. public abstract int size();
  357. }