From aff2ddae5b7219e9149e4806400147587e66fabf Mon Sep 17 00:00:00 2001 From: jenzur Date: Sun, 3 Mar 2019 23:32:36 +0100 Subject: [PATCH] replacing most arraylists with concurrentmaps due to them requirring less bytes per header since they should be stored bucketwise --- .../main/java/DataLayer/DBCPDataSource.java | 1 - .../src/main/java/DataLayer/DataMapper.java | 60 +-- .../java/FunctionLayer/MYSQLDatahandler.java | 345 +++++++++--------- .../FunctionLayer/MessageResponseHandler.java | 14 +- .../PresentationLayer/DiscordHandler.java | 7 +- 5 files changed, 200 insertions(+), 227 deletions(-) diff --git a/ArtificialAutism/src/main/java/DataLayer/DBCPDataSource.java b/ArtificialAutism/src/main/java/DataLayer/DBCPDataSource.java index 93574be7..ae48d11a 100644 --- a/ArtificialAutism/src/main/java/DataLayer/DBCPDataSource.java +++ b/ArtificialAutism/src/main/java/DataLayer/DBCPDataSource.java @@ -21,7 +21,6 @@ public class DBCPDataSource { try { ds.setDriver(new com.mysql.cj.jdbc.Driver()); ds.setUrl("jdbc:mysql://151.80.230.149:3306/ArtificialAutism?useLegacyDatetimeCode=false&serverTimezone=UTC"); - //ds.setUrl("jdbc:mysql://localhost:3306/ArtificialAutism?useLegacyDatetimeCode=false&serverTimezone=UTC"); ds.setUsername("ArtificialAutism"); ds.setPassword("b423b54bwbfb1340438fn"); ds.setMaxTotal(-1); diff --git a/ArtificialAutism/src/main/java/DataLayer/DataMapper.java b/ArtificialAutism/src/main/java/DataLayer/DataMapper.java index f1a0281f..f108d563 100644 --- a/ArtificialAutism/src/main/java/DataLayer/DataMapper.java +++ b/ArtificialAutism/src/main/java/DataLayer/DataMapper.java @@ -17,6 +17,7 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -72,24 +73,22 @@ public class DataMapper { return str; } - public static void InsertMYSQLStrings(List str) throws CustomError { + public static void InsertMYSQLStrings(ConcurrentMap str) throws CustomError { Connection l_cCon = null; PreparedStatement l_pStatement = null; ResultSet l_rsSearch = null; String l_sSQL = "INSERT IGNORE `Sentences` (`Strings`) VALUES (?)"; try { - if (str != null && str.size() > 0) { - l_cCon = DBCPDataSource.getConnection(); - l_pStatement = l_cCon.prepareStatement(l_sSQL, java.sql.ResultSet.TYPE_FORWARD_ONLY, - java.sql.ResultSet.CONCUR_READ_ONLY); - l_pStatement.setFetchSize(Integer.MIN_VALUE); - for (String str1 : str) { - System.out.println("adding str1: " + str1 + "\n"); - l_pStatement.setString(1, str1); - l_pStatement.addBatch(); - } - l_pStatement.executeBatch(); + l_cCon = DBCPDataSource.getConnection(); + l_pStatement = l_cCon.prepareStatement(l_sSQL, java.sql.ResultSet.TYPE_FORWARD_ONLY, + java.sql.ResultSet.CONCUR_READ_ONLY); + l_pStatement.setFetchSize(Integer.MIN_VALUE); + for (String str1 : str.values()) { + System.out.println("adding str1: " + str1 + "\n"); + l_pStatement.setString(1, str1); + l_pStatement.addBatch(); } + l_pStatement.executeBatch(); } catch (SQLException ex) { throw new CustomError("failed in DataMapper " + ex.getMessage()); } finally { @@ -111,40 +110,7 @@ public class DataMapper { return count; } - public static List getAllSementicMatrixes() throws CustomError { - //https://stackoverflow.com/questions/5157476/resultset-behavior-with-mysql-database-does-it-store-all-rows-in-memory/5159999#5159999 - //https://stackoverflow.com/questions/3682614/how-to-read-all-rows-from-huge-table - int count = getSementicsDBRows(); - int counter2 = 0; - int hardCapRetrieveCount = 500000; - List WS4JList = new ArrayList(count + 1); - while (count > counter2) { - try (Connection l_cCon = DBCPDataSource.getConnection()) { - l_cCon.setAutoCommit(false); - String l_sSQL = "SELECT * FROM `WordMatrix` WHERE ID > " + counter2 + " AND ID < " + (counter2 + hardCapRetrieveCount); - try (PreparedStatement l_pStatement = l_cCon.prepareStatement(l_sSQL, java.sql.ResultSet.TYPE_FORWARD_ONLY, - java.sql.ResultSet.CONCUR_READ_ONLY)) { - l_pStatement.setFetchSize(Integer.MIN_VALUE); - try (ResultSet l_rsSearch = l_pStatement.executeQuery()) { - int i = 0; - while (l_rsSearch.next() && i < hardCapRetrieveCount) { - SimilarityMatrix ws4j = new SimilarityMatrix(l_rsSearch.getString(1), l_rsSearch.getString(2), l_rsSearch.getDouble(3)); - //find something cheaper than arraylist probably - WS4JList.add(ws4j); - System.out.println("i: " + i + "\n" + "free memory: " + Runtime.getRuntime().freeMemory() + "\ncounter2: " + counter2 + "\n"); - i++; - counter2++; - } - } - } - } catch (SQLException ex) { - Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex); - } - } - return WS4JList; - } - - public static void insertSementicMatrixes(List WS4JListUpdate) throws CustomError { + public static void insertSementicMatrixes(ConcurrentMap WS4JListUpdate) throws CustomError { Connection l_cCon = null; PreparedStatement l_pStatement = null; ResultSet l_rsSearch = null; @@ -155,7 +121,7 @@ public class DataMapper { java.sql.ResultSet.CONCUR_READ_ONLY); l_pStatement.setFetchSize(Integer.MIN_VALUE); System.out.println("Matrix update size: " + WS4JListUpdate.size()); - for (SimilarityMatrix ws4j : WS4JListUpdate) { + for (SimilarityMatrix ws4j : WS4JListUpdate.values()) { l_pStatement.setString(1, ws4j.getPrimaryString()); l_pStatement.setString(2, ws4j.getSecondaryString()); l_pStatement.setDouble(3, ws4j.getDistance()); diff --git a/ArtificialAutism/src/main/java/FunctionLayer/MYSQLDatahandler.java b/ArtificialAutism/src/main/java/FunctionLayer/MYSQLDatahandler.java index c8bbc2b5..1f2e3988 100644 --- a/ArtificialAutism/src/main/java/FunctionLayer/MYSQLDatahandler.java +++ b/ArtificialAutism/src/main/java/FunctionLayer/MYSQLDatahandler.java @@ -27,16 +27,13 @@ import java.io.IOException; import java.io.StringReader; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Random; -import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutionException; @@ -44,7 +41,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import java.util.function.Consumer; import java.util.logging.Level; import java.util.logging.Logger; @@ -167,141 +163,76 @@ public class MYSQLDatahandler { public synchronized void checkIfUpdateMatrixes() { refreshMatrixFromDB = false; - int calculationBoundaries = 10; - int updateBadgesInteger = 500; - if (stopwatch1.elapsed(TimeUnit.SECONDS) >= EXPIRE_TIME_IN_SECONDS1) { - refreshMatrixFromDB = true; - if (threadCounter == 0) { - lHMSMX = DataMapper.getAllRelationScores(); - stopwatch1.reset(); - } - } - if (stringCache.values().size() > 10 && !refreshMatrixFromDB) { - if (multiprocessCalculations.size() <= (calculationBoundaries * calculationBoundaries)) { - threadCounter++; - List strList = new ArrayList(stringCache.values()); - List updateLocal = updatedRows; - int random = -1; - if (!updateLocal.contains(random)) { - updatedRows.add(random); - } - Collections.sort(updateLocal); - while (updateLocal.contains(random)) { - random = new Random().nextInt(strList.size() - 6); - int indexPrev = Collections.binarySearch(updateLocal, random); - int indexNext = Collections.binarySearch(updateLocal, random + 6); - //-1 will always be index 0 - if (indexPrev > 0 && indexNext > 0) { - indexPrev = updateLocal.get(indexPrev); - indexNext = updateLocal.get(indexNext); - } - random = indexPrev < random - 5 && indexNext < random ? random : -1; - } - updatedRows.add(random); - semeticsUpdateCount = random; - int beginindex = semeticsUpdateCount; - semeticsUpdateCount += calculationBoundaries / 2; - int temp = semeticsUpdateCount; - System.out.println("beginindex: " + beginindex + "\ntemp: " + temp + "\n"); - List strIndexNavigator = new ArrayList(); - strList.subList(beginindex, temp).forEach((str) -> { - strIndexNavigator.add(str); - multiprocessCalculations.add(str); - }); - new Thread(() -> { - LinkedHashMap> LHMSMXLocal = lHMSMX; - List strIndexNavigatorL = new ArrayList(strIndexNavigator); - List strIndexAll = new ArrayList(strList); - List randomIndexesToUpdate = new ArrayList(); - int indexes = updateBadgesInteger; - if (indexes >= strIndexAll.size()) { - indexes = strIndexAll.size() - 1; - } - int beginindexes = new Random().nextInt((strIndexAll.size()) - indexes); - strIndexAll.subList(beginindexes, beginindexes + indexes).forEach((str) -> { - randomIndexesToUpdate.add(str); - }); - List matrixUpdateList = new ArrayList(); - List> futures = new ArrayList(); - ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); - strIndexNavigatorL.forEach((str) -> { - randomIndexesToUpdate.stream().filter((str1) -> (!str.equals(str1))).forEachOrdered((str1) -> { - boolean present = false; - if (multiprocessCalculations.contains(str1)) { - present = true; - } else if (LHMSMXLocal.containsKey(str)) { - LinkedHashMap orDefault = LHMSMXLocal.get(str); - if (orDefault.containsKey(str1)) { - present = true; - } - } else if (LHMSMXLocal.containsKey(str1)) { - LinkedHashMap orDefault = LHMSMXLocal.get(str1); - if (orDefault.containsKey(str)) { - present = true; - } - } - if (!present) { - SimilarityMatrix SMX = new SimilarityMatrix(str, str1); - Callable worker = new SentimentAnalyzerTest(str, str1, SMX); - futures.add(executor.submit(worker)); - } - }); - }); - executor.shutdown(); - try { - System.out.println("finished worker assignment, futures size: " + futures.size() + "\n"); - for (Future future : futures) { - SimilarityMatrix SMX = future.get(); - System.out.println("SMX primary: " + SMX.getPrimaryString() + "\nSMX Secondary: " + SMX.getSecondaryString() - + "\nScore: " + SMX.getDistance() + "\n"); - LinkedHashMap get = lHMSMX.getOrDefault(SMX.getPrimaryString(), null); - if (get == null) { - get = new LinkedHashMap(); - } - get.put(SMX.getSecondaryString(), SMX.getDistance()); - lHMSMX.put(SMX.getPrimaryString(), get); - matrixUpdateList.add(SMX); - } - } catch (InterruptedException | ExecutionException ex) { - Logger.getLogger(MYSQLDatahandler.class.getName()).log(Level.SEVERE, null, ex); - } - new Thread(() -> { - try { - if (!matrixUpdateList.isEmpty()) { - DataMapper.insertSementicMatrixes(matrixUpdateList); - System.out.println("finished datamapper semetic insert"); - } - threadCounter--; - System.out.println("\nthreadCounter: " + threadCounter + "\n"); - } catch (CustomError ex) { - Logger.getLogger(MYSQLDatahandler.class - .getName()).log(Level.SEVERE, null, ex); - } - }).start(); - }). - start(); - } else { + int calculationBoundaries = 6; + int updateBadgesInteger = 65; + while (lHMSMX.size() < (stringCache.values().size() * stringCache.values().size()) - stringCache.values().size()) { + if (stopwatch1.elapsed(TimeUnit.SECONDS) >= EXPIRE_TIME_IN_SECONDS1) { + refreshMatrixFromDB = true; if (threadCounter == 0) { + lHMSMX = DataMapper.getAllRelationScores(); + stopwatch1.reset(); + } + } + if (stringCache.values().size() > 10 && !refreshMatrixFromDB) { + if (multiprocessCalculations.size() <= (calculationBoundaries * calculationBoundaries)) { threadCounter++; + ConcurrentMap stringCachelocal = stringCache; + List updateLocal = updatedRows; + int random = -1; + if (!updateLocal.contains(random)) { + updatedRows.add(random); + } + Collections.sort(updateLocal); + while (updateLocal.contains(random)) { + random = new Random().nextInt(stringCachelocal.values().size() - 6); + int indexPrev = Collections.binarySearch(updateLocal, random); + int indexNext = Collections.binarySearch(updateLocal, random + 6); + //-1 will always be index 0 + if (indexPrev > 0 && indexNext > 0) { + indexPrev = updateLocal.get(indexPrev); + indexNext = updateLocal.get(indexNext); + } + random = indexPrev < random - 5 && indexNext < random ? random : -1; + } + updatedRows.add(random); + semeticsUpdateCount = random; + int beginindex = semeticsUpdateCount; + semeticsUpdateCount += calculationBoundaries / 2; + int temp = semeticsUpdateCount; + System.out.println("beginindex: " + beginindex + "\ntemp: " + temp + "\n"); + ConcurrentMap strIndexNavigator = new MapMaker().concurrencyLevel(2).makeMap(); + int ij = 0; + while (beginindex + ij < temp) { + String get = stringCachelocal.get(beginindex + ij); + strIndexNavigator.put(ij, get); + multiprocessCalculations.add(get); + ij++; + } new Thread(() -> { LinkedHashMap> LHMSMXLocal = lHMSMX; - List strList = new ArrayList(stringCache.values()); - List matrixUpdateList = new ArrayList(); - List randomStrList = new ArrayList(); + ConcurrentMap strIndexAll = stringCachelocal; + ConcurrentMap strIndexNavigatorL = strIndexNavigator; + ConcurrentMap randomIndexesToUpdate = new MapMaker().concurrencyLevel(2).makeMap(); int indexes = updateBadgesInteger; - if (indexes >= strList.size()) { - indexes = strList.size() - 1; + if (indexes >= strIndexAll.size()) { + indexes = strIndexAll.size() - 1; } - int beginindexes = new Random().nextInt((strList.size()) - indexes); - strList.subList(beginindexes, beginindexes + indexes).forEach((str) -> { - randomStrList.add(str); - }); - List> futures = new ArrayList(); + int beginindexes = new Random().nextInt((strIndexAll.size()) - indexes); + int ij1 = 0; + while (beginindexes + ij1 < beginindexes + indexes) { + String get1 = strIndexAll.get(beginindexes + ij1); + randomIndexesToUpdate.put(ij1, get1); + ij1++; + } + ConcurrentMap matrixUpdateList = new MapMaker().concurrencyLevel(2).makeMap(); + ConcurrentMap> futures = new MapMaker().concurrencyLevel(2).makeMap(); ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); - multiprocessCalculations.forEach((str) -> { - randomStrList.forEach((str1) -> { + strIndexNavigatorL.values().forEach((str) -> { + randomIndexesToUpdate.values().stream().filter((str1) -> (!str.equals(str1))).forEachOrdered((str1) -> { boolean present = false; - if (LHMSMXLocal.containsKey(str)) { + if (multiprocessCalculations.contains(str1)) { + present = true; + } else if (LHMSMXLocal.containsKey(str)) { LinkedHashMap orDefault = LHMSMXLocal.get(str); if (orDefault.containsKey(str1)) { present = true; @@ -315,13 +246,14 @@ public class MYSQLDatahandler { if (!present) { SimilarityMatrix SMX = new SimilarityMatrix(str, str1); Callable worker = new SentimentAnalyzerTest(str, str1, SMX); - futures.add(executor.submit(worker)); + futures.put(futures.size() + 1, executor.submit(worker)); } }); }); executor.shutdown(); try { - for (Future future : futures) { + System.out.println("finished worker assignment, futures size: " + futures.size() + "\n"); + for (Future future : futures.values()) { SimilarityMatrix SMX = future.get(); LinkedHashMap get = lHMSMX.getOrDefault(SMX.getPrimaryString(), null); if (get == null) { @@ -329,24 +261,97 @@ public class MYSQLDatahandler { } get.put(SMX.getSecondaryString(), SMX.getDistance()); lHMSMX.put(SMX.getPrimaryString(), get); - matrixUpdateList.add(SMX); + matrixUpdateList.put(matrixUpdateList.size() + 1, SMX); } } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(MYSQLDatahandler.class.getName()).log(Level.SEVERE, null, ex); } - try { - if (!matrixUpdateList.isEmpty()) { - DataMapper.insertSementicMatrixes(matrixUpdateList); - System.out.println("finished datamapper semetic insert"); + new Thread(() -> { + try { + if (!matrixUpdateList.isEmpty()) { + DataMapper.insertSementicMatrixes(matrixUpdateList); + System.out.println("finished datamapper semetic insert"); + } + threadCounter--; + System.out.println("\nthreadCounter: " + threadCounter + "\n"); + } catch (CustomError ex) { + Logger.getLogger(MYSQLDatahandler.class + .getName()).log(Level.SEVERE, null, ex); } - } catch (CustomError ex) { - Logger.getLogger(MYSQLDatahandler.class - .getName()).log(Level.SEVERE, null, ex); - } - multiprocessCalculations = new ArrayList(); - updatedRows = new ArrayList(); - threadCounter--; - }).start(); + }).start(); + }). + start(); + } else { + if (threadCounter == 0) { + threadCounter++; + new Thread(() -> { + LinkedHashMap> LHMSMXLocal = lHMSMX; + ConcurrentMap strList = stringCache; + ConcurrentMap matrixUpdateList = new MapMaker().concurrencyLevel(2).makeMap(); + ConcurrentMap randomStrList = new MapMaker().concurrencyLevel(2).makeMap(); + int indexes = updateBadgesInteger; + if (indexes >= strList.size()) { + indexes = strList.size() - 1; + } + int beginindexes = new Random().nextInt((strList.size()) - indexes); + int ij1 = 0; + while (beginindexes + ij1 < beginindexes + indexes) { + String get1 = strList.get(beginindexes + ij1); + randomStrList.put(ij1, get1); + ij1++; + } + ConcurrentMap> futures = new MapMaker().concurrencyLevel(2).makeMap(); + ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + multiprocessCalculations.forEach((str) -> { + randomStrList.values().forEach((str1) -> { + boolean present = false; + if (LHMSMXLocal.containsKey(str)) { + LinkedHashMap orDefault = LHMSMXLocal.get(str); + if (orDefault.containsKey(str1)) { + present = true; + } + } else if (LHMSMXLocal.containsKey(str1)) { + LinkedHashMap orDefault = LHMSMXLocal.get(str1); + if (orDefault.containsKey(str)) { + present = true; + } + } + if (!present) { + SimilarityMatrix SMX = new SimilarityMatrix(str, str1); + Callable worker = new SentimentAnalyzerTest(str, str1, SMX); + futures.put(futures.size() + 1, executor.submit(worker)); + } + }); + }); + executor.shutdown(); + try { + for (Future future : futures.values()) { + SimilarityMatrix SMX = future.get(); + LinkedHashMap get = lHMSMX.getOrDefault(SMX.getPrimaryString(), null); + if (get == null) { + get = new LinkedHashMap(); + } + get.put(SMX.getSecondaryString(), SMX.getDistance()); + lHMSMX.put(SMX.getPrimaryString(), get); + matrixUpdateList.put(matrixUpdateList.size() + 1, SMX); + } + } catch (InterruptedException | ExecutionException ex) { + Logger.getLogger(MYSQLDatahandler.class.getName()).log(Level.SEVERE, null, ex); + } + try { + if (!matrixUpdateList.isEmpty()) { + DataMapper.insertSementicMatrixes(matrixUpdateList); + System.out.println("finished datamapper semetic insert"); + } + } catch (CustomError ex) { + Logger.getLogger(MYSQLDatahandler.class + .getName()).log(Level.SEVERE, null, ex); + } + multiprocessCalculations = new ArrayList(); + updatedRows = new ArrayList(); + threadCounter--; + }).start(); + } } } } @@ -355,22 +360,20 @@ public class MYSQLDatahandler { public synchronized void checkIfUpdateStrings() throws CustomError { if (stopwatch.elapsed(TimeUnit.SECONDS) >= EXPIRE_TIME_IN_SECONDS || !stopwatch.isRunning()) { new Thread(() -> { - List str = MessageResponseHandler.getStr(); + ConcurrentMap str = MessageResponseHandler.getStr(); str = cutContent(str); str = filterContent(str); str = removeSlacks(str); - List strUpdate = new ArrayList(); - strUpdate.addAll(str); try { - DataMapper.InsertMYSQLStrings(strUpdate); + DataMapper.InsertMYSQLStrings(str); } catch (CustomError ex) { Logger.getLogger(MYSQLDatahandler.class .getName()).log(Level.SEVERE, null, ex); } - MessageResponseHandler.setStr(new ArrayList()); + MessageResponseHandler.setStr(new MapMaker().concurrencyLevel(2).makeMap()); int j = stringCache.size() + 1; - for (String str1 : strUpdate) { + for (String str1 : str.values()) { stringCache.put(j, str1); j++; } @@ -394,7 +397,7 @@ public class MYSQLDatahandler { SimilarityMatrix SMXreturn = new SimilarityMatrix("", ""); System.out.println("pre mostSimilarSTR \n"); String mostSimilarSTR = mostSimilar(str, strArrs); - if (!mostSimilarSTR.isEmpty()) { + if (mostSimilarSTR != null && !mostSimilarSTR.isEmpty()) { System.out.println("mostSimilarSTR; " + mostSimilarSTR + "\n"); LinkedHashMap orDefault = LHMSMXLocal.getOrDefault(mostSimilarSTR, null); if (orDefault != null) { @@ -471,11 +474,13 @@ public class MYSQLDatahandler { for (Future future : futures) { DistanceObject d = future.get(); try { - int distance = d.getDistance(); - System.out.println("distance: " + distance + "\n"); - if (distance < minDistance) { - minDistance = distance; - similar = d.getSentence(); + if (d.getSentence() != null && d.getDistance() != null) { + int distance = d.getDistance(); + System.out.println("distance: " + distance + "\n"); + if (distance < minDistance) { + minDistance = distance; + similar = d.getSentence(); + } } } catch (NullPointerException ex) { System.out.println("failed future\n"); @@ -487,21 +492,21 @@ public class MYSQLDatahandler { return similar; } - public static List cutContent(List str) { - List returnlist = new ArrayList(); - for (String str1 : str) { + public static ConcurrentMap cutContent(ConcurrentMap str) { + ConcurrentMap returnlist = new MapMaker().concurrencyLevel(2).makeMap(); + for (String str1 : str.values()) { int iend = str1.indexOf("content: "); if (iend != -1) { String trs = str1.substring(iend + 9); - returnlist.add(trs.substring(0, trs.length() - 1)); + returnlist.put(returnlist.size() + 1, trs.substring(0, trs.length() - 1)); } } return returnlist; } - public static List filterContent(List str) { - List strlistreturn = new ArrayList(); - for (String str1 : str) { + public static ConcurrentMap filterContent(ConcurrentMap str) { + ConcurrentMap strlistreturn = new MapMaker().concurrencyLevel(2).makeMap(); + for (String str1 : str.values()) { if (str1.isEmpty() || str1.length() < 3) { continue; } @@ -580,18 +585,18 @@ public class MYSQLDatahandler { } str1 = str1.trim(); if (str1.length() > 2 && (!str1.startsWith("!"))) { - strlistreturn.add(str1); + strlistreturn.put(strlistreturn.size() + 1, str1); } } return strlistreturn; } - private List removeSlacks(List str) { + private ConcurrentMap removeSlacks(ConcurrentMap str) { ShiftReduceParser model = getModel(); MaxentTagger tagger = getTagger(); List taggedWords; - List strreturn = new ArrayList(); - for (String str1 : str) { + ConcurrentMap strreturn = new MapMaker().concurrencyLevel(2).makeMap(); + for (String str1 : str.values()) { int counter = 0; List TGWList = new ArrayList(); DocumentPreprocessor tokenizer = new DocumentPreprocessor(new StringReader(str1)); @@ -638,7 +643,7 @@ public class MYSQLDatahandler { } } if (!tooclosematch) { - strreturn.add(str1); + strreturn.put(strreturn.size() + 1, str1); } } } diff --git a/ArtificialAutism/src/main/java/FunctionLayer/MessageResponseHandler.java b/ArtificialAutism/src/main/java/FunctionLayer/MessageResponseHandler.java index d8c2aa1f..e21ea682 100644 --- a/ArtificialAutism/src/main/java/FunctionLayer/MessageResponseHandler.java +++ b/ArtificialAutism/src/main/java/FunctionLayer/MessageResponseHandler.java @@ -5,8 +5,10 @@ */ package FunctionLayer; +import com.google.common.collect.MapMaker; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ConcurrentMap; /** * @@ -14,13 +16,13 @@ import java.util.List; */ public class MessageResponseHandler { - private static List str = new ArrayList(); + private static ConcurrentMap str = new MapMaker().concurrencyLevel(2).makeMap(); - public static List getStr() { + public static ConcurrentMap getStr() { return str; } - public static void setStr(List str) { + public static void setStr(ConcurrentMap str) { MessageResponseHandler.str = str; } @@ -33,13 +35,13 @@ public class MessageResponseHandler { if (message.startsWith("[ *")) { message = message.substring(message.indexOf("]")); } - str.add(message); + str.put(str.size() + 1, message); } } public static String selectReponseMessage(String toString) throws CustomError { - List str1 = new ArrayList(); - str1.add(toString); + ConcurrentMap str1 = new MapMaker().concurrencyLevel(2).makeMap(); + str1.put(str1.size() +1 , toString); str1 = MYSQLDatahandler.cutContent(str1); String strreturn = str1.get(0); String getResponseMsg = MYSQLDatahandler.instance.getResponseMsg(strreturn); diff --git a/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java b/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java index 67851943..2f811438 100644 --- a/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java +++ b/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java @@ -7,7 +7,7 @@ kill $pid (number) nohup screen -d -m -S nonroot java -Xmx6048M -jar /home/javatests/ArtificialAutism-1.0.jar -nohup screen -d -m -S nonroot java -Xmx4048M -jar /home/javatests/ArtificialAutism-1.0.jar +nohup screen -d -m -S nonroot java -Xmx6800M -jar /home/javatests/ArtificialAutism-1.0.jar screen -ls (number1) screen -X -S (number1) quit @@ -35,7 +35,6 @@ import org.javacord.api.entity.user.User; public class DiscordHandler { public static void main(String[] args) { - MYSQLDatahandler.shiftReduceParserInitiate(); new Thread(() -> { try { MYSQLDatahandler.instance.initiateMYSQL(); @@ -44,6 +43,8 @@ public class DiscordHandler { Logger.getLogger(DiscordHandler.class.getName()).log(Level.SEVERE, null, ex); } }).start(); + MYSQLDatahandler.shiftReduceParserInitiate(); + MYSQLDatahandler.instance.checkIfUpdateMatrixes(); String token = "NTI5NzAxNTk5NjAyMjc4NDAx.Dw0vDg.7-aMjVWdQMYPl8qVNyvTCPS5F_A"; DiscordApi api = new DiscordApiBuilder().setToken(token).login().join(); api.addMessageCreateListener(event -> { @@ -81,7 +82,7 @@ public class DiscordHandler { MessageResponseHandler.getMessage(strresult); try { MYSQLDatahandler.instance.checkIfUpdateStrings(); - MYSQLDatahandler.instance.checkIfUpdateMatrixes(); + //MYSQLDatahandler.instance.checkIfUpdateMatrixes(); } catch (CustomError ex) { Logger.getLogger(DiscordHandler.class.getName()).log(Level.SEVERE, null, ex); }