1 package org.simantics.db.impl.query;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.concurrent.atomic.AtomicBoolean;
7 import org.simantics.db.AsyncReadGraph;
8 import org.simantics.db.RelationInfo;
9 import org.simantics.db.common.utils.Logger;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.impl.DebugPolicy;
12 import org.simantics.db.impl.graph.ReadGraphImpl;
13 import org.simantics.db.impl.procedure.InternalProcedure;
14 import org.simantics.db.procedure.AsyncMultiProcedure;
15 import org.simantics.db.procedure.AsyncProcedure;
16 import org.simantics.db.procedure.Listener;
17 import org.simantics.db.procedure.ListenerBase;
18 import org.simantics.db.request.AsyncMultiRead;
19 import org.simantics.db.request.AsyncRead;
20 import org.simantics.db.request.ExternalRead;
21 import org.simantics.db.request.MultiRead;
22 import org.simantics.db.request.Read;
24 import gnu.trove.map.hash.THashMap;
25 import gnu.trove.map.hash.TObjectIntHashMap;
27 public class QueryCacheBase {
31 final public int THREAD_MASK;
37 volatile public boolean dirty = false;
38 public boolean collecting = false;
40 final protected THashMap<String, URIToResource> uRIToResourceMap;
41 final protected THashMap<String, NamespaceIndex> namespaceIndexMap;
42 final protected DoubleKeyQueryHashMap<IntProcedure> objectsMap;
43 final protected DoubleKeyQueryHashMap<TripleIntProcedure> assertedStatementsMap;
44 final protected DoubleKeyQueryHashMap<IntProcedure> directObjectsMap;
45 final protected DoubleKeyQueryHashMap<TripleIntProcedure> statementsMap;
46 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> typesMap;
47 final protected UnaryQueryHashMap<IntProcedure> principalTypesMap;
48 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> predicatesMap;
49 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> superTypesMap;
50 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> typeHierarchyMap;
51 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> superRelationsMap;
53 final protected UnaryQueryHashMap<IntProcedure> orderedSetMap;
54 final protected UnaryQueryHashMap<IntProcedure> assertedPredicatesMap;
55 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> directPredicatesMap;
56 final protected UnaryQueryHashMap<IntProcedure> directSuperRelationsMap;
58 final protected UnaryQueryHashMap<InternalProcedure<RelationInfo>> relationInfoQueryMap;
59 final protected UnaryQueryHashMap<InternalProcedure<byte[]>> valueQueryMap;
61 final protected StableHashMap<AsyncRead, AsyncReadEntry> asyncReadEntryMap;
62 final protected StableHashMap<Read, ReadEntry> readEntryMap;
63 final protected StableHashMap<MultiRead, MultiReadEntry> multiReadEntryMap;
64 final protected StableHashMap<AsyncMultiRead, AsyncMultiReadEntry> asyncMultiReadEntryMap;
65 final protected StableHashMap<ExternalRead, ExternalReadEntry> externalReadEntryMap;
67 final THashMap<CacheEntry, ArrayList<ListenerEntry>> listeners;
69 final public QuerySupport querySupport;
71 public QueryCacheBase(QuerySupport querySupport, int threads) {
74 THREAD_MASK = threads - 1;
76 this.querySupport = querySupport;
77 directPredicatesMap = new UnaryQueryHashMap();
78 directSuperRelationsMap = new UnaryQueryHashMap();
79 valueQueryMap = new UnaryQueryHashMap();
80 principalTypesMap = new UnaryQueryHashMap();
81 uRIToResourceMap = new THashMap<String, URIToResource>();
82 namespaceIndexMap = new THashMap<String, NamespaceIndex>();
83 relationInfoQueryMap = new UnaryQueryHashMap();
84 typeHierarchyMap = new UnaryQueryHashMap();
85 superTypesMap = new UnaryQueryHashMap();
86 superRelationsMap = new UnaryQueryHashMap();
87 typesMap = new UnaryQueryHashMap();
88 objectsMap = new DoubleKeyQueryHashMap();
89 orderedSetMap = new UnaryQueryHashMap();
90 predicatesMap = new UnaryQueryHashMap();
91 statementsMap = new DoubleKeyQueryHashMap();
92 directObjectsMap = new DoubleKeyQueryHashMap();
93 assertedPredicatesMap = new UnaryQueryHashMap();
94 assertedStatementsMap = new DoubleKeyQueryHashMap();
95 asyncReadEntryMap = new StableHashMap<AsyncRead, AsyncReadEntry>();
96 readEntryMap = new StableHashMap<Read, ReadEntry>();
97 asyncMultiReadEntryMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>();
98 multiReadEntryMap = new StableHashMap<MultiRead, MultiReadEntry>();
99 externalReadEntryMap = new StableHashMap<ExternalRead, ExternalReadEntry>();
100 listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
103 // public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
105 // AsyncReadEntry<T> entry = (AsyncReadEntry<T>)entry_;
106 // AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
108 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
112 // query.perform(queryGraph, new AsyncProcedure<T>() {
115 // public void execute(AsyncReadGraph returnGraph, T result) {
116 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
117 // entry.addOrSet(parentGraph, result);
119 // procedure.execute(parentGraph, result);
120 // } catch (Throwable t) {
121 // t.printStackTrace();
123 //// parentBarrier.dec(query);
127 // public void exception(AsyncReadGraph returnGraph, Throwable t) {
128 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
129 //// AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
130 // entry.except(parentGraph, t);
132 // procedure.exception(parentGraph, t);
133 // } catch (Throwable t2) {
134 // t2.printStackTrace();
136 //// parentBarrier.dec(query);
140 // public String toString() {
141 // return procedure.toString();
146 // } catch (Throwable t) {
150 // procedure.exception(parentGraph, t);
151 // } catch (Throwable t2) {
152 // t2.printStackTrace();
154 //// parentBarrier.dec(query);
162 // public <T> Object performQuery(ReadGraphImpl parentGraph, final Read<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
164 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
166 // ReadEntry entry = (ReadEntry)entry_;
170 // T result = (T)query.perform(queryGraph);
171 // entry.addOrSet(queryGraph, result);
173 // return (T)entry.get(parentGraph, procedure_);
175 // } catch (Throwable t) {
178 // return (T)entry.get(parentGraph, procedure_);
184 public <T> Object performQuery(ReadGraphImpl parentGraph, final ExternalRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
186 ExternalReadEntry entry = (ExternalReadEntry)entry_;
187 AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
191 query.register(parentGraph, new Listener<T>() {
193 AtomicBoolean used = new AtomicBoolean(false);
196 public void execute(T result) {
199 if(entry.isDiscarded()) return;
200 if(entry.isExcepted()) entry.setPending();
202 if(used.compareAndSet(false, true)) {
203 entry.addOrSet(parentGraph.processor, result);
204 procedure.execute(parentGraph, result);
207 parentGraph.processor.updatePrimitive(query);
213 public void exception(Throwable t) {
217 if(used.compareAndSet(false, true)) {
218 procedure.exception(parentGraph, t);
220 // entry.queue(result);
221 parentGraph.processor.updatePrimitive(query);
227 public String toString() {
228 return procedure.toString();
232 public boolean isDisposed() {
233 return entry.isDiscarded() || !parentGraph.processor.isBound(entry);
238 return entry.getResult();
240 } catch (Throwable t) {
243 procedure.exception(parentGraph, t);
244 return entry.getResult();
250 public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncMultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
252 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
254 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
255 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
259 query.perform(queryGraph, new AsyncMultiProcedure<T>() {
262 public void execute(AsyncReadGraph graph, T result) {
263 ReadGraphImpl impl = (ReadGraphImpl)graph;
264 entry.addOrSet(result);
266 procedure.execute(parentGraph, result);
267 } catch (Throwable t) {
273 public void finished(AsyncReadGraph graph) {
274 ReadGraphImpl impl = (ReadGraphImpl)graph;
275 entry.finish(parentGraph);
277 procedure.finished(parentGraph);
278 } catch (Throwable t) {
284 public void exception(AsyncReadGraph graph, Throwable t) {
285 ReadGraphImpl impl = (ReadGraphImpl)graph;
286 entry.except(parentGraph, t);
288 procedure.exception(parentGraph, t);
289 } catch (Throwable t2) {
290 t2.printStackTrace();
296 return entry.getResult();
298 } catch (Throwable t) {
302 procedure.exception(parentGraph, t);
303 } catch (Throwable t2) {
304 t2.printStackTrace();
307 return entry.getResult();
313 public <T> Object performQuery(ReadGraphImpl parentGraph, final MultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
315 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
317 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
318 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
322 query.perform(queryGraph, new AsyncMultiProcedure<T>() {
325 public void execute(AsyncReadGraph graph, T result) {
326 ReadGraphImpl impl = (ReadGraphImpl)graph;
327 entry.addOrSet(result);
329 procedure.execute(parentGraph, result);
330 } catch (Throwable t) {
336 public void finished(AsyncReadGraph graph) {
337 ReadGraphImpl impl = (ReadGraphImpl)graph;
338 entry.finish(parentGraph);
340 procedure.finished(parentGraph);
341 } catch (Throwable t) {
347 public void exception(AsyncReadGraph graph, Throwable t) {
348 ReadGraphImpl impl = (ReadGraphImpl)graph;
349 entry.except(parentGraph, t);
351 procedure.exception(parentGraph, t);
352 } catch (Throwable t2) {
353 t2.printStackTrace();
359 return entry.getResult();
361 } catch (Throwable t) {
365 procedure.exception(parentGraph, t);
366 } catch (Throwable t2) {
367 t2.printStackTrace();
370 return entry.getResult();
376 synchronized public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) {
378 if (parent != null && !inferred) {
380 if(!child.isImmutable(graph))
381 child.addParent(parent);
382 } catch (DatabaseException e) {
383 Logger.defaultLogError(e);
385 if(DebugPolicy.DEPENDENCIES) System.out.println(child + " -> " + parent);
388 if (listener != null) {
389 return registerListener(child, listener, procedure);
396 public ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) {
398 assert (entry != null);
400 if (base.isDisposed())
403 return addListener(entry, base, procedure);
407 protected void primeListenerEntry(final ListenerEntry entry, final Object result) {
408 entry.setLastKnown(result);
411 private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) {
413 assert (entry != null);
414 assert (procedure != null);
416 ArrayList<ListenerEntry> list = listeners.get(entry);
418 list = new ArrayList<ListenerEntry>(1);
419 listeners.put(entry, list);
422 ListenerEntry result = new ListenerEntry(entry, base, procedure);
423 int currentIndex = list.indexOf(result);
424 // There was already a listener
425 if(currentIndex > -1) {
426 ListenerEntry current = list.get(currentIndex);
427 if(!current.base.isDisposed()) return null;
428 list.set(currentIndex, result);
433 if(DebugPolicy.LISTENER) {
434 new Exception().printStackTrace();
435 System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
443 public Collection<CacheEntry> getRootList() {
445 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
447 for (Object e : valueQueryMap.values()) {
448 result.add((CacheEntry) e);
450 for (Object e : directPredicatesMap.values()) {
451 result.add((CacheEntry) e);
453 for (Object e : directSuperRelationsMap.values()) {
454 result.add((CacheEntry) e);
456 for (Object e : objectsMap.values()) {
457 result.add((CacheEntry) e);
459 for (Object e : directObjectsMap.values()) {
460 result.add((CacheEntry) e);
462 for (Object e : principalTypesMap.values()) {
463 result.add((CacheEntry) e);
465 for (Object e : superRelationsMap.values()) {
466 result.add((CacheEntry) e);
468 for (Object e : superTypesMap.values()) {
469 result.add((CacheEntry) e);
471 for (Object e : typesMap.values()) {
472 result.add((CacheEntry) e);
474 for (Object e : objectsMap.values()) {
475 result.add((CacheEntry) e);
477 for (Object e : assertedStatementsMap.values()) {
478 result.add((CacheEntry) e);
480 for (Object e : readEntryMap.values()) {
481 if(e instanceof CacheEntry) {
482 result.add((CacheEntry) e);
484 System.err.println("e=" + e);
487 for (Object e : asyncReadEntryMap.values()) {
488 if(e instanceof CacheEntry) {
489 result.add((CacheEntry) e);
491 System.err.println("e=" + e);
494 for (Object e : externalReadEntryMap.values()) {
495 result.add((CacheEntry) e);
497 for (Object e : orderedSetMap.values()) {
498 result.add((CacheEntry) e);
505 public int calculateCurrentSize() {
509 realSize += directPredicatesMap.size();
510 realSize += directSuperRelationsMap.size();
511 realSize += principalTypesMap.size();
512 realSize += uRIToResourceMap.size();
513 realSize += namespaceIndexMap.size();
515 realSize += relationInfoQueryMap.size();
516 realSize += superTypesMap.size();
517 realSize += typeHierarchyMap.size();
518 realSize += superRelationsMap.size();
519 realSize += typesMap.size();
521 realSize += valueQueryMap.size();
522 realSize += directObjectsMap.size();
523 realSize += objectsMap.size();
524 realSize += orderedSetMap.size();
525 realSize += predicatesMap.size();
527 realSize += statementsMap.size();
528 realSize += assertedPredicatesMap.size();
529 realSize += assertedStatementsMap.size();
530 realSize += externalReadEntryMap.size();
531 realSize += asyncReadEntryMap.size();
533 realSize += readEntryMap.size();
534 realSize += asyncMultiReadEntryMap.size();
535 realSize += multiReadEntryMap.size();
541 CacheCollectionResult allCaches(CacheCollectionResult result) {
543 int level = Integer.MAX_VALUE;
544 directPredicatesMap.values(level, result);
545 directSuperRelationsMap.values(level, result);
546 principalTypesMap.values(level, result);
547 for(CacheEntryBase e : uRIToResourceMap.values())
548 if(e.getLevel() <= level)
550 for(CacheEntryBase e : namespaceIndexMap.values())
551 if(e.getLevel() <= level)
554 relationInfoQueryMap.values(level, result);
555 superTypesMap.values(level, result);
556 typeHierarchyMap.values(level, result);
557 superRelationsMap.values(level, result);
558 typesMap.values(level, result);
560 valueQueryMap.values(level, result);
561 directObjectsMap.values(level, result);
562 objectsMap.values(level, result);
563 orderedSetMap.values(level, result);
564 predicatesMap.values(level, result);
566 statementsMap.values(level, result);
567 assertedPredicatesMap.values(level, result);
568 assertedStatementsMap.values(level, result);
569 externalReadEntryMap.values(level, result);
570 asyncReadEntryMap.values(level, result);
572 readEntryMap.values(level, result);
573 asyncMultiReadEntryMap.values(level, result);
574 multiReadEntryMap.values(level, result);
580 public void scanPending() {
582 ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
584 entries.addAll(directPredicatesMap.values());
585 entries.addAll(directSuperRelationsMap.values());
586 entries.addAll(principalTypesMap.values());
587 entries.addAll(uRIToResourceMap.values());
588 entries.addAll(namespaceIndexMap.values());
589 entries.addAll(relationInfoQueryMap.values());
590 entries.addAll(superTypesMap.values());
591 entries.addAll(superRelationsMap.values());
592 entries.addAll(typesMap.values());
593 entries.addAll(valueQueryMap.values());
594 entries.addAll(directObjectsMap.values());
595 entries.addAll(objectsMap.values());
596 entries.addAll(orderedSetMap.values());
597 entries.addAll(predicatesMap.values());
598 entries.addAll(orderedSetMap.values());
599 entries.addAll(statementsMap.values());
600 // entries.addAll(assertedObjectsMap.values());
601 entries.addAll(assertedPredicatesMap.values());
602 entries.addAll(assertedStatementsMap.values());
603 entries.addAll(externalReadEntryMap.values());
604 entries.addAll(asyncReadEntryMap.values());
605 entries.addAll(externalReadEntryMap.values());
606 entries.addAll(readEntryMap.values());
607 entries.addAll(asyncMultiReadEntryMap.values());
608 entries.addAll(multiReadEntryMap.values());
609 entries.addAll(readEntryMap.values());
610 System.out.println(entries.size() + " entries.");
611 for(Object e : entries) {
612 if(e instanceof CacheEntry) {
613 CacheEntry en = (CacheEntry)e;
614 if(en.isPending()) System.out.println("pending " + e);
615 if(en.isExcepted()) System.out.println("excepted " + e);
616 if(en.isDiscarded()) System.out.println("discarded " + e);
617 if(en.isRefuted()) System.out.println("refuted " + e);
618 if(en.isFresh()) System.out.println("fresh " + e);
620 //System.out.println("Unknown object " + e);
625 public static void waitPending(CacheEntry entry) throws DatabaseException {
628 while(entry.isPending()) {
633 CacheEntryBase base = ((CacheEntryBase)entry);
634 // if(base.created != null) {
635 // System.err.println("created:");
636 // base.created.printStackTrace();
638 // if(base.performed != null) {
639 // System.err.println("performed:");
640 // base.performed.printStackTrace();
642 // if(base.ready != null) {
643 // System.err.println("ready:");
644 // base.ready.printStackTrace();
646 System.err.println("asd");
647 //base.getQuery().recompute(null, null, entry);
649 } catch (InterruptedException e) {
655 //////////////////////////////////////
657 static public Collection<Objects> entriesObjects(QueryProcessor processor, int r1) {
658 synchronized(processor.cache.objectsMap) {
659 return processor.cache.objectsMap.values(r1);
663 static public Collection<Objects> entriesObjects(QueryProcessor processor) {
664 synchronized(processor.cache.objectsMap) {
665 return processor.cache.objectsMap.values();
669 static public Collection<CacheEntry> entriesDirectPredicates(QueryProcessor processor) {
670 synchronized(processor.cache.directPredicatesMap) {
671 return processor.cache.directPredicatesMap.values();
675 final static Collection<DirectObjects> entriesDirectObjects(final QueryProcessor processor, final int r1) {
676 DoubleKeyQueryHashMap<IntProcedure> hash = processor.cache.directObjectsMap;
677 return hash.values(r1);
680 final static Collection<Statements> entriesStatements(final QueryProcessor processor, final int r1) {
681 return processor.cache.statementsMap.values(r1);
684 final static Types entryTypes(final QueryProcessor processor, final int r) {
685 return (Types)processor.cache.typesMap.get(r);
688 final static PrincipalTypes entryPrincipalTypes(final QueryProcessor processor, final int r) {
689 return (PrincipalTypes)processor.cache.principalTypesMap.get(r);
692 final static OrderedSet entryOrderedSet(final QueryProcessor processor, final int r) {
693 return (OrderedSet)processor.cache.orderedSetMap.get(r);
696 final static ValueQuery entryValueQuery(final QueryProcessor processor, final int r) {
697 return (ValueQuery)processor.cache.valueQueryMap.get(r);
700 final static DirectPredicates entryDirectPredicates(final QueryProcessor processor, final int r) {
701 return (DirectPredicates)processor.cache.directPredicatesMap.get(r);
704 public final static ReadEntry entryRead(final QueryProcessor processor, final Read request) {
705 return (ReadEntry)processor.cache.readEntryMap.get(request);
708 public final static MultiReadEntry entryMultiRead(final QueryProcessor processor, final MultiRead request) {
709 return (MultiReadEntry)processor.cache.multiReadEntryMap.get(request);
712 public final static AsyncReadEntry entryAsyncRead(final QueryProcessor processor, final AsyncRead request) {
713 return (AsyncReadEntry)processor.cache.asyncReadEntryMap.get(request);
716 public final static AsyncMultiReadEntry entryAsyncMultiRead(final QueryProcessor processor, final AsyncMultiRead request) {
717 return (AsyncMultiReadEntry)processor.cache.asyncMultiReadEntryMap.get(request);
720 final protected static long keyR2(long r1, long r2) {
721 long result = (r1<<32) | (r2 & 0xffffffffL);
725 final protected static <T> T id(T o) {
729 final protected static int keyR(int r) {
733 final protected static String keyID(String id) {
737 protected static InternalProcedure<IntSet> emptyIntSetProcedure = new InternalProcedure<IntSet>() {
740 public void execute(ReadGraphImpl graph, IntSet result) {
744 public void exception(ReadGraphImpl graph, Throwable throwable) {
749 protected static InternalProcedure<byte[]> emptyBytesProcedure = new InternalProcedure<byte[]>() {
752 public void execute(ReadGraphImpl graph, byte[] bytes) {
756 public void exception(ReadGraphImpl graph, Throwable throwable) {
761 protected static InternalProcedure<Integer> emptyIntegerProcedure = new InternalProcedure<Integer>() {
764 public void execute(ReadGraphImpl graph, Integer i) {
768 public void exception(ReadGraphImpl graph, Throwable throwable) {
774 protected static InternalProcedure<TObjectIntHashMap<String>> emptyNamespaceProcedure = new InternalProcedure<TObjectIntHashMap<String>>() {
777 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> i) {
781 public void exception(ReadGraphImpl graph, Throwable throwable) {
787 protected static InternalProcedure<RelationInfo> emptyRelationInfoProcedure = new InternalProcedure<RelationInfo>() {
790 public void execute(ReadGraphImpl graph, RelationInfo i) {
794 public void exception(ReadGraphImpl graph, Throwable throwable) {
800 protected static IntProcedure emptyIntProcedure = new IntProcedure() {
803 public void finished(ReadGraphImpl graph) {
807 public void execute(ReadGraphImpl graph, int i) {
811 public void exception(ReadGraphImpl graph, Throwable throwable) {
815 protected static TripleIntProcedure emptyTripleIntProcedure = new TripleIntProcedure() {
818 public void execute(ReadGraphImpl graph, int s, int p, int o) {
822 public void finished(ReadGraphImpl graph) {
826 public void exception(ReadGraphImpl graph, Throwable throwable) {
831 protected static AsyncProcedure<Object> emptyAsyncProcedure = new AsyncProcedure<Object>() {
834 public void execute(AsyncReadGraph graph, Object result) {
838 public void exception(AsyncReadGraph graph, Throwable throwable) {
843 protected static AsyncMultiProcedure<Object> emptyAsyncMultiProcedure = new AsyncMultiProcedure<Object>() {
846 public void execute(AsyncReadGraph graph, Object result) {
850 public void finished(AsyncReadGraph graph) {
854 public void exception(AsyncReadGraph graph, Throwable throwable) {
860 protected static InternalProcedure<IntSet> emptyProcedureTypes = emptyIntSetProcedure;
861 protected static InternalProcedure<IntSet> emptyProcedureSuperTypes = emptyIntSetProcedure;
862 protected static InternalProcedure<IntSet> emptyProcedureTypeHierarchy = emptyIntSetProcedure;
863 protected static InternalProcedure<IntSet> emptyProcedureSuperRelations = emptyIntSetProcedure;
864 protected static InternalProcedure<IntSet> emptyProcedurePredicates = emptyIntSetProcedure;
865 protected static InternalProcedure<IntSet> emptyProcedureDirectPredicates = emptyIntSetProcedure;
867 protected static IntProcedure emptyProcedureObjects = emptyIntProcedure;
868 protected static IntProcedure emptyProcedureDirectObjects = emptyIntProcedure;
869 protected static IntProcedure emptyProcedurePrincipalTypes = emptyIntProcedure;
870 protected static IntProcedure emptyProcedureDirectSuperRelations = emptyIntProcedure;
871 protected static IntProcedure emptyProcedureAssertedPredicates = emptyIntProcedure;
872 protected static IntProcedure emptyProcedureOrderedSet = emptyIntProcedure;
874 protected static TripleIntProcedure emptyProcedureStatements = emptyTripleIntProcedure;
875 protected static TripleIntProcedure emptyProcedureAssertedStatements = emptyTripleIntProcedure;
877 protected static InternalProcedure<byte[]> emptyProcedureValueQuery = emptyBytesProcedure;
879 protected static InternalProcedure<Integer> emptyProcedureURIToResource = emptyIntegerProcedure;
880 protected static InternalProcedure<TObjectIntHashMap<String>> emptyProcedureNamespaceIndex = emptyNamespaceProcedure;
881 protected static InternalProcedure<RelationInfo> emptyProcedureRelationInfoQuery = emptyRelationInfoProcedure;
883 protected static AsyncProcedure emptyProcedureReadEntry = emptyAsyncProcedure;
884 protected static AsyncProcedure emptyProcedureAsyncReadEntry = emptyAsyncProcedure;
885 protected static AsyncMultiProcedure emptyProcedureMultiReadEntry = emptyAsyncMultiProcedure;
886 protected static AsyncMultiProcedure emptyProcedureAsyncMultiReadEntry = emptyAsyncMultiProcedure;
887 protected static AsyncProcedure emptyProcedureExternalReadEntry = emptyAsyncProcedure;
889 static class AsyncProcedureWrapper<T> implements AsyncProcedure<T> {
891 private AsyncProcedure<T> procedure;
892 private T result = null;
893 private Throwable throwable = null;
895 AsyncProcedureWrapper(AsyncProcedure<T> procedure) {
896 this.procedure = procedure;
900 public void execute(AsyncReadGraph graph, T result) {
901 if(procedure != null) procedure.execute(graph, result);
902 this.result = result;
906 public void exception(AsyncReadGraph graph, Throwable throwable) {
907 if(procedure != null) procedure.exception(graph, throwable);
908 this.throwable = throwable;
911 public T get() throws DatabaseException {
912 if(throwable != null) {
913 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
914 else throw new DatabaseException(throwable);
923 static class InternalProcedureWrapper<T> implements InternalProcedure<T> {
925 private InternalProcedure<T> procedure;
926 private T result = null;
927 private Throwable throwable = null;
929 InternalProcedureWrapper(InternalProcedure<T> procedure) {
930 this.procedure = procedure;
934 public void execute(ReadGraphImpl graph, T result) throws DatabaseException {
935 if(procedure != null) procedure.execute(graph, result);
936 this.result = result;
940 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
941 if(procedure != null) procedure.exception(graph, throwable);
942 this.throwable = throwable;
945 public T get() throws DatabaseException {
946 if(throwable != null) {
947 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
948 else throw new DatabaseException(throwable);
956 static class IntSetWrapper implements IntProcedure {
958 private IntProcedure procedure;
959 private IntSet result = new IntSet();
960 private Throwable throwable = null;
962 IntSetWrapper(IntProcedure procedure) {
963 this.procedure = procedure;
967 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
968 if(procedure != null) procedure.execute(graph, i);
973 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
974 if(procedure != null) procedure.exception(graph, throwable);
975 this.throwable = throwable;
979 public void finished(ReadGraphImpl graph) throws DatabaseException {
980 if(procedure != null) procedure.finished(graph);
983 public IntSet get() throws DatabaseException {
984 if(throwable != null) {
985 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
986 else throw new DatabaseException(throwable);
994 static class TripleIntProcedureWrapper implements TripleIntProcedure {
996 private TripleIntProcedure procedure;
997 private IntArray result = new IntArray();
998 private Throwable throwable = null;
1000 TripleIntProcedureWrapper(TripleIntProcedure procedure) {
1001 this.procedure = procedure;
1005 public void execute(ReadGraphImpl graph, int i1, int i2, int i3) throws DatabaseException {
1006 if(procedure != null) procedure.execute(graph, i1, i2, i3);
1013 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1014 if(procedure != null) procedure.exception(graph, throwable);
1015 this.throwable = throwable;
1019 public void finished(ReadGraphImpl graph) throws DatabaseException {
1020 if(procedure != null) procedure.finished(graph);
1023 public IntArray get() throws DatabaseException {
1024 if(throwable != null) {
1025 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1026 else throw new DatabaseException(throwable);
1034 public static <T> T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1035 AsyncProcedureWrapper<T> wrap = new AsyncProcedureWrapper<>(procedure);
1036 QueryCache.runnerReadEntry(graph, r, parent, listener, wrap);
1040 public static byte[] resultValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<byte[]> procedure) throws DatabaseException {
1041 InternalProcedureWrapper<byte[]> wrap = new InternalProcedureWrapper<>(procedure);
1042 QueryCache.runnerValueQuery(graph, r, parent, listener, wrap);
1046 public static RelationInfo resultRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<RelationInfo> procedure) throws DatabaseException {
1047 InternalProcedureWrapper<RelationInfo> wrap = new InternalProcedureWrapper<>(procedure);
1048 QueryCache.runnerRelationInfoQuery(graph, r, parent, listener, wrap);
1052 public static IntSet resultSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
1053 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
1054 QueryCache.runnerSuperRelations(graph, r, parent, listener, wrap);
1058 public static IntSet resultSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
1059 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
1060 QueryCache.runnerSuperTypes(graph, r, parent, listener, wrap);
1064 public static IntSet resultTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
1065 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
1066 QueryCache.runnerTypes(graph, r, parent, listener, wrap);
1070 public static IntSet resultPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
1071 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
1072 QueryCache.runnerPredicates(graph, r, parent, listener, wrap);
1076 public static IntSet resultDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, InternalProcedure<IntSet> procedure) throws DatabaseException {
1077 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(procedure);
1078 QueryCache.runnerDirectPredicates(graph, r, parent, listener, wrap);
1082 public static IntArray resultAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, TripleIntProcedure procedure) throws DatabaseException {
1083 TripleIntProcedureWrapper wrap = new TripleIntProcedureWrapper(procedure);
1084 QueryCache.runnerAssertedStatements(graph, r1, r2, parent, listener, wrap);