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.ObjectResourceIdMap;
9 import org.simantics.db.RelationInfo;
10 import org.simantics.db.common.utils.Logger;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.impl.DebugPolicy;
13 import org.simantics.db.impl.graph.ReadGraphImpl;
14 import org.simantics.db.impl.procedure.InternalProcedure;
15 import org.simantics.db.procedure.AsyncMultiProcedure;
16 import org.simantics.db.procedure.AsyncProcedure;
17 import org.simantics.db.procedure.Listener;
18 import org.simantics.db.procedure.ListenerBase;
19 import org.simantics.db.procedure.Procedure;
20 import org.simantics.db.request.AsyncMultiRead;
21 import org.simantics.db.request.AsyncRead;
22 import org.simantics.db.request.ExternalRead;
23 import org.simantics.db.request.MultiRead;
24 import org.simantics.db.request.Read;
26 import gnu.trove.map.hash.THashMap;
27 import gnu.trove.map.hash.TObjectIntHashMap;
29 public class QueryCacheBase {
33 final public int THREAD_MASK;
39 volatile public boolean dirty = false;
40 public boolean collecting = false;
42 final protected THashMap<String, URIToResource> uRIToResourceMap;
43 //final protected THashMap<String, NamespaceIndex> namespaceIndexMap;
44 final protected UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>> childMapMap;
45 final protected DoubleKeyQueryHashMap<IntProcedure> objectsMap;
46 final protected DoubleKeyQueryHashMap<TripleIntProcedure> assertedStatementsMap;
47 final protected DoubleKeyQueryHashMap<IntProcedure> directObjectsMap;
48 final protected DoubleKeyQueryHashMap<TripleIntProcedure> statementsMap;
49 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> typesMap;
50 final protected UnaryQueryHashMap<IntProcedure> principalTypesMap;
51 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> predicatesMap;
52 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> superTypesMap;
53 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> typeHierarchyMap;
54 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> superRelationsMap;
56 final protected UnaryQueryHashMap<IntProcedure> orderedSetMap;
57 final protected UnaryQueryHashMap<IntProcedure> assertedPredicatesMap;
58 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> directPredicatesMap;
59 final protected UnaryQueryHashMap<IntProcedure> directSuperRelationsMap;
61 final protected UnaryQueryHashMap<InternalProcedure<RelationInfo>> relationInfoQueryMap;
62 final protected UnaryQueryHashMap<InternalProcedure<byte[]>> valueQueryMap;
64 final protected StableHashMap<AsyncRead, AsyncReadEntry> asyncReadEntryMap;
65 final protected StableHashMap<Read, ReadEntry> readEntryMap;
66 final protected StableHashMap<MultiRead, MultiReadEntry> multiReadEntryMap;
67 final protected StableHashMap<AsyncMultiRead, AsyncMultiReadEntry> asyncMultiReadEntryMap;
68 final protected StableHashMap<ExternalRead, ExternalReadEntry> externalReadEntryMap;
70 final THashMap<CacheEntry, ArrayList<ListenerEntry>> listeners;
72 final public QuerySupport querySupport;
74 public QueryCacheBase(QuerySupport querySupport, int threads) {
77 THREAD_MASK = threads - 1;
79 this.querySupport = querySupport;
80 directPredicatesMap = new UnaryQueryHashMap();
81 directSuperRelationsMap = new UnaryQueryHashMap();
82 valueQueryMap = new UnaryQueryHashMap();
83 principalTypesMap = new UnaryQueryHashMap();
84 uRIToResourceMap = new THashMap<String, URIToResource>();
85 //namespaceIndexMap = new THashMap<String, NamespaceIndex>();
86 childMapMap = new UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>>();
87 relationInfoQueryMap = new UnaryQueryHashMap();
88 typeHierarchyMap = new UnaryQueryHashMap();
89 superTypesMap = new UnaryQueryHashMap();
90 superRelationsMap = new UnaryQueryHashMap();
91 typesMap = new UnaryQueryHashMap();
92 objectsMap = new DoubleKeyQueryHashMap();
93 orderedSetMap = new UnaryQueryHashMap();
94 predicatesMap = new UnaryQueryHashMap();
95 statementsMap = new DoubleKeyQueryHashMap();
96 directObjectsMap = new DoubleKeyQueryHashMap();
97 assertedPredicatesMap = new UnaryQueryHashMap();
98 assertedStatementsMap = new DoubleKeyQueryHashMap();
99 asyncReadEntryMap = new StableHashMap<AsyncRead, AsyncReadEntry>();
100 readEntryMap = new StableHashMap<Read, ReadEntry>();
101 asyncMultiReadEntryMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>();
102 multiReadEntryMap = new StableHashMap<MultiRead, MultiReadEntry>();
103 externalReadEntryMap = new StableHashMap<ExternalRead, ExternalReadEntry>();
104 listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
107 // public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
109 // AsyncReadEntry<T> entry = (AsyncReadEntry<T>)entry_;
110 // AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
112 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
116 // query.perform(queryGraph, new AsyncProcedure<T>() {
119 // public void execute(AsyncReadGraph returnGraph, T result) {
120 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
121 // entry.addOrSet(parentGraph, result);
123 // procedure.execute(parentGraph, result);
124 // } catch (Throwable t) {
125 // t.printStackTrace();
127 //// parentBarrier.dec(query);
131 // public void exception(AsyncReadGraph returnGraph, Throwable t) {
132 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
133 //// AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
134 // entry.except(parentGraph, t);
136 // procedure.exception(parentGraph, t);
137 // } catch (Throwable t2) {
138 // t2.printStackTrace();
140 //// parentBarrier.dec(query);
144 // public String toString() {
145 // return procedure.toString();
150 // } catch (Throwable t) {
154 // procedure.exception(parentGraph, t);
155 // } catch (Throwable t2) {
156 // t2.printStackTrace();
158 //// parentBarrier.dec(query);
166 // public <T> Object performQuery(ReadGraphImpl parentGraph, final Read<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
168 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
170 // ReadEntry entry = (ReadEntry)entry_;
174 // T result = (T)query.perform(queryGraph);
175 // entry.addOrSet(queryGraph, result);
177 // return (T)entry.get(parentGraph, procedure_);
179 // } catch (Throwable t) {
182 // return (T)entry.get(parentGraph, procedure_);
188 public <T> Object performQuery(ReadGraphImpl parentGraph, final ExternalRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
190 ExternalReadEntry entry = (ExternalReadEntry)entry_;
191 AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
195 query.register(parentGraph, new Listener<T>() {
197 AtomicBoolean used = new AtomicBoolean(false);
200 public void execute(T result) {
203 if(entry.isDiscarded()) return;
205 if(used.compareAndSet(false, true)) {
206 //entry.setPending();
207 entry.addOrSet(parentGraph.processor, result);
208 procedure.execute(parentGraph, result);
211 parentGraph.processor.updatePrimitive(query);
217 public void exception(Throwable t) {
221 if(used.compareAndSet(false, true)) {
222 procedure.exception(parentGraph, t);
224 // entry.queue(result);
225 parentGraph.processor.updatePrimitive(query);
231 public String toString() {
232 return procedure.toString();
236 public boolean isDisposed() {
237 return entry.isDiscarded() || !parentGraph.processor.isBound(entry);
242 return entry.getResult();
244 } catch (Throwable t) {
247 procedure.exception(parentGraph, t);
248 return entry.getResult();
254 public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncMultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
256 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
258 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
259 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
263 query.perform(queryGraph, new AsyncMultiProcedure<T>() {
266 public void execute(AsyncReadGraph graph, T result) {
267 ReadGraphImpl impl = (ReadGraphImpl)graph;
268 entry.addOrSet(result);
270 procedure.execute(parentGraph, result);
271 } catch (Throwable t) {
277 public void finished(AsyncReadGraph graph) {
278 ReadGraphImpl impl = (ReadGraphImpl)graph;
279 entry.finish(parentGraph);
281 procedure.finished(parentGraph);
282 } catch (Throwable t) {
288 public void exception(AsyncReadGraph graph, Throwable t) {
289 ReadGraphImpl impl = (ReadGraphImpl)graph;
290 entry.except(parentGraph, t);
292 procedure.exception(parentGraph, t);
293 } catch (Throwable t2) {
294 t2.printStackTrace();
300 return entry.getResult();
302 } catch (Throwable t) {
306 procedure.exception(parentGraph, t);
307 } catch (Throwable t2) {
308 t2.printStackTrace();
311 return entry.getResult();
317 public <T> Object performQuery(ReadGraphImpl parentGraph, final MultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
319 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
321 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
322 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
326 query.perform(queryGraph, new AsyncMultiProcedure<T>() {
329 public void execute(AsyncReadGraph graph, T result) {
330 ReadGraphImpl impl = (ReadGraphImpl)graph;
331 entry.addOrSet(result);
333 procedure.execute(parentGraph, result);
334 } catch (Throwable t) {
340 public void finished(AsyncReadGraph graph) {
341 ReadGraphImpl impl = (ReadGraphImpl)graph;
342 entry.finish(parentGraph);
344 procedure.finished(parentGraph);
345 } catch (Throwable t) {
351 public void exception(AsyncReadGraph graph, Throwable t) {
352 ReadGraphImpl impl = (ReadGraphImpl)graph;
353 entry.except(parentGraph, t);
355 procedure.exception(parentGraph, t);
356 } catch (Throwable t2) {
357 t2.printStackTrace();
363 return entry.getResult();
365 } catch (Throwable t) {
369 procedure.exception(parentGraph, t);
370 } catch (Throwable t2) {
371 t2.printStackTrace();
374 return entry.getResult();
380 public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) {
382 if (parent != null && !inferred) {
384 if(!child.isImmutable(graph)) {
385 synchronized(child) {
386 child.addParent(parent);
389 } catch (DatabaseException e) {
390 Logger.defaultLogError(e);
392 if(DebugPolicy.DEPENDENCIES) System.out.println(child + " -> " + parent);
395 if (listener != null) {
396 return registerListener(child, listener, procedure);
403 public synchronized ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) {
405 assert (entry != null);
407 if (base.isDisposed())
410 return addListener(entry, base, procedure);
414 protected void primeListenerEntry(final ListenerEntry entry, final Object result) {
415 entry.setLastKnown(result);
418 private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) {
420 assert (entry != null);
421 assert (procedure != null);
423 ArrayList<ListenerEntry> list = listeners.get(entry);
425 list = new ArrayList<ListenerEntry>(1);
426 listeners.put(entry, list);
429 ListenerEntry result = new ListenerEntry(entry, base, procedure);
430 int currentIndex = list.indexOf(result);
431 // There was already a listener
432 if(currentIndex > -1) {
433 ListenerEntry current = list.get(currentIndex);
434 if(!current.base.isDisposed()) return null;
435 list.set(currentIndex, result);
440 if(DebugPolicy.LISTENER) {
441 new Exception().printStackTrace();
442 System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
450 public Collection<CacheEntry> getRootList() {
452 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
454 for (Object e : valueQueryMap.values()) {
455 result.add((CacheEntry) e);
457 for (Object e : directPredicatesMap.values()) {
458 result.add((CacheEntry) e);
460 for (Object e : directSuperRelationsMap.values()) {
461 result.add((CacheEntry) e);
463 for (Object e : objectsMap.values()) {
464 result.add((CacheEntry) e);
466 for (Object e : directObjectsMap.values()) {
467 result.add((CacheEntry) e);
469 for (Object e : principalTypesMap.values()) {
470 result.add((CacheEntry) e);
472 for (Object e : superRelationsMap.values()) {
473 result.add((CacheEntry) e);
475 for (Object e : superTypesMap.values()) {
476 result.add((CacheEntry) e);
478 for (Object e : typesMap.values()) {
479 result.add((CacheEntry) e);
481 for (Object e : objectsMap.values()) {
482 result.add((CacheEntry) e);
484 for (Object e : assertedStatementsMap.values()) {
485 result.add((CacheEntry) e);
487 for (Object e : readEntryMap.values()) {
488 if(e instanceof CacheEntry) {
489 result.add((CacheEntry) e);
491 System.err.println("e=" + e);
494 for (Object e : asyncReadEntryMap.values()) {
495 if(e instanceof CacheEntry) {
496 result.add((CacheEntry) e);
498 System.err.println("e=" + e);
501 for (Object e : externalReadEntryMap.values()) {
502 result.add((CacheEntry) e);
504 for (Object e : orderedSetMap.values()) {
505 result.add((CacheEntry) e);
512 public int calculateCurrentSize() {
516 realSize += directPredicatesMap.size();
517 realSize += directSuperRelationsMap.size();
518 realSize += principalTypesMap.size();
519 realSize += uRIToResourceMap.size();
520 //realSize += namespaceIndexMap.size();
521 realSize += childMapMap.size();
523 realSize += relationInfoQueryMap.size();
524 realSize += superTypesMap.size();
525 realSize += typeHierarchyMap.size();
526 realSize += superRelationsMap.size();
527 realSize += typesMap.size();
529 realSize += valueQueryMap.size();
530 realSize += directObjectsMap.size();
531 realSize += objectsMap.size();
532 realSize += orderedSetMap.size();
533 realSize += predicatesMap.size();
535 realSize += statementsMap.size();
536 realSize += assertedPredicatesMap.size();
537 realSize += assertedStatementsMap.size();
538 realSize += externalReadEntryMap.size();
539 realSize += asyncReadEntryMap.size();
541 realSize += readEntryMap.size();
542 realSize += asyncMultiReadEntryMap.size();
543 realSize += multiReadEntryMap.size();
549 CacheCollectionResult allCaches(CacheCollectionResult result) {
551 int level = Integer.MAX_VALUE;
552 directPredicatesMap.values(level, result);
553 directSuperRelationsMap.values(level, result);
554 principalTypesMap.values(level, result);
555 for(CacheEntryBase e : uRIToResourceMap.values())
556 if(e.getLevel() <= level)
558 // for(CacheEntryBase e : namespaceIndexMap.values())
559 // if(e.getLevel() <= level)
562 childMapMap.values(level, result);
564 relationInfoQueryMap.values(level, result);
565 superTypesMap.values(level, result);
566 typeHierarchyMap.values(level, result);
567 superRelationsMap.values(level, result);
568 typesMap.values(level, result);
570 valueQueryMap.values(level, result);
571 directObjectsMap.values(level, result);
572 objectsMap.values(level, result);
573 orderedSetMap.values(level, result);
574 predicatesMap.values(level, result);
576 statementsMap.values(level, result);
577 assertedPredicatesMap.values(level, result);
578 assertedStatementsMap.values(level, result);
579 externalReadEntryMap.values(level, result);
580 asyncReadEntryMap.values(level, result);
582 readEntryMap.values(level, result);
583 asyncMultiReadEntryMap.values(level, result);
584 multiReadEntryMap.values(level, result);
590 public void scanPending() {
592 ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
594 entries.addAll(directPredicatesMap.values());
595 entries.addAll(directSuperRelationsMap.values());
596 entries.addAll(principalTypesMap.values());
597 entries.addAll(uRIToResourceMap.values());
598 //entries.addAll(namespaceIndexMap.values());
599 entries.addAll(childMapMap.values());
600 entries.addAll(relationInfoQueryMap.values());
601 entries.addAll(superTypesMap.values());
602 entries.addAll(superRelationsMap.values());
603 entries.addAll(typesMap.values());
604 entries.addAll(valueQueryMap.values());
605 entries.addAll(directObjectsMap.values());
606 entries.addAll(objectsMap.values());
607 entries.addAll(orderedSetMap.values());
608 entries.addAll(predicatesMap.values());
609 entries.addAll(orderedSetMap.values());
610 entries.addAll(statementsMap.values());
611 // entries.addAll(assertedObjectsMap.values());
612 entries.addAll(assertedPredicatesMap.values());
613 entries.addAll(assertedStatementsMap.values());
614 entries.addAll(externalReadEntryMap.values());
615 entries.addAll(asyncReadEntryMap.values());
616 entries.addAll(externalReadEntryMap.values());
617 entries.addAll(readEntryMap.values());
618 entries.addAll(asyncMultiReadEntryMap.values());
619 entries.addAll(multiReadEntryMap.values());
620 entries.addAll(readEntryMap.values());
621 System.out.println(entries.size() + " entries.");
622 for(Object e : entries) {
623 if(e instanceof CacheEntry) {
624 CacheEntry en = (CacheEntry)e;
625 if(en.isPending()) System.out.println("pending " + e);
626 if(en.isExcepted()) System.out.println("excepted " + e);
627 if(en.isDiscarded()) System.out.println("discarded " + e);
628 if(en.isRefuted()) System.out.println("refuted " + e);
629 if(en.isFresh()) System.out.println("fresh " + e);
631 //System.out.println("Unknown object " + e);
636 public static void waitPending(CacheEntry entry) throws DatabaseException {
639 while(entry.isPending()) {
644 CacheEntryBase base = ((CacheEntryBase)entry);
645 // if(base.created != null) {
646 // System.err.println("created:");
647 // base.created.printStackTrace();
649 // if(base.performed != null) {
650 // System.err.println("performed:");
651 // base.performed.printStackTrace();
653 // if(base.ready != null) {
654 // System.err.println("ready:");
655 // base.ready.printStackTrace();
657 new Exception("Timeout waiting for request to complete: " + entry.getOriginalRequest().toString()).printStackTrace();
658 throw new DatabaseException("Timeout waiting for request to complete.");
659 //System.err.println("asd");
660 //base.getQuery().recompute(null, null, entry);
662 } catch (InterruptedException e) {
668 //////////////////////////////////////
670 static public Collection<Objects> entriesObjects(QueryProcessor processor, int r1) {
671 synchronized(processor.cache.objectsMap) {
672 return processor.cache.objectsMap.values(r1);
676 static public Collection<Objects> entriesObjects(QueryProcessor processor) {
677 synchronized(processor.cache.objectsMap) {
678 return processor.cache.objectsMap.values();
682 static public Collection<CacheEntry> entriesDirectPredicates(QueryProcessor processor) {
683 synchronized(processor.cache.directPredicatesMap) {
684 return processor.cache.directPredicatesMap.values();
688 final static Collection<DirectObjects> entriesDirectObjects(final QueryProcessor processor, final int r1) {
689 DoubleKeyQueryHashMap<IntProcedure> hash = processor.cache.directObjectsMap;
690 return hash.values(r1);
693 final static Collection<Statements> entriesStatements(final QueryProcessor processor, final int r1) {
694 return processor.cache.statementsMap.values(r1);
697 final static Types entryTypes(final QueryProcessor processor, final int r) {
698 return (Types)processor.cache.typesMap.get(r);
701 final static PrincipalTypes entryPrincipalTypes(final QueryProcessor processor, final int r) {
702 return (PrincipalTypes)processor.cache.principalTypesMap.get(r);
705 final static OrderedSet entryOrderedSet(final QueryProcessor processor, final int r) {
706 return (OrderedSet)processor.cache.orderedSetMap.get(r);
709 final static ValueQuery entryValueQuery(final QueryProcessor processor, final int r) {
710 return (ValueQuery)processor.cache.valueQueryMap.get(r);
713 final static DirectPredicates entryDirectPredicates(final QueryProcessor processor, final int r) {
714 return (DirectPredicates)processor.cache.directPredicatesMap.get(r);
717 public final static ReadEntry entryRead(final QueryProcessor processor, final Read request) {
718 return (ReadEntry)processor.cache.readEntryMap.get(request);
721 public final static MultiReadEntry entryMultiRead(final QueryProcessor processor, final MultiRead request) {
722 return (MultiReadEntry)processor.cache.multiReadEntryMap.get(request);
725 public final static AsyncReadEntry entryAsyncRead(final QueryProcessor processor, final AsyncRead request) {
726 return (AsyncReadEntry)processor.cache.asyncReadEntryMap.get(request);
729 public final static AsyncMultiReadEntry entryAsyncMultiRead(final QueryProcessor processor, final AsyncMultiRead request) {
730 return (AsyncMultiReadEntry)processor.cache.asyncMultiReadEntryMap.get(request);
733 final protected static long keyR2(long r1, long r2) {
734 long result = (r1<<32) | (r2 & 0xffffffffL);
738 final protected static <T> T id(T o) {
742 final protected static int keyR(int r) {
746 final protected static String keyID(String id) {
750 protected static InternalProcedure<IntSet> emptyIntSetProcedure = new InternalProcedure<IntSet>() {
753 public void execute(ReadGraphImpl graph, IntSet result) {
757 public void exception(ReadGraphImpl graph, Throwable throwable) {
762 protected static InternalProcedure<byte[]> emptyBytesProcedure = new InternalProcedure<byte[]>() {
765 public void execute(ReadGraphImpl graph, byte[] bytes) {
769 public void exception(ReadGraphImpl graph, Throwable throwable) {
774 protected static InternalProcedure<Integer> emptyIntegerProcedure = new InternalProcedure<Integer>() {
777 public void execute(ReadGraphImpl graph, Integer i) {
781 public void exception(ReadGraphImpl graph, Throwable throwable) {
787 protected static InternalProcedure<TObjectIntHashMap<String>> emptyNamespaceProcedure = new InternalProcedure<TObjectIntHashMap<String>>() {
790 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> i) {
794 public void exception(ReadGraphImpl graph, Throwable throwable) {
800 protected static InternalProcedure<RelationInfo> emptyRelationInfoProcedure = new InternalProcedure<RelationInfo>() {
803 public void execute(ReadGraphImpl graph, RelationInfo i) {
807 public void exception(ReadGraphImpl graph, Throwable throwable) {
812 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyChildMapProcedure = new InternalProcedure<ObjectResourceIdMap<String>>() {
815 public void execute(ReadGraphImpl graph, ObjectResourceIdMap<String> i) {
819 public void exception(ReadGraphImpl graph, Throwable throwable) {
826 protected static IntProcedure emptyIntProcedure = new IntProcedure() {
829 public void finished(ReadGraphImpl graph) {
833 public void execute(ReadGraphImpl graph, int i) {
837 public void exception(ReadGraphImpl graph, Throwable throwable) {
841 protected static TripleIntProcedure emptyTripleIntProcedure = new TripleIntProcedure() {
844 public void execute(ReadGraphImpl graph, int s, int p, int o) {
848 public void finished(ReadGraphImpl graph) {
852 public void exception(ReadGraphImpl graph, Throwable throwable) {
857 protected static AsyncProcedure<Object> emptyAsyncProcedure = new AsyncProcedure<Object>() {
860 public void execute(AsyncReadGraph graph, Object result) {
864 public void exception(AsyncReadGraph graph, Throwable throwable) {
869 protected static AsyncMultiProcedure<Object> emptyAsyncMultiProcedure = new AsyncMultiProcedure<Object>() {
872 public void execute(AsyncReadGraph graph, Object result) {
876 public void finished(AsyncReadGraph graph) {
880 public void exception(AsyncReadGraph graph, Throwable throwable) {
886 protected static InternalProcedure<IntSet> emptyProcedureTypes = emptyIntSetProcedure;
887 protected static InternalProcedure<IntSet> emptyProcedureSuperTypes = emptyIntSetProcedure;
888 protected static InternalProcedure<IntSet> emptyProcedureTypeHierarchy = emptyIntSetProcedure;
889 protected static InternalProcedure<IntSet> emptyProcedureSuperRelations = emptyIntSetProcedure;
890 protected static InternalProcedure<IntSet> emptyProcedurePredicates = emptyIntSetProcedure;
891 protected static InternalProcedure<IntSet> emptyProcedureDirectPredicates = emptyIntSetProcedure;
893 protected static IntProcedure emptyProcedureObjects = emptyIntProcedure;
894 protected static IntProcedure emptyProcedureDirectObjects = emptyIntProcedure;
895 protected static IntProcedure emptyProcedurePrincipalTypes = emptyIntProcedure;
896 protected static IntProcedure emptyProcedureDirectSuperRelations = emptyIntProcedure;
897 protected static IntProcedure emptyProcedureAssertedPredicates = emptyIntProcedure;
898 protected static IntProcedure emptyProcedureOrderedSet = emptyIntProcedure;
900 protected static TripleIntProcedure emptyProcedureStatements = emptyTripleIntProcedure;
901 protected static TripleIntProcedure emptyProcedureAssertedStatements = emptyTripleIntProcedure;
903 protected static InternalProcedure<byte[]> emptyProcedureValueQuery = emptyBytesProcedure;
905 protected static InternalProcedure<Integer> emptyProcedureURIToResource = emptyIntegerProcedure;
906 protected static InternalProcedure<TObjectIntHashMap<String>> emptyProcedureNamespaceIndex = emptyNamespaceProcedure;
907 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyProcedureChildMap = emptyChildMapProcedure;
908 protected static InternalProcedure<RelationInfo> emptyProcedureRelationInfoQuery = emptyRelationInfoProcedure;
910 protected static AsyncProcedure emptyProcedureReadEntry = emptyAsyncProcedure;
911 protected static AsyncProcedure emptyProcedureAsyncReadEntry = emptyAsyncProcedure;
912 protected static AsyncMultiProcedure emptyProcedureMultiReadEntry = emptyAsyncMultiProcedure;
913 protected static AsyncMultiProcedure emptyProcedureAsyncMultiReadEntry = emptyAsyncMultiProcedure;
914 protected static AsyncProcedure emptyProcedureExternalReadEntry = emptyAsyncProcedure;
916 static class AsyncProcedureWrapper<T> implements AsyncProcedure<T> {
918 private AsyncProcedure<T> procedure;
919 private T result = null;
920 private Throwable throwable = null;
922 AsyncProcedureWrapper(AsyncProcedure<T> procedure) {
923 this.procedure = procedure;
927 public void execute(AsyncReadGraph graph, T result) {
928 if(procedure != null) procedure.execute(graph, result);
929 this.result = result;
933 public void exception(AsyncReadGraph graph, Throwable throwable) {
934 if(procedure != null) procedure.exception(graph, throwable);
935 this.throwable = throwable;
938 public T get() throws DatabaseException {
939 if(throwable != null) {
940 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
941 else throw new DatabaseException(throwable);
949 static class ExternalProcedureWrapper<T> implements AsyncProcedure<T> {
951 private Procedure<T> procedure;
952 private T result = null;
953 private Throwable throwable = null;
955 ExternalProcedureWrapper(Procedure<T> procedure) {
956 this.procedure = procedure;
960 public void execute(AsyncReadGraph graph, T result) {
961 if(procedure != null) procedure.execute(result);
962 this.result = result;
966 public void exception(AsyncReadGraph graph, Throwable throwable) {
967 if(procedure != null) procedure.exception(throwable);
968 this.throwable = throwable;
971 public T get() throws DatabaseException {
972 if(throwable != null) {
973 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
974 else throw new DatabaseException(throwable);
983 static class InternalProcedureWrapper<T> implements InternalProcedure<T> {
985 private InternalProcedure<T> procedure;
986 private T result = null;
987 private Throwable throwable = null;
989 InternalProcedureWrapper(InternalProcedure<T> procedure) {
990 this.procedure = procedure;
994 public void execute(ReadGraphImpl graph, T result) throws DatabaseException {
995 if(procedure != null) procedure.execute(graph, result);
996 this.result = result;
1000 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1001 if(procedure != null) procedure.exception(graph, throwable);
1002 this.throwable = throwable;
1005 public T get() throws DatabaseException {
1006 if(throwable != null) {
1007 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1008 else throw new DatabaseException(throwable);
1016 static class IntSetWrapper implements IntProcedure {
1018 private IntProcedure procedure;
1019 final private IntSet result;
1020 private Throwable throwable = null;
1022 IntSetWrapper(ReadGraphImpl graph, IntProcedure procedure) {
1023 this.procedure = procedure;
1024 result = new IntSet(graph.processor.querySupport);
1028 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
1029 if(procedure != null) procedure.execute(graph, i);
1034 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1035 if(procedure != null) procedure.exception(graph, throwable);
1036 this.throwable = throwable;
1040 public void finished(ReadGraphImpl graph) throws DatabaseException {
1041 if(procedure != null) procedure.finished(graph);
1044 public IntSet get() throws DatabaseException {
1045 if(throwable != null) {
1046 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1047 else throw new DatabaseException(throwable);
1055 static class TripleIntProcedureWrapper implements TripleIntProcedure {
1057 private TripleIntProcedure procedure;
1058 private IntArray result = new IntArray();
1059 private Throwable throwable = null;
1061 TripleIntProcedureWrapper(TripleIntProcedure procedure) {
1062 this.procedure = procedure;
1066 public void execute(ReadGraphImpl graph, int i1, int i2, int i3) throws DatabaseException {
1067 if(procedure != null) procedure.execute(graph, i1, i2, i3);
1074 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1075 if(procedure != null) procedure.exception(graph, throwable);
1076 this.throwable = throwable;
1080 public void finished(ReadGraphImpl graph) throws DatabaseException {
1081 if(procedure != null) procedure.finished(graph);
1084 public IntArray get() throws DatabaseException {
1085 if(throwable != null) {
1086 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1087 else throw new DatabaseException(throwable);
1095 public static <T> T resultExternalReadEntry(ReadGraphImpl graph, ExternalRead r, CacheEntry parent, ListenerBase listener, Procedure<T> procedure) throws DatabaseException {
1096 ExternalProcedureWrapper<T> wrap = new ExternalProcedureWrapper<>(procedure);
1097 QueryCache.runnerExternalReadEntry(graph, r, parent, listener, wrap);
1101 public static <T> T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1102 AsyncProcedureWrapper<T> wrap = new AsyncProcedureWrapper<>(procedure);
1103 QueryCache.runnerReadEntry(graph, r, parent, listener, wrap);
1107 public static byte[] resultValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1108 InternalProcedureWrapper<byte[]> wrap = new InternalProcedureWrapper<>(null);
1109 QueryCache.runnerValueQuery(graph, r, parent, listener, wrap);
1113 public static RelationInfo resultRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1114 InternalProcedureWrapper<RelationInfo> wrap = new InternalProcedureWrapper<>(null);
1115 QueryCache.runnerRelationInfoQuery(graph, r, parent, listener, wrap);
1119 public static IntSet resultSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1120 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1121 QueryCache.runnerSuperRelations(graph, r, parent, listener, wrap);
1125 public static IntSet resultSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1126 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1127 QueryCache.runnerSuperTypes(graph, r, parent, listener, wrap);
1131 public static IntSet resultTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1132 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1133 QueryCache.runnerTypes(graph, r, parent, listener, wrap);
1137 public static IntSet resultPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1138 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1139 QueryCache.runnerPredicates(graph, r, parent, listener, wrap);
1143 public static IntSet resultDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1144 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1145 QueryCache.runnerDirectPredicates(graph, r, parent, listener, wrap);
1149 public static IntArray resultAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1150 TripleIntProcedureWrapper wrap = new TripleIntProcedureWrapper(null);
1151 QueryCache.runnerAssertedStatements(graph, r1, r2, parent, listener, wrap);
1155 public static Integer resultURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1156 InternalProcedureWrapper<Integer> wrap = new InternalProcedureWrapper<Integer>(null);
1157 QueryCache.runnerURIToResource(graph, id, parent, listener, wrap);
1161 public static ObjectResourceIdMap<String> resultChildMap(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1162 InternalProcedureWrapper<ObjectResourceIdMap<String>> wrap = new InternalProcedureWrapper<ObjectResourceIdMap<String>>(null);
1163 QueryCache.runnerChildMap(graph, r, parent, listener, wrap);
1167 static boolean shouldCache(QueryProcessor processor, int r) {
1168 return processor.isImmutable(r);
1171 static boolean shouldCache(QueryProcessor processor, int r, int r2) {
1172 return processor.isImmutable(r);
1175 static boolean shouldCache(QueryProcessor processor, Object o) {