--- /dev/null
+package fi.vtt.simantics.procore.internal;\r
+\r
+import java.io.File;\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 ClusterSetsSupportImpl implements ClusterSetsSupport, Disposable {\r
+ \r
+ final private static boolean DEBUG = false;\r
+ final private static Map<String, ClusterSets> sClusterSets = new HashMap<String, ClusterSets>();\r
+ final private File filePath;\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
+ ClusterSetsSupportImpl(File filePath) {\r
+ this.filePath = filePath;\r
+ this.databaseId = null;\r
+ this.clusterSets = null;\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(filePath, filePath, 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
+ }\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()\r
+ 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
+ // Nothing to do here\r
+ }\r
+ \r
+}\r