import java.io.ByteArrayInputStream;
import java.io.InputStream;
+import java.util.function.Consumer;
import org.simantics.db.Resource;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.impl.Table;
import org.simantics.db.impl.TableHeader;
import org.simantics.db.impl.graph.ReadGraphImpl;
-import org.simantics.db.procedure.AsyncContextMultiProcedure;
-import org.simantics.db.procedure.AsyncMultiProcedure;
+import org.simantics.db.procedure.SyncContextMultiProcedure;
+import org.simantics.db.procedure.SyncMultiProcedure;
import org.simantics.db.service.ClusterUID;
import org.simantics.db.service.ResourceUID;
-import org.simantics.utils.datastructures.Callback;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import fi.vtt.simantics.procore.DebugPolicy;
import fi.vtt.simantics.procore.internal.ClusterChange;
import fi.vtt.simantics.procore.internal.ClusterTable;
import fi.vtt.simantics.procore.internal.SessionImplSocket;
import gnu.trove.map.hash.TIntShortHashMap;
-import gnu.trove.procedure.TIntProcedure;
import gnu.trove.set.hash.TIntHashSet;
final public class ClusterSmall extends ClusterImpl {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ClusterSmall.class);
private static final int TABLE_HEADER_SIZE = TableHeader.HEADER_SIZE + TableHeader.EXTRA_SIZE;
private static final int RESOURCE_TABLE_OFFSET = 0;
return objectTable.getSingleObject(objectIndex, support, this);
}
- public void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, AsyncMultiProcedure<Resource> procedure,
+ public void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, SyncMultiProcedure<Resource> procedure,
ClusterSupport support) throws DatabaseException {
if (deleted) return;
if (DEBUG)
objectTable.foreachObject(graph, objectIndex, procedure, this);
}
- public <C> void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, C context, AsyncContextMultiProcedure<C, Resource> procedure,
+ public <C> void forObjects(ReadGraphImpl graph, int resourceKey, int predicateKey, int objectIndex, C context, SyncContextMultiProcedure<C, Resource> procedure,
ClusterSupport support) throws DatabaseException {
if (DEBUG)
System.out.println("ClusterSmall.forObjects1: rk=" + resourceKey + " pk=" + predicateKey);
@Override
public void forObjects(ReadGraphImpl graph, int resourceKey,
- int predicateKey, AsyncMultiProcedure<Resource> procedure) throws DatabaseException {
+ int predicateKey, SyncMultiProcedure<Resource> procedure) throws DatabaseException {
if (deleted) return;
SessionImplSocket session = (SessionImplSocket)graph.getSession();
ClusterSupport support = session.clusterTranslator;
if (DEBUG)
System.out.println("ClusterSmall.forPredicates: rk=" + resourceKey );
if (deleted) return false;
+ if(proxy) {
+ ClusterImpl cluster = clusterTable.load2(clusterId, clusterKey);
+ return cluster.forPredicates(resourceKey, procedure, context, support);
+ }
+
final int resourceIndex = getLocalReference(resourceKey);
final int predicateIndex = resourceTable.getPredicateIndex(resourceIndex);
if (0 == predicateIndex)
public boolean hasValue(int resourceKey, ClusterSupport support)
throws DatabaseException {
if (deleted) return false;
+ if(proxy) {
+ ClusterImpl cluster = clusterTable.load2(clusterId, clusterKey);
+ return cluster.hasValue(resourceKey, support);
+ }
int resourceIndex = getLocalReference(resourceKey);
return resourceTable.hasValue(resourceIndex);
}
}
@Override
public boolean hasResource(int resourceKey, ClusterSupport support) {
+ if(proxy) {
+ try {
+ ClusterImpl cluster = clusterTable.load2(clusterId, clusterKey);
+ return cluster.hasResource(resourceKey, support);
+ } catch (DatabaseException e) {
+ LOGGER.error("Failed to load cluster with clusterId " + clusterId);
+ return false;
+ }
+ }
if (deleted) return false;
int clusterKey = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(resourceKey);
if (this.clusterKey != clusterKey) // foreign resource
if (ClusterTraitsSmall.resourceRefIsLocal(resourceRef)) {
key = clusterBits | resourceRef;
} else {
- foreignTable.fillResourceUID(ClusterTraitsSmall.resourceRefGetForeignIndex((short)resourceRef), this);
- key = ClusterTraitsBase.createResourceKey(clusterSupport.getClusterKeyByClusterUIDOrMake(clusterUID1, clusterUID2), executeIndex);
+ // TODO: not so nice
+ synchronized(this) {
+ foreignTable.fillResourceUID(ClusterTraitsSmall.resourceRefGetForeignIndex((short)resourceRef), this);
+ key = ClusterTraitsBase.createResourceKey(clusterSupport.getClusterKeyByClusterUIDOrMake(clusterUID1, clusterUID2), executeIndex);
+ }
}
if (DEBUG)
System.out.println("ClusterSmall.execute key=" + key);
@Override
public void load() {
if (deleted) return;
- throw new Error("Not supported.");
+ try {
+ clusterTable.load2(clusterId, clusterKey);
+ } catch (DatabaseException e) {
+ LOGGER.error("Failed to load cluster with clusterId " + clusterId, e);
+ }
}
@Override
- public void load(Callback<DatabaseException> r) {
+ public void load(Consumer<DatabaseException> r) {
if (deleted) return;
- throw new Error("Not supported.");
+ try {
+ clusterTable.load2(clusterId, clusterKey);
+ } catch (DatabaseException e) {
+ r.accept(e);
+ }
}
public boolean contains(int resourceKey) {
clusterTable.load2(clusterId, clusterKey);
callback.run();
} catch (DatabaseException e) {
- e.printStackTrace();
+ LOGGER.error("Failed to load cluster with clusterId " + clusterId, e);
}
}
}
if (deleted) return null; // Can't convert deleted cluster to big.
ClusterBig big = new ClusterBig(getClusterUID(), clusterKey, support);
+ big.setImportance(importance);
big.cc = this.cc;
big.cc.clusterImpl = this;
resourceTable.toBig(big, support, this);
public String toString() {
if (deleted) return "ClusterSmall[" + getClusterId() + " - has been deleted or hasn't been created.]";
try {
- final TIntHashSet set = new TIntHashSet();
- TIntShortHashMap map = foreignTable.getResourceHashMap();
- map.forEachKey(new TIntProcedure() {
- @Override
- public boolean execute(int value) {
- set.add(value & 0xfffff000);
- return true;
- }
+ ForeignTableSmall ft = foreignTable;
+ if (ft == null)
+ return "ClusterSmall[" + getClusterId() + " - " + getNumberOfResources() + "]";
+ TIntShortHashMap map = ft.getResourceHashMap();
+ TIntHashSet set = new TIntHashSet();
+ map.forEachKey(value -> {
+ set.add(value & 0xfffff000);
+ return true;
});
return "ClusterSmall[" + getClusterId() + " - " + getNumberOfResources() + " - " + foreignTable.getResourceHashMap().size() + " - " + set.size() + "]";
} catch (Throwable e) {