Guest User

Untitled

a guest
Jun 14th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.69 KB | None | 0 0
  1. ## XLanguageSystem.java
  2. import java.util.HashMap;
  3. import java.util.LinkedList;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9.  
  10. public class XLanguageSystem {
  11. private String defaultLanguage = "en";
  12. private String tablePrefix = "lang_";
  13. private int cacheSize = 2;
  14.  
  15. private Connection connection = null;
  16. private Statement statement = null;
  17. private ResultSet result = null;
  18. private HashMap<String, HashMap<String, String>> cache = new HashMap<String, HashMap<String, String>>();
  19.  
  20. public XLanguageSystem() {
  21. System.out.println("Starting...");
  22.  
  23. // --- database specific variables - begin ---
  24. String host = "localhost";
  25. String username = "xcache";
  26. String password = "C3sbMsKp5zFXKRyy";
  27. String database = "xcache";
  28. // --- database specific variables - end ---
  29.  
  30. // --- connect to database - start ---
  31. try {
  32. System.out.print("Connection to database... ");
  33. Class.forName("com.mysql.jdbc.Driver").newInstance();
  34.  
  35. this.connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, username, password);
  36. System.out.println("DONE");
  37.  
  38. System.out.print("Creating Statement... ");
  39. this.statement = connection.createStatement();
  40. System.out.println("DONE\n");
  41. }
  42. catch(Exception e) {
  43. this.failure(e);
  44. }
  45. // --- connect to database - end ---
  46.  
  47. // --- getting languages - begin ---
  48. this.selectQuery("SHOW TABLES LIKE '" + this.tablePrefix + "%'");
  49.  
  50. try {
  51. while(this.result.next())
  52. this.createCache(this.result.getString(1).replace(this.tablePrefix, ""));
  53. }
  54. catch(Exception e) {
  55. this.failure(e);
  56. }
  57.  
  58. this.freeResultSet();
  59. // --- getting languages - end ---
  60. }
  61.  
  62. public void close() {
  63. System.out.println("\nShutting down...");
  64.  
  65. this.freeResultSet();
  66.  
  67. try {
  68. if(this.statement != null) {
  69. System.out.print("Closing Statement... ");
  70. this.statement.close();
  71. System.out.println("DONE");
  72. }
  73.  
  74. if(this.connection != null) {
  75. System.out.print("Closing Connection... ");
  76. this.connection.close();
  77. System.out.println("DONE");
  78. }
  79.  
  80. System.exit(0);
  81. }
  82. catch(Exception e) {
  83. this.failure(e);
  84. }
  85. }
  86.  
  87.  
  88. public String getEntry(String lang, String trans_id) {
  89. if(this.languageExists(lang)) { // language exists...
  90. if(this.cacheElementExists(lang, trans_id)) // element exists in language-cache...
  91. return this.getCacheElement(lang, trans_id); // return element from the language-cache
  92. else { // element does not exist in language-cache...
  93. String trans = this.selectTranslationFromDB(lang, trans_id); // select trans_id value from lang-table
  94.  
  95. if(trans == trans_id) // trans-id value was not found in lang-table...
  96. trans = this.getDefaultTranslation(trans_id); // get default-translation for trans_id
  97.  
  98. return trans;
  99. }
  100. }
  101. else // language does not exist...
  102. return this.getDefaultTranslation(trans_id);
  103. }
  104.  
  105. public void addLanguage(String lang) {
  106. if(!this.languageExists(lang)) { // if language to add does not exists allready...
  107. System.out.print("Adding language '" + lang + "'... ");
  108.  
  109. this.createCache(lang);
  110.  
  111. String sqlQuery = "CREATE TABLE " + this.tablePrefix + lang + " ("
  112. + "id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  113. + "trans_id VARCHAR(200) NOT NULL,"
  114. + "trans LONGTEXT NOT NULL,"
  115. + "userid INT(11) NOT NULL,"
  116. + "active TINYINT(1) DEFAULT '0',"
  117. + "KEY trans_id (trans_id),"
  118. + "KEY userid (userid)"
  119. + ") ENGINE=MyISAM;";
  120.  
  121. this.updateQuery(sqlQuery);
  122.  
  123. System.out.println("DONE");
  124. }
  125. else
  126. System.out.println("Language '" + lang + "' does allready exist.");
  127. }
  128.  
  129. public LinkedList<String> getAvailableLanguages() {
  130. return new LinkedList<String>(this.cache.keySet());
  131. }
  132.  
  133. public void removeCacheElement(String lang, String trans_id) {
  134. // if langueage exists and key exists in this language-cache... remove it...
  135. if(this.languageExists(lang) && this.cacheElementExists(lang, trans_id))
  136. this.cache.get(lang).remove(trans_id);
  137. }
  138.  
  139.  
  140. private void failure(Exception e) {
  141. System.out.println("FAILED\n");
  142.  
  143. e.printStackTrace();
  144.  
  145. System.exit(-1);
  146. }
  147.  
  148. private void updateQuery(String sqlQuery) {
  149. try {
  150. this.statement.executeUpdate(sqlQuery);
  151. }
  152. catch(Exception e) {
  153. this.failure(e);
  154. }
  155. }
  156.  
  157. private void selectQuery(String sqlQuery) {
  158. try {
  159. this.result = this.statement.executeQuery(sqlQuery);
  160. }
  161. catch(Exception e) {
  162. this.failure(e);
  163. }
  164. }
  165.  
  166. private void freeResultSet() {
  167. try {
  168. if(this.result != null)
  169. this.result.close();
  170. }
  171. catch(Exception e) {
  172. this.failure(e);
  173. }
  174. }
  175.  
  176. private void createCache(String lang) {
  177. this.cache.put(lang, new HashMap<String, String>());
  178. }
  179.  
  180. private String getCacheElement(String lang, String trans_id) {
  181. System.out.println("Fetching '" + trans_id + "' for language '" + lang + "' from cache...");
  182.  
  183. // get element and remove it temporarly from the cache...
  184. String trans = this.cache.get(lang).remove(trans_id);
  185.  
  186. this.cache.get(lang).put(trans_id, trans); // add element back to the cache {element needs to be removed and readded to implement LFU}
  187.  
  188. return trans;
  189. }
  190.  
  191. private String selectTranslationFromDB(String lang, String trans_id) {
  192. System.out.println("Fetching '" + trans_id + "' for language '" + lang + "' from database...");
  193.  
  194. this.selectQuery("SELECT trans FROM " + this.tablePrefix + lang + " WHERE trans_id = '" + trans_id + "' AND active = '1'");
  195.  
  196. String trans = "";
  197.  
  198. try {
  199. if(this.result.next()) { // found entry in lang-table...
  200. trans = this.result.getString(1); // get entry
  201.  
  202. if(this.cache.get(lang).size() == this.cacheSize) { // cache is full...
  203. // remove first key entry {is the oldes in terms of LFU-policy}...
  204. for(String key : this.cache.get(lang).keySet()) {
  205. this.cache.get(lang).remove(key);
  206. break;
  207. }
  208. }
  209.  
  210. this.cache.get(lang).put(trans_id, trans); // add found trans to cache...
  211. }
  212. else // entry not found in database...
  213. trans = trans_id; // set trans_id to be returned...
  214. }
  215. catch(Exception e) {
  216. this.failure(e);
  217. }
  218.  
  219. this.freeResultSet();
  220.  
  221. return trans;
  222. }
  223.  
  224. private String getDefaultTranslation(String trans_id) {
  225. if(this.cacheElementExists(this.defaultLanguage, trans_id)) // trans_id exists in lang-cache of default language...
  226. return this.getCacheElement(this.defaultLanguage, trans_id); // return trans-id value from cache
  227. else // trans_id does not exist in lang-cache of default language...
  228. return this.selectTranslationFromDB(this.defaultLanguage, trans_id); // select trans-id value from lang-table...
  229. }
  230.  
  231. private boolean languageExists(String lang) {
  232. return this.cache.containsKey(lang);
  233. }
  234.  
  235. private boolean cacheElementExists(String lang, String trans_id) {
  236. return this.cache.get(lang).containsKey(trans_id);
  237. }
  238.  
  239.  
  240. public void printCache(String lang) {
  241. System.out.println("\n==========");
  242.  
  243. for(String trans_id : this.cache.get(lang).keySet())
  244. System.out.println(trans_id + " => " + this.cache.get(lang).get(trans_id));
  245.  
  246. System.out.println("==========\n");
  247. }
  248.  
  249. }
  250.  
  251. ## Xtest.java
  252. public class Xtest {
  253. public static void main(String[] args) {
  254. XLanguageSystem xcache = new XLanguageSystem();
  255.  
  256. System.out.println("Available languages:");
  257.  
  258. for(String lang : xcache.getAvailableLanguages())
  259. System.out.println(lang);
  260.  
  261. System.out.println();
  262.  
  263. xcache.addLanguage("de");
  264. xcache.addLanguage("fr");
  265.  
  266. xcache.printCache("de");
  267.  
  268. System.out.println(xcache.getEntry("de", "foo"));
  269.  
  270. xcache.printCache("de");
  271.  
  272. System.out.println(xcache.getEntry("de", "bar"));
  273.  
  274. xcache.printCache("de");
  275.  
  276. System.out.println(xcache.getEntry("de", "tar"));
  277.  
  278. xcache.printCache("de");
  279.  
  280. System.out.println(xcache.getEntry("de", "bar"));
  281.  
  282. xcache.printCache("de");
  283.  
  284. System.out.println(xcache.getEntry("de", "foo"));
  285.  
  286. xcache.printCache("de");
  287.  
  288. System.out.println("Removing trans_id 'foo' from cache of language 'de'...");
  289.  
  290. xcache.removeCacheElement("de", "foo");
  291.  
  292. xcache.printCache("de");
  293.  
  294. xcache.close();
  295. }
  296. }
  297.  
  298. ## Xtest_output
  299. Starting...
  300. Connection to database... DONE
  301. Creating Statement... DONE
  302.  
  303. Available languages:
  304. de
  305. fr
  306. en
  307.  
  308. Language 'de' does allready exist.
  309. Language 'fr' does allready exist.
  310.  
  311. ==========
  312. ==========
  313.  
  314. Fetching 'foo' for language 'de' from database...
  315. foo ist foobar
  316.  
  317. ==========
  318. foo => foo ist foobar
  319. ==========
  320.  
  321. Fetching 'bar' for language 'de' from database...
  322. bar is barfoo
  323.  
  324. ==========
  325. foo => foo ist foobar
  326. bar => bar is barfoo
  327. ==========
  328.  
  329. Fetching 'tar' for language 'de' from database...
  330. tar ungleich zip
  331.  
  332. ==========
  333. tar => tar ungleich zip
  334. bar => bar is barfoo
  335. ==========
  336.  
  337. Fetching 'bar' for language 'de' from cache...
  338. bar is barfoo
  339.  
  340. ==========
  341. tar => tar ungleich zip
  342. bar => bar is barfoo
  343. ==========
  344.  
  345. Fetching 'foo' for language 'de' from database...
  346. foo ist foobar
  347.  
  348. ==========
  349. foo => foo ist foobar
  350. bar => bar is barfoo
  351. ==========
  352.  
  353. Removing trans_id 'foo' from cache of language 'de'...
  354.  
  355. ==========
  356. bar => bar is barfoo
  357. ==========
  358.  
  359.  
  360. Shutting down...
  361. Closing Statement... DONE
  362. Closing Connection... DONE
Add Comment
Please, Sign In to add comment