]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterSetsSupportImpl.java
Sync git svn branch with SVN repository r33308.
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ClusterSetsSupportImpl.java
1 package fi.vtt.simantics.procore.internal;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.nio.file.Path;\r
6 import java.util.HashMap;\r
7 import java.util.Map;\r
8 \r
9 import org.simantics.db.Disposable;\r
10 import org.simantics.db.service.ClusterSets;\r
11 import org.simantics.db.service.ClusterSetsSupport;\r
12 \r
13 public class ClusterSetsSupportImpl implements ClusterSetsSupport, Disposable {\r
14     \r
15     final private static boolean DEBUG = false;\r
16     final private static Map<String, ClusterSets> sClusterSets = new HashMap<String, ClusterSets>();\r
17     final private File filePath;\r
18     private String databaseId; // Unique identifier for database of session. Initialized in connect. \r
19     private ClusterSets clusterSets; // Cluster sets for session. Initialized in connect.\r
20     ClusterSetsSupportImpl(File filePath) {\r
21         this.filePath = filePath;\r
22         this.databaseId = null;\r
23         this.clusterSets = null;\r
24     }\r
25     public synchronized void connect(String databaseId) {\r
26         assert(null == this.databaseId);\r
27         this.databaseId = databaseId;\r
28         clusterSets = sClusterSets.get(databaseId);\r
29         int count = 1;\r
30         if (null == clusterSets) {\r
31             clusterSets = new ClusterSets(filePath, filePath, databaseId);\r
32             sClusterSets.put(databaseId, clusterSets);\r
33         } else {\r
34             count = clusterSets.inc();\r
35         }\r
36         if (DEBUG)\r
37             System.out.println("Starting session " + count + " for database=" + databaseId);\r
38     }\r
39     @Override\r
40     public synchronized void dispose() {\r
41         if (clusterSets == null)\r
42             return;\r
43         clusterSets.dispose();\r
44         if (0 >= clusterSets.dec()) {\r
45             sClusterSets.remove(databaseId);\r
46             if (DEBUG)\r
47                 System.out.println("Stopping session for database=" + databaseId);\r
48         }            \r
49         clusterSets = null;\r
50     }\r
51     public synchronized boolean containsKey(long resourceId) {\r
52         return clusterSets.containsKey(resourceId);\r
53     }\r
54     public synchronized Long get(Long resourceId) {\r
55         return clusterSets.get(resourceId);\r
56     }\r
57     public synchronized void put(long resourceId, long clusterId) {\r
58         clusterSets.put(resourceId, clusterId);\r
59     }\r
60     public synchronized void save()\r
61     throws IOException {\r
62         clusterSets.save();\r
63     }\r
64     \r
65     public synchronized Long getSet(long clusterId) {\r
66         return clusterSets.getClusterSet(clusterId);\r
67     }\r
68     @Override\r
69     public void clear() {\r
70         clusterSets.clear();\r
71     }\r
72     \r
73     @Override\r
74     public void setReadDirectory(Path read) {\r
75         // TODO Auto-generated method stub\r
76         \r
77     }\r
78     @Override\r
79     public void updateWriteDirectory(Path write) {\r
80         // Nothing to do here\r
81     }\r
82     \r
83 }\r