X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Futils%2FCommonDBUtils.java;h=77eacf2055e9002c86aa99da5fed5c96a3f15906;hp=1bcfb0a8a27e614400f9d6d0abc64d6bab613cc4;hb=9f0fd59be54719b1fe9322d8fd37e4950857308c;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java index 1bcfb0a8a..77eacf205 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java @@ -13,17 +13,31 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Statement; import org.simantics.db.WriteGraph; +import org.simantics.db.common.procedure.adapter.DirectStatementProcedure; import org.simantics.db.common.request.IsParent; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.PossibleObjectWithType; import org.simantics.db.common.request.PossibleOwner; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.InvalidResourceReferenceException; +import org.simantics.db.service.ClusterUID; import org.simantics.db.service.ClusteringSupport; +import org.simantics.db.service.DirectQuerySupport; +import org.simantics.db.service.SerialisationSupport; +import org.simantics.db.service.XSupport; import org.simantics.layer0.Layer0; import org.simantics.utils.datastructures.collections.CollectionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import gnu.trove.list.array.TIntArrayList; +import gnu.trove.procedure.TIntProcedure; +import gnu.trove.set.hash.TIntHashSet; public class CommonDBUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(CommonDBUtils.class); + public static boolean isParent(ReadGraph graph, Resource possibleParent, Resource possibleChild) throws DatabaseException { return graph.sync(new IsParent(possibleParent, possibleChild)); } @@ -33,11 +47,11 @@ public class CommonDBUtils { } public static String possibleRelatedString(ReadGraph graph, Resource subject, Resource relation) throws DatabaseException { - return graph.getRelatedValue(subject, relation, Bindings.STRING); + return graph.getPossibleRelatedValue(subject, relation, Bindings.STRING); } public static Integer possibleRelatedInteger(ReadGraph graph, Resource subject, Resource relation) throws DatabaseException { - return graph.getRelatedValue(subject, relation, Bindings.INTEGER); + return graph.getPossibleRelatedValue(subject, relation, Bindings.INTEGER); } public static Resource getPossibleOwner(ReadGraph graph, Resource resource) throws DatabaseException { @@ -193,4 +207,123 @@ public class CommonDBUtils { return graph.syncRequest(new PossibleObjectWithType(subject, relation, type)); } + public static List listClusters(ReadGraph graph) throws DatabaseException { + XSupport xs = graph.getService(XSupport.class); + ClusterUID uids[] = xs.listClusters(); + ArrayList result = new ArrayList<>(uids.length); + for(ClusterUID uid : uids) result.add(uid); + return result; + } + + public static List resourcesByCluster(ReadGraph graph, ClusterUID uid) throws DatabaseException { + SerialisationSupport ss = graph.getService(SerialisationSupport.class); + ArrayList result = new ArrayList(); + // Index 0 is illegal + for(int i=1;i<1<<12;i++) { + try { + result.add(ss.getResource(uid.toRID(i))); + } catch (InvalidResourceReferenceException e) { + } + } + return result; + } + + public static List directStatements(ReadGraph graph, Resource resource, boolean ignoreVirtual) throws DatabaseException { + + DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class); + DirectStatementProcedure proc = new DirectStatementProcedure(); + + if (ignoreVirtual) { + return dqs.getDirectPersistentStatements(graph, resource); + } else { + return dqs.getDirectStatements(graph, resource); + } + + } + + public static List garbageResources(ReadGraph graph) throws DatabaseException { + + SerialisationSupport ss = graph.getService(SerialisationSupport.class); + + TIntArrayList refs = new TIntArrayList(); + TIntArrayList res = new TIntArrayList(); + + // Find all statements in the database + for(ClusterUID uid : listClusters(graph)) { + for(Resource r : resourcesByCluster(graph, uid)) { + int sid = ss.getTransientId(r); + for(Statement stm : directStatements(graph, r, true)) { + int oid = ss.getTransientId(stm.getObject()); + refs.add(sid); + refs.add(oid); + } + res.add(sid); + } + } + + TIntHashSet reached = new TIntHashSet(); + + // Initialize root + int root = ss.getTransientId(graph.getRootLibrary()); + reached.add(root); + + int[] refArray = refs.toArray(); + + boolean changes = true; + + while(changes) { + changes = false; + for(int i=0;i