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 public final int THREAD_MASK;
43 public volatile boolean dirty = false;
44 public boolean collecting = false;
46 protected final THashMap<String, URIToResource> uRIToResourceMap;
47 //protected final THashMap<String, NamespaceIndex> namespaceIndexMap;
48 protected final UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>> childMapMap;
49 protected final DoubleKeyQueryHashMap<IntProcedure> objectsMap;
50 protected final DoubleKeyQueryHashMap<TripleIntProcedure> assertedStatementsMap;
51 protected final DoubleKeyQueryHashMap<IntProcedure> directObjectsMap;
52 protected final DoubleKeyQueryHashMap<TripleIntProcedure> statementsMap;
53 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> typesMap;
54 protected final UnaryQueryHashMap<IntProcedure> principalTypesMap;
55 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> predicatesMap;
56 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> superTypesMap;
57 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> typeHierarchyMap;
58 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> superRelationsMap;
60 protected final UnaryQueryHashMap<IntProcedure> orderedSetMap;
61 protected final UnaryQueryHashMap<IntProcedure> assertedPredicatesMap;
62 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> directPredicatesMap;
63 protected final UnaryQueryHashMap<IntProcedure> directSuperRelationsMap;
65 protected final UnaryQueryHashMap<InternalProcedure<RelationInfo>> relationInfoQueryMap;
66 protected final UnaryQueryHashMap<InternalProcedure<byte[]>> valueQueryMap;
68 protected final StableHashMap<AsyncRead, AsyncReadEntry> asyncReadEntryMap;
69 protected final StableHashMap<Read, ReadEntry> readEntryMap;
70 protected final StableHashMap<MultiRead, MultiReadEntry> multiReadEntryMap;
71 protected final StableHashMap<AsyncMultiRead, AsyncMultiReadEntry> asyncMultiReadEntryMap;
72 protected final StableHashMap<ExternalRead, ExternalReadEntry> externalReadEntryMap;
74 final THashMap<CacheEntry, ArrayList<ListenerEntry>> listeners;
76 public final 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(ReadGraphImpl graph, CacheEntry entry) throws DatabaseException {
643 while(entry.isPending()) {
645 boolean performed = graph.performPending();
649 if(counter > 30000) {
650 CacheEntryBase base = ((CacheEntryBase)entry);
651 // if(base.created != null) {
652 // System.err.println("created:");
653 // base.created.printStackTrace();
655 // if(base.performed != null) {
656 // System.err.println("performed:");
657 // base.performed.printStackTrace();
659 // if(base.ready != null) {
660 // System.err.println("ready:");
661 // base.ready.printStackTrace();
663 new Exception("Timeout waiting for request to complete: " + entry.getOriginalRequest()).printStackTrace();
664 throw new DatabaseException("Timeout waiting for request to complete." + entry.getOriginalRequest());
665 //System.err.println("asd");
666 //base.getQuery().recompute(null, null, entry);
669 } catch (InterruptedException e) {
675 //////////////////////////////////////
677 public static Collection<Objects> entriesObjects(QueryProcessor processor, int r1) {
678 synchronized(processor.cache.objectsMap) {
679 return processor.cache.objectsMap.values(r1);
683 public static Collection<Objects> entriesObjects(QueryProcessor processor) {
684 synchronized(processor.cache.objectsMap) {
685 return processor.cache.objectsMap.values();
689 public static Collection<CacheEntry> entriesDirectPredicates(QueryProcessor processor) {
690 synchronized(processor.cache.directPredicatesMap) {
691 return processor.cache.directPredicatesMap.values();
695 static final Collection<DirectObjects> entriesDirectObjects(final QueryProcessor processor, final int r1) {
696 DoubleKeyQueryHashMap<IntProcedure> hash = processor.cache.directObjectsMap;
697 return hash.values(r1);
700 static final Collection<Statements> entriesStatements(final QueryProcessor processor, final int r1) {
701 return processor.cache.statementsMap.values(r1);
704 static final Types entryTypes(final QueryProcessor processor, final int r) {
705 return (Types)processor.cache.typesMap.get(r);
708 static final PrincipalTypes entryPrincipalTypes(final QueryProcessor processor, final int r) {
709 return (PrincipalTypes)processor.cache.principalTypesMap.get(r);
712 static final OrderedSet entryOrderedSet(final QueryProcessor processor, final int r) {
713 return (OrderedSet)processor.cache.orderedSetMap.get(r);
716 static final ValueQuery entryValueQuery(final QueryProcessor processor, final int r) {
717 return (ValueQuery)processor.cache.valueQueryMap.get(r);
720 static final DirectPredicates entryDirectPredicates(final QueryProcessor processor, final int r) {
721 return (DirectPredicates)processor.cache.directPredicatesMap.get(r);
724 public static final ReadEntry entryRead(final QueryProcessor processor, final Read request) {
725 return (ReadEntry)processor.cache.readEntryMap.get(request);
728 public static final MultiReadEntry entryMultiRead(final QueryProcessor processor, final MultiRead request) {
729 return (MultiReadEntry)processor.cache.multiReadEntryMap.get(request);
732 public static final AsyncReadEntry entryAsyncRead(final QueryProcessor processor, final AsyncRead request) {
733 return (AsyncReadEntry)processor.cache.asyncReadEntryMap.get(request);
736 public static final AsyncMultiReadEntry entryAsyncMultiRead(final QueryProcessor processor, final AsyncMultiRead request) {
737 return (AsyncMultiReadEntry)processor.cache.asyncMultiReadEntryMap.get(request);
740 protected static final long keyR2(long r1, long r2) {
741 long result = (r1<<32) | (r2 & 0xffffffffL);
745 protected static final <T> T id(T o) {
749 protected static final int keyR(int r) {
753 protected static final String keyID(String id) {
757 protected static InternalProcedure<IntSet> emptyIntSetProcedure = new InternalProcedure<IntSet>() {
760 public void execute(ReadGraphImpl graph, IntSet result) {
764 public void exception(ReadGraphImpl graph, Throwable throwable) {
769 protected static InternalProcedure<byte[]> emptyBytesProcedure = new InternalProcedure<byte[]>() {
772 public void execute(ReadGraphImpl graph, byte[] bytes) {
776 public void exception(ReadGraphImpl graph, Throwable throwable) {
781 protected static InternalProcedure<Integer> emptyIntegerProcedure = new InternalProcedure<Integer>() {
784 public void execute(ReadGraphImpl graph, Integer i) {
788 public void exception(ReadGraphImpl graph, Throwable throwable) {
794 protected static InternalProcedure<TObjectIntHashMap<String>> emptyNamespaceProcedure = new InternalProcedure<TObjectIntHashMap<String>>() {
797 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> i) {
801 public void exception(ReadGraphImpl graph, Throwable throwable) {
807 protected static InternalProcedure<RelationInfo> emptyRelationInfoProcedure = new InternalProcedure<RelationInfo>() {
810 public void execute(ReadGraphImpl graph, RelationInfo i) {
814 public void exception(ReadGraphImpl graph, Throwable throwable) {
819 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyChildMapProcedure = new InternalProcedure<ObjectResourceIdMap<String>>() {
822 public void execute(ReadGraphImpl graph, ObjectResourceIdMap<String> i) {
826 public void exception(ReadGraphImpl graph, Throwable throwable) {
831 protected static IntProcedure emptyIntProcedure = new IntProcedure() {
834 public void finished(ReadGraphImpl graph) {
838 public void execute(ReadGraphImpl graph, int i) {
842 public void exception(ReadGraphImpl graph, Throwable throwable) {
846 protected static TripleIntProcedure emptyTripleIntProcedure = new TripleIntProcedure() {
849 public void execute(ReadGraphImpl graph, int s, int p, int o) {
853 public void finished(ReadGraphImpl graph) {
857 public void exception(ReadGraphImpl graph, Throwable throwable) {
862 protected static AsyncProcedure<Object> emptyAsyncProcedure = new AsyncProcedure<Object>() {
865 public void execute(AsyncReadGraph graph, Object result) {
869 public void exception(AsyncReadGraph graph, Throwable throwable) {
874 protected static AsyncMultiProcedure<Object> emptyAsyncMultiProcedure = new AsyncMultiProcedure<Object>() {
877 public void execute(AsyncReadGraph graph, Object result) {
881 public void finished(AsyncReadGraph graph) {
885 public void exception(AsyncReadGraph graph, Throwable throwable) {
890 protected static SyncMultiProcedure<Object> emptySyncMultiProcedure = new SyncMultiProcedure<Object>() {
893 public void execute(ReadGraph graph, Object result) {
897 public void finished(ReadGraph graph) {
901 public void exception(ReadGraph graph, Throwable throwable) {
906 protected static InternalProcedure<IntSet> emptyProcedureTypes = emptyIntSetProcedure;
907 protected static InternalProcedure<IntSet> emptyProcedureSuperTypes = emptyIntSetProcedure;
908 protected static InternalProcedure<IntSet> emptyProcedureTypeHierarchy = emptyIntSetProcedure;
909 protected static InternalProcedure<IntSet> emptyProcedureSuperRelations = emptyIntSetProcedure;
910 protected static InternalProcedure<IntSet> emptyProcedurePredicates = emptyIntSetProcedure;
911 protected static InternalProcedure<IntSet> emptyProcedureDirectPredicates = emptyIntSetProcedure;
913 protected static IntProcedure emptyProcedureObjects = emptyIntProcedure;
914 protected static IntProcedure emptyProcedureDirectObjects = emptyIntProcedure;
915 protected static IntProcedure emptyProcedurePrincipalTypes = emptyIntProcedure;
916 protected static IntProcedure emptyProcedureDirectSuperRelations = emptyIntProcedure;
917 protected static IntProcedure emptyProcedureAssertedPredicates = emptyIntProcedure;
918 protected static IntProcedure emptyProcedureOrderedSet = emptyIntProcedure;
920 protected static TripleIntProcedure emptyProcedureStatements = emptyTripleIntProcedure;
921 protected static TripleIntProcedure emptyProcedureAssertedStatements = emptyTripleIntProcedure;
923 protected static InternalProcedure<byte[]> emptyProcedureValueQuery = emptyBytesProcedure;
925 protected static InternalProcedure<Integer> emptyProcedureURIToResource = emptyIntegerProcedure;
926 protected static InternalProcedure<TObjectIntHashMap<String>> emptyProcedureNamespaceIndex = emptyNamespaceProcedure;
927 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyProcedureChildMap = emptyChildMapProcedure;
928 protected static InternalProcedure<RelationInfo> emptyProcedureRelationInfoQuery = emptyRelationInfoProcedure;
930 protected static AsyncProcedure emptyProcedureReadEntry = emptyAsyncProcedure;
931 protected static AsyncProcedure emptyProcedureAsyncReadEntry = emptyAsyncProcedure;
932 protected static SyncMultiProcedure emptyProcedureMultiReadEntry = emptySyncMultiProcedure;
933 protected static AsyncMultiProcedure emptyProcedureAsyncMultiReadEntry = emptyAsyncMultiProcedure;
934 protected static AsyncProcedure emptyProcedureExternalReadEntry = emptyAsyncProcedure;
936 static class AsyncProcedureWrapper<T> implements AsyncProcedure<T> {
938 private AsyncProcedure<T> procedure;
939 private T result = null;
940 private Throwable throwable = null;
941 private Semaphore s = new Semaphore(0);
943 AsyncProcedureWrapper(AsyncProcedure<T> procedure) {
944 this.procedure = procedure;
948 public void execute(AsyncReadGraph graph, T result) {
949 if(procedure != null) procedure.execute(graph, result);
950 this.result = result;
955 public void exception(AsyncReadGraph graph, Throwable throwable) {
956 if(procedure != null) procedure.exception(graph, throwable);
957 this.throwable = throwable;
961 public T get() throws DatabaseException {
964 } catch (InterruptedException e) {
967 if(throwable != null) {
968 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
969 else throw new DatabaseException(throwable);
977 static class ExternalProcedureWrapper<T> implements AsyncProcedure<T> {
979 private Procedure<T> procedure;
980 private T result = null;
981 private Throwable throwable = null;
983 ExternalProcedureWrapper(Procedure<T> procedure) {
984 this.procedure = procedure;
988 public void execute(AsyncReadGraph graph, T result) {
989 if(procedure != null) procedure.execute(result);
990 this.result = result;
994 public void exception(AsyncReadGraph graph, Throwable throwable) {
995 if(procedure != null) procedure.exception(throwable);
996 this.throwable = throwable;
999 public T get() throws DatabaseException {
1000 if(throwable != null) {
1001 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1002 else throw new DatabaseException(throwable);
1011 static class InternalProcedureWrapper<T> implements InternalProcedure<T> {
1013 private InternalProcedure<T> procedure;
1014 private T result = null;
1015 private Throwable throwable = null;
1017 InternalProcedureWrapper(InternalProcedure<T> procedure) {
1018 this.procedure = procedure;
1022 public void execute(ReadGraphImpl graph, T result) throws DatabaseException {
1023 if(procedure != null) procedure.execute(graph, result);
1024 this.result = result;
1028 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1029 if(procedure != null) procedure.exception(graph, throwable);
1030 this.throwable = throwable;
1033 public T get() throws DatabaseException {
1034 if(throwable != null) {
1035 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1036 else throw new DatabaseException(throwable);
1044 static class IntSetWrapper implements IntProcedure {
1046 private IntProcedure procedure;
1047 private final IntSet result;
1048 private Throwable throwable = null;
1050 IntSetWrapper(ReadGraphImpl graph, IntProcedure procedure) {
1051 this.procedure = procedure;
1052 result = new IntSet(graph.processor.querySupport);
1056 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
1057 if(procedure != null) procedure.execute(graph, i);
1062 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1063 if(procedure != null) procedure.exception(graph, throwable);
1064 this.throwable = throwable;
1068 public void finished(ReadGraphImpl graph) throws DatabaseException {
1069 if(procedure != null) procedure.finished(graph);
1072 public IntSet get() throws DatabaseException {
1073 if(throwable != null) {
1074 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1075 else throw new DatabaseException(throwable);
1083 static class TripleIntProcedureWrapper implements TripleIntProcedure {
1085 private TripleIntProcedure procedure;
1086 private IntArray result = new IntArray();
1087 private Throwable throwable = null;
1089 TripleIntProcedureWrapper(TripleIntProcedure procedure) {
1090 this.procedure = procedure;
1094 public void execute(ReadGraphImpl graph, int i1, int i2, int i3) throws DatabaseException {
1095 if(procedure != null) procedure.execute(graph, i1, i2, i3);
1102 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1103 if(procedure != null) procedure.exception(graph, throwable);
1104 this.throwable = throwable;
1108 public void finished(ReadGraphImpl graph) throws DatabaseException {
1109 if(procedure != null) procedure.finished(graph);
1112 public IntArray get() throws DatabaseException {
1113 if(throwable != null) {
1114 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1115 else throw new DatabaseException(throwable);
1123 public static <T> T resultExternalReadEntry(ReadGraphImpl graph, ExternalRead r, CacheEntry parent, ListenerBase listener, Procedure<T> procedure) throws DatabaseException {
1124 ExternalProcedureWrapper<T> wrap = new ExternalProcedureWrapper<>(procedure);
1125 QueryCache.runnerExternalReadEntry(graph, r, parent, listener, wrap);
1129 public static <T> T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1130 return (T)QueryCache.runnerReadEntry(graph, r, parent, listener, procedure, true);
1133 public static <T> T resultAsyncReadEntry(ReadGraphImpl graph, AsyncRead r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1134 return (T)QueryCache.runnerAsyncReadEntry(graph, r, parent, listener, procedure, true);
1137 public static byte[] resultValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1138 InternalProcedureWrapper<byte[]> wrap = new InternalProcedureWrapper<>(null);
1139 QueryCache.runnerValueQuery(graph, r, parent, listener, wrap);
1143 public static RelationInfo resultRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1144 InternalProcedureWrapper<RelationInfo> wrap = new InternalProcedureWrapper<>(null);
1145 QueryCache.runnerRelationInfoQuery(graph, r, parent, listener, wrap);
1149 public static IntSet resultSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1150 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1151 QueryCache.runnerSuperRelations(graph, r, parent, listener, wrap);
1155 public static IntSet resultSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1156 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1157 QueryCache.runnerSuperTypes(graph, r, parent, listener, wrap);
1161 public static IntSet resultTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1162 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1163 QueryCache.runnerTypes(graph, r, parent, listener, wrap);
1167 public static IntSet resultPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1168 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1169 QueryCache.runnerPredicates(graph, r, parent, listener, wrap);
1173 public static IntSet resultDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1174 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1175 QueryCache.runnerDirectPredicates(graph, r, parent, listener, wrap);
1179 public static IntArray resultAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1180 TripleIntProcedureWrapper wrap = new TripleIntProcedureWrapper(null);
1181 QueryCache.runnerAssertedStatements(graph, r1, r2, parent, listener, wrap);
1185 public static Integer resultURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1186 InternalProcedureWrapper<Integer> wrap = new InternalProcedureWrapper<Integer>(null);
1187 QueryCache.runnerURIToResource(graph, id, parent, listener, wrap);
1191 public static ObjectResourceIdMap<String> resultChildMap(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1192 InternalProcedureWrapper<ObjectResourceIdMap<String>> wrap = new InternalProcedureWrapper<ObjectResourceIdMap<String>>(null);
1193 QueryCache.runnerChildMap(graph, r, parent, listener, wrap);
1197 static boolean shouldCache(QueryProcessor processor, int r) {
1198 return processor.isImmutable(r);
1201 static boolean shouldCache(QueryProcessor processor, int r, int r2) {
1202 return processor.isImmutable(r);
1205 static boolean shouldCache(QueryProcessor processor, Object o) {