1 package fi.vtt.simantics.procore.internal;
3 import java.io.IOException;
4 import java.nio.file.Path;
5 import java.util.HashMap;
8 import org.simantics.db.Disposable;
9 import org.simantics.db.service.ClusterSets;
10 import org.simantics.db.service.ClusterSetsSupport;
12 public class ClusterSetsSupportImpl2 implements ClusterSetsSupport, Disposable {
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;
25 public synchronized void connect(String databaseId) {
26 assert(null == this.databaseId);
27 this.databaseId = databaseId;
28 clusterSets = sClusterSets.get(databaseId);
30 if (null == clusterSets) {
31 clusterSets = new ClusterSets(readDirectory.toFile(), writeDirectory.toFile(), databaseId);
32 sClusterSets.put(databaseId, clusterSets);
34 count = clusterSets.inc();
37 System.out.println("Starting session " + count + " for database=" + databaseId);
41 public synchronized void dispose() {
42 if (clusterSets == null)
44 clusterSets.dispose();
45 if (0 >= clusterSets.dec()) {
46 sClusterSets.remove(databaseId);
48 System.out.println("Stopping session for database=" + databaseId);
52 public synchronized boolean containsKey(long resourceId) {
53 return clusterSets.containsKey(resourceId);
55 public synchronized Long get(Long resourceId) {
56 return clusterSets.get(resourceId);
58 public synchronized void put(long resourceId, long clusterId) {
59 clusterSets.put(resourceId, clusterId);
61 public synchronized void save() throws IOException {
65 public synchronized Long getSet(long clusterId) {
66 return clusterSets.getClusterSet(clusterId);
73 public void updateWriteDirectory(Path write) {
74 this.writeDirectory = write;
75 if(clusterSets != null) {
76 clusterSets.setWriteDirectory(writeDirectory.toFile());
82 public void setReadDirectory(Path read) {
83 this.readDirectory = read;