1 package org.simantics.db.impl.query;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.concurrent.Semaphore;
6 import java.util.concurrent.atomic.AtomicBoolean;
8 import org.simantics.db.AsyncReadGraph;
9 import org.simantics.db.ObjectResourceIdMap;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.RelationInfo;
12 import org.simantics.db.common.utils.Logger;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.impl.DebugPolicy;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.impl.procedure.InternalProcedure;
17 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
18 import org.simantics.db.procedure.AsyncMultiProcedure;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.db.procedure.Listener;
21 import org.simantics.db.procedure.ListenerBase;
22 import org.simantics.db.procedure.Procedure;
23 import org.simantics.db.procedure.SyncMultiProcedure;
24 import org.simantics.db.request.AsyncMultiRead;
25 import org.simantics.db.request.AsyncRead;
26 import org.simantics.db.request.ExternalRead;
27 import org.simantics.db.request.MultiRead;
28 import org.simantics.db.request.Read;
30 import gnu.trove.map.hash.THashMap;
31 import gnu.trove.map.hash.TObjectIntHashMap;
33 public class QueryCacheBase {
37 final public int THREAD_MASK;
43 volatile public boolean dirty = false;
44 public boolean collecting = false;
46 final protected THashMap<String, URIToResource> uRIToResourceMap;
47 //final protected THashMap<String, NamespaceIndex> namespaceIndexMap;
48 final protected UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>> childMapMap;
49 final protected DoubleKeyQueryHashMap<IntProcedure> objectsMap;
50 final protected DoubleKeyQueryHashMap<TripleIntProcedure> assertedStatementsMap;
51 final protected DoubleKeyQueryHashMap<IntProcedure> directObjectsMap;
52 final protected DoubleKeyQueryHashMap<TripleIntProcedure> statementsMap;
53 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> typesMap;
54 final protected UnaryQueryHashMap<IntProcedure> principalTypesMap;
55 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> predicatesMap;
56 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> superTypesMap;
57 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> typeHierarchyMap;
58 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> superRelationsMap;
60 final protected UnaryQueryHashMap<IntProcedure> orderedSetMap;
61 final protected UnaryQueryHashMap<IntProcedure> assertedPredicatesMap;
62 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> directPredicatesMap;
63 final protected UnaryQueryHashMap<IntProcedure> directSuperRelationsMap;
65 final protected UnaryQueryHashMap<InternalProcedure<RelationInfo>> relationInfoQueryMap;
66 final protected UnaryQueryHashMap<InternalProcedure<byte[]>> valueQueryMap;
68 final protected StableHashMap<AsyncRead, AsyncReadEntry> asyncReadEntryMap;
69 final protected StableHashMap<Read, ReadEntry> readEntryMap;
70 final protected StableHashMap<MultiRead, MultiReadEntry> multiReadEntryMap;
71 final protected StableHashMap<AsyncMultiRead, AsyncMultiReadEntry> asyncMultiReadEntryMap;
72 final protected StableHashMap<ExternalRead, ExternalReadEntry> externalReadEntryMap;
74 final THashMap<CacheEntry, ArrayList<ListenerEntry>> listeners;
76 final public QuerySupport querySupport;
78 public QueryCacheBase(QuerySupport querySupport, int threads) {
81 THREAD_MASK = threads - 1;
83 this.querySupport = querySupport;
84 directPredicatesMap = new UnaryQueryHashMap();
85 directSuperRelationsMap = new UnaryQueryHashMap();
86 valueQueryMap = new UnaryQueryHashMap();
87 principalTypesMap = new UnaryQueryHashMap();
88 uRIToResourceMap = new THashMap<String, URIToResource>();
89 //namespaceIndexMap = new THashMap<String, NamespaceIndex>();
90 childMapMap = new UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>>();
91 relationInfoQueryMap = new UnaryQueryHashMap();
92 typeHierarchyMap = new UnaryQueryHashMap();
93 superTypesMap = new UnaryQueryHashMap();
94 superRelationsMap = new UnaryQueryHashMap();
95 typesMap = new UnaryQueryHashMap();
96 objectsMap = new DoubleKeyQueryHashMap();
97 orderedSetMap = new UnaryQueryHashMap();
98 predicatesMap = new UnaryQueryHashMap();
99 statementsMap = new DoubleKeyQueryHashMap();
100 directObjectsMap = new DoubleKeyQueryHashMap();
101 assertedPredicatesMap = new UnaryQueryHashMap();
102 assertedStatementsMap = new DoubleKeyQueryHashMap();
103 asyncReadEntryMap = new StableHashMap<AsyncRead, AsyncReadEntry>();
104 readEntryMap = new StableHashMap<Read, ReadEntry>();
105 asyncMultiReadEntryMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>();
106 multiReadEntryMap = new StableHashMap<MultiRead, MultiReadEntry>();
107 externalReadEntryMap = new StableHashMap<ExternalRead, ExternalReadEntry>();
108 listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
111 // public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
113 // AsyncReadEntry<T> entry = (AsyncReadEntry<T>)entry_;
114 // AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
116 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
120 // query.perform(queryGraph, new AsyncProcedure<T>() {
123 // public void execute(AsyncReadGraph returnGraph, T result) {
124 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
125 // entry.addOrSet(parentGraph, result);
127 // procedure.execute(parentGraph, result);
128 // } catch (Throwable t) {
129 // t.printStackTrace();
131 //// parentBarrier.dec(query);
135 // public void exception(AsyncReadGraph returnGraph, Throwable t) {
136 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
137 //// AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
138 // entry.except(parentGraph, t);
140 // procedure.exception(parentGraph, t);
141 // } catch (Throwable t2) {
142 // t2.printStackTrace();
144 //// parentBarrier.dec(query);
148 // public String toString() {
149 // return procedure.toString();
154 // } catch (Throwable t) {
158 // procedure.exception(parentGraph, t);
159 // } catch (Throwable t2) {
160 // t2.printStackTrace();
162 //// parentBarrier.dec(query);
170 // public <T> Object performQuery(ReadGraphImpl parentGraph, final Read<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
172 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
174 // ReadEntry entry = (ReadEntry)entry_;
178 // T result = (T)query.perform(queryGraph);
179 // entry.addOrSet(queryGraph, result);
181 // return (T)entry.get(parentGraph, procedure_);
183 // } catch (Throwable t) {
186 // return (T)entry.get(parentGraph, procedure_);
192 public <T> Object performQuery(ReadGraphImpl parentGraph, final ExternalRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
194 ExternalReadEntry entry = (ExternalReadEntry)entry_;
195 AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
199 query.register(parentGraph, new Listener<T>() {
201 AtomicBoolean used = new AtomicBoolean(false);
204 public void execute(T result) {
207 if(entry.isDiscarded()) return;
209 if(used.compareAndSet(false, true)) {
210 //entry.setPending();
211 entry.addOrSet(parentGraph.processor, result);
212 procedure.execute(parentGraph, result);
215 parentGraph.processor.updatePrimitive(query);
221 public void exception(Throwable t) {
225 if(used.compareAndSet(false, true)) {
226 procedure.exception(parentGraph, t);
228 // entry.queue(result);
229 parentGraph.processor.updatePrimitive(query);
235 public String toString() {
236 return procedure.toString();
240 public boolean isDisposed() {
241 return entry.isDiscarded() || !parentGraph.processor.isBound(entry);
246 return entry.getResult();
248 } catch (Throwable t) {
251 procedure.exception(parentGraph, t);
252 return entry.getResult();
258 public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncMultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
260 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
262 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
263 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
267 query.perform(queryGraph, new AsyncMultiProcedure<T>() {
270 public void execute(AsyncReadGraph graph, T result) {
271 ReadGraphImpl impl = (ReadGraphImpl)graph;
272 entry.addOrSet(result);
274 procedure.execute(parentGraph, result);
275 } catch (Throwable t) {
281 public void finished(AsyncReadGraph graph) {
282 ReadGraphImpl impl = (ReadGraphImpl)graph;
283 entry.finish(parentGraph);
285 procedure.finished(parentGraph);
286 } catch (Throwable t) {
292 public void exception(AsyncReadGraph graph, Throwable t) {
293 ReadGraphImpl impl = (ReadGraphImpl)graph;
294 entry.except(parentGraph, t);
296 procedure.exception(parentGraph, t);
297 } catch (Throwable t2) {
298 t2.printStackTrace();
304 return entry.getResult();
306 } catch (Throwable t) {
310 procedure.exception(parentGraph, t);
311 } catch (Throwable t2) {
312 t2.printStackTrace();
315 return entry.getResult();
321 public <T> Object performQuery(ReadGraphImpl parentGraph, final MultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
323 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
325 MultiReadEntry entry = (MultiReadEntry)entry_;
326 SyncMultiProcedure<T> procedure = (SyncMultiProcedure<T>)procedure_;
330 query.perform(queryGraph, new SyncMultiProcedure<T>() {
333 public void execute(ReadGraph graph, T result) {
334 ReadGraphImpl impl = (ReadGraphImpl)graph;
335 entry.addOrSet(result);
337 procedure.execute(parentGraph, result);
338 } catch (Throwable t) {
344 public void finished(ReadGraph graph) {
345 ReadGraphImpl impl = (ReadGraphImpl)graph;
346 entry.finish(parentGraph);
348 procedure.finished(parentGraph);
349 } catch (Throwable t) {
355 public void exception(ReadGraph graph, Throwable t) {
356 ReadGraphImpl impl = (ReadGraphImpl)graph;
357 entry.except((DatabaseException)t);
359 procedure.exception(parentGraph, t);
360 } catch (Throwable t2) {
361 t2.printStackTrace();
367 return entry.getResult();
369 } catch (Throwable t) {
373 procedure.exception(parentGraph, t);
374 } catch (Throwable t2) {
375 t2.printStackTrace();
378 return entry.getResult();
384 public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) {
386 if (parent != null && !inferred) {
388 if(!child.isImmutable(graph)) {
389 synchronized(child) {
390 child.addParent(parent);
393 } catch (DatabaseException e) {
394 Logger.defaultLogError(e);
396 if(DebugPolicy.DEPENDENCIES) System.out.println(child + " -> " + parent);
399 if (listener != null) {
400 return registerListener(child, listener, procedure);
407 public synchronized ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) {
409 assert (entry != null);
411 if (base.isDisposed())
414 return addListener(entry, base, procedure);
418 protected void primeListenerEntry(final ListenerEntry entry, final Object result) {
419 entry.setLastKnown(result);
422 private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) {
424 assert (entry != null);
425 assert (procedure != null);
427 ArrayList<ListenerEntry> list = listeners.get(entry);
429 list = new ArrayList<ListenerEntry>(1);
430 listeners.put(entry, list);
433 ListenerEntry result = new ListenerEntry(entry, base, procedure);
434 int currentIndex = list.indexOf(result);
435 // There was already a listener
436 if(currentIndex > -1) {
437 ListenerEntry current = list.get(currentIndex);
438 if(!current.base.isDisposed()) return null;
439 list.set(currentIndex, result);
444 if(DebugPolicy.LISTENER) {
445 new Exception().printStackTrace();
446 System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
454 public Collection<CacheEntry> getRootList() {
456 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
458 for (Object e : valueQueryMap.values()) {
459 result.add((CacheEntry) e);
461 for (Object e : directPredicatesMap.values()) {
462 result.add((CacheEntry) e);
464 for (Object e : directSuperRelationsMap.values()) {
465 result.add((CacheEntry) e);
467 for (Object e : objectsMap.values()) {
468 result.add((CacheEntry) e);
470 for (Object e : directObjectsMap.values()) {
471 result.add((CacheEntry) e);
473 for (Object e : principalTypesMap.values()) {
474 result.add((CacheEntry) e);
476 for (Object e : superRelationsMap.values()) {
477 result.add((CacheEntry) e);
479 for (Object e : superTypesMap.values()) {
480 result.add((CacheEntry) e);
482 for (Object e : typesMap.values()) {
483 result.add((CacheEntry) e);
485 for (Object e : objectsMap.values()) {
486 result.add((CacheEntry) e);
488 for (Object e : assertedStatementsMap.values()) {
489 result.add((CacheEntry) e);
491 for (Object e : readEntryMap.values()) {
492 if(e instanceof CacheEntry) {
493 result.add((CacheEntry) e);
495 System.err.println("e=" + e);
498 for (Object e : asyncReadEntryMap.values()) {
499 if(e instanceof CacheEntry) {
500 result.add((CacheEntry) e);
502 System.err.println("e=" + e);
505 for (Object e : externalReadEntryMap.values()) {
506 result.add((CacheEntry) e);
508 for (Object e : orderedSetMap.values()) {
509 result.add((CacheEntry) e);
516 public int calculateCurrentSize() {
520 realSize += directPredicatesMap.size();
521 realSize += directSuperRelationsMap.size();
522 realSize += principalTypesMap.size();
523 realSize += uRIToResourceMap.size();
524 //realSize += namespaceIndexMap.size();
525 realSize += childMapMap.size();
527 realSize += relationInfoQueryMap.size();
528 realSize += superTypesMap.size();
529 realSize += typeHierarchyMap.size();
530 realSize += superRelationsMap.size();
531 realSize += typesMap.size();
533 realSize += valueQueryMap.size();
534 realSize += directObjectsMap.size();
535 realSize += objectsMap.size();
536 realSize += orderedSetMap.size();
537 realSize += predicatesMap.size();
539 realSize += statementsMap.size();
540 realSize += assertedPredicatesMap.size();
541 realSize += assertedStatementsMap.size();
542 realSize += externalReadEntryMap.size();
543 realSize += asyncReadEntryMap.size();
545 realSize += readEntryMap.size();
546 realSize += asyncMultiReadEntryMap.size();
547 realSize += multiReadEntryMap.size();
553 CacheCollectionResult allCaches(CacheCollectionResult result) {
555 int level = Integer.MAX_VALUE;
556 directPredicatesMap.values(level, result);
557 directSuperRelationsMap.values(level, result);
558 principalTypesMap.values(level, result);
559 for(CacheEntryBase e : uRIToResourceMap.values())
560 if(e.getLevel() <= level)
562 // for(CacheEntryBase e : namespaceIndexMap.values())
563 // if(e.getLevel() <= level)
566 childMapMap.values(level, result);
568 relationInfoQueryMap.values(level, result);
569 superTypesMap.values(level, result);
570 typeHierarchyMap.values(level, result);
571 superRelationsMap.values(level, result);
572 typesMap.values(level, result);
574 valueQueryMap.values(level, result);
575 directObjectsMap.values(level, result);
576 objectsMap.values(level, result);
577 orderedSetMap.values(level, result);
578 predicatesMap.values(level, result);
580 statementsMap.values(level, result);
581 assertedPredicatesMap.values(level, result);
582 assertedStatementsMap.values(level, result);
583 externalReadEntryMap.values(level, result);
584 asyncReadEntryMap.values(level, result);
586 readEntryMap.values(level, result);
587 asyncMultiReadEntryMap.values(level, result);
588 multiReadEntryMap.values(level, result);
594 public void scanPending() {
596 ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
598 entries.addAll(directPredicatesMap.values());
599 entries.addAll(directSuperRelationsMap.values());
600 entries.addAll(principalTypesMap.values());
601 entries.addAll(uRIToResourceMap.values());
602 //entries.addAll(namespaceIndexMap.values());
603 entries.addAll(childMapMap.values());
604 entries.addAll(relationInfoQueryMap.values());
605 entries.addAll(superTypesMap.values());
606 entries.addAll(superRelationsMap.values());
607 entries.addAll(typesMap.values());
608 entries.addAll(valueQueryMap.values());
609 entries.addAll(directObjectsMap.values());
610 entries.addAll(objectsMap.values());
611 entries.addAll(orderedSetMap.values());
612 entries.addAll(predicatesMap.values());
613 entries.addAll(orderedSetMap.values());
614 entries.addAll(statementsMap.values());
615 // entries.addAll(assertedObjectsMap.values());
616 entries.addAll(assertedPredicatesMap.values());
617 entries.addAll(assertedStatementsMap.values());
618 entries.addAll(externalReadEntryMap.values());
619 entries.addAll(asyncReadEntryMap.values());
620 entries.addAll(externalReadEntryMap.values());
621 entries.addAll(readEntryMap.values());
622 entries.addAll(asyncMultiReadEntryMap.values());
623 entries.addAll(multiReadEntryMap.values());
624 entries.addAll(readEntryMap.values());
625 System.out.println(entries.size() + " entries.");
626 for(Object e : entries) {
627 if(e instanceof CacheEntry) {
628 CacheEntry en = (CacheEntry)e;
629 if(en.isPending()) System.out.println("pending " + e);
630 if(en.isExcepted()) System.out.println("excepted " + e);
631 if(en.isDiscarded()) System.out.println("discarded " + e);
632 if(en.isRefuted()) System.out.println("refuted " + e);
633 if(en.isFresh()) System.out.println("fresh " + e);
635 //System.out.println("Unknown object " + e);
640 public static void waitPending(QueryProcessor processor, CacheEntry entry) throws DatabaseException {
643 while(entry.isPending()) {
645 SessionTask task = null;//processor.getOwnTask(processor.thread.get());
647 task.run(processor.thread.get());
652 CacheEntryBase base = ((CacheEntryBase)entry);
653 // if(base.created != null) {
654 // System.err.println("created:");
655 // base.created.printStackTrace();
657 // if(base.performed != null) {
658 // System.err.println("performed:");
659 // base.performed.printStackTrace();
661 // if(base.ready != null) {
662 // System.err.println("ready:");
663 // base.ready.printStackTrace();
665 new Exception("Timeout waiting for request to complete: " + entry.getOriginalRequest().toString()).printStackTrace();
666 throw new DatabaseException("Timeout waiting for request to complete.");
667 //System.err.println("asd");
668 //base.getQuery().recompute(null, null, entry);
671 } catch (InterruptedException e) {
677 //////////////////////////////////////
679 static public Collection<Objects> entriesObjects(QueryProcessor processor, int r1) {
680 synchronized(processor.cache.objectsMap) {
681 return processor.cache.objectsMap.values(r1);
685 static public Collection<Objects> entriesObjects(QueryProcessor processor) {
686 synchronized(processor.cache.objectsMap) {
687 return processor.cache.objectsMap.values();
691 static public Collection<CacheEntry> entriesDirectPredicates(QueryProcessor processor) {
692 synchronized(processor.cache.directPredicatesMap) {
693 return processor.cache.directPredicatesMap.values();
697 final static Collection<DirectObjects> entriesDirectObjects(final QueryProcessor processor, final int r1) {
698 DoubleKeyQueryHashMap<IntProcedure> hash = processor.cache.directObjectsMap;
699 return hash.values(r1);
702 final static Collection<Statements> entriesStatements(final QueryProcessor processor, final int r1) {
703 return processor.cache.statementsMap.values(r1);
706 final static Types entryTypes(final QueryProcessor processor, final int r) {
707 return (Types)processor.cache.typesMap.get(r);
710 final static PrincipalTypes entryPrincipalTypes(final QueryProcessor processor, final int r) {
711 return (PrincipalTypes)processor.cache.principalTypesMap.get(r);
714 final static OrderedSet entryOrderedSet(final QueryProcessor processor, final int r) {
715 return (OrderedSet)processor.cache.orderedSetMap.get(r);
718 final static ValueQuery entryValueQuery(final QueryProcessor processor, final int r) {
719 return (ValueQuery)processor.cache.valueQueryMap.get(r);
722 final static DirectPredicates entryDirectPredicates(final QueryProcessor processor, final int r) {
723 return (DirectPredicates)processor.cache.directPredicatesMap.get(r);
726 public final static ReadEntry entryRead(final QueryProcessor processor, final Read request) {
727 return (ReadEntry)processor.cache.readEntryMap.get(request);
730 public final static MultiReadEntry entryMultiRead(final QueryProcessor processor, final MultiRead request) {
731 return (MultiReadEntry)processor.cache.multiReadEntryMap.get(request);
734 public final static AsyncReadEntry entryAsyncRead(final QueryProcessor processor, final AsyncRead request) {
735 return (AsyncReadEntry)processor.cache.asyncReadEntryMap.get(request);
738 public final static AsyncMultiReadEntry entryAsyncMultiRead(final QueryProcessor processor, final AsyncMultiRead request) {
739 return (AsyncMultiReadEntry)processor.cache.asyncMultiReadEntryMap.get(request);
742 final protected static long keyR2(long r1, long r2) {
743 long result = (r1<<32) | (r2 & 0xffffffffL);
747 final protected static <T> T id(T o) {
751 final protected static int keyR(int r) {
755 final protected static String keyID(String id) {
759 protected static InternalProcedure<IntSet> emptyIntSetProcedure = new InternalProcedure<IntSet>() {
762 public void execute(ReadGraphImpl graph, IntSet result) {
766 public void exception(ReadGraphImpl graph, Throwable throwable) {
771 protected static InternalProcedure<byte[]> emptyBytesProcedure = new InternalProcedure<byte[]>() {
774 public void execute(ReadGraphImpl graph, byte[] bytes) {
778 public void exception(ReadGraphImpl graph, Throwable throwable) {
783 protected static InternalProcedure<Integer> emptyIntegerProcedure = new InternalProcedure<Integer>() {
786 public void execute(ReadGraphImpl graph, Integer i) {
790 public void exception(ReadGraphImpl graph, Throwable throwable) {
796 protected static InternalProcedure<TObjectIntHashMap<String>> emptyNamespaceProcedure = new InternalProcedure<TObjectIntHashMap<String>>() {
799 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> i) {
803 public void exception(ReadGraphImpl graph, Throwable throwable) {
809 protected static InternalProcedure<RelationInfo> emptyRelationInfoProcedure = new InternalProcedure<RelationInfo>() {
812 public void execute(ReadGraphImpl graph, RelationInfo i) {
816 public void exception(ReadGraphImpl graph, Throwable throwable) {
821 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyChildMapProcedure = new InternalProcedure<ObjectResourceIdMap<String>>() {
824 public void execute(ReadGraphImpl graph, ObjectResourceIdMap<String> i) {
828 public void exception(ReadGraphImpl graph, Throwable throwable) {
835 protected static IntProcedure emptyIntProcedure = new IntProcedure() {
838 public void finished(ReadGraphImpl graph) {
842 public void execute(ReadGraphImpl graph, int i) {
846 public void exception(ReadGraphImpl graph, Throwable throwable) {
850 protected static TripleIntProcedure emptyTripleIntProcedure = new TripleIntProcedure() {
853 public void execute(ReadGraphImpl graph, int s, int p, int o) {
857 public void finished(ReadGraphImpl graph) {
861 public void exception(ReadGraphImpl graph, Throwable throwable) {
866 protected static AsyncProcedure<Object> emptyAsyncProcedure = new AsyncProcedure<Object>() {
869 public void execute(AsyncReadGraph graph, Object result) {
873 public void exception(AsyncReadGraph graph, Throwable throwable) {
878 protected static AsyncMultiProcedure<Object> emptyAsyncMultiProcedure = new AsyncMultiProcedure<Object>() {
881 public void execute(AsyncReadGraph graph, Object result) {
885 public void finished(AsyncReadGraph graph) {
889 public void exception(AsyncReadGraph graph, Throwable throwable) {
895 protected static SyncMultiProcedure<Object> emptySyncMultiProcedure = new SyncMultiProcedure<Object>() {
898 public void execute(ReadGraph graph, Object result) {
902 public void finished(ReadGraph graph) {
906 public void exception(ReadGraph graph, Throwable throwable) {
912 protected static InternalProcedure<IntSet> emptyProcedureTypes = emptyIntSetProcedure;
913 protected static InternalProcedure<IntSet> emptyProcedureSuperTypes = emptyIntSetProcedure;
914 protected static InternalProcedure<IntSet> emptyProcedureTypeHierarchy = emptyIntSetProcedure;
915 protected static InternalProcedure<IntSet> emptyProcedureSuperRelations = emptyIntSetProcedure;
916 protected static InternalProcedure<IntSet> emptyProcedurePredicates = emptyIntSetProcedure;
917 protected static InternalProcedure<IntSet> emptyProcedureDirectPredicates = emptyIntSetProcedure;
919 protected static IntProcedure emptyProcedureObjects = emptyIntProcedure;
920 protected static IntProcedure emptyProcedureDirectObjects = emptyIntProcedure;
921 protected static IntProcedure emptyProcedurePrincipalTypes = emptyIntProcedure;
922 protected static IntProcedure emptyProcedureDirectSuperRelations = emptyIntProcedure;
923 protected static IntProcedure emptyProcedureAssertedPredicates = emptyIntProcedure;
924 protected static IntProcedure emptyProcedureOrderedSet = emptyIntProcedure;
926 protected static TripleIntProcedure emptyProcedureStatements = emptyTripleIntProcedure;
927 protected static TripleIntProcedure emptyProcedureAssertedStatements = emptyTripleIntProcedure;
929 protected static InternalProcedure<byte[]> emptyProcedureValueQuery = emptyBytesProcedure;
931 protected static InternalProcedure<Integer> emptyProcedureURIToResource = emptyIntegerProcedure;
932 protected static InternalProcedure<TObjectIntHashMap<String>> emptyProcedureNamespaceIndex = emptyNamespaceProcedure;
933 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyProcedureChildMap = emptyChildMapProcedure;
934 protected static InternalProcedure<RelationInfo> emptyProcedureRelationInfoQuery = emptyRelationInfoProcedure;
936 protected static AsyncProcedure emptyProcedureReadEntry = emptyAsyncProcedure;
937 protected static AsyncProcedure emptyProcedureAsyncReadEntry = emptyAsyncProcedure;
938 protected static SyncMultiProcedure emptyProcedureMultiReadEntry = emptySyncMultiProcedure;
939 protected static AsyncMultiProcedure emptyProcedureAsyncMultiReadEntry = emptyAsyncMultiProcedure;
940 protected static AsyncProcedure emptyProcedureExternalReadEntry = emptyAsyncProcedure;
942 static class AsyncProcedureWrapper<T> implements AsyncProcedure<T> {
944 private AsyncProcedure<T> procedure;
945 private T result = null;
946 private Throwable throwable = null;
947 private Semaphore s = new Semaphore(0);
949 AsyncProcedureWrapper(AsyncProcedure<T> procedure) {
950 this.procedure = procedure;
954 public void execute(AsyncReadGraph graph, T result) {
955 if(procedure != null) procedure.execute(graph, result);
956 this.result = result;
961 public void exception(AsyncReadGraph graph, Throwable throwable) {
962 if(procedure != null) procedure.exception(graph, throwable);
963 this.throwable = throwable;
967 public T get() throws DatabaseException {
970 } catch (InterruptedException e) {
973 if(throwable != null) {
974 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
975 else throw new DatabaseException(throwable);
983 static class ExternalProcedureWrapper<T> implements AsyncProcedure<T> {
985 private Procedure<T> procedure;
986 private T result = null;
987 private Throwable throwable = null;
989 ExternalProcedureWrapper(Procedure<T> procedure) {
990 this.procedure = procedure;
994 public void execute(AsyncReadGraph graph, T result) {
995 if(procedure != null) procedure.execute(result);
996 this.result = result;
1000 public void exception(AsyncReadGraph graph, Throwable throwable) {
1001 if(procedure != null) procedure.exception(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);
1017 static class InternalProcedureWrapper<T> implements InternalProcedure<T> {
1019 private InternalProcedure<T> procedure;
1020 private T result = null;
1021 private Throwable throwable = null;
1023 InternalProcedureWrapper(InternalProcedure<T> procedure) {
1024 this.procedure = procedure;
1028 public void execute(ReadGraphImpl graph, T result) throws DatabaseException {
1029 if(procedure != null) procedure.execute(graph, result);
1030 this.result = result;
1034 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1035 if(procedure != null) procedure.exception(graph, throwable);
1036 this.throwable = throwable;
1039 public T get() throws DatabaseException {
1040 if(throwable != null) {
1041 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1042 else throw new DatabaseException(throwable);
1050 static class IntSetWrapper implements IntProcedure {
1052 private IntProcedure procedure;
1053 final private IntSet result;
1054 private Throwable throwable = null;
1056 IntSetWrapper(ReadGraphImpl graph, IntProcedure procedure) {
1057 this.procedure = procedure;
1058 result = new IntSet(graph.processor.querySupport);
1062 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
1063 if(procedure != null) procedure.execute(graph, i);
1068 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1069 if(procedure != null) procedure.exception(graph, throwable);
1070 this.throwable = throwable;
1074 public void finished(ReadGraphImpl graph) throws DatabaseException {
1075 if(procedure != null) procedure.finished(graph);
1078 public IntSet get() throws DatabaseException {
1079 if(throwable != null) {
1080 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1081 else throw new DatabaseException(throwable);
1089 static class TripleIntProcedureWrapper implements TripleIntProcedure {
1091 private TripleIntProcedure procedure;
1092 private IntArray result = new IntArray();
1093 private Throwable throwable = null;
1095 TripleIntProcedureWrapper(TripleIntProcedure procedure) {
1096 this.procedure = procedure;
1100 public void execute(ReadGraphImpl graph, int i1, int i2, int i3) throws DatabaseException {
1101 if(procedure != null) procedure.execute(graph, i1, i2, i3);
1108 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1109 if(procedure != null) procedure.exception(graph, throwable);
1110 this.throwable = throwable;
1114 public void finished(ReadGraphImpl graph) throws DatabaseException {
1115 if(procedure != null) procedure.finished(graph);
1118 public IntArray get() throws DatabaseException {
1119 if(throwable != null) {
1120 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1121 else throw new DatabaseException(throwable);
1129 public static <T> T resultExternalReadEntry(ReadGraphImpl graph, ExternalRead r, CacheEntry parent, ListenerBase listener, Procedure<T> procedure) throws DatabaseException {
1130 ExternalProcedureWrapper<T> wrap = new ExternalProcedureWrapper<>(procedure);
1131 QueryCache.runnerExternalReadEntry(graph, r, parent, listener, wrap);
1135 public static <T> T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1136 AsyncProcedureWrapper<T> wrap = new AsyncProcedureWrapper<>(procedure);
1137 QueryCache.runnerReadEntry(graph, r, parent, listener, wrap, true);
1141 public static <T> T resultAsyncReadEntry(ReadGraphImpl graph, AsyncRead r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1142 AsyncProcedureWrapper<T> wrap = new AsyncProcedureWrapper<>(procedure);
1143 QueryCache.runnerAsyncReadEntry(graph, r, parent, listener, wrap, true);
1147 public static byte[] resultValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1148 InternalProcedureWrapper<byte[]> wrap = new InternalProcedureWrapper<>(null);
1149 QueryCache.runnerValueQuery(graph, r, parent, listener, wrap);
1153 public static RelationInfo resultRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1154 InternalProcedureWrapper<RelationInfo> wrap = new InternalProcedureWrapper<>(null);
1155 QueryCache.runnerRelationInfoQuery(graph, r, parent, listener, wrap);
1159 public static IntSet resultSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1160 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1161 QueryCache.runnerSuperRelations(graph, r, parent, listener, wrap);
1165 public static IntSet resultSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1166 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1167 QueryCache.runnerSuperTypes(graph, r, parent, listener, wrap);
1171 public static IntSet resultTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1172 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1173 QueryCache.runnerTypes(graph, r, parent, listener, wrap);
1177 public static IntSet resultPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1178 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1179 QueryCache.runnerPredicates(graph, r, parent, listener, wrap);
1183 public static IntSet resultDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1184 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1185 QueryCache.runnerDirectPredicates(graph, r, parent, listener, wrap);
1189 public static IntArray resultAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1190 TripleIntProcedureWrapper wrap = new TripleIntProcedureWrapper(null);
1191 QueryCache.runnerAssertedStatements(graph, r1, r2, parent, listener, wrap);
1195 public static Integer resultURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1196 InternalProcedureWrapper<Integer> wrap = new InternalProcedureWrapper<Integer>(null);
1197 QueryCache.runnerURIToResource(graph, id, parent, listener, wrap);
1201 public static ObjectResourceIdMap<String> resultChildMap(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1202 InternalProcedureWrapper<ObjectResourceIdMap<String>> wrap = new InternalProcedureWrapper<ObjectResourceIdMap<String>>(null);
1203 QueryCache.runnerChildMap(graph, r, parent, listener, wrap);
1207 static boolean shouldCache(QueryProcessor processor, int r) {
1208 return processor.isImmutable(r);
1211 static boolean shouldCache(QueryProcessor processor, int r, int r2) {
1212 return processor.isImmutable(r);
1215 static boolean shouldCache(QueryProcessor processor, Object o) {