--- /dev/null
+package fi.vtt.simantics.procore.internal;\r
+\r
+import java.io.IOException;\r
+import java.nio.file.Path;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.simantics.db.Disposable;\r
+import org.simantics.db.service.ClusterSets;\r
+import org.simantics.db.service.ClusterSetsSupport;\r
+\r
+public class ClusterSetsSupportImpl2 implements ClusterSetsSupport, Disposable {\r
+\r
+ final private static boolean DEBUG = false;\r
+ final private static Map<String, ClusterSets> sClusterSets = new HashMap<String, ClusterSets>();\r
+ private Path writeDirectory;\r
+ private Path readDirectory;\r
+ private String databaseId; // Unique identifier for database of session. Initialized in connect. \r
+ private ClusterSets clusterSets; // Cluster sets for session. Initialized in connect.\r
+ public ClusterSetsSupportImpl2() {\r
+ this.databaseId = null;\r
+ this.clusterSets = null;\r
+ }\r
+\r
+ public synchronized void connect(String databaseId) {\r
+ assert(null == this.databaseId);\r
+ this.databaseId = databaseId;\r
+ clusterSets = sClusterSets.get(databaseId);\r
+ int count = 1;\r
+ if (null == clusterSets) {\r
+ clusterSets = new ClusterSets(readDirectory.toFile(), writeDirectory.toFile(), databaseId);\r
+ sClusterSets.put(databaseId, clusterSets);\r
+ } else {\r
+ count = clusterSets.inc();\r
+ }\r
+ if (DEBUG)\r
+ System.out.println("Starting session " + count + " for database=" + databaseId);\r
+ clusterSets.touch();\r
+ }\r
+ @Override\r
+ public synchronized void dispose() {\r
+ if (clusterSets == null)\r
+ return;\r
+ clusterSets.dispose();\r
+ if (0 >= clusterSets.dec()) {\r
+ sClusterSets.remove(databaseId);\r
+ if (DEBUG)\r
+ System.out.println("Stopping session for database=" + databaseId);\r
+ } \r
+ clusterSets = null;\r
+ }\r
+ public synchronized boolean containsKey(long resourceId) {\r
+ return clusterSets.containsKey(resourceId);\r
+ }\r
+ public synchronized Long get(Long resourceId) {\r
+ return clusterSets.get(resourceId);\r
+ }\r
+ public synchronized void put(long resourceId, long clusterId) {\r
+ clusterSets.put(resourceId, clusterId);\r
+ }\r
+ public synchronized void save() throws IOException {\r
+ clusterSets.save();\r
+ }\r
+ \r
+ public synchronized Long getSet(long clusterId) {\r
+ return clusterSets.getClusterSet(clusterId);\r
+ }\r
+ @Override\r
+ public void clear() {\r
+ clusterSets.clear();\r
+ }\r
+ @Override\r
+ public void updateReadAndWriteDirectories(Path read, Path write) {\r
+ setDirectories(read, write);\r
+ }\r
+ \r
+ private void setDirectories(Path readDirectory, Path writeDirectory) {\r
+ this.readDirectory = readDirectory;\r
+ this.writeDirectory = writeDirectory;\r
+ if(clusterSets != null) {\r
+ clusterSets.setWriteDirectory(writeDirectory.toFile());\r
+ clusterSets.touch();\r
+ }\r
+ }\r
+\r
+}\r