IdFactory.java 20 KB

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