]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterSetsSupportImpl2.java
Merge commit 'd7afa23'
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ClusterSetsSupportImpl2.java
1 package fi.vtt.simantics.procore.internal;\r
2 \r
3 import java.io.IOException;\r
4 import java.nio.file.Path;\r
5 import java.util.HashMap;\r
6 import java.util.Map;\r
7 \r
8 import org.simantics.db.Disposable;\r
9 import org.simantics.db.service.ClusterSets;\r
10 import org.simantics.db.service.ClusterSetsSupport;\r
11 \r
12 public class ClusterSetsSupportImpl2 implements ClusterSetsSupport, Disposable {\r
13 \r
14     final private static boolean DEBUG = false;\r
15     final private static Map<String, ClusterSets> sClusterSets = new HashMap<String, ClusterSets>();\r
16     private Path readDirectory;\r
17     private Path writeDirectory;\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     public ClusterSetsSupportImpl2() {\r
21         this.databaseId = null;\r
22         this.clusterSets = null;\r
23     }\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(readDirectory.toFile(), writeDirectory.toFile(), 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         clusterSets.touch();\r
39     }\r
40     @Override\r
41     public synchronized void dispose() {\r
42         if (clusterSets == null)\r
43             return;\r
44         clusterSets.dispose();\r
45         if (0 >= clusterSets.dec()) {\r
46             sClusterSets.remove(databaseId);\r
47             if (DEBUG)\r
48                 System.out.println("Stopping session for database=" + databaseId);\r
49         }            \r
50         clusterSets = null;\r
51     }\r
52     public synchronized boolean containsKey(long resourceId) {\r
53         return clusterSets.containsKey(resourceId);\r
54     }\r
55     public synchronized Long get(Long resourceId) {\r
56         return clusterSets.get(resourceId);\r
57     }\r
58     public synchronized void put(long resourceId, long clusterId) {\r
59         clusterSets.put(resourceId, clusterId);\r
60     }\r
61     public synchronized void save() 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     @Override\r
73     public void updateWriteDirectory(Path write) {\r
74         this.writeDirectory = write;\r
75         if(clusterSets != null) {\r
76             clusterSets.setWriteDirectory(writeDirectory.toFile());\r
77             clusterSets.touch();\r
78         }\r
79     }\r
80     \r
81     @Override\r
82     public void setReadDirectory(Path read) {\r
83         this.readDirectory = read;\r
84     }\r
85 \r
86 }\r