1 package org.simantics.db.impl.query;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.concurrent.Semaphore;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.db.AsyncReadGraph;
9 import org.simantics.db.DevelopmentKeys;
10 import org.simantics.db.ObjectResourceIdMap;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.RelationInfo;
13 import org.simantics.db.common.utils.Logger;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.impl.procedure.InternalProcedure;
17 import org.simantics.db.procedure.AsyncMultiProcedure;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.db.procedure.ListenerBase;
20 import org.simantics.db.procedure.Procedure;
21 import org.simantics.db.procedure.SyncMultiProcedure;
22 import org.simantics.db.request.AsyncMultiRead;
23 import org.simantics.db.request.AsyncRead;
24 import org.simantics.db.request.ExternalRead;
25 import org.simantics.db.request.MultiRead;
26 import org.simantics.db.request.Read;
27 import org.simantics.utils.Development;
29 import gnu.trove.map.hash.THashMap;
30 import gnu.trove.map.hash.TObjectIntHashMap;
32 public class QueryCacheBase {
36 public final int THREAD_MASK;
42 public volatile boolean dirty = false;
43 public boolean collecting = false;
45 protected final THashMap<String, URIToResource> uRIToResourceMap;
46 //protected final THashMap<String, NamespaceIndex> namespaceIndexMap;
47 protected final UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>> childMapMap;
48 protected final DoubleKeyQueryHashMap<IntProcedure> objectsMap;
49 protected final DoubleKeyQueryHashMap<TripleIntProcedure> assertedStatementsMap;
50 protected final DoubleKeyQueryHashMap<IntProcedure> directObjectsMap;
51 protected final DoubleKeyQueryHashMap<TripleIntProcedure> statementsMap;
52 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> typesMap;
53 protected final UnaryQueryHashMap<IntProcedure> principalTypesMap;
54 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> predicatesMap;
55 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> superTypesMap;
56 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> typeHierarchyMap;
57 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> superRelationsMap;
59 protected final UnaryQueryHashMap<IntProcedure> orderedSetMap;
60 protected final UnaryQueryHashMap<IntProcedure> assertedPredicatesMap;
61 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> directPredicatesMap;
62 protected final UnaryQueryHashMap<IntProcedure> directSuperRelationsMap;
64 protected final UnaryQueryHashMap<InternalProcedure<RelationInfo>> relationInfoQueryMap;
65 protected final UnaryQueryHashMap<InternalProcedure<byte[]>> valueQueryMap;
67 protected final StableHashMap<AsyncRead, AsyncReadEntry> asyncReadEntryMap;
68 protected final StableHashMap<Read, ReadEntry> readEntryMap;
69 protected final StableHashMap<MultiRead, MultiReadEntry> multiReadEntryMap;
70 protected final StableHashMap<AsyncMultiRead, AsyncMultiReadEntry> asyncMultiReadEntryMap;
71 protected final StableHashMap<ExternalRead, ExternalReadEntry> externalReadEntryMap;
73 public final QuerySupport querySupport;
75 public QueryCacheBase(QuerySupport querySupport, int threads) {
78 THREAD_MASK = threads - 1;
80 this.querySupport = querySupport;
81 directPredicatesMap = new UnaryQueryHashMap();
82 directSuperRelationsMap = new UnaryQueryHashMap();
83 valueQueryMap = new UnaryQueryHashMap();
84 principalTypesMap = new UnaryQueryHashMap();
85 uRIToResourceMap = new THashMap<String, URIToResource>();
86 //namespaceIndexMap = new THashMap<String, NamespaceIndex>();
87 childMapMap = new UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>>();
88 relationInfoQueryMap = new UnaryQueryHashMap();
89 typeHierarchyMap = new UnaryQueryHashMap();
90 superTypesMap = new UnaryQueryHashMap();
91 superRelationsMap = new UnaryQueryHashMap();
92 typesMap = new UnaryQueryHashMap();
93 objectsMap = new DoubleKeyQueryHashMap();
94 orderedSetMap = new UnaryQueryHashMap();
95 predicatesMap = new UnaryQueryHashMap();
96 statementsMap = new DoubleKeyQueryHashMap();
97 directObjectsMap = new DoubleKeyQueryHashMap();
98 assertedPredicatesMap = new UnaryQueryHashMap();
99 assertedStatementsMap = new DoubleKeyQueryHashMap();
100 asyncReadEntryMap = new StableHashMap<AsyncRead, AsyncReadEntry>();
101 readEntryMap = new StableHashMap<Read, ReadEntry>();
102 asyncMultiReadEntryMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>();
103 multiReadEntryMap = new StableHashMap<MultiRead, MultiReadEntry>();
104 externalReadEntryMap = new StableHashMap<ExternalRead, ExternalReadEntry>();
107 public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncMultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
109 ReadGraphImpl queryGraph = parentGraph.withParent(entry_, null, false);
111 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
112 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
116 query.perform(queryGraph, new AsyncMultiProcedure<T>() {
119 public void execute(AsyncReadGraph graph, T result) {
120 ReadGraphImpl impl = (ReadGraphImpl)graph;
121 entry.addOrSet(result);
123 procedure.execute(parentGraph, result);
124 } catch (Throwable t) {
130 public void finished(AsyncReadGraph graph) {
131 ReadGraphImpl impl = (ReadGraphImpl)graph;
132 entry.finish(parentGraph);
134 procedure.finished(parentGraph);
135 } catch (Throwable t) {
141 public void exception(AsyncReadGraph graph, Throwable t) {
142 ReadGraphImpl impl = (ReadGraphImpl)graph;
143 entry.except(parentGraph, t);
145 procedure.exception(parentGraph, t);
146 } catch (Throwable t2) {
147 t2.printStackTrace();
153 return entry.getResult();
155 } catch (Throwable t) {
159 procedure.exception(parentGraph, t);
160 } catch (Throwable t2) {
161 t2.printStackTrace();
164 return entry.getResult();
170 public <T> Object performQuery(ReadGraphImpl parentGraph, final MultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
172 ReadGraphImpl queryGraph = parentGraph.withParent(entry_, null, true);
174 MultiReadEntry entry = (MultiReadEntry)entry_;
175 SyncMultiProcedure<T> procedure = (SyncMultiProcedure<T>)procedure_;
179 query.perform(queryGraph, new SyncMultiProcedure<T>() {
182 public void execute(ReadGraph graph, T result) {
183 ReadGraphImpl impl = (ReadGraphImpl)graph;
184 entry.addOrSet(result);
186 procedure.execute(parentGraph, result);
187 } catch (Throwable t) {
193 public void finished(ReadGraph graph) {
194 ReadGraphImpl impl = (ReadGraphImpl)graph;
195 entry.finish(parentGraph);
197 procedure.finished(parentGraph);
198 } catch (Throwable t) {
204 public void exception(ReadGraph graph, Throwable t) {
205 ReadGraphImpl impl = (ReadGraphImpl)graph;
206 entry.except((DatabaseException)t);
208 procedure.exception(parentGraph, t);
209 } catch (Throwable t2) {
210 t2.printStackTrace();
216 return entry.getResult();
218 } catch (Throwable t) {
222 procedure.exception(parentGraph, t);
223 } catch (Throwable t2) {
224 t2.printStackTrace();
227 return entry.getResult();
233 public Collection<CacheEntry> getRootList() {
235 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
237 for (Object e : valueQueryMap.values()) {
238 result.add((CacheEntry) e);
240 for (Object e : directPredicatesMap.values()) {
241 result.add((CacheEntry) e);
243 for (Object e : directSuperRelationsMap.values()) {
244 result.add((CacheEntry) e);
246 for (Object e : objectsMap.values()) {
247 result.add((CacheEntry) e);
249 for (Object e : directObjectsMap.values()) {
250 result.add((CacheEntry) e);
252 for (Object e : principalTypesMap.values()) {
253 result.add((CacheEntry) e);
255 for (Object e : superRelationsMap.values()) {
256 result.add((CacheEntry) e);
258 for (Object e : superTypesMap.values()) {
259 result.add((CacheEntry) e);
261 for (Object e : typesMap.values()) {
262 result.add((CacheEntry) e);
264 for (Object e : objectsMap.values()) {
265 result.add((CacheEntry) e);
267 for (Object e : assertedStatementsMap.values()) {
268 result.add((CacheEntry) e);
270 for (Object e : readEntryMap.values()) {
271 if(e instanceof CacheEntry) {
272 result.add((CacheEntry) e);
274 System.err.println("e=" + e);
277 for (Object e : asyncReadEntryMap.values()) {
278 if(e instanceof CacheEntry) {
279 result.add((CacheEntry) e);
281 System.err.println("e=" + e);
284 for (Object e : externalReadEntryMap.values()) {
285 result.add((CacheEntry) e);
287 for (Object e : orderedSetMap.values()) {
288 result.add((CacheEntry) e);
295 public int calculateCurrentSize() {
299 realSize += directPredicatesMap.size();
300 realSize += directSuperRelationsMap.size();
301 realSize += principalTypesMap.size();
302 realSize += uRIToResourceMap.size();
303 //realSize += namespaceIndexMap.size();
304 realSize += childMapMap.size();
306 realSize += relationInfoQueryMap.size();
307 realSize += superTypesMap.size();
308 realSize += typeHierarchyMap.size();
309 realSize += superRelationsMap.size();
310 realSize += typesMap.size();
312 realSize += valueQueryMap.size();
313 realSize += directObjectsMap.size();
314 realSize += objectsMap.size();
315 realSize += orderedSetMap.size();
316 realSize += predicatesMap.size();
318 realSize += statementsMap.size();
319 realSize += assertedPredicatesMap.size();
320 realSize += assertedStatementsMap.size();
321 realSize += externalReadEntryMap.size();
322 realSize += asyncReadEntryMap.size();
324 realSize += readEntryMap.size();
325 realSize += asyncMultiReadEntryMap.size();
326 realSize += multiReadEntryMap.size();
332 CacheCollectionResult allCaches(CacheCollectionResult result) {
334 int level = Integer.MAX_VALUE;
335 directPredicatesMap.values(level, result);
336 directSuperRelationsMap.values(level, result);
337 principalTypesMap.values(level, result);
338 for(CacheEntryBase e : uRIToResourceMap.values())
339 if(e.getLevel() <= level)
341 // for(CacheEntryBase e : namespaceIndexMap.values())
342 // if(e.getLevel() <= level)
345 childMapMap.values(level, result);
347 relationInfoQueryMap.values(level, result);
348 superTypesMap.values(level, result);
349 typeHierarchyMap.values(level, result);
350 superRelationsMap.values(level, result);
351 typesMap.values(level, result);
353 valueQueryMap.values(level, result);
354 directObjectsMap.values(level, result);
355 objectsMap.values(level, result);
356 orderedSetMap.values(level, result);
357 predicatesMap.values(level, result);
359 statementsMap.values(level, result);
360 assertedPredicatesMap.values(level, result);
361 assertedStatementsMap.values(level, result);
362 externalReadEntryMap.values(level, result);
363 asyncReadEntryMap.values(level, result);
365 readEntryMap.values(level, result);
366 asyncMultiReadEntryMap.values(level, result);
367 multiReadEntryMap.values(level, result);
373 public void scanPending() {
375 ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
377 entries.addAll(directPredicatesMap.values());
378 entries.addAll(directSuperRelationsMap.values());
379 entries.addAll(principalTypesMap.values());
380 entries.addAll(uRIToResourceMap.values());
381 //entries.addAll(namespaceIndexMap.values());
382 entries.addAll(childMapMap.values());
383 entries.addAll(relationInfoQueryMap.values());
384 entries.addAll(superTypesMap.values());
385 entries.addAll(superRelationsMap.values());
386 entries.addAll(typesMap.values());
387 entries.addAll(valueQueryMap.values());
388 entries.addAll(directObjectsMap.values());
389 entries.addAll(objectsMap.values());
390 entries.addAll(orderedSetMap.values());
391 entries.addAll(predicatesMap.values());
392 entries.addAll(orderedSetMap.values());
393 entries.addAll(statementsMap.values());
394 // entries.addAll(assertedObjectsMap.values());
395 entries.addAll(assertedPredicatesMap.values());
396 entries.addAll(assertedStatementsMap.values());
397 entries.addAll(externalReadEntryMap.values());
398 entries.addAll(asyncReadEntryMap.values());
399 entries.addAll(externalReadEntryMap.values());
400 entries.addAll(readEntryMap.values());
401 entries.addAll(asyncMultiReadEntryMap.values());
402 entries.addAll(multiReadEntryMap.values());
403 entries.addAll(readEntryMap.values());
404 System.out.println(entries.size() + " entries.");
405 for(Object e : entries) {
406 if(e instanceof CacheEntry) {
407 CacheEntry en = (CacheEntry)e;
408 if(en.isPending()) System.out.println("pending " + e);
409 if(en.isExcepted()) System.out.println("excepted " + e);
410 if(en.isDiscarded()) System.out.println("discarded " + e);
411 if(en.isRefuted()) System.out.println("refuted " + e);
412 if(en.isFresh()) System.out.println("fresh " + e);
414 //System.out.println("Unknown object " + e);
419 public static void waitPending(ReadGraphImpl graph, CacheEntry entry) throws DatabaseException {
422 while(entry.isPending()) {
424 boolean performed = false;//graph.performPending();
428 if(counter > 30000) {
429 CacheEntryBase base = ((CacheEntryBase)entry);
430 // if(base.created != null) {
431 // System.err.println("created:");
432 // base.created.printStackTrace();
434 // if(base.performed != null) {
435 // System.err.println("performed:");
436 // base.performed.printStackTrace();
438 // if(base.ready != null) {
439 // System.err.println("ready:");
440 // base.ready.printStackTrace();
442 new Exception("Timeout waiting for request to complete: " + entry.getOriginalRequest()).printStackTrace();
443 throw new DatabaseException("Timeout waiting for request to complete." + entry.getOriginalRequest());
444 //System.err.println("asd");
445 //base.getQuery().recompute(null, null, entry);
448 } catch (InterruptedException e) {
454 //////////////////////////////////////
456 public static Collection<Objects> entriesObjects(QueryProcessor processor, int r1) {
457 synchronized(processor.cache.objectsMap) {
458 return processor.cache.objectsMap.values(r1);
462 public static Collection<Objects> entriesObjects(QueryProcessor processor) {
463 synchronized(processor.cache.objectsMap) {
464 return processor.cache.objectsMap.values();
468 public static Collection<CacheEntry> entriesDirectPredicates(QueryProcessor processor) {
469 synchronized(processor.cache.directPredicatesMap) {
470 return processor.cache.directPredicatesMap.values();
474 static final Collection<DirectObjects> entriesDirectObjects(final QueryProcessor processor, final int r1) {
475 DoubleKeyQueryHashMap<IntProcedure> hash = processor.cache.directObjectsMap;
476 return hash.values(r1);
479 static final Collection<Statements> entriesStatements(final QueryProcessor processor, final int r1) {
480 return processor.cache.statementsMap.values(r1);
483 static final Types entryTypes(final QueryProcessor processor, final int r) {
484 return (Types)processor.cache.typesMap.get(r);
487 static final PrincipalTypes entryPrincipalTypes(final QueryProcessor processor, final int r) {
488 return (PrincipalTypes)processor.cache.principalTypesMap.get(r);
491 static final OrderedSet entryOrderedSet(final QueryProcessor processor, final int r) {
492 return (OrderedSet)processor.cache.orderedSetMap.get(r);
495 static final ValueQuery entryValueQuery(final QueryProcessor processor, final int r) {
496 return (ValueQuery)processor.cache.valueQueryMap.get(r);
499 static final DirectPredicates entryDirectPredicates(final QueryProcessor processor, final int r) {
500 return (DirectPredicates)processor.cache.directPredicatesMap.get(r);
503 public static final ReadEntry entryRead(final QueryProcessor processor, final Read request) {
504 return (ReadEntry)processor.cache.readEntryMap.get(request);
507 public static final MultiReadEntry entryMultiRead(final QueryProcessor processor, final MultiRead request) {
508 return (MultiReadEntry)processor.cache.multiReadEntryMap.get(request);
511 public static final AsyncReadEntry entryAsyncRead(final QueryProcessor processor, final AsyncRead request) {
512 return (AsyncReadEntry)processor.cache.asyncReadEntryMap.get(request);
515 public static final AsyncMultiReadEntry entryAsyncMultiRead(final QueryProcessor processor, final AsyncMultiRead request) {
516 return (AsyncMultiReadEntry)processor.cache.asyncMultiReadEntryMap.get(request);
519 protected static final long keyR2(long r1, long r2) {
520 long result = (r1<<32) | (r2 & 0xffffffffL);
524 protected static final <T> T id(T o) {
528 protected static final int keyR(int r) {
532 protected static final String keyID(String id) {
536 protected static InternalProcedure<IntSet> emptyIntSetProcedure = new InternalProcedure<IntSet>() {
539 public void execute(ReadGraphImpl graph, IntSet result) {
543 public void exception(ReadGraphImpl graph, Throwable throwable) {
548 protected static InternalProcedure<byte[]> emptyBytesProcedure = new InternalProcedure<byte[]>() {
551 public void execute(ReadGraphImpl graph, byte[] bytes) {
555 public void exception(ReadGraphImpl graph, Throwable throwable) {
560 protected static InternalProcedure<Integer> emptyIntegerProcedure = new InternalProcedure<Integer>() {
563 public void execute(ReadGraphImpl graph, Integer i) {
567 public void exception(ReadGraphImpl graph, Throwable throwable) {
573 protected static InternalProcedure<TObjectIntHashMap<String>> emptyNamespaceProcedure = new InternalProcedure<TObjectIntHashMap<String>>() {
576 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> i) {
580 public void exception(ReadGraphImpl graph, Throwable throwable) {
586 protected static InternalProcedure<RelationInfo> emptyRelationInfoProcedure = new InternalProcedure<RelationInfo>() {
589 public void execute(ReadGraphImpl graph, RelationInfo i) {
593 public void exception(ReadGraphImpl graph, Throwable throwable) {
598 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyChildMapProcedure = new InternalProcedure<ObjectResourceIdMap<String>>() {
601 public void execute(ReadGraphImpl graph, ObjectResourceIdMap<String> i) {
605 public void exception(ReadGraphImpl graph, Throwable throwable) {
610 protected static IntProcedure emptyIntProcedure = new IntProcedure() {
613 public void finished(ReadGraphImpl graph) {
617 public void execute(ReadGraphImpl graph, int i) {
621 public void exception(ReadGraphImpl graph, Throwable throwable) {
625 protected static TripleIntProcedure emptyTripleIntProcedure = new TripleIntProcedure() {
628 public void execute(ReadGraphImpl graph, int s, int p, int o) {
632 public void finished(ReadGraphImpl graph) {
636 public void exception(ReadGraphImpl graph, Throwable throwable) {
641 protected static AsyncProcedure<Object> emptyAsyncProcedure = new AsyncProcedure<Object>() {
644 public void execute(AsyncReadGraph graph, Object result) {
648 public void exception(AsyncReadGraph graph, Throwable throwable) {
653 protected static AsyncMultiProcedure<Object> emptyAsyncMultiProcedure = new AsyncMultiProcedure<Object>() {
656 public void execute(AsyncReadGraph graph, Object result) {
660 public void finished(AsyncReadGraph graph) {
664 public void exception(AsyncReadGraph graph, Throwable throwable) {
669 protected static SyncMultiProcedure<Object> emptySyncMultiProcedure = new SyncMultiProcedure<Object>() {
672 public void execute(ReadGraph graph, Object result) {
676 public void finished(ReadGraph graph) {
680 public void exception(ReadGraph graph, Throwable throwable) {
685 protected static InternalProcedure<IntSet> emptyProcedureTypes = emptyIntSetProcedure;
686 protected static InternalProcedure<IntSet> emptyProcedureSuperTypes = emptyIntSetProcedure;
687 protected static InternalProcedure<IntSet> emptyProcedureTypeHierarchy = emptyIntSetProcedure;
688 protected static InternalProcedure<IntSet> emptyProcedureSuperRelations = emptyIntSetProcedure;
689 protected static InternalProcedure<IntSet> emptyProcedurePredicates = emptyIntSetProcedure;
690 protected static InternalProcedure<IntSet> emptyProcedureDirectPredicates = emptyIntSetProcedure;
692 protected static IntProcedure emptyProcedureObjects = emptyIntProcedure;
693 protected static IntProcedure emptyProcedureDirectObjects = emptyIntProcedure;
694 protected static IntProcedure emptyProcedurePrincipalTypes = emptyIntProcedure;
695 protected static IntProcedure emptyProcedureDirectSuperRelations = emptyIntProcedure;
696 protected static IntProcedure emptyProcedureAssertedPredicates = emptyIntProcedure;
697 protected static IntProcedure emptyProcedureOrderedSet = emptyIntProcedure;
699 protected static TripleIntProcedure emptyProcedureStatements = emptyTripleIntProcedure;
700 protected static TripleIntProcedure emptyProcedureAssertedStatements = emptyTripleIntProcedure;
702 protected static InternalProcedure<byte[]> emptyProcedureValueQuery = emptyBytesProcedure;
704 protected static InternalProcedure<Integer> emptyProcedureURIToResource = emptyIntegerProcedure;
705 protected static InternalProcedure<TObjectIntHashMap<String>> emptyProcedureNamespaceIndex = emptyNamespaceProcedure;
706 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyProcedureChildMap = emptyChildMapProcedure;
707 protected static InternalProcedure<RelationInfo> emptyProcedureRelationInfoQuery = emptyRelationInfoProcedure;
709 protected static AsyncProcedure emptyProcedureReadEntry = emptyAsyncProcedure;
710 protected static AsyncProcedure emptyProcedureAsyncReadEntry = emptyAsyncProcedure;
711 protected static SyncMultiProcedure emptyProcedureMultiReadEntry = emptySyncMultiProcedure;
712 protected static AsyncMultiProcedure emptyProcedureAsyncMultiReadEntry = emptyAsyncMultiProcedure;
713 protected static AsyncProcedure emptyProcedureExternalReadEntry = emptyAsyncProcedure;
715 static class AsyncProcedureWrapper<T> implements AsyncProcedure<T> {
717 private AsyncProcedure<T> procedure;
718 private T result = null;
719 private Throwable throwable = null;
720 private Semaphore s = new Semaphore(0);
722 AsyncProcedureWrapper(AsyncProcedure<T> procedure) {
723 this.procedure = procedure;
727 public void execute(AsyncReadGraph graph, T result) {
728 if(procedure != null) procedure.execute(graph, result);
729 this.result = result;
734 public void exception(AsyncReadGraph graph, Throwable throwable) {
735 if(procedure != null) procedure.exception(graph, throwable);
736 this.throwable = throwable;
740 public T get() throws DatabaseException {
743 } catch (InterruptedException e) {
746 if(throwable != null) {
747 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
748 else throw new DatabaseException(throwable);
756 static class ExternalProcedureWrapper<T> implements AsyncProcedure<T> {
758 private Procedure<T> procedure;
759 private T result = null;
760 private Throwable throwable = null;
762 ExternalProcedureWrapper(Procedure<T> procedure) {
763 this.procedure = procedure;
767 public void execute(AsyncReadGraph graph, T result) {
768 if(procedure != null) procedure.execute(result);
769 this.result = result;
773 public void exception(AsyncReadGraph graph, Throwable throwable) {
774 if(procedure != null) procedure.exception(throwable);
775 this.throwable = throwable;
778 public T get() throws DatabaseException {
779 if(throwable != null) {
780 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
781 else throw new DatabaseException(throwable);
790 static class InternalProcedureWrapper<T> implements InternalProcedure<T> {
792 private InternalProcedure<T> procedure;
793 private T result = null;
794 private Throwable throwable = null;
796 InternalProcedureWrapper(InternalProcedure<T> procedure) {
797 this.procedure = procedure;
801 public void execute(ReadGraphImpl graph, T result) throws DatabaseException {
802 if(procedure != null) procedure.execute(graph, result);
803 this.result = result;
807 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
808 if(procedure != null) procedure.exception(graph, throwable);
809 this.throwable = throwable;
812 public T get() throws DatabaseException {
813 if(throwable != null) {
814 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
815 else throw new DatabaseException(throwable);
823 static class IntSetWrapper implements IntProcedure {
825 private IntProcedure procedure;
826 private final IntSet result;
827 private Throwable throwable = null;
829 IntSetWrapper(ReadGraphImpl graph, IntProcedure procedure) {
830 this.procedure = procedure;
831 result = new IntSet(graph.processor.querySupport);
835 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
836 if(procedure != null) procedure.execute(graph, i);
841 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
842 if(procedure != null) procedure.exception(graph, throwable);
843 this.throwable = throwable;
847 public void finished(ReadGraphImpl graph) throws DatabaseException {
848 if(procedure != null) procedure.finished(graph);
851 public IntSet get() throws DatabaseException {
852 if(throwable != null) {
853 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
854 else throw new DatabaseException(throwable);
862 static class TripleIntProcedureWrapper implements TripleIntProcedure {
864 private TripleIntProcedure procedure;
865 private IntArray result = new IntArray();
866 private Throwable throwable = null;
868 TripleIntProcedureWrapper(TripleIntProcedure procedure) {
869 this.procedure = procedure;
873 public void execute(ReadGraphImpl graph, int i1, int i2, int i3) throws DatabaseException {
874 if(procedure != null) procedure.execute(graph, i1, i2, i3);
881 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
882 if(procedure != null) procedure.exception(graph, throwable);
883 this.throwable = throwable;
887 public void finished(ReadGraphImpl graph) throws DatabaseException {
888 if(procedure != null) procedure.finished(graph);
891 public IntArray get() throws DatabaseException {
892 if(throwable != null) {
893 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
894 else throw new DatabaseException(throwable);
902 public static <T> T resultExternalReadEntry(ReadGraphImpl graph, ExternalRead r, CacheEntry parent, ListenerBase listener, Procedure<T> procedure) throws DatabaseException {
903 ExternalProcedureWrapper<T> wrap = new ExternalProcedureWrapper<>(procedure);
904 QueryCache.runnerExternalReadEntry(graph, r, parent, listener, wrap);
908 public static <T> T resultAsyncReadEntry(ReadGraphImpl graph, AsyncRead r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
909 return (T)QueryCache.runnerAsyncReadEntry(graph, r, parent, listener, procedure, true);
912 public static byte[] resultValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
913 InternalProcedureWrapper<byte[]> wrap = new InternalProcedureWrapper<>(null);
914 QueryCache.runnerValueQuery(graph, r, parent, listener, wrap);
918 public static RelationInfo resultRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
919 InternalProcedureWrapper<RelationInfo> wrap = new InternalProcedureWrapper<>(null);
920 QueryCache.runnerRelationInfoQuery(graph, r, parent, listener, wrap);
924 public static IntSet resultSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
925 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
926 QueryCache.runnerSuperRelations(graph, r, parent, listener, wrap);
930 public static IntSet resultSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
931 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
932 QueryCache.runnerSuperTypes(graph, r, parent, listener, wrap);
936 public static IntSet resultTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
937 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
938 QueryCache.runnerTypes(graph, r, parent, listener, wrap);
942 public static IntSet resultPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
943 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
944 QueryCache.runnerPredicates(graph, r, parent, listener, wrap);
948 public static IntSet resultDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
949 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
950 QueryCache.runnerDirectPredicates(graph, r, parent, listener, wrap);
954 public static IntArray resultAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener) throws DatabaseException {
955 TripleIntProcedureWrapper wrap = new TripleIntProcedureWrapper(null);
956 QueryCache.runnerAssertedStatements(graph, r1, r2, parent, listener, wrap);
960 public static Integer resultURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener) throws DatabaseException {
961 InternalProcedureWrapper<Integer> wrap = new InternalProcedureWrapper<Integer>(null);
962 QueryCache.runnerURIToResource(graph, id, parent, listener, wrap);
966 public static ObjectResourceIdMap<String> resultChildMap(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
967 InternalProcedureWrapper<ObjectResourceIdMap<String>> wrap = new InternalProcedureWrapper<ObjectResourceIdMap<String>>(null);
968 QueryCache.runnerChildMap(graph, r, parent, listener, wrap);
972 static boolean shouldCache(QueryProcessor processor, int r) {
973 return processor.isImmutable(r);
976 static boolean shouldCache(QueryProcessor processor, int r, int r2) {
977 return processor.isImmutable(r);
980 static boolean shouldCache(QueryProcessor processor, Object o) {