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