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.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.TimeUnit;
|
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.entities.AddWordResult;
|
||||||
import io.github.sspanak.tt9.db.wordPairs.WordPairStore;
|
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.preferences.settings.SettingsStore;
|
||||||
import io.github.sspanak.tt9.util.ConsumerCompat;
|
import io.github.sspanak.tt9.util.ConsumerCompat;
|
||||||
import io.github.sspanak.tt9.util.Logger;
|
import io.github.sspanak.tt9.util.Logger;
|
||||||
|
import io.github.sspanak.tt9.util.Timer;
|
||||||
|
|
||||||
public class DataStore {
|
public class DataStore {
|
||||||
private final static String LOG_TAG = DataStore.class.getSimpleName();
|
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) {
|
public static void deleteLanguages(Runnable notification, @NonNull ArrayList<Language> languages) {
|
||||||
runInTransaction(
|
Timer.start(LOG_TAG);
|
||||||
() -> { words.remove(languages); pairs.remove(languages); },
|
|
||||||
notification,
|
AtomicInteger progress = new AtomicInteger(languages.size());
|
||||||
"Failed deleting languages."
|
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) {
|
public void remove(@NonNull ArrayList<Language> languages) {
|
||||||
if (!checkOrNotify()) {
|
if (!checkOrNotify()) {
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -118,20 +118,10 @@ public class WordStore extends BaseSyncStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void remove(ArrayList<Language> languages) {
|
public void remove(Language language) {
|
||||||
if (!checkOrNotify()) {
|
if (checkOrNotify() && readOps.exists(sqlite.getDb(), language.getId())) {
|
||||||
return;
|
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