Optimized language deletion not to run all languages in one transaction. This used to take a huge amount of storage and could potentially make deleting a long language list impossible
This commit is contained in:
parent
c80bb7ba42
commit
b405b2807a
3 changed files with 33 additions and 18 deletions
|
|
@ -11,6 +11,7 @@ import java.util.concurrent.ExecutorService;
|
|||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import io.github.sspanak.tt9.db.entities.AddWordResult;
|
||||
import io.github.sspanak.tt9.db.wordPairs.WordPairStore;
|
||||
|
|
@ -20,6 +21,7 @@ import io.github.sspanak.tt9.languages.Language;
|
|||
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
|
||||
import io.github.sspanak.tt9.util.ConsumerCompat;
|
||||
import io.github.sspanak.tt9.util.Logger;
|
||||
import io.github.sspanak.tt9.util.Timer;
|
||||
|
||||
public class DataStore {
|
||||
private final static String LOG_TAG = DataStore.class.getSimpleName();
|
||||
|
|
@ -79,11 +81,23 @@ public class DataStore {
|
|||
|
||||
|
||||
public static void deleteLanguages(Runnable notification, @NonNull ArrayList<Language> languages) {
|
||||
runInTransaction(
|
||||
() -> { words.remove(languages); pairs.remove(languages); },
|
||||
notification,
|
||||
"Failed deleting languages."
|
||||
);
|
||||
Timer.start(LOG_TAG);
|
||||
|
||||
AtomicInteger progress = new AtomicInteger(languages.size());
|
||||
Runnable onDeleted = () -> {
|
||||
if (progress.decrementAndGet() == 0) {
|
||||
Logger.d(LOG_TAG, "Deleted " + languages.size() + " languages. Time: " + Timer.stop(LOG_TAG) + " ms");
|
||||
notification.run();
|
||||
}
|
||||
};
|
||||
|
||||
for (Language language : languages) {
|
||||
runInTransaction(
|
||||
() -> { words.remove(language); pairs.remove(language); },
|
||||
onDeleted,
|
||||
"Failed deleting languages."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -165,6 +165,17 @@ public class WordPairStore extends BaseSyncStore {
|
|||
}
|
||||
|
||||
|
||||
public void remove(@NonNull Language language) {
|
||||
if (!checkOrNotify()) {
|
||||
return;
|
||||
}
|
||||
|
||||
DeleteOps.deleteWordPairs(sqlite.getDb(), language.getId());
|
||||
slowestLoadTime = 0;
|
||||
slowestSaveTime = 0;
|
||||
}
|
||||
|
||||
|
||||
public void remove(@NonNull ArrayList<Language> languages) {
|
||||
if (!checkOrNotify()) {
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -118,20 +118,10 @@ public class WordStore extends BaseSyncStore {
|
|||
}
|
||||
|
||||
|
||||
public void remove(ArrayList<Language> languages) {
|
||||
if (!checkOrNotify()) {
|
||||
return;
|
||||
public void remove(Language language) {
|
||||
if (checkOrNotify() && readOps.exists(sqlite.getDb(), language.getId())) {
|
||||
DeleteOps.delete(sqlite.getDb(), language.getId());
|
||||
}
|
||||
|
||||
Timer.start(LOG_TAG);
|
||||
|
||||
for (Language lang : languages) {
|
||||
if (readOps.exists(sqlite.getDb(), lang.getId())) {
|
||||
DeleteOps.delete(sqlite.getDb(), lang.getId());
|
||||
}
|
||||
}
|
||||
|
||||
Logger.d(LOG_TAG, "Deleted " + languages.size() + " languages. Time: " + Timer.stop(LOG_TAG) + " ms");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue