1 package fi.vtt.simantics.procore.internal;
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5 import java.util.Collection;
6 import java.util.function.Consumer;
8 import org.simantics.db.ObjectResourceIdMap;
9 import org.simantics.db.Resource;
10 import org.simantics.db.Session;
11 import org.simantics.db.Statement;
12 import org.simantics.db.VirtualGraph;
13 import org.simantics.db.WriteGraph;
14 import org.simantics.db.common.StandardStatement;
15 import org.simantics.db.common.request.WriteRequest;
16 import org.simantics.db.common.utils.Logger;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.exception.InvalidResourceReferenceException;
19 import org.simantics.db.exception.ResourceNotFoundException;
20 import org.simantics.db.impl.ClusterI;
21 import org.simantics.db.impl.ClusterSupport;
22 import org.simantics.db.impl.ForEachObjectContextProcedure;
23 import org.simantics.db.impl.ForEachObjectProcedure;
24 import org.simantics.db.impl.ResourceImpl;
25 import org.simantics.db.impl.TransientGraph;
26 import org.simantics.db.impl.VirtualGraphImpl;
27 import org.simantics.db.impl.graph.ReadGraphImpl;
28 import org.simantics.db.impl.query.IntProcedure;
29 import org.simantics.db.impl.query.QueryProcessor;
30 import org.simantics.db.impl.query.QuerySupport;
31 import org.simantics.db.impl.support.BuiltinSupport;
32 import org.simantics.db.impl.support.ResourceSupport;
33 import org.simantics.db.procore.cluster.ClusterImpl;
34 import org.simantics.db.procore.cluster.ClusterSmall;
35 import org.simantics.db.service.SerialisationSupport;
36 import org.simantics.utils.DataContainer;
37 import org.slf4j.LoggerFactory;
39 import gnu.trove.set.hash.TIntHashSet;
41 public class QuerySupportImpl implements QuerySupport {
43 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(QuerySupportImpl.class);
45 final SessionImplSocket session;
47 final ClusterTable clusterTable;
48 final BuiltinSupport builtinSupport;
49 final ClusterSupport clusterSupport;
50 final ResourceSupport resourceSupport;
51 final SerialisationSupport serializationSupport;
52 final VirtualGraphServerSupportImpl virtualGraphServerSupport;
53 final GraphSession graphSession;
54 final SessionRequestManager syncThreads;
57 private boolean pendingPrimitives = false;
59 QuerySupportImpl(SessionImplSocket session, ClusterSupport clusterSupport, SerialisationSupport serializationSupport, SessionRequestManager syncThreads) {
60 this.session = session;
61 this.state = session.state;
62 this.clusterTable = session.clusterTable;
63 this.resourceSupport = session.resourceSupport;
64 this.virtualGraphServerSupport = session.virtualGraphServerSupport;
65 this.graphSession = session.graphSession;
66 this.builtinSupport = session.builtinSupport;
67 this.clusterSupport = clusterSupport;
68 this.serializationSupport = serializationSupport;
69 this.syncThreads = syncThreads;
70 this.root = getBuiltin("http:/");
71 assert(this.session != null);
72 assert(this.state != null);
73 assert(this.clusterTable != null);
74 assert(this.resourceSupport != null);
75 assert(this.virtualGraphServerSupport != null);
76 assert(this.graphSession != null);
77 assert(this.builtinSupport != null);
78 assert(this.clusterSupport != null);
79 assert(this.serializationSupport != null);
80 assert(this.syncThreads != null);
84 public ResourceSupport getSupport() {
85 return resourceSupport;
89 public ClusterSupport getClusterSupport() {
90 return clusterSupport;
94 public ObjectResourceIdMap<String> createChildMap() {
95 return new ObjectResourceMap<String>(session);
99 public Statement getStatement(int s, int p, int o) {
100 return getStatement(null, s, p, o);
104 public Statement getStatement(ReadGraphImpl graph, int s, int p, int o) {
105 Resource sr = getResource(s);
106 Resource pr = getResource(p);
107 Resource or = getResource(o);
108 return new StandardStatement(sr, pr, or);
112 public Session getSession() {
117 public long getClusterId(int id) {
118 ClusterI cluster = clusterTable.getClusterByResourceKey(id);
119 if(cluster == null) return 0;
120 return cluster.getClusterId();
124 public boolean isImmutable(int id) {
125 // Virtuals are mutable
126 if(id < 0) return false;
127 // Root library is mutable
128 if(root == id) return false;
129 // Anything goes in service mode
130 if(session.serviceMode > 0) return false;
131 return clusterTable.isImmutable(id);
135 public int getId(Resource resource) {
136 if (resource instanceof ResourceImpl)
137 return ((ResourceImpl)resource).id;
142 public Resource getResource(int id) {
144 return serializationSupport.getResource(id);
145 } catch (DatabaseException e) {
146 // TODO: consider changing this method to throw something
147 LOGGER.error("getResource(" + id + ") failed", e);
153 public boolean resume(ReadGraphImpl graph) {
155 return syncThreads.session.queryProvider2.resume(graph);
160 // final public void sync(int resumeThread, final SessionRunnable runnable) {
162 // syncThreads.session.queryProvider2.schedule(Integer.MIN_VALUE, new SessionTask(resumeThread) {
165 // public void run(int thread) {
166 // runnable.run(thread);
174 // final public int nextSyncThread() {
175 // throw new Error();
176 //// return syncThreads.nextThread();
180 public void dirtyPrimitives() {
181 session.dirtyPrimitives = true;
182 if(state.getWriteCount() == 0 && !pendingPrimitives) {
183 pendingPrimitives = true;
184 session.asyncRequest(new WriteRequest() {
187 public void perform(WriteGraph graph) throws DatabaseException {
188 pendingPrimitives = false;
197 final public void aboutToRead() {
201 // public void increaseReferenceCount(int callerThread, int subject) {
204 // ClusterI proxy = clusterTable.getClusterByResourceKey(subject);
205 // if (null == proxy)
207 // proxy.increaseReferenceCount(callerThread, 1);
211 // public void decreaseReferenceCount(int callerThread, int subject) {
214 // ClusterProxy proxy = clusterTable.getClusterByResourceKey(subject);
215 // if (null == proxy)
217 // proxy.decreaseReferenceCount(callerThread, 1);
221 public void getObjects4(final ReadGraphImpl graph, final int subject, final ForEachObjectProcedure procedure) {
225 for(TransientGraph g : virtualGraphServerSupport.providers) {
226 for (final int id : g.getObjects(subject, procedure.predicateKey)) {
228 // int suggestSchedule = graph.processor.processor.resourceThread(id);
229 // if(graph.callerThread == suggestSchedule) {
231 procedure.execute(graph, new ResourceImpl(resourceSupport, id));
232 } catch (DatabaseException e) {
233 LOGGER.error("Unexpected exception while handling object", e);
236 // graph.processor.processor.schedule(graph.callerThread, new SessionTask(suggestSchedule) {
239 // public void run(int thread) {
240 // procedure.execute(graph.newAsync(thread), new ResourceImpl(resourceSupport, id));
249 procedure.finished(graph);
250 } catch (DatabaseException e) {
251 LOGGER.error("Unexpected exception while handling objects", e);
258 final ClusterImpl cluster = (ClusterImpl)clusterTable.getClusterByResourceKey(subject);
259 if(!cluster.isLoaded()) {
262 } catch (DatabaseException e) {
263 LOGGER.error("Unexpected exception while handling objects", e);
265 getObjects4(graph, subject, procedure);
269 if (cluster.hasVirtual() && virtualGraphServerSupport.virtuals.contains(subject)) {
271 for(TransientGraph g : virtualGraphServerSupport.providers) {
272 for (final int id : g.getObjects(subject, procedure.predicateKey)) {
274 procedure.execute(graph, new ResourceImpl(resourceSupport, id));
275 } catch (DatabaseException e) {
276 LOGGER.error("Unexpected exception while handling objects", e);
280 // int suggestSchedule = graph.processor.processor.resourceThread(id);
281 // if(graph.callerThread == suggestSchedule) {
283 // graph.processor.processor.schedule(graph.callerThread, new SessionTask(suggestSchedule) {
286 // public void run(int thread) {
287 // procedure.execute(graph.newAsync(thread), new ResourceImpl(resourceSupport, id));
296 cluster.forObjects(graph, subject, procedure);
297 } catch (DatabaseException e) {
304 cluster.forObjects(graph, subject, procedure);
305 } catch (DatabaseException e) {
313 public <C> void getObjects4(final ReadGraphImpl graph, final int subject, final C context, final ForEachObjectContextProcedure<C> procedure) {
317 for(TransientGraph g : virtualGraphServerSupport.providers) {
318 for (final int id : g.getObjects(subject, procedure.predicateKey)) {
320 // int suggestSchedule = graph.processor.processor.resourceThread(id);
321 // if(graph.callerThread == suggestSchedule) {
324 procedure.execute(graph, context, new ResourceImpl(resourceSupport, id));
325 } catch (DatabaseException e) {
326 LOGGER.error("Unexpected exception while handling objects", e);
332 // graph.processor.processor.schedule(graph.callerThread, new SessionTask(suggestSchedule) {
335 // public void run(int thread) {
336 // procedure.execute(graph.newAsync(thread), context, new ResourceImpl(resourceSupport, id));
346 procedure.finished(graph, context);
347 } catch (DatabaseException e) {
348 LOGGER.error("Unexpected exception while handling objects", e);
357 final ClusterImpl cluster = (ClusterImpl)clusterTable.getClusterByResourceKey(subject);
358 if(!cluster.isLoaded()) {
359 cluster.load(session.clusterTranslator, new Runnable() {
363 getObjects4(graph, subject, context, procedure);
370 if (cluster.hasVirtual() && virtualGraphServerSupport.virtuals.contains(subject)) {
372 for(TransientGraph g : virtualGraphServerSupport.providers) {
373 for (final int id : g.getObjects(subject, procedure.predicateKey)) {
374 // int suggestSchedule = graph.processor.processor.resourceThread(id);
375 // if(graph.callerThread == suggestSchedule) {
377 procedure.execute(graph, context, new ResourceImpl(resourceSupport, id));
378 } catch (DatabaseException e) {
379 LOGGER.error("Unexpected exception while handling objects", e);
382 // graph.processor.processor.schedule(graph.callerThread, new SessionTask(suggestSchedule) {
385 // public void run(int thread) {
386 // procedure.execute(graph.newAsync(thread), context, new ResourceImpl(resourceSupport, id));
395 cluster.forObjects(graph, subject, context, procedure);
396 } catch (DatabaseException e) {
403 cluster.forObjects(graph, subject, context, procedure);
404 } catch (DatabaseException e) {
413 public int getSingleInstance(final int subject) {
415 // Do not process this information for virtual resources
416 if(subject < 0) return 0;
418 final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
420 System.out.println("null cluster: " + Integer.toString(subject, 16));
421 assert (cluster != null);
425 ClusterI.CompleteTypeEnum type = cluster.getCompleteType(subject, clusterSupport);
426 if(ClusterI.CompleteTypeEnum.InstanceOf == type) {
427 int result = cluster.getCompleteObjectKey(subject, clusterSupport);
434 } catch (DatabaseException e) {
436 Logger.defaultLogError(e);
440 // This happens is the resource is bogus
441 catch (Throwable t) {
443 analyseProblem(cluster);
445 Logger.defaultLogError(t);
454 public int getSingleSuperrelation(final int subject) {
456 // Do not process this information for virtual resources
457 if(subject < 0) return 0;
459 final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
461 System.out.println("null cluster: " + Integer.toString(subject, 16));
462 assert (cluster != null);
466 ClusterI.CompleteTypeEnum type = cluster.getCompleteType(subject, clusterSupport);
467 if(ClusterI.CompleteTypeEnum.SubrelationOf == type) {
468 int result = cluster.getCompleteObjectKey(subject, clusterSupport);
475 } catch (DatabaseException e) {
477 Logger.defaultLogError(e);
485 public boolean getObjects(final ReadGraphImpl graph, final int subject, final int predicate, final IntProcedure procedure) throws DatabaseException {
487 assert (subject != 0);
488 assert (predicate != 0);
492 boolean found = false;
494 for(TransientGraph g : virtualGraphServerSupport.providers) {
495 for (final int id : g.getObjects(subject, predicate)) {
497 procedure.execute(graph, id);
505 final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
506 assert(cluster.isLoaded());
508 if (cluster.hasVirtual() && virtualGraphServerSupport.virtuals.contains(subject)) {
510 final DataContainer<Boolean> found = new DataContainer<Boolean>(Boolean.FALSE);
511 final TIntHashSet result = new TIntHashSet(5);
513 for(TransientGraph g : virtualGraphServerSupport.providers) {
514 for (final int id : g.getObjects(subject, predicate)) {
517 if (result.add(id)) {
518 procedure.execute(graph, id);
524 // Virtual predicates are not found from persistent clusters
525 if(predicate < 0) return found.get();
527 // wheels within wheels
528 final ClusterI.ObjectProcedure<Object> proc = new ClusterI.ObjectProcedure<Object>() {
531 public boolean execute(Object context, int object) {
534 if (result.add(object)) {
536 procedure.execute(graph, object);
537 } catch (DatabaseException e) {
538 Logger.defaultLogError(e);
542 return false; // continue looping
549 cluster.forObjects(subject, predicate, proc, null, clusterSupport);
550 } catch (DatabaseException e) {
558 // Virtual predicates are not found from persistent clusters
559 if(predicate < 0) return false;
561 class A implements ClusterI.ObjectProcedure<Object> {
563 boolean found = false;
566 public boolean execute(Object context, int object) {
570 procedure.execute(graph, object);
571 } catch (DatabaseException e) {
572 Logger.defaultLogError(e);
574 return false; // continue looping
578 public boolean found() {
584 // wheels within wheels
585 final A proc = new A();
588 cluster.forObjects(subject, predicate, proc, null, clusterSupport);
589 } catch (DatabaseException e) {
590 Logger.defaultLogError(e);
592 // This happens if resource is bogus
593 catch (Throwable t) {
595 analyseProblem(cluster);
597 Logger.defaultLogError(t);
608 // public boolean getFunctionalObject(final ReadGraphImpl graph, final int subject, final int predicate,
609 // final IntProcedure procedure) {
611 // assert (subject != 0);
612 // assert (predicate != 0);
616 // boolean found = false;
618 // Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(subject);
619 // if (providers != null) {
621 // for (VirtualGraph provider : providers) {
623 // for (int id : ((VirtualGraphImpl)provider).getObjects(subject, predicate)) {
625 // procedure.execute(graph, id);
636 // final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
638 // if (cluster.hasVirtual() && virtualGraphServerSupport.virtuals.contains(subject)) {
640 // final DataContainer<Boolean> found = new DataContainer<Boolean>(false);
641 // final TIntHashSet result = new TIntHashSet(5);
643 // Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(subject);
644 // if (providers != null) {
646 // for (VirtualGraph provider : providers) {
648 // for (int id : ((VirtualGraphImpl)provider).getObjects(subject, predicate)) {
650 // procedure.execute(graph, id);
657 // // wheels within wheels
658 // final ClusterI.ObjectProcedure<Object> proc = new ClusterI.ObjectProcedure<Object>() {
661 // public boolean execute(int callerThread, Object context, int object) {
664 // System.out.println("-found object " + object);
665 // if (result.add(object)) {
666 // procedure.execute(graph.newAsync(callerThread), object);
669 // return false; // continue looping
674 // public boolean found() {
675 // throw new UnsupportedOperationException();
681 // cluster.forObjects(graph.callerThread, subject, predicate, proc, null, clusterSupport);
682 // } catch (DatabaseException e) {
683 // Logger.defaultLogError(e);
685 // } catch (Throwable t) {
686 // Logger.defaultLogError(t);
687 // t.printStackTrace();
690 // return found.get();
694 // // wheels within wheels
695 // final ClusterI.ObjectProcedure<Object> proc = new ClusterI.ObjectProcedure<Object>() {
697 // boolean found = false;
700 // public boolean execute(int callerThread, Object context, int object) {
703 // procedure.execute(graph.newAsync(callerThread), object);
704 // return false; // continue looping
709 // public boolean found() {
714 // public String toString() {
715 // return "Wheels for " + procedure;
721 // cluster.forObjects(graph.callerThread, subject, predicate, proc, null, clusterSupport);
722 // } catch (DatabaseException e) {
723 // Logger.defaultLogError(e);
725 // } catch (Throwable t) {
726 // t.printStackTrace();
727 // Logger.defaultLogError(t);
731 // return proc.found();
738 public int getFunctionalObject(final int subject, final int predicate) {
740 assert (subject != 0);
741 assert (predicate != 0);
747 Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(subject);
748 if (providers != null) {
750 for (VirtualGraph provider : providers) {
752 for (int id : ((VirtualGraphImpl)provider).getObjects(subject, predicate)) {
753 if(found == 0) found = id;
762 // if(found == -1) return 0;
763 // else return found;
767 final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
769 if (cluster.hasVirtual() && virtualGraphServerSupport.virtuals.contains(subject)) {
772 Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(subject);
773 if (providers != null) {
775 for (VirtualGraph provider : providers) {
777 for (int id : ((VirtualGraphImpl)provider).getObjects(subject, predicate)) {
778 if(result == 0) result = id;
786 if(result != 0) return result;
789 return cluster.getSingleObject(subject, predicate, clusterSupport);
790 } catch (DatabaseException e) {
797 return cluster.getSingleObject(subject, predicate, clusterSupport);
798 } catch (DatabaseException e) {
801 // This comes if the resource is bogus
802 catch (Throwable t) {
804 analyseProblem(cluster);
806 Logger.defaultLogError(t);
817 // public boolean getStatements(final ReadGraphImpl graph, final int subject, final int predicate,
818 // final TripleIntProcedure procedure, final Statements entry) {
820 // assert (subject != 0);
821 // assert (predicate != 0);
823 // final TIntHashSet result = new TIntHashSet(16);
824 // final DataContainer<Boolean> found = new DataContainer<Boolean>(false);
826 // Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(subject);
828 // if (providers != null) {
830 // for (VirtualGraph provider : providers) {
832 // for (int id : ((VirtualGraphImpl)provider).getObjects(subject, predicate)) {
836 // if (result.add(id)) {
837 // if (null != entry) {
838 // entry.addOrSet(subject, predicate, id);
839 // procedure.execute(graph, subject, predicate, id);
840 // return true; // break;
842 // procedure.execute(graph, subject, predicate, id);
853 // return found.get();
855 // final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
856 // assert (cluster != null);
858 // // wheels within wheels
859 // final ClusterI.ObjectProcedure<Object> proc = new ClusterI.ObjectProcedure<Object>() {
861 // public boolean execute(int callerThread, Object context, int object) {
865 // if (result.add(object)) {
866 // if (null != entry) {
867 // entry.addOrSet(subject, predicate, object);
868 // procedure.execute(graph.newAsync(callerThread), subject, predicate, object);
869 // return true; // break;
871 // procedure.execute(graph.newAsync(callerThread), subject, predicate, object);
875 // return false; // continue looping
880 // public boolean found() {
881 // throw new UnsupportedOperationException();
887 // cluster.forObjects(graph.callerThread, subject, predicate, proc, null, clusterSupport);
888 // } catch (DatabaseException e) {
889 // Logger.defaultLogError(e);
891 // return found.get();
896 public org.simantics.db.DirectStatements getStatements(final ReadGraphImpl graph, final int subject, final QueryProcessor processor, boolean ignoreVirtual) {
898 assert (subject != 0);
900 final DirectStatementsImpl result = new DirectStatementsImpl(resourceSupport, subject);
902 if (!ignoreVirtual) {
903 Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(subject);
904 if (providers != null) {
905 for (TransientGraph provider : providers) {
906 for (int p : provider.getPredicates(subject)) {
907 for (int o : provider.getObjects(subject, p)) {
908 result.addStatement(p, o);
918 final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
919 assert (cluster != null);
920 doGetStatements(graph, cluster, subject, result);
928 public void getPredicates(final ReadGraphImpl graph, final int subject, final IntProcedure procedure) throws DatabaseException {
930 final TIntHashSet result = new TIntHashSet(16);
932 Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(subject);
934 if (providers != null) {
936 for (VirtualGraph provider : providers) {
938 for (int id : ((VirtualGraphImpl)provider).getPredicates(subject)) {
940 if (result.add(id)) {
941 procedure.execute(graph, id);
951 procedure.finished(graph);
955 ClusterI proxy = clusterTable.getClusterByResourceKey(subject);
956 assert (proxy != null);
958 final DataContainer<Integer> got = new DataContainer<Integer>(0);
959 ClusterI.PredicateProcedure<Object> proc = new ClusterI.PredicateProcedure<Object>() {
961 public boolean execute(Object context, int predicate, int oi) {
962 if (result.add(predicate)) {
964 procedure.execute(graph, predicate);
965 } catch (DatabaseException e) {
966 Logger.defaultLogError(e);
969 got.set(got.get() + 1);
970 return false; // continue looping
974 proxy.forPredicates(subject, proc, null, clusterSupport);
975 } catch (DatabaseException e) {
976 Logger.defaultLogError(e);
978 procedure.finished(graph);
985 // public void getValue(ReadGraphImpl graph, int resource, InternalProcedure<byte[]> procedure) {
987 // if(resource < 0) {
989 // Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(resource);
991 // if (providers != null) {
993 // for (VirtualGraph provider : providers) {
995 // Object value = ((VirtualGraphImpl)provider).getValue(resource);
996 // if (value != null) {
997 // procedure.execute(graph, (byte[])value);
1009 // ClusterI cluster = clusterTable.getClusterByResourceKey(resource);
1010 // if (cluster.hasVirtual() && virtualGraphServerSupport.virtuals.contains(resource)) {
1012 // Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(resource);
1014 // if (providers != null) {
1016 // for (VirtualGraph provider : providers) {
1018 // Object value = ((VirtualGraphImpl)provider).getValue(resource);
1019 // if (value != null) {
1020 // procedure.execute(graph, (byte[])value);
1030 // byte[] data = cluster.getValue(resource, clusterSupport);
1031 // if (null == data || 0 == data.length) {
1032 // procedure.execute(graph, null);
1034 // procedure.execute(graph, data);
1037 // } catch (DatabaseException e) {
1038 // Logger.defaultLogError(e);
1045 // byte[] data = cluster.getValue(resource, clusterSupport);
1046 // if (null == data || 0 == data.length) {
1047 // procedure.execute(graph, null);
1049 // procedure.execute(graph, data);
1052 // } catch (DatabaseException e) {
1053 // Logger.defaultLogError(e);
1062 public byte[] getValue(ReadGraphImpl graph, int resource) {
1066 Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(resource);
1068 if (providers != null) {
1070 for (VirtualGraph provider : providers) {
1072 Object value = ((VirtualGraphImpl)provider).getValue(resource);
1073 if (value != null) {
1074 return (byte[])value;
1085 ClusterI cluster = clusterTable.getClusterByResourceKey(resource);
1086 if (cluster.hasVirtual() && virtualGraphServerSupport.virtuals.contains(resource)) {
1088 Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(resource);
1090 if (providers != null) {
1092 for (VirtualGraph provider : providers) {
1094 Object value = ((VirtualGraphImpl)provider).getValue(resource);
1095 if (value != null) {
1096 return (byte[])value;
1105 byte[] data = cluster.getValue(resource, clusterSupport);
1106 if (null != data && 0 != data.length) {
1110 } catch (DatabaseException e) {
1111 Logger.defaultLogError(e);
1120 byte[] data = cluster.getValue(resource, clusterSupport);
1121 if (null != data && 0 != data.length) {
1125 } catch (DatabaseException e) {
1126 Logger.defaultLogError(e);
1136 public InputStream getValueStream(ReadGraphImpl graph, int resource) {
1140 Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(resource);
1142 if (providers != null) {
1144 for (VirtualGraph provider : providers) {
1146 Object value = ((VirtualGraphImpl)provider).getValue(resource);
1147 if (value != null) {
1148 return new ByteArrayInputStream((byte[])value);
1159 ClusterI cluster = clusterTable.getClusterByResourceKey(resource);
1160 if (cluster.hasVirtual() && virtualGraphServerSupport.virtuals.contains(resource)) {
1162 Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(resource);
1164 if (providers != null) {
1166 for (VirtualGraph provider : providers) {
1168 Object value = ((VirtualGraphImpl)provider).getValue(resource);
1169 if (value != null) {
1170 return new ByteArrayInputStream((byte[])value);
1179 return cluster.getValueStream(resource, clusterSupport);
1181 } catch (DatabaseException e) {
1182 Logger.defaultLogError(e);
1191 return cluster.getValueStream(resource, clusterSupport);
1193 } catch (DatabaseException e) {
1194 Logger.defaultLogError(e);
1204 public void requestCluster(ReadGraphImpl graph, final long clusterId, final Runnable r) {
1206 class CallbackAdapter implements Consumer<DatabaseException> {
1208 CallbackAdapter(final Runnable r) {
1212 public void accept(DatabaseException e) {
1214 e.printStackTrace();
1221 double p = clusterTable.getLoadProbability();
1222 // System.out.print("Load cluster " + clusterId + " with probability " + p +
1224 final ClusterI proxy = clusterSupport.getClusterByClusterId(clusterId);
1225 if (!proxy.isLoaded()) {
1227 if (Math.random() < p) {
1228 proxy.load(new CallbackAdapter(r));
1239 public int getBuiltin(String uri) {
1240 return builtinSupport.getBuiltin(uri);
1244 public void checkTasks() {
1245 System.out.println(syncThreads.toString());
1249 // public void asyncWrite(Write request) {
1251 //// if(plainWrite(writer) && sameProvider(request)) {
1252 //// writer.writeSupport.pushRequest(request);
1254 // asyncRequest(request);
1265 private void doGetStatements(ReadGraphImpl graph, final ClusterI cluster, final int subject, final DirectStatementsImpl result) {
1267 final class Proc implements ClusterI.PredicateProcedure<Object> {
1270 public boolean execute(Object context, final int predicate, final int objectIndex) {
1272 doExecute(null, predicate, objectIndex);
1277 private void doExecute(Object context, final int predicate, final int objectIndex) {
1280 cluster.forObjects(subject, predicate, new ClusterI.ObjectProcedure<Object>() {
1283 public boolean execute(Object context, int object) {
1284 result.addStatement(predicate, object);
1288 }, null, clusterSupport);
1289 } catch (DatabaseException e) {
1290 e.printStackTrace();
1298 cluster.forPredicates(subject, new Proc(), null, clusterSupport);
1299 } catch (DatabaseException e) {
1300 e.printStackTrace();
1305 // private void getDirectObjects4(final int callerThread, final ClusterI cluster, final int subject, final int predicate, final QueryProcessor processor, final ReadGraphImpl graph, final ForEachObjectProcedure procedure) {
1307 // if(!cluster.isLoaded()) {
1309 // requestCluster(callerThread, cluster.getClusterId(), new Callback<Integer>() {
1312 // public void run(Integer i) {
1314 // processor.schedule(i, new SessionTask(callerThread) {
1317 // public void run(int thread) {
1319 // getDirectObjects4(thread, cluster, subject, predicate, processor, graph, procedure);
1332 // cluster.forObjects(graph, subject, predicate, procedure);
1333 // } catch (DatabaseException e) {
1334 // e.printStackTrace();
1337 //// procedure.finished(graph);
1338 //// graph.state.barrier.dec();
1340 //// System.err.println("ai2=" + ai2.decrementAndGet());
1347 // AtomicInteger ai2 =new AtomicInteger(0);
1349 // private boolean testCluster(int subject, ClusterI proxy) {
1351 // if (proxy == null)
1352 // System.out.println("null cluster: " + Integer.toString(subject, 16));
1354 // return proxy != null;
1358 long getCluster(int id) {
1360 if(id < 0) return 0;
1361 ClusterI proxy = clusterTable.getClusterByResourceKey(id);
1362 if(proxy == null) return 0;
1363 else return proxy.getClusterId();
1367 public int getRandomAccessReference(String id) throws ResourceNotFoundException {
1370 Resource res = serializationSupport.getResourceSerializer().getResource(id);
1371 if(res == null) return 0;
1372 else return ((ResourceImpl)res).id;
1373 } catch (InvalidResourceReferenceException e) {
1374 //e.printStackTrace();
1381 public void ensureLoaded(final ReadGraphImpl graph, final int subject, final int predicate) {
1385 SessionImplSocket.loadVirtualStatements(virtualGraphServerSupport, graph, subject, predicate, g -> {});
1389 final ClusterI cluster = clusterTable.checkedGetClusterByResourceKey(subject);
1391 if(cluster.isLoaded()) {
1393 if(cluster.hasVirtual() && virtualGraphServerSupport.virtuals.contains(subject) && !SessionImplSocket.areVirtualStatementsLoaded(virtualGraphServerSupport, subject, predicate)) {
1395 SessionImplSocket.loadVirtualStatements(virtualGraphServerSupport, graph, subject, predicate, g -> {});
1403 //new Exception().printStackTrace();
1405 cluster.load(session.clusterTranslator, new Runnable() {
1410 if(cluster.hasVirtual() && virtualGraphServerSupport.virtuals.contains(subject) && !SessionImplSocket.areVirtualStatementsLoaded(virtualGraphServerSupport, subject, predicate)) {
1412 SessionImplSocket.loadVirtualStatements(virtualGraphServerSupport, graph, subject, predicate, g -> {});
1430 public void ensureLoaded(final ReadGraphImpl graph, final int subject) {
1434 SessionImplSocket.loadVirtualStatements(virtualGraphServerSupport, graph, subject, g -> {});
1438 final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
1440 if(cluster.isLoaded()) {
1442 if(cluster.hasVirtual() && virtualGraphServerSupport.virtuals.contains(subject) && !SessionImplSocket.areVirtualStatementsLoaded(virtualGraphServerSupport, subject)) {
1444 SessionImplSocket.loadVirtualStatements(virtualGraphServerSupport, graph, subject, g -> {});
1452 // System.err.println("cluster not loaded " + subject);
1453 //new Exception().printStackTrace();
1455 cluster.load(session.clusterTranslator, new Runnable() {
1460 if(cluster.hasVirtual() && virtualGraphServerSupport.virtuals.contains(subject) && !SessionImplSocket.areVirtualStatementsLoaded(virtualGraphServerSupport, subject)) {
1462 SessionImplSocket.loadVirtualStatements(virtualGraphServerSupport, graph, subject, g -> {});
1480 public boolean isLoaded(int subject) {
1481 ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
1482 return cluster.isLoaded();
1486 public void ceased(int thread) {
1488 session.ceased(thread);
1493 public Object getLock() {
1495 return session.requestManager;
1500 public VirtualGraph getProvider(int subject, int predicate, int object) {
1503 ClusterImpl cluster = (ClusterImpl)clusterTable.getClusterByResourceKey(subject);
1504 // This persistent resource does not have virtual statements => must deny in persistent graph
1505 if(!cluster.hasVirtual() || !virtualGraphServerSupport.virtuals.contains(subject)) return null;
1508 for(TransientGraph g : virtualGraphServerSupport.providers) {
1509 for (final int id : g.getObjects(subject, predicate)) {
1510 if(object == id) return g;
1514 // Nothing found from virtual graphs
1520 public VirtualGraph getProvider(int subject, int predicate) {
1523 ClusterImpl cluster = (ClusterImpl)clusterTable.getClusterByResourceKey(subject);
1524 // This persistent resource does not have virtual statements => must deny in persistent graph
1525 if(!cluster.hasVirtual() || !virtualGraphServerSupport.virtuals.contains(subject)) return null;
1528 TransientGraph result = null;
1529 for(TransientGraph g : virtualGraphServerSupport.providers) {
1530 if(g.getObjects(subject, predicate).length > 0) {
1531 // Found multiple, return null;
1532 if(result != null) return null;
1541 public VirtualGraph getValueProvider(int subject) {
1544 ClusterImpl cluster = (ClusterImpl)clusterTable.getClusterByResourceKey(subject);
1545 // This persistent resource does not have virtual statements => must deny in persistent graph
1546 if(!cluster.hasVirtual() || !virtualGraphServerSupport.virtuals.contains(subject)) return null;
1549 TransientGraph result = null;
1550 for(TransientGraph g : virtualGraphServerSupport.providers) {
1551 if(g.getValue(subject) != null) {
1552 if(result != null) return null;
1561 public void exit(Throwable t) {
1565 private void analyseProblem(ClusterI cluster) {
1567 if(cluster instanceof ClusterSmall)
1569 ((ClusterSmall)cluster).check();
1570 } catch (DatabaseException e) {
1571 LOGGER.error("ClusterSmall.check failed", e);