From 8949c65f4fabc34277e23fc1f06d2525c79f3cb8 Mon Sep 17 00:00:00 2001 From: Dimo Karaivanov Date: Thu, 11 May 2023 17:00:47 +0300 Subject: [PATCH] Fixed only 8 suggestions appearing, when there are actually more (#257) * fixed a regession after the optimizations, that caused only 8 exact word matches to appear, when there are more * code cleanup: replaced Thread classes with lambdas in DictionaryDb --- .../github/sspanak/tt9/db/DictionaryDb.java | 56 +++++++------------ .../github/sspanak/tt9/db/room/TT9Room.java | 2 +- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/src/io/github/sspanak/tt9/db/DictionaryDb.java b/src/io/github/sspanak/tt9/db/DictionaryDb.java index 92d8cb3c..2cf2e551 100644 --- a/src/io/github/sspanak/tt9/db/DictionaryDb.java +++ b/src/io/github/sspanak/tt9/db/DictionaryDb.java @@ -45,7 +45,7 @@ public class DictionaryDb { } - private static void printDebug(String tag, String title, WordList words, long startTime) { + private static void printDebug(String tag, String title, String sequence, WordList words, long startTime) { if (!Logger.isDebugLevel()) { return; } @@ -54,9 +54,11 @@ public class DictionaryDb { debugText .append("\n") .append("Word Count: ").append(words.size()) - .append(". Time: ").append(System.currentTimeMillis() - startTime).append(" ms"); + .append(". Time: ").append(System.currentTimeMillis() - startTime).append(" ms."); if (words.size() > 0) { debugText.append("\n").append(words); + } else { + debugText.append(" Sequence: ").append(sequence); } Logger.d(tag, debugText.toString()); @@ -92,9 +94,7 @@ public class DictionaryDb { * This query will finish immediately, if there is nothing to do. It's safe to run it often. */ public static void normalizeWordFrequencies(SettingsStore settings) { - new Thread() { - @Override - public void run() { + new Thread(() -> { long time = System.currentTimeMillis(); int affectedRows = getInstance().wordsDao().normalizeFrequencies( @@ -107,18 +107,16 @@ public class DictionaryDb { "Normalized " + affectedRows + " words in: " + (System.currentTimeMillis() - time) + " ms" ); } - }.start(); + ).start(); } public static void areThereWords(ConsumerCompat notification, Language language) { - new Thread() { - @Override - public void run() { + new Thread(() -> { int langId = language != null ? language.getId() : -1; notification.accept(getInstance().wordsDao().count(langId) > 0); } - }.start(); + ).start(); } @@ -137,9 +135,7 @@ public class DictionaryDb { public static void deleteWords(Runnable notification, ArrayList languageIds) { - new Thread() { - @Override - public void run() { + new Thread(() -> { if (languageIds == null) { getInstance().clearAllTables(); } else if (languageIds.size() > 0) { @@ -147,7 +143,7 @@ public class DictionaryDb { } notification.run(); } - }.start(); + ).start(); } @@ -167,9 +163,7 @@ public class DictionaryDb { dbWord.length = word.length(); dbWord.frequency = 1; - new Thread() { - @Override - public void run() { + new Thread(() -> { try { getInstance().wordsDao().insert(dbWord); getInstance().wordsDao().incrementFrequency(dbWord.langId, dbWord.word, dbWord.sequence); @@ -185,7 +179,7 @@ public class DictionaryDb { statusHandler.accept(2); } } - }.start(); + ).start(); } @@ -212,9 +206,7 @@ public class DictionaryDb { throw new Exception("Cannot increment word frequency. Word: " + word + " | Sequence: " + sequence); } - new Thread() { - @Override - public void run() { + new Thread(() -> { try { int affectedRows = getInstance().wordsDao().incrementFrequency(language.getId(), word, sequence); @@ -246,7 +238,7 @@ public class DictionaryDb { ); } } - }.start(); + ).start(); } @@ -264,7 +256,7 @@ public class DictionaryDb { filter == null || filter.equals("") ? null : filter )); - printDebug("loadWordsExact", "===== Exact Word Matches =====", matches, start); + printDebug("loadWordsExact", "===== Exact Word Matches =====", sequence, matches, start); return matches.toStringList(); } @@ -299,7 +291,7 @@ public class DictionaryDb { matches.addAll(getInstance().wordsDao().getCustom(sql)); } - printDebug("loadWordsFuzzy", "~=~=~=~ Fuzzy Word Matches ~=~=~=~", matches, start); + printDebug("loadWordsFuzzy", "~=~=~=~ Fuzzy Word Matches ~=~=~=~", sequence, matches, start); return matches.toStringList(); } @@ -327,21 +319,15 @@ public class DictionaryDb { return; } - new Thread() { - @Override - public void run() { - if (sequence.length() == 1) { - wordList.addAll(loadWordsExact(language, sequence, filter, maxWords)); - } else { - wordList.addAll(loadWordsFuzzy(language, sequence, filter, minWords)); - } + new Thread(() -> { + wordList.addAll(loadWordsExact(language, sequence, filter, maxWords)); - if (wordList.size() == 0) { - Logger.i("db.getWords", "No suggestions for sequence: " + sequence); + if (sequence.length() > 1 && wordList.size() < minWords) { + wordList.addAll(loadWordsFuzzy(language, sequence, filter, minWords - wordList.size())); } sendWords(dataHandler, wordList); } - }.start(); + ).start(); } } diff --git a/src/io/github/sspanak/tt9/db/room/TT9Room.java b/src/io/github/sspanak/tt9/db/room/TT9Room.java index db75aadd..dfe7d01c 100644 --- a/src/io/github/sspanak/tt9/db/room/TT9Room.java +++ b/src/io/github/sspanak/tt9/db/room/TT9Room.java @@ -34,7 +34,7 @@ public abstract class TT9Room extends RoomDatabase { " WHERE 1" + " AND lang = " + langId + " AND len BETWEEN " + minWordLength + " AND " + maxWordLength + - " AND seq BETWEEN " + sequence + " AND " + sequence + "99 " + + " AND seq > " + sequence + " AND seq <= " + sequence + "99 " + " ORDER BY len ASC, freq DESC " + " LIMIT " + limit;