+ public static List<ClusterUID> listClusters(ReadGraph graph) throws DatabaseException {
+ XSupport xs = graph.getService(XSupport.class);
+ ClusterUID uids[] = xs.listClusters();
+ ArrayList<ClusterUID> result = new ArrayList<>(uids.length);
+ for(ClusterUID uid : uids) result.add(uid);
+ return result;
+ }
+
+ public static List<Resource> resourcesByCluster(ReadGraph graph, ClusterUID uid) throws DatabaseException {
+ SerialisationSupport ss = graph.getService(SerialisationSupport.class);
+ ArrayList<Resource> result = new ArrayList<Resource>();
+ // 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<Statement> 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<Resource> 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<refArray.length;i+=2) {
+ int s = refArray[i];
+ int o = refArray[i+1];
+ if(reached.contains(s)) {
+ if(reached.add(o)) {
+ changes = true;
+ }
+ }
+ }
+
+ System.err.println("Reachability iteration, changes = " + changes);
+ }
+
+ ArrayList<Resource> result = new ArrayList<>();
+ for(int i=0;i<refArray.length;i+=2) {
+ int s = refArray[i];
+ if(reached.contains(s)) {
+ if(reached.add(refArray[i+1]))
+ changes = true;
+ }
+ }
+
+ res.forEach(new TIntProcedure() {
+
+ @Override
+ public boolean execute(int r) {
+ if(!reached.contains(r)) {
+ try {
+ result.add(ss.getResource(r));
+ } catch (DatabaseException e) {
+ LOGGER.error("Unexpected error while resolving garbage resources.", e);
+ }
+ }
+ return true;
+ }
+
+ });
+
+ return result;
+
+ }
+
+ public static ClusterUID clusterUIDOfResource(ReadGraph graph, Resource resource) throws DatabaseException {
+ SerialisationSupport ss = graph.getService(SerialisationSupport.class);
+ return ss.getUID(resource).asCID();
+ }
+
+ public static boolean isClusterLoaded(ReadGraph graph, ClusterUID clusterUID) throws DatabaseException {
+ XSupport xs = graph.getService(XSupport.class);
+ return xs.isClusterLoaded(clusterUID);
+ }
+
+ public static Type getSCLType(ReadGraph graph, RuntimeEnvironment runtimeEnvironment, String typeText) throws DatabaseException {
+ try {
+ return Environments.getType(runtimeEnvironment.getEnvironment(), typeText);
+ } catch (SCLExpressionCompilationException e) {
+ throw new DatabaseException(e);
+ }
+ }
+
+ public static Type getSCLType(ReadGraph graph, Resource resource, String typeText) throws DatabaseException {
+ try {
+ RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(resource));
+ return Environments.getType(runtimeEnvironment.getEnvironment(), typeText);
+ } catch (SCLExpressionCompilationException e) {
+ throw new DatabaseException(e);
+ }
+ }
+
+ public static Resource getPossibleChild(ReadGraph graph, Resource resource, String name) throws DatabaseException {
+ return graph.sync(new PossibleChild(resource, name));
+ }
+
+ public static Resource getPossibleChild(ReadGraph graph, Resource resource, Resource type, String name) throws DatabaseException {
+ Resource child = graph.sync(new PossibleChild(resource, name));
+ if(child == null) return null;
+ if(!graph.isInstanceOf(child, type)) return null;
+ return child;
+ }
+