1 package fi.vtt.simantics.procore.internal;
3 import org.simantics.db.Resource;
4 import org.simantics.db.exception.DatabaseException;
5 import org.simantics.db.exception.ResourceNotFoundException;
6 import org.simantics.db.exception.RuntimeDatabaseException;
7 import org.simantics.db.impl.ClusterI;
8 import org.simantics.db.impl.ResourceImpl;
9 import org.simantics.db.impl.TransientGraph;
10 import org.simantics.db.procore.cluster.ClusterImpl;
11 import org.simantics.db.service.ClusteringSupport;
13 public class ClusteringSupportImpl implements ClusteringSupport {
15 final private SessionImplSocket session;
17 ClusteringSupportImpl(SessionImplSocket session) {
18 this.session = session;
22 public long createCluster() {
25 id = session.graphSession.newClusterId();
26 } catch (DatabaseException e) {
27 throw new RuntimeDatabaseException("Failed to get new cluster id.", e);
29 session.clusterTable.makeCluster(id, session.writeOnly);
34 public long getCluster(Resource r) {
35 int id = session.querySupport.getId(r);
36 if(id < 0) // Virtual resource
37 return TransientGraph.getVirtualClusterKey(id);
38 return session.clusterTable.getClusterIdByResourceKeyNoThrow(id);
42 public int getNumberOfResources(long clusterId) throws DatabaseException {
43 return session.clusterTable.getClusterByClusterId(clusterId).getNumberOfResources(session.clusterTranslator);
47 public Resource getResourceByKey(int resourceKey)
48 throws ResourceNotFoundException {
49 return session.getResourceByKey(resourceKey);
53 public Resource getResourceByIndexAndCluster(int resourceIndex, long clusterId)
54 throws DatabaseException, ResourceNotFoundException {
55 if (resourceIndex < 1)
56 throw new ResourceNotFoundException("Illegal resource index=" + resourceIndex + " cluster=" + clusterId);
57 ClusterI cluster = session.getClusterTable().getLoadOrThrow(clusterId);
58 int n = cluster.getNumberOfResources(null);
59 if (resourceIndex > n)
60 throw new ResourceNotFoundException("Illegal resource index=" + resourceIndex + " cluster=" + clusterId
62 return session.getResource(resourceIndex, clusterId);
65 // public Id getModifiedId(long clusterId) throws DatabaseException {
66 // return session.clusterTable.getClusterByClusterId(clusterId).getModifiedId();
70 public Resource getClusterSetOfCluster(Resource r) throws DatabaseException {
71 if(!r.isPersistent()) return null;
72 ClusterImpl cluster = session.clusterTable.getClusterByResourceKey(((ResourceImpl)r).id);
73 Long rid = session.clusterSetsSupport.getSet(cluster.getClusterId());
74 if(rid == null || rid == 0) return null;
75 return session.resourceSerializer.getResource(rid);
79 public Resource getClusterSetOfCluster(long cluster) throws DatabaseException {
80 Long rid = session.clusterSetsSupport.getSet(cluster);
81 if(rid == null || rid == 0) return null;
82 return session.resourceSerializer.getResource(rid);
86 public boolean isClusterSet(Resource r) throws DatabaseException {
87 return session.containsClusterSet(r);