X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FBackupFiles.java;h=14c1260bbac561167382e2af1f2f2329ad79a589;hb=427c5af6eb8378c4a9d5bf6a967f55ab8fd253a5;hp=529e1b2acfe66f2f65fc1bcae8d491b72972f777;hpb=69f09dd6dbf67c00c27fdcfecbbcc932e7eac6a9;p=jalview.git diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index 529e1b2..14c1260 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -32,6 +32,11 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import jalview.bin.Cache; import jalview.bin.Console; @@ -106,8 +111,14 @@ public class BackupFiles private static final String oldTempFileSuffix = "_oldfile_tobedeleted"; + // thread pool used for completablefutures + private static final ExecutorService executorService = Executors + .newFixedThreadPool(3); + private static List savesInProgress = new ArrayList<>(); + private CompletableFuture myFuture = null; + private boolean addSaveInProgress() { if (savesInProgress.contains(this)) @@ -116,42 +127,101 @@ public class BackupFiles } else { + this.setMyFuture(); savesInProgress.add(this); return true; } } - private boolean removeSaveInProgress() + private boolean removeSaveInProgress(boolean ret) { if (savesInProgress.contains(this)) { + this.getMyFuture().complete(ret); // remove all occurrences while (savesInProgress.remove(this)) { } return true; } - else + return false; + } + + private static CompletableFuture getNewFuture() + { + return new CompletableFuture() { - return false; - } + }; + } + + private CompletableFuture getMyFuture() + { + return this.myFuture; + } + + private void setMyFuture() + { + this.myFuture = getNewFuture(); } public static boolean hasSavesInProgress() { - return savesInProgress.size() > 0; + boolean has = false; + for (CompletableFuture cf : savesInProgressCompletableFutures(true)) + { + has |= !cf.isDone(); + } + return has; } - public static List savesInProgressFiles() + public static List savesInProgressFiles(boolean all) { List files = new ArrayList<>(); for (BackupFiles bfile : savesInProgress) { - files.add(bfile.getFile()); + if (all || !bfile.getMyFuture().isDone()) + files.add(bfile.getFile()); } return files; } + public static List> savesInProgressCompletableFutures( + boolean all) + { + List> cfs = new ArrayList<>(); + for (BackupFiles bfile : savesInProgress) + { + if (all || !bfile.getMyFuture().isDone()) + cfs.add(bfile.getMyFuture()); + } + return cfs; + } + + public static Future allSaved() + { + CompletableFuture f = new CompletableFuture<>(); + + executorService.submit(() -> { + for (BackupFiles buf : savesInProgress) + { + boolean allSaved = true; + try + { + allSaved &= buf.getMyFuture().get(); + } catch (InterruptedException e) + { + Console.debug("InterruptedException waiting for files to save", + e); + } catch (ExecutionException e) + { + Console.debug("ExecutionException waiting for files to save", e); + } + f.complete(allSaved); + } + }); + return f; + } + public BackupFiles(String filename) { this(new File(filename)); @@ -183,6 +253,7 @@ public class BackupFiles { String tempfilename = file.getName(); File tempdir = file.getParentFile(); + tempdir.mkdirs(); Console.trace( "BACKUPFILES [file!=null] attempting to create temp file for " + tempfilename + " in dir " + tempdir); @@ -871,7 +942,7 @@ public class BackupFiles } // remove this file from the save in progress stack - removeSaveInProgress(); + removeSaveInProgress(rename); return rename; }