fix: don't discard initial work if it doesn't get finished

This commit is contained in:
Johannes Frohnmeyer 2024-05-10 14:27:23 +02:00
parent 6a0cfdb26a
commit fa4fa75944
Signed by: Johannes
GPG Key ID: E76429612C2929F4

View File

@ -1,5 +1,6 @@
package io.gitlab.jfronny.translater.transformer;
import io.gitlab.jfronny.commons.concurrent.AsyncRequestState;
import io.gitlab.jfronny.libjf.config.api.v2.ConfigHolder;
import io.gitlab.jfronny.translater.Cfg;
import io.gitlab.jfronny.translater.Translater;
@ -10,7 +11,7 @@ import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
public class CachingTransformer implements Transformer {
@ -32,7 +33,7 @@ public class CachingTransformer implements Transformer {
return str;
}
cache.put(str, transformed);
save();
scheduleSave();
}
//Return cached result
return (String) cache.get(str);
@ -40,7 +41,7 @@ public class CachingTransformer implements Transformer {
@Override
public void transformMultiple(Stream<? extends String> strings, ResultConsumer results) {
AtomicBoolean bl = new AtomicBoolean(false);
AtomicInteger count = new AtomicInteger(0);
transformer.transformMultiple(strings.filter(s -> {
String translation = (String) cache.get(s);
if (translation != null) {
@ -57,9 +58,12 @@ public class CachingTransformer implements Transformer {
return true;
}), (str, translation) -> {
cache.put(str, translation);
bl.set(true);
if (count.incrementAndGet() == 50) {
count.addAndGet(-50);
scheduleSave();
}
});
if (bl.get()) save();
if (count.get() > 0) scheduleSave();
}
public CachingTransformer(Transformer baseTransformer) {
@ -92,15 +96,21 @@ public class CachingTransformer implements Transformer {
}
defaultCache = p;
} else defaultCache = null;
save();
scheduleSave();
}
private void save() {
if (cache.isEmpty()) return;
try (OutputStream outS = Files.newOutputStream(CACHE_FILE)) {
cache.store(outS, "---Lang---");
} catch (IOException e) {
Translater.LOGGER.error("Could not save translater cache");
private final AsyncRequestState state = new AsyncRequestState();
private void scheduleSave() {
if (state.request().shouldStart()) {
do {
if (!cache.isEmpty()) {
try (OutputStream outS = Files.newOutputStream(CACHE_FILE)) {
cache.store(outS, "---Lang---");
} catch (IOException e) {
Translater.LOGGER.error("Could not save translater cache");
}
}
} while (state.emitFinished().shouldContinue());
}
}
}