1 package fi.vtt.simantics.procore.internal;
\r
3 import org.simantics.db.AsyncReadGraph;
\r
4 import org.simantics.db.DirectStatements;
\r
5 import org.simantics.db.ReadGraph;
\r
6 import org.simantics.db.RelationInfo;
\r
7 import org.simantics.db.Resource;
\r
8 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
\r
9 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
\r
10 import org.simantics.db.exception.DatabaseException;
\r
11 import org.simantics.db.impl.ClusterI;
\r
12 import org.simantics.db.impl.ClusterI.ClusterTypeEnum;
\r
13 import org.simantics.db.impl.ForEachObjectContextProcedure;
\r
14 import org.simantics.db.impl.ForEachObjectProcedure;
\r
15 import org.simantics.db.impl.ForPossibleRelatedValueContextProcedure;
\r
16 import org.simantics.db.impl.ForPossibleRelatedValueProcedure;
\r
17 import org.simantics.db.impl.ResourceImpl;
\r
18 import org.simantics.db.impl.TransientGraph;
\r
19 import org.simantics.db.impl.graph.ReadGraphImpl;
\r
20 import org.simantics.db.procedure.AsyncContextMultiProcedure;
\r
21 import org.simantics.db.procedure.AsyncContextProcedure;
\r
22 import org.simantics.db.procedure.AsyncMultiProcedure;
\r
23 import org.simantics.db.procedure.AsyncProcedure;
\r
24 import org.simantics.db.procedure.Procedure;
\r
25 import org.simantics.db.procedure.SyncProcedure;
\r
26 import org.simantics.db.procore.cluster.ClusterBig;
\r
27 import org.simantics.db.procore.cluster.ClusterImpl;
\r
28 import org.simantics.db.procore.cluster.ClusterSmall;
\r
29 import org.simantics.db.procore.cluster.ResourceTableSmall;
\r
30 import org.simantics.db.procore.cluster.ValueTableSmall;
\r
31 import org.simantics.db.request.AsyncRead;
\r
32 import org.simantics.db.service.DirectQuerySupport;
\r
33 import org.simantics.utils.datastructures.Callback;
\r
35 public class DirectQuerySupportImpl implements DirectQuerySupport {
\r
37 final private SessionImplSocket session;
\r
39 DirectQuerySupportImpl(SessionImplSocket session) {
\r
40 this.session = session;
\r
44 final public void forEachDirectPersistentStatement(AsyncReadGraph graph, final Resource subject, final AsyncProcedure<DirectStatements> procedure) {
\r
45 ReadGraphImpl impl = (ReadGraphImpl)graph;
\r
46 impl.processor.forEachDirectStatement(impl, subject, procedure, true);
\r
50 final public void forEachDirectStatement(AsyncReadGraph graph, final Resource subject, final AsyncProcedure<DirectStatements> procedure) {
\r
51 ReadGraphImpl impl = (ReadGraphImpl)graph;
\r
52 impl.processor.forEachDirectStatement(impl, subject, procedure, false);
\r
56 public void forEachDirectStatement(AsyncReadGraph graph, Resource subject, SyncProcedure<DirectStatements> procedure) {
\r
57 forEachDirectStatement(graph, subject, new SyncToAsyncProcedure<DirectStatements>(procedure));
\r
61 public void forEachDirectStatement(AsyncReadGraph graph, Resource subject, Procedure<DirectStatements> procedure) {
\r
62 ReadGraphImpl impl = (ReadGraphImpl)graph;
\r
63 impl.processor.forEachDirectStatement(impl, subject, procedure);
\r
67 public void forRelationInfo(AsyncReadGraph graph, Resource subject, AsyncProcedure<RelationInfo> procedure) {
\r
68 ReadGraphImpl impl = (ReadGraphImpl)graph;
\r
69 impl.processor.forRelationInfo(impl, subject, procedure);
\r
73 public void forRelationInfo(AsyncReadGraph graph, Resource subject, SyncProcedure<RelationInfo> procedure) {
\r
74 forRelationInfo(graph, subject, new SyncToAsyncProcedure<RelationInfo>(procedure));
\r
78 public void forRelationInfo(AsyncReadGraph graph, Resource subject, Procedure<RelationInfo> procedure) {
\r
79 forRelationInfo(graph, subject, new NoneToAsyncProcedure<RelationInfo>(procedure));
\r
83 public AsyncMultiProcedure<Resource> compileForEachObject(ReadGraph graph, final Resource relation, AsyncMultiProcedure<Resource> user) {
\r
86 RelationInfo info = graph.syncRequest(new AsyncRead<RelationInfo>() {
\r
89 public void perform(AsyncReadGraph graph, AsyncProcedure<RelationInfo> procedure) {
\r
90 forRelationInfo(graph, relation, procedure);
\r
94 public int threadHash() {
\r
99 public int getFlags() {
\r
104 final int predicateKey = ((ResourceImpl)relation).id;
\r
105 return new ForEachObjectProcedure(predicateKey, info, session.queryProvider2, user);
\r
106 } catch (DatabaseException e) {
\r
113 public <C> AsyncContextMultiProcedure<C, Resource> compileForEachObject(ReadGraph graph, final Resource relation, AsyncContextMultiProcedure<C, Resource> user) {
\r
116 RelationInfo info = graph.syncRequest(new AsyncRead<RelationInfo>() {
\r
119 public void perform(AsyncReadGraph graph, AsyncProcedure<RelationInfo> procedure) {
\r
120 forRelationInfo(graph, relation, procedure);
\r
124 public int threadHash() {
\r
129 public int getFlags() {
\r
134 final int predicateKey = ((ResourceImpl)relation).id;
\r
135 return new ForEachObjectContextProcedure<C>(predicateKey, info, session.queryProvider2, user);
\r
136 } catch (DatabaseException e) {
\r
143 public <T> AsyncProcedure<T> compilePossibleRelatedValue(ReadGraph graph, final Resource relation, AsyncProcedure<T> user) {
\r
146 RelationInfo info = graph.syncRequest(new AsyncRead<RelationInfo>() {
\r
149 public void perform(AsyncReadGraph graph, AsyncProcedure<RelationInfo> procedure) {
\r
150 forRelationInfo(graph, relation, procedure);
\r
154 public int threadHash() {
\r
159 public int getFlags() {
\r
164 final int predicateKey = ((ResourceImpl)relation).id;
\r
165 return new ForPossibleRelatedValueProcedure<T>(predicateKey, info, user);
\r
166 } catch (DatabaseException e) {
\r
173 public <C, T> AsyncContextProcedure<C, T> compilePossibleRelatedValue(ReadGraph graph, final Resource relation, AsyncContextProcedure<C, T> user) {
\r
176 RelationInfo info = graph.syncRequest(new AsyncRead<RelationInfo>() {
\r
179 public void perform(AsyncReadGraph graph, AsyncProcedure<RelationInfo> procedure) {
\r
180 forRelationInfo(graph, relation, procedure);
\r
184 public int threadHash() {
\r
189 public int getFlags() {
\r
194 final int predicateKey = ((ResourceImpl)relation).id;
\r
195 return new ForPossibleRelatedValueContextProcedure<C, T>(predicateKey, info, user);
\r
196 } catch (DatabaseException e) {
\r
203 public void forEachObjectCompiled(AsyncReadGraph graph, Resource subject, final AsyncMultiProcedure<Resource> procedure) {
\r
205 assert(subject != null);
\r
207 final ForEachObjectProcedure proc = (ForEachObjectProcedure)procedure;
\r
208 // final RelationInfo info = proc.info;
\r
210 final ReadGraphImpl impl = (ReadGraphImpl)graph;
\r
211 final int subjectId = ((ResourceImpl)subject).id;
\r
213 // int callerThread = impl.callerThread;
\r
214 // int suggestSchedule = (subjectId>>16) & queryProvider2.THREAD_MASK;
\r
218 // if(callerThread == suggestSchedule) {
\r
220 // if(info.isFunctional) {
\r
221 // querySupport.getObjects4(impl, subjectId, proc);
\r
223 session.querySupport.getObjects4(impl, subjectId, proc);
\r
228 // impl.state.barrier.inc();
\r
229 // impl.state.barrier.dec(callerThread);
\r
231 // queryProvider2.schedule(callerThread, new SessionTask(suggestSchedule) {
\r
234 // public void run(int thread) {
\r
236 // impl.state.barrier.inc(thread);
\r
237 // impl.state.barrier.dec();
\r
239 // if(info.isFunctional) {
\r
240 // querySupport.getObjects4(impl.newAsync(thread), subjectId, proc);
\r
242 // querySupport.getObjects4(impl.newAsync(thread), subjectId, proc);
\r
248 // public String toString() {
\r
259 public <C> void forEachObjectCompiled(AsyncReadGraph graph, Resource subject, C context, final AsyncContextMultiProcedure<C, Resource> procedure) {
\r
261 assert(subject != null);
\r
263 final ForEachObjectContextProcedure<C> proc = (ForEachObjectContextProcedure<C>)procedure;
\r
264 final RelationInfo info = proc.info;
\r
266 final ReadGraphImpl impl = (ReadGraphImpl)graph;
\r
267 final int subjectId = ((ResourceImpl)subject).id;
\r
269 // int callerThread = impl.callerThread;
\r
270 // int suggestSchedule = (subjectId>>16) & queryProvider2.THREAD_MASK;
\r
274 if(info.isFunctional) {
\r
275 session.querySupport.getObjects4(impl, subjectId, context, proc);
\r
277 session.querySupport.getObjects4(impl, subjectId, context, proc);
\r
283 public <T> void forPossibleRelatedValueCompiled(AsyncReadGraph graph, Resource subject, final AsyncProcedure<T> procedure) {
\r
285 assert(subject != null);
\r
287 final ForPossibleRelatedValueProcedure<T> proc = (ForPossibleRelatedValueProcedure<T>)procedure;
\r
288 final RelationInfo info = proc.info;
\r
290 final ReadGraphImpl impl = (ReadGraphImpl)graph;
\r
291 final int subjectId = ((ResourceImpl)subject).id;
\r
293 // int callerThread = impl.callerThread;
\r
294 // int suggestSchedule = (subjectId>>16) & session.queryProvider2.THREAD_MASK;
\r
298 // if(callerThread == suggestSchedule) {
\r
300 if(info.isFunctional) {
\r
301 getRelatedValue4(impl, subjectId, proc);
\r
303 getRelatedValue4(impl, subjectId, proc);
\r
308 // impl.state.barrier.inc();
\r
309 // impl.state.barrier.dec(callerThread);
\r
311 // queryProvider2.schedule(callerThread, new SessionTask(suggestSchedule) {
\r
314 // public void run(int thread) {
\r
316 // impl.state.barrier.inc(thread);
\r
317 // impl.state.barrier.dec();
\r
319 // if(info.isFunctional) {
\r
320 // getRelatedValue4(impl.newAsync(thread), subjectId, proc);
\r
322 // getRelatedValue4(impl.newAsync(thread), subjectId, proc);
\r
328 // public String toString() {
\r
329 // return "gaff11";
\r
339 public <C, T> void forPossibleRelatedValueCompiled(AsyncReadGraph graph, Resource subject, C context, final AsyncContextProcedure<C, T> procedure) {
\r
341 assert(subject != null);
\r
343 final ForPossibleRelatedValueContextProcedure<C, T> proc = (ForPossibleRelatedValueContextProcedure<C, T>)procedure;
\r
344 final RelationInfo info = proc.info;
\r
346 final ReadGraphImpl impl = (ReadGraphImpl)graph;
\r
347 final int subjectId = ((ResourceImpl)subject).id;
\r
349 // int callerThread = impl.callerThread;
\r
350 // int suggestSchedule = (subjectId>>16) & session.queryProvider2.THREAD_MASK;
\r
354 if(info.isFunctional) {
\r
355 getRelatedValue4(impl, subjectId, context, proc);
\r
357 getRelatedValue4(impl, subjectId, context, proc);
\r
363 public <T> void forPossibleType(final AsyncReadGraph graph, Resource subject, final AsyncProcedure<Resource> procedure) {
\r
365 assert(subject != null);
\r
367 final ReadGraphImpl impl = (ReadGraphImpl)graph;
\r
368 final int subjectId = ((ResourceImpl)subject).id;
\r
372 final ClusterI cluster = session.clusterTable.getClusterByResourceKey(subjectId);
\r
373 if(!cluster.isLoaded()) {
\r
375 // impl.state.inc(0);
\r
377 session.queryProvider2.requestCluster(impl, cluster.getClusterId(), new Runnable() {
\r
380 public void run() {
\r
384 int result = cluster.getCompleteObjectKey(subjectId, session.clusterTranslator);
\r
385 procedure.execute(graph, new ResourceImpl(session.resourceSupport, result));
\r
387 // impl.state.dec(0);
\r
389 } catch (DatabaseException e) {
\r
390 e.printStackTrace();
\r
399 int result = cluster.getCompleteObjectKey(subjectId, session.clusterTranslator);
\r
400 procedure.execute(graph, new ResourceImpl(session.resourceSupport, result));
\r
404 } catch (DatabaseException e) {
\r
405 e.printStackTrace();
\r
413 public <C> void forPossibleDirectType(final AsyncReadGraph graph, Resource subject, final C context, final AsyncContextProcedure<C, Resource> procedure) {
\r
415 assert(subject != null);
\r
417 final ReadGraphImpl impl = (ReadGraphImpl)graph;
\r
418 final int subjectId = ((ResourceImpl)subject).id;
\r
422 final ClusterI cluster = session.clusterTable.getClusterByResourceKey(subjectId);
\r
423 if(!cluster.isLoaded()) {
\r
425 // impl.state.inc(0);
\r
427 session.queryProvider2.requestCluster(impl, cluster.getClusterId(), new Runnable() {
\r
430 public void run() {
\r
434 ClusterI.CompleteTypeEnum type = cluster.getCompleteType(subjectId, session.clusterTranslator);
\r
435 if(ClusterI.CompleteTypeEnum.InstanceOf == type) {
\r
436 int result = cluster.getCompleteObjectKey(subjectId, session.clusterTranslator);
\r
437 procedure.execute(graph, context, new ResourceImpl(session.resourceSupport, result));
\r
439 procedure.execute(graph, context, null);
\r
442 // impl.state.dec(0);
\r
444 } catch (DatabaseException e) {
\r
445 e.printStackTrace();
\r
454 ClusterI.CompleteTypeEnum type = cluster.getCompleteType(subjectId, session.clusterTranslator);
\r
455 if(ClusterI.CompleteTypeEnum.InstanceOf == type) {
\r
456 int result = cluster.getCompleteObjectKey(subjectId, session.clusterTranslator);
\r
457 procedure.execute(graph, context, new ResourceImpl(session.resourceSupport, result));
\r
459 procedure.execute(graph, context, null);
\r
464 } catch (DatabaseException e) {
\r
466 procedure.execute(graph, context, null);
\r
468 } catch (Throwable t) {
\r
470 t.printStackTrace();
\r
471 procedure.execute(graph, context, null);
\r
478 private <C, T> void getRelatedValue4(final ReadGraphImpl graph, final int subject, final C context, final ForPossibleRelatedValueContextProcedure<C, T> procedure) {
\r
482 final int predicate = procedure.predicateKey;
\r
486 if(!SessionImplSocket.areVirtualStatementsLoaded(session.virtualGraphServerSupport, subject, predicate)) {
\r
487 SessionImplSocket.loadVirtualStatements(session.virtualGraphServerSupport, graph, subject, predicate, new Callback<ReadGraphImpl>() {
\r
490 public void run(ReadGraphImpl graph) {
\r
491 getRelatedValue4(graph, subject, context, procedure);
\r
498 for(TransientGraph g : session.virtualGraphServerSupport.providers) {
\r
499 for (int id : g.getObjects(subject, predicate)) {
\r
501 procedure.exception(graph, new DatabaseException("Multiple objects"));
\r
512 procedure.exception(graph, new DatabaseException("No objects for " + subject ));
\r
518 getValue4(graph, null, result, context, procedure);
\r
525 final org.simantics.db.procore.cluster.ClusterImpl cluster = session.clusterTable.getClusterByResourceKey(subject);
\r
526 if(!cluster.isLoaded()) {
\r
527 cluster.load(session.clusterTranslator, new Runnable() {
\r
530 public void run() {
\r
531 getRelatedValue4(graph, subject, context, procedure);
\r
538 if(cluster.hasVirtual() && session.virtualGraphServerSupport.virtuals.contains(subject)) {
\r
540 if(!SessionImplSocket.areVirtualStatementsLoaded(session.virtualGraphServerSupport, subject, predicate)) {
\r
541 SessionImplSocket.loadVirtualStatements(session.virtualGraphServerSupport, graph, subject, predicate, new Callback<ReadGraphImpl>() {
\r
544 public void run(ReadGraphImpl graph) {
\r
545 getRelatedValue4(graph, subject, context, procedure);
\r
552 for(TransientGraph g : session.virtualGraphServerSupport.providers) {
\r
553 for (int id : g.getObjects(subject, predicate)) {
\r
555 procedure.exception(graph, new DatabaseException("Multiple objects"));
\r
564 getRelatedDirectValue4(graph, cluster, subject, result, context, procedure);
\r
568 getRelatedDirectValue4(graph, cluster, subject, 0, context, procedure);
\r
574 private <T> void getValue4(final ReadGraphImpl graph, final ClusterImpl containerCluster, final int subject, final ForPossibleRelatedValueProcedure<T> procedure) {
\r
576 Object result = null;
\r
580 if(!SessionImplSocket.areVirtualStatementsLoaded(session.virtualGraphServerSupport, subject)) {
\r
581 SessionImplSocket.loadVirtualStatements(session.virtualGraphServerSupport, graph, subject, new Callback<ReadGraphImpl>() {
\r
584 public void run(ReadGraphImpl graph) {
\r
585 getValue4(graph, containerCluster, subject, procedure);
\r
592 for(TransientGraph g : session.virtualGraphServerSupport.providers) {
\r
593 Object value = g.getValue(subject);
\r
594 if(value != null) {
\r
595 if(result != null) {
\r
596 procedure.exception(graph, new DatabaseException("Multiple values"));
\r
605 procedure.execute(graph, (T)"name");
\r
611 ClusterImpl cluster = containerCluster;
\r
612 if(!containerCluster.contains(subject)) {
\r
613 cluster = session.clusterTable.getClusterByResourceKey(subject);
\r
614 if(!cluster.isLoaded()) {
\r
615 cluster.load(session.clusterTranslator, new Runnable() {
\r
618 public void run() {
\r
619 getValue4(graph, containerCluster, subject, procedure);
\r
627 if(cluster.hasVirtual() && session.virtualGraphServerSupport.virtuals.contains(subject)) {
\r
629 if(!SessionImplSocket.areVirtualStatementsLoaded(session.virtualGraphServerSupport, subject)) {
\r
630 SessionImplSocket.loadVirtualStatements(session.virtualGraphServerSupport, graph, subject, new Callback<ReadGraphImpl>() {
\r
633 public void run(ReadGraphImpl graph) {
\r
634 getValue4(graph, containerCluster, subject, procedure);
\r
641 for(TransientGraph g : session.virtualGraphServerSupport.providers) {
\r
642 Object value = g.getValue(subject);
\r
643 if(value != null) {
\r
644 if(result != null) {
\r
645 procedure.exception(graph, new DatabaseException("Multiple values"));
\r
654 if(result != null) {
\r
656 procedure.execute(graph, (T)result);
\r
657 // graph.state.barrier.dec();
\r
661 if(ClusterTypeEnum.SMALL == cluster.getType())
\r
662 getDirectValue4(graph, (ClusterSmall)cluster, subject, procedure);
\r
664 getDirectValue4(graph, (ClusterBig)cluster, subject, procedure);
\r
669 if(ClusterTypeEnum.SMALL == cluster.getType())
\r
670 getDirectValue4(graph, (ClusterSmall)cluster, subject, procedure);
\r
672 getDirectValue4(graph, (ClusterBig)cluster, subject, procedure);
\r
678 private <C, T> void getValue4(final ReadGraphImpl graph, final ClusterImpl containerCluster, final int subject, final C context, final ForPossibleRelatedValueContextProcedure<C, T> procedure) {
\r
680 Object result = null;
\r
684 if(!SessionImplSocket.areVirtualStatementsLoaded(session.virtualGraphServerSupport, subject)) {
\r
685 SessionImplSocket.loadVirtualStatements(session.virtualGraphServerSupport, graph, subject, new Callback<ReadGraphImpl>() {
\r
688 public void run(ReadGraphImpl graph) {
\r
689 getValue4(graph, containerCluster, subject, context, procedure);
\r
696 for(TransientGraph g : session.virtualGraphServerSupport.providers) {
\r
697 Object value = g.getValue(subject);
\r
698 if(value != null) {
\r
699 if(result != null) {
\r
700 procedure.exception(graph, new DatabaseException("Multiple values"));
\r
709 procedure.execute(graph, context, (T)"name");
\r
715 ClusterImpl cluster = containerCluster;
\r
716 if(!containerCluster.contains(subject)) {
\r
717 cluster = session.clusterTable.getClusterByResourceKey(subject);
\r
718 if(!cluster.isLoaded()) {
\r
719 cluster.load(session.clusterTranslator, new Runnable() {
\r
722 public void run() {
\r
723 getValue4(graph, containerCluster, subject, context, procedure);
\r
731 if(cluster.hasVirtual() && session.virtualGraphServerSupport.virtuals.contains(subject)) {
\r
733 if(!SessionImplSocket.areVirtualStatementsLoaded(session.virtualGraphServerSupport, subject)) {
\r
734 SessionImplSocket.loadVirtualStatements(session.virtualGraphServerSupport, graph, subject, new Callback<ReadGraphImpl>() {
\r
737 public void run(ReadGraphImpl graph) {
\r
738 getValue4(graph, containerCluster, subject, context, procedure);
\r
745 for(TransientGraph g : session.virtualGraphServerSupport.providers) {
\r
746 Object value = g.getValue(subject);
\r
747 if(value != null) {
\r
748 if(result != null) {
\r
749 procedure.exception(graph, new DatabaseException("Multiple values"));
\r
758 if(result != null) {
\r
760 procedure.execute(graph, context, (T)result);
\r
761 // graph.state.barrier.dec();
\r
765 if(ClusterTypeEnum.SMALL == cluster.getType())
\r
766 getDirectValue4(graph, (ClusterSmall)cluster, subject, context, procedure);
\r
768 getDirectValue4(graph, (ClusterBig)cluster, subject, context, procedure);
\r
773 if(ClusterTypeEnum.SMALL == cluster.getType())
\r
774 getDirectValue4(graph, (ClusterSmall)cluster, subject, context, procedure);
\r
776 getDirectValue4(graph, (ClusterBig)cluster, subject, context, procedure);
\r
782 private <T> void getRelatedDirectValue4(final ReadGraphImpl graph, final ClusterImpl cluster, final int subject, final int result, final ForPossibleRelatedValueProcedure<T> procedure) {
\r
786 int so = cluster.getSingleObject(subject, procedure, session.clusterTranslator);
\r
789 procedure.exception(graph, new DatabaseException("No objects " + subject + " " + procedure.predicateKey));
\r
792 getValue4(graph, cluster, result, procedure);
\r
796 getValue4(graph, cluster, so, procedure);
\r
798 procedure.exception(graph, new DatabaseException("Multiple objects"));
\r
803 } catch (DatabaseException e) {
\r
804 e.printStackTrace();
\r
809 private <C, T> void getRelatedDirectValue4(final ReadGraphImpl graph, final ClusterImpl cluster, final int subject, final int result, final C context, final ForPossibleRelatedValueContextProcedure<C, T> procedure) {
\r
813 int so = cluster.getSingleObject(subject, procedure, session.clusterTranslator);
\r
816 procedure.exception(graph, new DatabaseException("No objects " + subject + " " + procedure.predicateKey));
\r
819 getValue4(graph, cluster, result, context, procedure);
\r
823 getValue4(graph, cluster, so, context, procedure);
\r
825 procedure.exception(graph, new DatabaseException("Multiple objects"));
\r
830 } catch (DatabaseException e) {
\r
831 e.printStackTrace();
\r
836 public <T> void getRelatedValue4(final ReadGraphImpl graph, final int subject, final ForPossibleRelatedValueProcedure<T> procedure) {
\r
840 final int predicate = procedure.predicateKey;
\r
844 if(!SessionImplSocket.areVirtualStatementsLoaded(session.virtualGraphServerSupport, subject, predicate)) {
\r
845 SessionImplSocket.loadVirtualStatements(session.virtualGraphServerSupport, graph, subject, predicate, new Callback<ReadGraphImpl>() {
\r
848 public void run(ReadGraphImpl graph) {
\r
849 getRelatedValue4(graph, subject, procedure);
\r
856 for(TransientGraph g : session.virtualGraphServerSupport.providers) {
\r
857 for (int id : g.getObjects(subject, predicate)) {
\r
859 procedure.exception(graph, new DatabaseException("Multiple objects"));
\r
870 procedure.exception(graph, new DatabaseException("No objects for " + subject ));
\r
876 getValue4(graph, null, result, procedure);
\r
883 final org.simantics.db.procore.cluster.ClusterImpl cluster = session.clusterTable.getClusterByResourceKey(subject);
\r
884 if(!cluster.isLoaded()) {
\r
885 cluster.load(session.clusterTranslator, new Runnable() {
\r
888 public void run() {
\r
889 getRelatedValue4(graph, subject, procedure);
\r
896 if(cluster.hasVirtual() && session.virtualGraphServerSupport.virtuals.contains(subject)) {
\r
898 if(!SessionImplSocket.areVirtualStatementsLoaded(session.virtualGraphServerSupport, subject, predicate)) {
\r
899 SessionImplSocket.loadVirtualStatements(session.virtualGraphServerSupport, graph, subject, predicate, new Callback<ReadGraphImpl>() {
\r
902 public void run(ReadGraphImpl graph) {
\r
903 getRelatedValue4(graph, subject, procedure);
\r
910 for(TransientGraph g : session.virtualGraphServerSupport.providers) {
\r
911 for (int id : g.getObjects(subject, predicate)) {
\r
913 procedure.exception(graph, new DatabaseException("Multiple objects"));
\r
922 getRelatedDirectValue4(graph, cluster, subject, result, procedure);
\r
926 getRelatedDirectValue4(graph, cluster, subject, 0, procedure);
\r
932 private <C, T> void getDirectValue4(final ReadGraphImpl graph, final ClusterSmall cluster, final int subject, final C context, final ForPossibleRelatedValueContextProcedure<C, T> procedure) {
\r
935 byte[] bytes = cluster.getValue(subject, session.clusterTranslator);
\r
936 T value = (T)utf(bytes);
\r
937 procedure.execute(graph, context, value);
\r
938 } catch (DatabaseException e) {
\r
939 procedure.execute(graph, context, null);
\r
946 private <T> void getDirectValue4(final ReadGraphImpl graph, final ClusterBig cluster, final int subject, final ForPossibleRelatedValueProcedure<T> procedure) {
\r
949 byte[] bytes = cluster.getValue(subject, session.clusterTranslator);
\r
950 T value = (T)utf(bytes);
\r
951 procedure.execute(graph, value);
\r
952 } catch (DatabaseException e) {
\r
953 procedure.execute(graph, null);
\r
960 private <C, T> void getDirectValue4(final ReadGraphImpl graph, final ClusterBig cluster, final int subject, final C context, final ForPossibleRelatedValueContextProcedure<C, T> procedure) {
\r
963 byte[] bytes = cluster.getValue(subject, session.clusterTranslator);
\r
964 if(bytes == null) {
\r
965 procedure.execute(graph, context, null);
\r
967 T value = (T)utf(bytes);
\r
968 procedure.execute(graph, context, value);
\r
970 } catch (DatabaseException e) {
\r
971 procedure.execute(graph, context, null);
\r
978 private final char[] chars = new char[1024];
\r
980 private <T> void getDirectValue4(final ReadGraphImpl graph, final ClusterSmall cluster, final int subject, final ForPossibleRelatedValueProcedure<T> procedure) {
\r
982 ResourceTableSmall rt = cluster.resourceTable;
\r
983 ValueTableSmall vt = cluster.valueTable;
\r
985 byte[] bs = vt.table;
\r
986 long[] ls = rt.table;
\r
988 int index = ((subject&0xFFFF) << 1) - 1 + rt.offset;
\r
990 int valueIndex = (int)(ls[index] >>> 24) & 0x3FFFFF + vt.offset;
\r
992 int size = (int)bs[valueIndex++]-1;
\r
994 for(int i=0;i<size;i++) {
\r
995 chars[i] = (char)bs[valueIndex++];
\r
998 T value = (T)new String(chars, 0, size);
\r
1000 procedure.execute(graph, value);
\r
1005 final private String utf(byte[] bytes) {
\r
1007 if(bytes == null) return null;
\r
1010 int length = bytes[index++]&0xff;
\r
1011 if(length >= 0x80) {
\r
1012 if(length >= 0xc0) {
\r
1013 if(length >= 0xe0) {
\r
1014 if(length >= 0xf0) {
\r
1016 length += ((bytes[index++]&0xff)<<3);
\r
1017 length += ((bytes[index++]&0xff)<<11);
\r
1018 length += ((bytes[index++]&0xff)<<19);
\r
1019 length += 0x10204080;
\r
1023 length += ((bytes[index++]&0xff)<<4);
\r
1024 length += ((bytes[index++]&0xff)<<12);
\r
1025 length += ((bytes[index++]&0xff)<<20);
\r
1026 length += 0x204080;
\r
1031 length += ((bytes[index++]&0xff)<<5);
\r
1032 length += ((bytes[index++]&0xff)<<13);
\r
1038 length += ((bytes[index++]&0xff)<<6);
\r
1044 int target = length+index;
\r
1045 while(index < target) {
\r
1046 int c = bytes[index++]&0xff;
\r
1048 chars[i++] = (char)(c&0x7F);
\r
1049 } else if (c > 0x07FF) {
\r
1050 int c2 = bytes[index++]&0xff;
\r
1051 int c3 = bytes[index++]&0xff;
\r
1052 chars[i++] = (char)(((c&0xf)<<12) + ((c2&0x3f)<<6) + (c3&0x3f));
\r
1054 int c2 = bytes[index++]&0xff;
\r
1055 chars[i++] = (char)(((c&0x1f)<<6) + (c2&0x3f));
\r
1059 // if (!((c >= 0x0001) && (c <= 0x007F))) {
\r
1063 // if ((c >= 0x0001) && (c <= 0x007F)) {
\r
1064 // bytearr[byteIndex++] = (byte)( c );
\r
1065 // } else if (c > 0x07FF) {
\r
1066 // bytearr[byteIndex++] = (byte)(0xE0 | ((c >> 12) & 0x0F));
\r
1067 // bytearr[byteIndex++] = (byte)(0x80 | ((c >> 6) & 0x3F));
\r
1068 // bytearr[byteIndex++] = (byte)(0x80 | ((c >> 0) & 0x3F));
\r
1070 // bytearr[byteIndex++] = (byte)(0xC0 | ((c >> 6) & 0x1F));
\r
1071 // bytearr[byteIndex++] = (byte)(0x80 | ((c >> 0) & 0x3F));
\r
1077 return new String(chars, 0, i);
\r