From fa4fa759442159daf3f097f4c4894d5cd0d2396c Mon Sep 17 00:00:00 2001 From: JFronny Date: Fri, 10 May 2024 14:27:23 +0200 Subject: [PATCH] fix: don't discard initial work if it doesn't get finished --- .../transformer/CachingTransformer.java | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/client/java/io/gitlab/jfronny/translater/transformer/CachingTransformer.java b/src/client/java/io/gitlab/jfronny/translater/transformer/CachingTransformer.java index 7e21afe..79d53e9 100644 --- a/src/client/java/io/gitlab/jfronny/translater/transformer/CachingTransformer.java +++ b/src/client/java/io/gitlab/jfronny/translater/transformer/CachingTransformer.java @@ -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 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()); } } }