From d7d5164d3f2ea59e02df51d00442cedd5f2dde8f Mon Sep 17 00:00:00 2001
From: Jan Kozusznik <jan@kozusznik.cz>
Date: Thu, 1 Feb 2018 10:53:58 +0100
Subject: [PATCH] iss1007:fileExists in accessor, limit queries to server

---
 .../src/main/java/cz/it4i/fiji/haas/Job.java  |  5 ++
 .../core/BenchmarkJobManager.java             | 34 +++++------
 .../core/SPIMComputationAccessor.java         |  8 ++-
 ...mputationAccessorDecoratorWithTimeout.java | 61 +++++++++++++++++++
 4 files changed, 89 insertions(+), 19 deletions(-)
 create mode 100644 haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/SPIMComputationAccessorDecoratorWithTimeout.java

diff --git a/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/Job.java b/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/Job.java
index cecce16b..a314a7e4 100644
--- a/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/Job.java
+++ b/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/Job.java
@@ -5,6 +5,7 @@ import java.io.InputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Calendar;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
@@ -243,6 +244,10 @@ public class Job {
 
 	}
 
+	public Collection<String> getChangedFiles() {
+		return haasClientSupplier.get().getChangedFiles(getId());
+	}
+
 	
 
 	
diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/BenchmarkJobManager.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/BenchmarkJobManager.java
index 756073c4..3d792c9f 100644
--- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/BenchmarkJobManager.java
+++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/BenchmarkJobManager.java
@@ -1,7 +1,6 @@
 package cz.it4i.fiji.haas_spim_benchmark.core;
 
 import java.io.BufferedReader;
-import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileWriter;
 import java.io.IOException;
@@ -56,27 +55,28 @@ public class BenchmarkJobManager {
 		
 		private List<Task> tasks;
 
-		private SPIMComputationAccessor computationAccessor = new SPIMComputationAccessor() {
-			
-			private HaaSOutputHolder outputOfSnakemake =
-					new HaaSOutputHolderImpl(BenchmarkJob.this, SynchronizableFileType.StandardErrorFile);
-			
-			@Override
-			public String getActualOutput() {
-				return outputOfSnakemake.getActualOutput();
-			}
-			
-			@Override
-			public boolean fileExists(String filePath) {
-				File f = new File(job.getDirectory().resolve(filePath).toString());
-				return f.exists() && !f.isDirectory();
-			}
-		};
+		private SPIMComputationAccessor computationAccessor;
 		
 		
 		public BenchmarkJob(Job job) {
 			super();
 			this.job = job;
+			computationAccessor = new SPIMComputationAccessor() {
+				
+				private HaaSOutputHolder outputOfSnakemake =
+						new HaaSOutputHolderImpl(BenchmarkJob.this, SynchronizableFileType.StandardErrorFile);
+				
+				@Override
+				public String getActualOutput() {
+					return outputOfSnakemake.getActualOutput();
+				}
+				
+				public java.util.Collection<String> getChangedFiles() {
+					return job.getChangedFiles();
+				};
+			};
+			
+			computationAccessor = new SPIMComputationAccessorDecoratorWithTimeout(computationAccessor, Constants.HAAS_UPDATE_TIMEOUT);
 		}
 
 		public void startJob(Progress progress) throws IOException {
diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/SPIMComputationAccessor.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/SPIMComputationAccessor.java
index 5c01528d..b1dc0c16 100644
--- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/SPIMComputationAccessor.java
+++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/SPIMComputationAccessor.java
@@ -1,9 +1,13 @@
 package cz.it4i.fiji.haas_spim_benchmark.core;
 
-import java.nio.file.Path;
+import java.util.Collection;
 
 import cz.it4i.fiji.haas.HaaSOutputHolder;
 
 public interface SPIMComputationAccessor extends HaaSOutputHolder {
-	boolean fileExists(String fileName);
+	default boolean fileExists(String fileName) {
+		return getChangedFiles().contains(fileName); 
+	}
+	
+	Collection<String> getChangedFiles();
 }
diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/SPIMComputationAccessorDecoratorWithTimeout.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/SPIMComputationAccessorDecoratorWithTimeout.java
new file mode 100644
index 00000000..8f215c3a
--- /dev/null
+++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/SPIMComputationAccessorDecoratorWithTimeout.java
@@ -0,0 +1,61 @@
+package cz.it4i.fiji.haas_spim_benchmark.core;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.function.Function;
+
+public class SPIMComputationAccessorDecoratorWithTimeout implements SPIMComputationAccessor {
+	private long intervalForQueryInMs;
+
+	private P_ResultCacheHolder<String> outputCache;
+
+	private P_ResultCacheHolder<Set<String>> changedFilesCache;
+
+	public SPIMComputationAccessorDecoratorWithTimeout(SPIMComputationAccessor decorated, long intervalForQueryInMs) {
+		super();
+		this.intervalForQueryInMs = intervalForQueryInMs;
+		outputCache = new P_ResultCacheHolder<>(x -> decorated.getActualOutput());
+		changedFilesCache = new P_ResultCacheHolder<>(set -> {
+			if (set == null) {
+				set = new HashSet<>();
+			} else {
+				set.clear();
+			}
+			set.addAll(decorated.getChangedFiles());
+			return null;
+		});
+	}
+
+	@Override
+	public String getActualOutput() {
+
+		return outputCache.getResult();
+	}
+
+	@Override
+	public Collection<String> getChangedFiles() {
+		return changedFilesCache.getResult();
+	}
+
+	private class P_ResultCacheHolder<T> {
+		private Long lastQuery;
+		private T value;
+		private Function<T, T> producer;
+
+		public P_ResultCacheHolder(Function<T, T> producer) {
+			super();
+			this.producer = producer;
+		}
+
+		public T getResult() {
+			long time = System.currentTimeMillis();
+
+			if (lastQuery == null || (time - lastQuery) > intervalForQueryInMs) {
+				value = producer.apply(value);
+				lastQuery = time;
+			}
+			return value;
+		}
+	}
+}
-- 
GitLab