]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.acorn/src/org/simantics/acorn/backup/AcornBackupProvider.java
Fixed invalid comparison from AcornBackupProvider.RestoreCopyVisitor
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / backup / AcornBackupProvider.java
index 3977ad73d0d86bb1e2a11dba50899ea3955494a2..cb4b3cdeea1d18f5477387d0fc0e5b4a6a93b2a4 100644 (file)
@@ -1,6 +1,5 @@
 package org.simantics.acorn.backup;
 
-import java.io.BufferedReader;
 import java.io.IOException;
 import java.nio.file.FileVisitResult;
 import java.nio.file.Files;
@@ -23,6 +22,8 @@ import org.simantics.backup.BackupException;
 import org.simantics.backup.IBackupProvider;
 import org.simantics.db.server.ProCoreException;
 import org.simantics.utils.FileUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * @author Jani
@@ -31,6 +32,8 @@ import org.simantics.utils.FileUtils;
  */
 public class AcornBackupProvider implements IBackupProvider {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(AcornBackupProvider.class);
+
     private static final String IDENTIFIER = "AcornBackupProvider";
     private long trId = -1;
     private final Semaphore lock = new Semaphore(1);
@@ -39,11 +42,11 @@ public class AcornBackupProvider implements IBackupProvider {
     public AcornBackupProvider() {
         this.client = AcornSessionManagerImpl.getInstance().getClient();
     }
-    
-    private static Path getAcornMetadataFile(Path dbFolder) {
+
+    public static Path getAcornMetadataFile(Path dbFolder) {
         return dbFolder.getParent().resolve(IDENTIFIER);
     }
-    
+
     @Override
     public void lock() throws BackupException {
         try {
@@ -51,7 +54,7 @@ public class AcornBackupProvider implements IBackupProvider {
                 throw new IllegalStateException(this + " backup provider is already locked");
             trId = client.askWriteTransaction(-1).getTransactionId();
         } catch (ProCoreException e) {
-            e.printStackTrace();
+            LOGGER.error("Failed to lock backup provider", e);
         }
     }
 
@@ -60,25 +63,9 @@ public class AcornBackupProvider implements IBackupProvider {
         boolean releaseLock = true;
         try {
             lock.acquire();
-
-            client.makeSnapshot(true);
-
-            Path dbDir = client.getDbFolder();
-            int newestFolder = client.clusters.mainState.headDir - 1;
-            int latestFolder = -2;
-            Path AcornMetadataFile = getAcornMetadataFile(dbDir);
-            if (Files.exists(AcornMetadataFile)) {
-                try (BufferedReader br = Files.newBufferedReader(AcornMetadataFile)) {
-                    latestFolder = Integer.parseInt( br.readLine() );
-                }
-            }
-
-            AcornBackupRunnable r = new AcornBackupRunnable(
-                    lock, targetPath, revision, dbDir, latestFolder, newestFolder);
-            new Thread(r, "Acorn backup thread").start();
-
-             releaseLock = false;
-             return r;
+            Future<BackupException> r = client.getBackupRunnable(lock, targetPath, revision);
+            releaseLock = false;
+            return r;
         } catch (InterruptedException e) {
             releaseLock = false;
             throw new BackupException("Failed to lock Acorn for backup.", e);
@@ -127,9 +114,9 @@ public class AcornBackupProvider implements IBackupProvider {
             if (dbRoot != restorePath) {
                 FileUtils.deleteAll(dbRoot.toFile());
                 Files.move(restorePath, dbRoot);
-            } 
+            }
         } catch (IOException e) {
-            e.printStackTrace();
+            LOGGER.error("Failed to restore database revision {} from backup {}", revision, fromPath.toString(), e);
         }
     }
 
@@ -152,9 +139,7 @@ public class AcornBackupProvider implements IBackupProvider {
                 return FileVisitResult.CONTINUE;
             } else if (dir.getParent().getFileName().toString().equals(IDENTIFIER)) {
                 Path targetPath = toPath.resolve(dirName);
-                if (!Files.exists(targetPath)) {
-                    Files.createDirectory(targetPath);
-                }
+                Files.createDirectories(targetPath);
                 return FileVisitResult.CONTINUE;
             } else if (dirName.toString().length() == 1 && Character.isDigit(dirName.toString().charAt(0))) {
                 int dirNameInt = Integer.parseInt(dirName.toString());
@@ -172,13 +157,14 @@ public class AcornBackupProvider implements IBackupProvider {
         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
             if (file.getFileName().toString().endsWith(".tar.gz"))
                 return FileVisitResult.CONTINUE;
-            System.out.println("Restore " + file + " to " + toPath.resolve(currentSubFolder.relativize(file)));
+            if (LOGGER.isTraceEnabled())
+                LOGGER.trace("Restore " + file + " to " + toPath.resolve(currentSubFolder.relativize(file)));
             Files.copy(file, toPath.resolve(currentSubFolder.relativize(file)), StandardCopyOption.REPLACE_EXISTING);
             return FileVisitResult.CONTINUE;
         }
     }
 
-    private static class AcornBackupRunnable implements Runnable, Future<BackupException> {
+    public static class AcornBackupRunnable implements Runnable, Future<BackupException> {
 
         private final Semaphore lock;
         private final Path targetPath;
@@ -218,8 +204,7 @@ public class AcornBackupProvider implements IBackupProvider {
 
         private void doBackup() throws IOException {
             Path target = targetPath.resolve(String.valueOf(revision)).resolve(IDENTIFIER);
-            if (!Files.exists(target))
-                Files.createDirectories(target);
+            Files.createDirectories(target);
             Files.walkFileTree(baseDir,
                     new BackupCopyVisitor(baseDir, target));
         }
@@ -253,22 +238,19 @@ public class AcornBackupProvider implements IBackupProvider {
             @Override
             public FileVisitResult preVisitDirectory(Path dir,
                     BasicFileAttributes attrs) throws IOException {
-                Path dirName = dir.getFileName();
-                if (dirName.equals(fromPath)) {
+                if (dir.equals(fromPath)) {
                     Path targetPath = toPath.resolve(fromPath.relativize(dir));
-                    if (!Files.exists(targetPath)) {
-                        Files.createDirectory(targetPath);
-                    }
+                    Files.createDirectories(targetPath);
                     return FileVisitResult.CONTINUE;
                 } else {
-                    int dirNameInt = Integer.parseInt(dirName.toString());
-                    if (latestFolder < dirNameInt && dirNameInt <= newestFolder) {
-                        Path targetPath = toPath.resolve(fromPath
-                                .relativize(dir));
-                        if (!Files.exists(targetPath)) {
-                            Files.createDirectory(targetPath);
+                    try {
+                        int dirNameInt = Integer.parseInt(dir.getFileName().toString());
+                        if (latestFolder < dirNameInt && dirNameInt <= newestFolder) {
+                            Path targetPath = toPath.resolve(fromPath.relativize(dir));
+                            Files.createDirectories(targetPath);
+                            return FileVisitResult.CONTINUE;
                         }
-                        return FileVisitResult.CONTINUE;
+                    } catch (NumberFormatException e) {
                     }
                     return FileVisitResult.SKIP_SUBTREE;
                 }
@@ -277,8 +259,8 @@ public class AcornBackupProvider implements IBackupProvider {
             @Override
             public FileVisitResult visitFile(Path file,
                     BasicFileAttributes attrs) throws IOException {
-                System.out.println("Backup " + file + " to "
-                        + toPath.resolve(fromPath.relativize(file)));
+                if (LOGGER.isTraceEnabled())
+                    LOGGER.trace("Backup " + file + " to " + toPath.resolve(fromPath.relativize(file)));
                 Files.copy(file, toPath.resolve(fromPath.relativize(file)),
                         StandardCopyOption.REPLACE_EXISTING);
                 return FileVisitResult.CONTINUE;