IdFactory.java 19 KB

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