import org.simantics.databoard.Bindings;
import org.simantics.db.ClusterCreator;
import org.simantics.db.Database;
+import org.simantics.db.Database.Session.ClusterChanges;
import org.simantics.db.DevelopmentKeys;
import org.simantics.db.SessionVariables;
-import org.simantics.db.Database.Session.ClusterChanges;
import org.simantics.db.common.utils.Logger;
import org.simantics.db.exception.ClusterDoesNotExistException;
import org.simantics.db.exception.DatabaseException;
ClusterImpl getNewResourceCluster(ClusterSupport cs, GraphSession graphSession, boolean writeOnly)
throws DatabaseException {
+ ClusterImpl result = null;
if (Constants.NewClusterId == newResourceClusterId) {
newResourceClusterId = graphSession.newClusterId();
- return getClusterByClusterIdOrMake(newResourceClusterId, writeOnly);
+ result = getClusterByClusterIdOrMake(newResourceClusterId, writeOnly);
} else {
ClusterImpl cluster = getClusterByClusterIdOrThrow(newResourceClusterId);
if (cluster.getNumberOfResources(cs) >= CLUSTER_FILL_SIZE) {
newResourceClusterId = graphSession.newClusterId();
cluster = getClusterByClusterIdOrMake(newResourceClusterId, writeOnly);
}
- return cluster;
+ result = cluster;
}
+ return ensureLoaded(result);
}
void flushCluster(GraphSession graphSession) {
TLongIntHashMap clusterLoadHistogram = new TLongIntHashMap();
int clusterLoadCounter = 0;
- @SuppressWarnings("unchecked")
- public final <T extends ClusterI> T getClusterByResourceKey(final int resourceKey) {
- int clusterKey = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(resourceKey);
- if (ClusterTraitsBase.isVirtualClusterKey(clusterKey))
- throw new RuntimeException("Tried to get a persistent cluster for a virtual resource.");
- ClusterI c = clusterArray[clusterKey];
- if (c == null)
- return null;
- if (c.isLoaded()) {
- if ((counter++ & 4095) == 0)
- refreshImportance((ClusterImpl) c);
- return (T) c;
- }
- if (!(c instanceof ClusterSmall)) {
- Logger.defaultLogError("Proxy must be instance of ClusterSmall");
- return null;
- }
+ private <T extends ClusterI> T ensureLoaded(T c) {
ClusterI cluster;
- ClusterSmall cs = (ClusterSmall) c;
+ ClusterImpl cs = (ClusterImpl) c;
try {
if(DebugPolicy.REPORT_CLUSTER_LOADING) {
long start = System.nanoTime();
Logger.defaultLogError(e);
if (DebugPolicy.REPORT_CLUSTER_EVENTS)
e.printStackTrace();
- String msg = "Failed to load cluster " + cs.getClusterUID() + " for resource key " + resourceKey
- + " resourceId=" + (((cs.getClusterId() << 16 + (resourceKey & 65535))));
+ String msg = "Failed to load cluster " + cs.getClusterUID();// + " resourceId=" + (((cs.getClusterId() << 16 + (resourceKey & 65535))));
// TODO: this jams the system => needs refactoring.
throw new RuntimeDatabaseException(msg, e);
}
return (T) cluster;
}
+
+ @SuppressWarnings("unchecked")
+ public final <T extends ClusterI> T getClusterByResourceKey(final int resourceKey) {
+ int clusterKey = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(resourceKey);
+ if (ClusterTraitsBase.isVirtualClusterKey(clusterKey))
+ throw new RuntimeException("Tried to get a persistent cluster for a virtual resource.");
+ ClusterI c = clusterArray[clusterKey];
+ if (c == null)
+ return null;
+ if (c.isLoaded()) {
+ if ((counter++ & 4095) == 0)
+ refreshImportance((ClusterImpl) c);
+ return (T) c;
+ }
+ if (!(c instanceof ClusterSmall)) {
+ Logger.defaultLogError("Proxy must be instance of ClusterSmall");
+ return null;
+ }
+ return ensureLoaded((T)c);
+ }
@SuppressWarnings("unchecked")
final <T extends ClusterI> T checkedGetClusterByResourceKey(final int resourceKey) {
private void maintainCluster(ClusterImpl before, ClusterI after_) {
if(after_ != null && after_ != before) {
ClusterImpl after = (ClusterImpl)after_;
- if(currentCluster == before) currentCluster = after;
+ if(currentCluster == before) {
+ currentCluster = after;
+ }
clusterTable.replaceCluster(after);
}
}
public int createResourceKey(int foreignCounter) throws DatabaseException {
- if(currentCluster == null)
+ if(currentCluster == null) {
currentCluster = getNewResourceCluster();
+ }
if(currentCluster.getNumberOfResources(clusterTranslator) == ClusterTable.CLUSTER_FILL_SIZE) {
ClusterWriteOnly newCluster = (ClusterWriteOnly)getNewResourceCluster();
newCluster.foreignLookup = new byte[foreignCounter];