]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.acorn/src/org/simantics/acorn/ClusterManager.java
Fixing problems in the database unit testing environment with Acorn
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / ClusterManager.java
index e0bcbebf7421906a61cd13ff626719aac0f00ef0..2f04d7eda1af9f9976fdb182b1cab3e89b89cd01 100644 (file)
@@ -47,8 +47,9 @@ public class ClusterManager {
        private ArrayList<String> currentChanges = new ArrayList<String>();
 
        public final Path dbFolder;
-       public Path lastSessionDirectory;
-       public Path workingDirectory;
+       private FileCache fileCache;
+       Path lastSessionDirectory;
+       Path workingDirectory;
 
        public LRU<String, ClusterStreamChunk> streamLRU;
        public LRU<Long, ChangeSetInfo> csLRU;
@@ -67,8 +68,9 @@ public class ClusterManager {
         * 
         */
 
-       public ClusterManager(Path dbFolder) {
+       public ClusterManager(Path dbFolder, FileCache fileCache) {
                this.dbFolder = dbFolder;
+               this.fileCache = fileCache;
        }
 
        public ArrayList<String> getChanges(long changeSetId) throws AcornAccessVerificationException, IllegalAcornStateException {
@@ -263,11 +265,11 @@ public class ClusterManager {
                // Nothing has been written => no need to do anything
                long amountOfFiles = countFiles(workingDirectory);
                if(!fullSave && amountOfFiles == 0) {
-    //             System.err.println("amountOfFiles < 3");
+                       //LOGGER.info("makeSnapshot: " + amountOfFiles + " files, skipping snapshot");
                    return false;
                }
     
-               LOGGER.info("makeSnapshot");
+               LOGGER.info("makeSnapshot: start with " + amountOfFiles + " files");
     
                // Schedule writing of all data to disk
             refreshHeadState();
@@ -287,13 +289,11 @@ public class ClusterManager {
 
                persistHeadState();
 
-               if (fullSave)
-                   mainState.save(dbFolder);
-    
-               amountOfFiles = countFiles(workingDirectory);
-               
-               LOGGER.info(" -finished: amount of files is {}", amountOfFiles);
-    
+               if (LOGGER.isInfoEnabled()) {
+                       amountOfFiles = countFiles(workingDirectory);
+                       LOGGER.info(" -finished: amount of files is {}", amountOfFiles);
+               }
+
                workingDirectory = dbFolder.resolve(Integer.toString(mainState.headDir));
                if (!Files.exists(workingDirectory)) {
                    Files.createDirectories(workingDirectory);
@@ -408,35 +408,39 @@ public class ClusterManager {
                lastSessionDirectory = dbFolder.resolve(Integer.toString(mainState.headDir - 1));
                
                // Head State
-               try {
-            state = HeadState.load(lastSessionDirectory);
-        } catch (InvalidHeadStateException e) {
-            // For backwards compatibility only!
-            Throwable cause = e.getCause();
-            if (cause instanceof Throwable) {
-                try {
-                    org.simantics.db.javacore.HeadState oldState = org.simantics.db.javacore.HeadState.load(lastSessionDirectory);
-                    
-                    HeadState newState = new HeadState();
-                    newState.clusters = oldState.clusters;
-                    newState.cs = oldState.cs;
-                    newState.files = oldState.files;
-                    newState.stream = oldState.stream;
-                    newState.headChangeSetId = oldState.headChangeSetId;
-                    newState.reservedIds = oldState.reservedIds;
-                    newState.transactionId = oldState.transactionId;
-                    state = newState;
-                } catch (InvalidHeadStateException e1) {
-                    throw new IOException("Could not load HeadState due to corruption", e1);
-                }
-            } else {
-                // This should never happen as MainState.load() checks the integrity
-                // of head.state files and rolls back in cases of corruption until a
-                // consistent state is found (could be case 0 - initial db state)
-                // IF this does happen something is completely wrong
-                throw new IOException("Could not load HeadState due to corruption", e);
-            }
-        }
+               if (mainState.isInitial()) {
+                       state = new HeadState();
+               } else {
+                       try {
+                   state = HeadState.load(lastSessionDirectory);
+               } catch (InvalidHeadStateException e) {
+                   // For backwards compatibility only!
+                   Throwable cause = e.getCause();
+                   if (cause instanceof Throwable) {
+                       try {
+                           org.simantics.db.javacore.HeadState oldState = org.simantics.db.javacore.HeadState.load(lastSessionDirectory);
+                           
+                           HeadState newState = new HeadState();
+                           newState.clusters = oldState.clusters;
+                           newState.cs = oldState.cs;
+                           newState.files = oldState.files;
+                           newState.stream = oldState.stream;
+                           newState.headChangeSetId = oldState.headChangeSetId;
+                           newState.reservedIds = oldState.reservedIds;
+                           newState.transactionId = oldState.transactionId;
+                           state = newState;
+                       } catch (InvalidHeadStateException e1) {
+                           throw new IOException("Could not load HeadState due to corruption", e1);
+                       }
+                   } else {
+                       // This should never happen as MainState.load() checks the integrity
+                       // of head.state files and rolls back in cases of corruption until a
+                       // consistent state is found (could be case 0 - initial db state)
+                       // IF this does happen something is completely wrong
+                       throw new IOException("Could not load HeadState due to corruption", e);
+                   }
+               }
+               }
                try {
                workingDirectory = dbFolder.resolve(Integer.toString(mainState.headDir));
                Files.createDirectories(workingDirectory);
@@ -467,7 +471,7 @@ public class ClusterManager {
                        Path readDir = dbFolder.resolve(parts[1]);
                        int offset = Integer.parseInt(parts[2]);
                        int length = Integer.parseInt(parts[3]);
-                       FileInfo info = new FileInfo(fileLRU, readDir, parts[0], offset, length);
+                       FileInfo info = new FileInfo(fileLRU, fileCache, readDir, parts[0], offset, length);
                        fileLRU.map(info);
                }
                // Update chunks
@@ -488,7 +492,7 @@ public class ClusterManager {
                        Long revisionId = Long.parseLong(parts[0]);
                        int offset = Integer.parseInt(parts[2]);
                        int length = Integer.parseInt(parts[3]);
-                       ChangeSetInfo info = new ChangeSetInfo(csLRU, readDir, revisionId, offset, length);
+                       ChangeSetInfo info = new ChangeSetInfo(csLRU, fileCache, readDir, revisionId, offset, length);
                        csLRU.map(info);
                }
                
@@ -602,7 +606,7 @@ public class ClusterManager {
                try {
                        ArrayList<String> csids = new ArrayList<String>(currentChanges);
                        currentChanges = new ArrayList<String>();
-                       new ChangeSetInfo(csLRU, changeSetId, data, csids);
+                       new ChangeSetInfo(csLRU, fileCache, changeSetId, data, csids);
                } catch (Throwable t) {
                        throw new IllegalAcornStateException(t);
                } finally {
@@ -669,7 +673,7 @@ public class ClusterManager {
                        try {
                                info = fileLRU.get(key);
                                if (info == null) {
-                                       info = new FileInfo(fileLRU, key, (int) (offset + size));
+                                       info = new FileInfo(fileLRU, fileCache, key, (int) (offset + size));
                                }
                        } catch (Throwable t) {
                                throw new IllegalAcornStateException(t);
@@ -705,5 +709,9 @@ public class ClusterManager {
     public long getTailChangeSetId() {
        return state.tailChangeSetId;
     }
-    
+
+    public FileCache getFileCache() {
+        return fileCache;
+    }
+
 }