1 package fi.vtt.simantics.procore.internal;
\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
8 import org.simantics.db.Disposable;
\r
9 import org.simantics.db.service.ClusterSets;
\r
10 import org.simantics.db.service.ClusterSetsSupport;
\r
12 public class ClusterSetsSupportImpl2 implements ClusterSetsSupport, Disposable {
\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
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
30 if (null == clusterSets) {
\r
31 clusterSets = new ClusterSets(readDirectory.toFile(), writeDirectory.toFile(), databaseId);
\r
32 sClusterSets.put(databaseId, clusterSets);
\r
34 count = clusterSets.inc();
\r
37 System.out.println("Starting session " + count + " for database=" + databaseId);
\r
38 clusterSets.touch();
\r
41 public synchronized void dispose() {
\r
42 if (clusterSets == null)
\r
44 clusterSets.dispose();
\r
45 if (0 >= clusterSets.dec()) {
\r
46 sClusterSets.remove(databaseId);
\r
48 System.out.println("Stopping session for database=" + databaseId);
\r
52 public synchronized boolean containsKey(long resourceId) {
\r
53 return clusterSets.containsKey(resourceId);
\r
55 public synchronized Long get(Long resourceId) {
\r
56 return clusterSets.get(resourceId);
\r
58 public synchronized void put(long resourceId, long clusterId) {
\r
59 clusterSets.put(resourceId, clusterId);
\r
61 public synchronized void save() throws IOException {
\r
65 public synchronized Long getSet(long clusterId) {
\r
66 return clusterSets.getClusterSet(clusterId);
\r
69 public void clear() {
\r
70 clusterSets.clear();
\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
82 public void setReadDirectory(Path read) {
\r
83 this.readDirectory = read;
\r