replacing for loops with get methods

This commit is contained in:
jenzur 2019-03-10 01:15:50 +01:00
parent 64dc2719a0
commit 7ab6c7ee0b
2 changed files with 54 additions and 60 deletions

View File

@ -174,7 +174,6 @@ public class MYSQLDatahandler {
public synchronized void checkIfUpdateMatrixes() { public synchronized void checkIfUpdateMatrixes() {
refreshMatrixFromDB = false; refreshMatrixFromDB = false;
int updateBadgesInteger = 900;
int counter = 0; int counter = 0;
if (stopwatch1.elapsed(TimeUnit.SECONDS) >= EXPIRE_TIME_IN_SECONDS1) { if (stopwatch1.elapsed(TimeUnit.SECONDS) >= EXPIRE_TIME_IN_SECONDS1) {
refreshMatrixFromDB = true; refreshMatrixFromDB = true;
@ -197,14 +196,11 @@ public class MYSQLDatahandler {
} }
if (!updatepresent) { if (!updatepresent) {
selectUpdate = ij2; selectUpdate = ij2;
System.out.println("selectUpdate in primary: " + selectUpdate + "\n");
LinkedHashMap<String, Double> newLHM = new LinkedHashMap();
lHMSMX.put(str, newLHM);
break; break;
} }
ij2++; ij2++;
} }
if (selectUpdate == -1) { if (selectUpdate == -1 || selectUpdate + 1 == stringCachelocal.size()) {
int valueSize = stringCachelocal.size(); int valueSize = stringCachelocal.size();
if (secondaryIterator >= valueSize) { if (secondaryIterator >= valueSize) {
secondaryIterator = 0; secondaryIterator = 0;
@ -212,44 +208,44 @@ public class MYSQLDatahandler {
selectUpdate = secondaryIterator; selectUpdate = secondaryIterator;
secondaryIterator++; secondaryIterator++;
} }
if (selectUpdate == -1 || selectUpdate >= stringCachelocal.size()) {
selectUpdate = new Random().nextInt(stringCachelocal.size() - 1);
}
int beginindex = selectUpdate; int beginindex = selectUpdate;
System.out.println("beginindex: " + beginindex + "\n"); System.out.println("beginindex: " + beginindex + "\n");
ConcurrentMap<Integer, String> strIndexNavigator = new MapMaker().concurrencyLevel(2).makeMap(); ConcurrentMap<Integer, String> strIndexNavigator = new MapMaker().concurrencyLevel(2).makeMap();
String get = stringCachelocal.get(beginindex); String get = stringCachelocal.get(beginindex);
strIndexNavigator.put(0, get); strIndexNavigator.put(0, get);
ConcurrentMap<Integer, String> randomIndexesToUpdate = new MapMaker().concurrencyLevel(2).makeMap();
int indexes = updateBadgesInteger;
if (indexes >= stringCachelocal.size()) {
indexes = stringCachelocal.size() - 1;
}
int beginindexes = new Random().nextInt((stringCachelocal.size()) - indexes);
int ij1 = 0;
while (beginindexes + ij1 < beginindexes + indexes) {
String get1 = stringCachelocal.get(beginindexes + ij1);
randomIndexesToUpdate.put(ij1, get1);
ij1++;
}
ConcurrentMap<Integer, SimilarityMatrix> matrixUpdateList = new MapMaker().concurrencyLevel(2).makeMap(); ConcurrentMap<Integer, SimilarityMatrix> matrixUpdateList = new MapMaker().concurrencyLevel(2).makeMap();
ConcurrentMap<Integer, Future<SimilarityMatrix>> futures = new MapMaker().concurrencyLevel(2).makeMap(); ConcurrentMap<Integer, Future<SimilarityMatrix>> futures = new MapMaker().concurrencyLevel(2).makeMap();
ConcurrentMap<Integer, ConcurrentMap<String, String>> strMap = new MapMaker().concurrencyLevel(2).makeMap();
strIndexNavigator.values().forEach((str) -> { strIndexNavigator.values().forEach((str) -> {
randomIndexesToUpdate.values().stream().filter((str1) -> (!str.equals(str1))).forEachOrdered((str1) -> { stringCachelocal.values().stream().filter((str1) -> (!str.equals(str1))).forEachOrdered((str1) -> {
boolean present = false; boolean present = false;
LinkedHashMap<String, Double> orDefault = LHMSMXLocal.getOrDefault(str, null); LinkedHashMap<String, Double> orDefault = LHMSMXLocal.getOrDefault(str, null);
if (orDefault != null) { if (orDefault != null) {
for (String strdefault : orDefault.keySet()) { Double orDefault1 = orDefault.getOrDefault(str1, null);
if (strdefault.equals(str1)) { if (orDefault1 != null) {
// System.out.println("present true \nstr: " + str + "\nstr1: " + str1 + "\n");
present = true;
}
}
if (!present) {
orDefault = LHMSMXLocal.getOrDefault(str1, null);
if (orDefault != null) {
Double orDefault1 = orDefault.getOrDefault(str, null);
if (orDefault1 != null) {
// System.out.println("present true \nstr: " + str + "\nstr1: " + str1 + "\n");
present = true; present = true;
break;
} }
} }
} }
orDefault = LHMSMXLocal.getOrDefault(str1, null); if (!present) {
if (orDefault != null) { for (ConcurrentMap<String, String> strconcuritr : strMap.values()) {
for (String strdefault : orDefault.keySet()) { String orDefault1 = strconcuritr.getOrDefault(str, null);
if (strdefault.equals(str)) { if (orDefault1 != null && orDefault1.equals(str1)) {
present = true;
break;
}
orDefault1 = strconcuritr.getOrDefault(str1, null);
if (orDefault1 != null && orDefault1.equals(str)) {
present = true; present = true;
break; break;
} }
@ -259,6 +255,9 @@ public class MYSQLDatahandler {
SimilarityMatrix SMX = new SimilarityMatrix(str, str1); SimilarityMatrix SMX = new SimilarityMatrix(str, str1);
Callable<SimilarityMatrix> worker = new SentimentAnalyzerTest(str, str1, SMX); Callable<SimilarityMatrix> worker = new SentimentAnalyzerTest(str, str1, SMX);
futures.put(futures.size() + 1, executor.submit(worker)); futures.put(futures.size() + 1, executor.submit(worker));
ConcurrentMap<String, String> strmapLocal = new MapMaker().concurrencyLevel(2).makeMap();
strmapLocal.put(str, str1);
strMap.put(strMap.size() + 1, strmapLocal);
} }
}); });
}); });
@ -293,25 +292,23 @@ public class MYSQLDatahandler {
public synchronized void checkIfUpdateStrings() throws CustomError { public synchronized void checkIfUpdateStrings() throws CustomError {
if (stopwatch.elapsed(TimeUnit.SECONDS) >= EXPIRE_TIME_IN_SECONDS || !stopwatch.isRunning()) { if (stopwatch.elapsed(TimeUnit.SECONDS) >= EXPIRE_TIME_IN_SECONDS || !stopwatch.isRunning()) {
new Thread(() -> { ConcurrentMap<Integer, String> str = MessageResponseHandler.getStr();
ConcurrentMap<Integer, String> str = MessageResponseHandler.getStr(); str = cutContent(str);
str = cutContent(str); str = filterContent(str);
str = filterContent(str); str = removeSlacks(str);
str = removeSlacks(str); str = verifyCalculationFitness(str);
str = verifyCalculationFitness(str); try {
try { DataMapper.InsertMYSQLStrings(str);
DataMapper.InsertMYSQLStrings(str); } catch (CustomError ex) {
} catch (CustomError ex) { Logger.getLogger(MYSQLDatahandler.class
Logger.getLogger(MYSQLDatahandler.class .getName()).log(Level.SEVERE, null, ex);
.getName()).log(Level.SEVERE, null, ex); }
} MessageResponseHandler.setStr(new MapMaker().concurrencyLevel(2).makeMap());
MessageResponseHandler.setStr(new MapMaker().concurrencyLevel(2).makeMap()); int j = stringCache.size() + 1;
int j = stringCache.size() + 1; for (String str1 : str.values()) {
for (String str1 : str.values()) { stringCache.put(j, str1);
stringCache.put(j, str1); j++;
j++; }
}
}).start();
if (!stopwatch.isRunning()) { if (!stopwatch.isRunning()) {
stopwatch.start(); stopwatch.start();
} else { } else {
@ -609,15 +606,7 @@ public class MYSQLDatahandler {
if (allStrings.isEmpty()) { if (allStrings.isEmpty()) {
return strmap; return strmap;
} }
ConcurrentMap<Integer, String> intervalMap = new MapMaker().concurrencyLevel(2).makeMap();
int intervalAprove = 450; int intervalAprove = 450;
int hardcap = allStrings.size() > intervalAprove ? intervalAprove : allStrings.size();
int intervalAproveRand = new Random().nextInt(hardcap < intervalAprove ? 1 : allStrings.size() - intervalAprove);
int ij = 0;
while (ij < hardcap && allStrings.size() > intervalAproveRand + ij) {
intervalMap.put(ij, allStrings.get(intervalAproveRand + ij));
ij++;
}
String str1 = "and to revise the questions and materials and such"; String str1 = "and to revise the questions and materials and such";
String str2 = "as William said earlier. Visiting your dentist once or twice a year is enough"; String str2 = "as William said earlier. Visiting your dentist once or twice a year is enough";
String str3 = "At least, you have more time to prepare then"; String str3 = "At least, you have more time to prepare then";
@ -629,11 +618,16 @@ public class MYSQLDatahandler {
futures.put(futures.size() + 1, executor.submit(worker)); futures.put(futures.size() + 1, executor.submit(worker));
worker = new SentimentAnalyzerTest(str, str3, new SimilarityMatrix(str, str3)); worker = new SentimentAnalyzerTest(str, str3, new SimilarityMatrix(str, str3));
futures.put(futures.size() + 1, executor.submit(worker)); futures.put(futures.size() + 1, executor.submit(worker));
for (String strValues : intervalMap.values()) { int ij2 = 0;
for (String strValues : allStrings.values()) {
if (ij2 > intervalAprove) {
break;
}
if (!strValues.equals(str1) && !strValues.equals(str2) && !strValues.equals(str3)) { if (!strValues.equals(str1) && !strValues.equals(str2) && !strValues.equals(str3)) {
worker = new SentimentAnalyzerTest(str, strValues, new SimilarityMatrix(str, strValues)); worker = new SentimentAnalyzerTest(str, strValues, new SimilarityMatrix(str, strValues));
futures.put(futures.size() + 1, executor.submit(worker)); futures.put(futures.size() + 1, executor.submit(worker));
} }
ij2++;
} }
int counter = 0; int counter = 0;
boolean calculationIssues = false; boolean calculationIssues = false;

View File

@ -45,14 +45,14 @@ public class DiscordHandler {
}).start(); }).start();
MYSQLDatahandler.shiftReduceParserInitiate(); MYSQLDatahandler.shiftReduceParserInitiate();
MYSQLDatahandler.instance.instantiateExecutor(); MYSQLDatahandler.instance.instantiateExecutor();
/*
if (MYSQLDatahandler.instance.getstringCacheSize() != 0) { if (MYSQLDatahandler.instance.getstringCacheSize() != 0) {
while (MYSQLDatahandler.instance.getlHMSMXSize() * 3 < (MYSQLDatahandler.instance.getstringCacheSize() while (MYSQLDatahandler.instance.getlHMSMXSize() * MYSQLDatahandler.instance.getlHMSMXSize() * 1.5
* MYSQLDatahandler.instance.getstringCacheSize()) - MYSQLDatahandler.instance.getstringCacheSize()) { < (MYSQLDatahandler.instance.getstringCacheSize()
* MYSQLDatahandler.instance.getstringCacheSize())
- MYSQLDatahandler.instance.getstringCacheSize()) {
MYSQLDatahandler.instance.checkIfUpdateMatrixes(); MYSQLDatahandler.instance.checkIfUpdateMatrixes();
} }
} }
*/
String token = "NTI5NzAxNTk5NjAyMjc4NDAx.Dw0vDg.7-aMjVWdQMYPl8qVNyvTCPS5F_A"; String token = "NTI5NzAxNTk5NjAyMjc4NDAx.Dw0vDg.7-aMjVWdQMYPl8qVNyvTCPS5F_A";
DiscordApi api = new DiscordApiBuilder().setToken(token).login().join(); DiscordApi api = new DiscordApiBuilder().setToken(token).login().join();
api.addMessageCreateListener(event -> { api.addMessageCreateListener(event -> {