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