1 package org.simantics.db.impl.query;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.concurrent.Semaphore;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.db.AsyncReadGraph;
9 import org.simantics.db.DevelopmentKeys;
10 import org.simantics.db.ObjectResourceIdMap;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.RelationInfo;
13 import org.simantics.db.common.utils.Logger;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.impl.procedure.InternalProcedure;
17 import org.simantics.db.procedure.AsyncMultiProcedure;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.db.procedure.ListenerBase;
20 import org.simantics.db.procedure.Procedure;
21 import org.simantics.db.procedure.SyncMultiProcedure;
22 import org.simantics.db.request.AsyncMultiRead;
23 import org.simantics.db.request.AsyncRead;
24 import org.simantics.db.request.ExternalRead;
25 import org.simantics.db.request.MultiRead;
26 import org.simantics.db.request.Read;
27 import org.simantics.utils.Development;
29 import gnu.trove.map.hash.THashMap;
30 import gnu.trove.map.hash.TObjectIntHashMap;
32 public class QueryCacheBase {
36 public final int THREAD_MASK;
42 public volatile boolean dirty = false;
43 public boolean collecting = false;
45 protected final THashMap<String, URIToResource> uRIToResourceMap;
46 //protected final THashMap<String, NamespaceIndex> namespaceIndexMap;
47 protected final UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>> childMapMap;
48 protected final DoubleKeyQueryHashMap<IntProcedure> objectsMap;
49 protected final DoubleKeyQueryHashMap<TripleIntProcedure> assertedStatementsMap;
50 protected final DoubleKeyQueryHashMap<IntProcedure> directObjectsMap;
51 protected final DoubleKeyQueryHashMap<TripleIntProcedure> statementsMap;
52 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> typesMap;
53 protected final UnaryQueryHashMap<IntProcedure> principalTypesMap;
54 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> predicatesMap;
55 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> superTypesMap;
56 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> typeHierarchyMap;
57 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> superRelationsMap;
59 protected final UnaryQueryHashMap<IntProcedure> orderedSetMap;
60 protected final UnaryQueryHashMap<IntProcedure> assertedPredicatesMap;
61 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> directPredicatesMap;
62 protected final UnaryQueryHashMap<IntProcedure> directSuperRelationsMap;
64 protected final UnaryQueryHashMap<InternalProcedure<RelationInfo>> relationInfoQueryMap;
65 protected final UnaryQueryHashMap<InternalProcedure<byte[]>> valueQueryMap;
67 protected final StableHashMap<AsyncRead, AsyncReadEntry> asyncReadEntryMap;
68 protected final StableHashMap<Read, ReadEntry> readEntryMap;
69 protected final StableHashMap<MultiRead, MultiReadEntry> multiReadEntryMap;
70 protected final StableHashMap<AsyncMultiRead, AsyncMultiReadEntry> asyncMultiReadEntryMap;
71 protected final StableHashMap<ExternalRead, ExternalReadEntry> externalReadEntryMap;
73 final THashMap<CacheEntry, ArrayList<ListenerEntry>> listeners;
75 public final QuerySupport querySupport;
77 public QueryCacheBase(QuerySupport querySupport, int threads) {
80 THREAD_MASK = threads - 1;
82 this.querySupport = querySupport;
83 directPredicatesMap = new UnaryQueryHashMap();
84 directSuperRelationsMap = new UnaryQueryHashMap();
85 valueQueryMap = new UnaryQueryHashMap();
86 principalTypesMap = new UnaryQueryHashMap();
87 uRIToResourceMap = new THashMap<String, URIToResource>();
88 //namespaceIndexMap = new THashMap<String, NamespaceIndex>();
89 childMapMap = new UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>>();
90 relationInfoQueryMap = new UnaryQueryHashMap();
91 typeHierarchyMap = new UnaryQueryHashMap();
92 superTypesMap = new UnaryQueryHashMap();
93 superRelationsMap = new UnaryQueryHashMap();
94 typesMap = new UnaryQueryHashMap();
95 objectsMap = new DoubleKeyQueryHashMap();
96 orderedSetMap = new UnaryQueryHashMap();
97 predicatesMap = new UnaryQueryHashMap();
98 statementsMap = new DoubleKeyQueryHashMap();
99 directObjectsMap = new DoubleKeyQueryHashMap();
100 assertedPredicatesMap = new UnaryQueryHashMap();
101 assertedStatementsMap = new DoubleKeyQueryHashMap();
102 asyncReadEntryMap = new StableHashMap<AsyncRead, AsyncReadEntry>();
103 readEntryMap = new StableHashMap<Read, ReadEntry>();
104 asyncMultiReadEntryMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>();
105 multiReadEntryMap = new StableHashMap<MultiRead, MultiReadEntry>();
106 externalReadEntryMap = new StableHashMap<ExternalRead, ExternalReadEntry>();
107 listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
110 // public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
112 // AsyncReadEntry<T> entry = (AsyncReadEntry<T>)entry_;
113 // AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
115 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
119 // query.perform(queryGraph, new AsyncProcedure<T>() {
122 // public void execute(AsyncReadGraph returnGraph, T result) {
123 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
124 // entry.addOrSet(parentGraph, result);
126 // procedure.execute(parentGraph, result);
127 // } catch (Throwable t) {
128 // t.printStackTrace();
130 //// parentBarrier.dec(query);
134 // public void exception(AsyncReadGraph returnGraph, Throwable t) {
135 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
136 //// AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
137 // entry.except(parentGraph, t);
139 // procedure.exception(parentGraph, t);
140 // } catch (Throwable t2) {
141 // t2.printStackTrace();
143 //// parentBarrier.dec(query);
147 // public String toString() {
148 // return procedure.toString();
153 // } catch (Throwable t) {
157 // procedure.exception(parentGraph, t);
158 // } catch (Throwable t2) {
159 // t2.printStackTrace();
161 //// parentBarrier.dec(query);
169 // public <T> Object performQuery(ReadGraphImpl parentGraph, final Read<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
171 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
173 // ReadEntry entry = (ReadEntry)entry_;
177 // T result = (T)query.perform(queryGraph);
178 // entry.addOrSet(queryGraph, result);
180 // return (T)entry.get(parentGraph, procedure_);
182 // } catch (Throwable t) {
185 // return (T)entry.get(parentGraph, procedure_);
192 public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncMultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
194 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
196 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
197 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
201 query.perform(queryGraph, new AsyncMultiProcedure<T>() {
204 public void execute(AsyncReadGraph graph, T result) {
205 ReadGraphImpl impl = (ReadGraphImpl)graph;
206 entry.addOrSet(result);
208 procedure.execute(parentGraph, result);
209 } catch (Throwable t) {
215 public void finished(AsyncReadGraph graph) {
216 ReadGraphImpl impl = (ReadGraphImpl)graph;
217 entry.finish(parentGraph);
219 procedure.finished(parentGraph);
220 } catch (Throwable t) {
226 public void exception(AsyncReadGraph graph, Throwable t) {
227 ReadGraphImpl impl = (ReadGraphImpl)graph;
228 entry.except(parentGraph, t);
230 procedure.exception(parentGraph, t);
231 } catch (Throwable t2) {
232 t2.printStackTrace();
238 return entry.getResult();
240 } catch (Throwable t) {
244 procedure.exception(parentGraph, t);
245 } catch (Throwable t2) {
246 t2.printStackTrace();
249 return entry.getResult();
255 public <T> Object performQuery(ReadGraphImpl parentGraph, final MultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
257 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
259 MultiReadEntry entry = (MultiReadEntry)entry_;
260 SyncMultiProcedure<T> procedure = (SyncMultiProcedure<T>)procedure_;
264 query.perform(queryGraph, new SyncMultiProcedure<T>() {
267 public void execute(ReadGraph graph, T result) {
268 ReadGraphImpl impl = (ReadGraphImpl)graph;
269 entry.addOrSet(result);
271 procedure.execute(parentGraph, result);
272 } catch (Throwable t) {
278 public void finished(ReadGraph graph) {
279 ReadGraphImpl impl = (ReadGraphImpl)graph;
280 entry.finish(parentGraph);
282 procedure.finished(parentGraph);
283 } catch (Throwable t) {
289 public void exception(ReadGraph graph, Throwable t) {
290 ReadGraphImpl impl = (ReadGraphImpl)graph;
291 entry.except((DatabaseException)t);
293 procedure.exception(parentGraph, t);
294 } catch (Throwable t2) {
295 t2.printStackTrace();
301 return entry.getResult();
303 } catch (Throwable t) {
307 procedure.exception(parentGraph, t);
308 } catch (Throwable t2) {
309 t2.printStackTrace();
312 return entry.getResult();
318 public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) {
320 if (parent != null && !inferred) {
322 if(!child.isImmutable(graph)) {
323 synchronized(child) {
324 child.addParent(parent);
327 } catch (DatabaseException e) {
328 Logger.defaultLogError(e);
330 if (Development.DEVELOPMENT) {
331 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_DEPENDENCIES, Bindings.BOOLEAN)) {
332 System.err.println(child + " -> " + parent);
337 if (listener != null) {
338 return registerListener(child, listener, procedure);
345 public synchronized ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) {
347 assert (entry != null);
349 if (base.isDisposed())
352 return addListener(entry, base, procedure);
356 protected void primeListenerEntry(final ListenerEntry entry, final Object result) {
357 entry.setLastKnown(result);
360 private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) {
362 assert (entry != null);
363 assert (procedure != null);
365 ArrayList<ListenerEntry> list = listeners.get(entry);
367 list = new ArrayList<ListenerEntry>(1);
368 listeners.put(entry, list);
371 ListenerEntry result = new ListenerEntry(entry, base, procedure);
372 int currentIndex = list.indexOf(result);
373 // There was already a listener
374 if(currentIndex > -1) {
375 ListenerEntry current = list.get(currentIndex);
376 if(!current.base.isDisposed()) return null;
377 list.set(currentIndex, result);
382 if (Development.DEVELOPMENT) {
383 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_LISTENERS, Bindings.BOOLEAN)) {
384 new Exception().printStackTrace();
385 System.err.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
394 public Collection<CacheEntry> getRootList() {
396 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
398 for (Object e : valueQueryMap.values()) {
399 result.add((CacheEntry) e);
401 for (Object e : directPredicatesMap.values()) {
402 result.add((CacheEntry) e);
404 for (Object e : directSuperRelationsMap.values()) {
405 result.add((CacheEntry) e);
407 for (Object e : objectsMap.values()) {
408 result.add((CacheEntry) e);
410 for (Object e : directObjectsMap.values()) {
411 result.add((CacheEntry) e);
413 for (Object e : principalTypesMap.values()) {
414 result.add((CacheEntry) e);
416 for (Object e : superRelationsMap.values()) {
417 result.add((CacheEntry) e);
419 for (Object e : superTypesMap.values()) {
420 result.add((CacheEntry) e);
422 for (Object e : typesMap.values()) {
423 result.add((CacheEntry) e);
425 for (Object e : objectsMap.values()) {
426 result.add((CacheEntry) e);
428 for (Object e : assertedStatementsMap.values()) {
429 result.add((CacheEntry) e);
431 for (Object e : readEntryMap.values()) {
432 if(e instanceof CacheEntry) {
433 result.add((CacheEntry) e);
435 System.err.println("e=" + e);
438 for (Object e : asyncReadEntryMap.values()) {
439 if(e instanceof CacheEntry) {
440 result.add((CacheEntry) e);
442 System.err.println("e=" + e);
445 for (Object e : externalReadEntryMap.values()) {
446 result.add((CacheEntry) e);
448 for (Object e : orderedSetMap.values()) {
449 result.add((CacheEntry) e);
456 public int calculateCurrentSize() {
460 realSize += directPredicatesMap.size();
461 realSize += directSuperRelationsMap.size();
462 realSize += principalTypesMap.size();
463 realSize += uRIToResourceMap.size();
464 //realSize += namespaceIndexMap.size();
465 realSize += childMapMap.size();
467 realSize += relationInfoQueryMap.size();
468 realSize += superTypesMap.size();
469 realSize += typeHierarchyMap.size();
470 realSize += superRelationsMap.size();
471 realSize += typesMap.size();
473 realSize += valueQueryMap.size();
474 realSize += directObjectsMap.size();
475 realSize += objectsMap.size();
476 realSize += orderedSetMap.size();
477 realSize += predicatesMap.size();
479 realSize += statementsMap.size();
480 realSize += assertedPredicatesMap.size();
481 realSize += assertedStatementsMap.size();
482 realSize += externalReadEntryMap.size();
483 realSize += asyncReadEntryMap.size();
485 realSize += readEntryMap.size();
486 realSize += asyncMultiReadEntryMap.size();
487 realSize += multiReadEntryMap.size();
493 CacheCollectionResult allCaches(CacheCollectionResult result) {
495 int level = Integer.MAX_VALUE;
496 directPredicatesMap.values(level, result);
497 directSuperRelationsMap.values(level, result);
498 principalTypesMap.values(level, result);
499 for(CacheEntryBase e : uRIToResourceMap.values())
500 if(e.getLevel() <= level)
502 // for(CacheEntryBase e : namespaceIndexMap.values())
503 // if(e.getLevel() <= level)
506 childMapMap.values(level, result);
508 relationInfoQueryMap.values(level, result);
509 superTypesMap.values(level, result);
510 typeHierarchyMap.values(level, result);
511 superRelationsMap.values(level, result);
512 typesMap.values(level, result);
514 valueQueryMap.values(level, result);
515 directObjectsMap.values(level, result);
516 objectsMap.values(level, result);
517 orderedSetMap.values(level, result);
518 predicatesMap.values(level, result);
520 statementsMap.values(level, result);
521 assertedPredicatesMap.values(level, result);
522 assertedStatementsMap.values(level, result);
523 externalReadEntryMap.values(level, result);
524 asyncReadEntryMap.values(level, result);
526 readEntryMap.values(level, result);
527 asyncMultiReadEntryMap.values(level, result);
528 multiReadEntryMap.values(level, result);
534 public void scanPending() {
536 ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
538 entries.addAll(directPredicatesMap.values());
539 entries.addAll(directSuperRelationsMap.values());
540 entries.addAll(principalTypesMap.values());
541 entries.addAll(uRIToResourceMap.values());
542 //entries.addAll(namespaceIndexMap.values());
543 entries.addAll(childMapMap.values());
544 entries.addAll(relationInfoQueryMap.values());
545 entries.addAll(superTypesMap.values());
546 entries.addAll(superRelationsMap.values());
547 entries.addAll(typesMap.values());
548 entries.addAll(valueQueryMap.values());
549 entries.addAll(directObjectsMap.values());
550 entries.addAll(objectsMap.values());
551 entries.addAll(orderedSetMap.values());
552 entries.addAll(predicatesMap.values());
553 entries.addAll(orderedSetMap.values());
554 entries.addAll(statementsMap.values());
555 // entries.addAll(assertedObjectsMap.values());
556 entries.addAll(assertedPredicatesMap.values());
557 entries.addAll(assertedStatementsMap.values());
558 entries.addAll(externalReadEntryMap.values());
559 entries.addAll(asyncReadEntryMap.values());
560 entries.addAll(externalReadEntryMap.values());
561 entries.addAll(readEntryMap.values());
562 entries.addAll(asyncMultiReadEntryMap.values());
563 entries.addAll(multiReadEntryMap.values());
564 entries.addAll(readEntryMap.values());
565 System.out.println(entries.size() + " entries.");
566 for(Object e : entries) {
567 if(e instanceof CacheEntry) {
568 CacheEntry en = (CacheEntry)e;
569 if(en.isPending()) System.out.println("pending " + e);
570 if(en.isExcepted()) System.out.println("excepted " + e);
571 if(en.isDiscarded()) System.out.println("discarded " + e);
572 if(en.isRefuted()) System.out.println("refuted " + e);
573 if(en.isFresh()) System.out.println("fresh " + e);
575 //System.out.println("Unknown object " + e);
580 public static void waitPending(ReadGraphImpl graph, CacheEntry entry) throws DatabaseException {
583 while(entry.isPending()) {
585 boolean performed = graph.performPending();
589 if(counter > 30000) {
590 CacheEntryBase base = ((CacheEntryBase)entry);
591 // if(base.created != null) {
592 // System.err.println("created:");
593 // base.created.printStackTrace();
595 // if(base.performed != null) {
596 // System.err.println("performed:");
597 // base.performed.printStackTrace();
599 // if(base.ready != null) {
600 // System.err.println("ready:");
601 // base.ready.printStackTrace();
603 new Exception("Timeout waiting for request to complete: " + entry.getOriginalRequest()).printStackTrace();
604 throw new DatabaseException("Timeout waiting for request to complete." + entry.getOriginalRequest());
605 //System.err.println("asd");
606 //base.getQuery().recompute(null, null, entry);
609 } catch (InterruptedException e) {
615 //////////////////////////////////////
617 public static Collection<Objects> entriesObjects(QueryProcessor processor, int r1) {
618 synchronized(processor.cache.objectsMap) {
619 return processor.cache.objectsMap.values(r1);
623 public static Collection<Objects> entriesObjects(QueryProcessor processor) {
624 synchronized(processor.cache.objectsMap) {
625 return processor.cache.objectsMap.values();
629 public static Collection<CacheEntry> entriesDirectPredicates(QueryProcessor processor) {
630 synchronized(processor.cache.directPredicatesMap) {
631 return processor.cache.directPredicatesMap.values();
635 static final Collection<DirectObjects> entriesDirectObjects(final QueryProcessor processor, final int r1) {
636 DoubleKeyQueryHashMap<IntProcedure> hash = processor.cache.directObjectsMap;
637 return hash.values(r1);
640 static final Collection<Statements> entriesStatements(final QueryProcessor processor, final int r1) {
641 return processor.cache.statementsMap.values(r1);
644 static final Types entryTypes(final QueryProcessor processor, final int r) {
645 return (Types)processor.cache.typesMap.get(r);
648 static final PrincipalTypes entryPrincipalTypes(final QueryProcessor processor, final int r) {
649 return (PrincipalTypes)processor.cache.principalTypesMap.get(r);
652 static final OrderedSet entryOrderedSet(final QueryProcessor processor, final int r) {
653 return (OrderedSet)processor.cache.orderedSetMap.get(r);
656 static final ValueQuery entryValueQuery(final QueryProcessor processor, final int r) {
657 return (ValueQuery)processor.cache.valueQueryMap.get(r);
660 static final DirectPredicates entryDirectPredicates(final QueryProcessor processor, final int r) {
661 return (DirectPredicates)processor.cache.directPredicatesMap.get(r);
664 public static final ReadEntry entryRead(final QueryProcessor processor, final Read request) {
665 return (ReadEntry)processor.cache.readEntryMap.get(request);
668 public static final MultiReadEntry entryMultiRead(final QueryProcessor processor, final MultiRead request) {
669 return (MultiReadEntry)processor.cache.multiReadEntryMap.get(request);
672 public static final AsyncReadEntry entryAsyncRead(final QueryProcessor processor, final AsyncRead request) {
673 return (AsyncReadEntry)processor.cache.asyncReadEntryMap.get(request);
676 public static final AsyncMultiReadEntry entryAsyncMultiRead(final QueryProcessor processor, final AsyncMultiRead request) {
677 return (AsyncMultiReadEntry)processor.cache.asyncMultiReadEntryMap.get(request);
680 protected static final long keyR2(long r1, long r2) {
681 long result = (r1<<32) | (r2 & 0xffffffffL);
685 protected static final <T> T id(T o) {
689 protected static final int keyR(int r) {
693 protected static final String keyID(String id) {
697 protected static InternalProcedure<IntSet> emptyIntSetProcedure = new InternalProcedure<IntSet>() {
700 public void execute(ReadGraphImpl graph, IntSet result) {
704 public void exception(ReadGraphImpl graph, Throwable throwable) {
709 protected static InternalProcedure<byte[]> emptyBytesProcedure = new InternalProcedure<byte[]>() {
712 public void execute(ReadGraphImpl graph, byte[] bytes) {
716 public void exception(ReadGraphImpl graph, Throwable throwable) {
721 protected static InternalProcedure<Integer> emptyIntegerProcedure = new InternalProcedure<Integer>() {
724 public void execute(ReadGraphImpl graph, Integer i) {
728 public void exception(ReadGraphImpl graph, Throwable throwable) {
734 protected static InternalProcedure<TObjectIntHashMap<String>> emptyNamespaceProcedure = new InternalProcedure<TObjectIntHashMap<String>>() {
737 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> i) {
741 public void exception(ReadGraphImpl graph, Throwable throwable) {
747 protected static InternalProcedure<RelationInfo> emptyRelationInfoProcedure = new InternalProcedure<RelationInfo>() {
750 public void execute(ReadGraphImpl graph, RelationInfo i) {
754 public void exception(ReadGraphImpl graph, Throwable throwable) {
759 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyChildMapProcedure = new InternalProcedure<ObjectResourceIdMap<String>>() {
762 public void execute(ReadGraphImpl graph, ObjectResourceIdMap<String> i) {
766 public void exception(ReadGraphImpl graph, Throwable throwable) {
771 protected static IntProcedure emptyIntProcedure = new IntProcedure() {
774 public void finished(ReadGraphImpl graph) {
778 public void execute(ReadGraphImpl graph, int i) {
782 public void exception(ReadGraphImpl graph, Throwable throwable) {
786 protected static TripleIntProcedure emptyTripleIntProcedure = new TripleIntProcedure() {
789 public void execute(ReadGraphImpl graph, int s, int p, int o) {
793 public void finished(ReadGraphImpl graph) {
797 public void exception(ReadGraphImpl graph, Throwable throwable) {
802 protected static AsyncProcedure<Object> emptyAsyncProcedure = new AsyncProcedure<Object>() {
805 public void execute(AsyncReadGraph graph, Object result) {
809 public void exception(AsyncReadGraph graph, Throwable throwable) {
814 protected static AsyncMultiProcedure<Object> emptyAsyncMultiProcedure = new AsyncMultiProcedure<Object>() {
817 public void execute(AsyncReadGraph graph, Object result) {
821 public void finished(AsyncReadGraph graph) {
825 public void exception(AsyncReadGraph graph, Throwable throwable) {
830 protected static SyncMultiProcedure<Object> emptySyncMultiProcedure = new SyncMultiProcedure<Object>() {
833 public void execute(ReadGraph graph, Object result) {
837 public void finished(ReadGraph graph) {
841 public void exception(ReadGraph graph, Throwable throwable) {
846 protected static InternalProcedure<IntSet> emptyProcedureTypes = emptyIntSetProcedure;
847 protected static InternalProcedure<IntSet> emptyProcedureSuperTypes = emptyIntSetProcedure;
848 protected static InternalProcedure<IntSet> emptyProcedureTypeHierarchy = emptyIntSetProcedure;
849 protected static InternalProcedure<IntSet> emptyProcedureSuperRelations = emptyIntSetProcedure;
850 protected static InternalProcedure<IntSet> emptyProcedurePredicates = emptyIntSetProcedure;
851 protected static InternalProcedure<IntSet> emptyProcedureDirectPredicates = emptyIntSetProcedure;
853 protected static IntProcedure emptyProcedureObjects = emptyIntProcedure;
854 protected static IntProcedure emptyProcedureDirectObjects = emptyIntProcedure;
855 protected static IntProcedure emptyProcedurePrincipalTypes = emptyIntProcedure;
856 protected static IntProcedure emptyProcedureDirectSuperRelations = emptyIntProcedure;
857 protected static IntProcedure emptyProcedureAssertedPredicates = emptyIntProcedure;
858 protected static IntProcedure emptyProcedureOrderedSet = emptyIntProcedure;
860 protected static TripleIntProcedure emptyProcedureStatements = emptyTripleIntProcedure;
861 protected static TripleIntProcedure emptyProcedureAssertedStatements = emptyTripleIntProcedure;
863 protected static InternalProcedure<byte[]> emptyProcedureValueQuery = emptyBytesProcedure;
865 protected static InternalProcedure<Integer> emptyProcedureURIToResource = emptyIntegerProcedure;
866 protected static InternalProcedure<TObjectIntHashMap<String>> emptyProcedureNamespaceIndex = emptyNamespaceProcedure;
867 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyProcedureChildMap = emptyChildMapProcedure;
868 protected static InternalProcedure<RelationInfo> emptyProcedureRelationInfoQuery = emptyRelationInfoProcedure;
870 protected static AsyncProcedure emptyProcedureReadEntry = emptyAsyncProcedure;
871 protected static AsyncProcedure emptyProcedureAsyncReadEntry = emptyAsyncProcedure;
872 protected static SyncMultiProcedure emptyProcedureMultiReadEntry = emptySyncMultiProcedure;
873 protected static AsyncMultiProcedure emptyProcedureAsyncMultiReadEntry = emptyAsyncMultiProcedure;
874 protected static AsyncProcedure emptyProcedureExternalReadEntry = emptyAsyncProcedure;
876 static class AsyncProcedureWrapper<T> implements AsyncProcedure<T> {
878 private AsyncProcedure<T> procedure;
879 private T result = null;
880 private Throwable throwable = null;
881 private Semaphore s = new Semaphore(0);
883 AsyncProcedureWrapper(AsyncProcedure<T> procedure) {
884 this.procedure = procedure;
888 public void execute(AsyncReadGraph graph, T result) {
889 if(procedure != null) procedure.execute(graph, result);
890 this.result = result;
895 public void exception(AsyncReadGraph graph, Throwable throwable) {
896 if(procedure != null) procedure.exception(graph, throwable);
897 this.throwable = throwable;
901 public T get() throws DatabaseException {
904 } catch (InterruptedException e) {
907 if(throwable != null) {
908 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
909 else throw new DatabaseException(throwable);
917 static class ExternalProcedureWrapper<T> implements AsyncProcedure<T> {
919 private Procedure<T> procedure;
920 private T result = null;
921 private Throwable throwable = null;
923 ExternalProcedureWrapper(Procedure<T> procedure) {
924 this.procedure = procedure;
928 public void execute(AsyncReadGraph graph, T result) {
929 if(procedure != null) procedure.execute(result);
930 this.result = result;
934 public void exception(AsyncReadGraph graph, Throwable throwable) {
935 if(procedure != null) procedure.exception(throwable);
936 this.throwable = throwable;
939 public T get() throws DatabaseException {
940 if(throwable != null) {
941 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
942 else throw new DatabaseException(throwable);
951 static class InternalProcedureWrapper<T> implements InternalProcedure<T> {
953 private InternalProcedure<T> procedure;
954 private T result = null;
955 private Throwable throwable = null;
957 InternalProcedureWrapper(InternalProcedure<T> procedure) {
958 this.procedure = procedure;
962 public void execute(ReadGraphImpl graph, T result) throws DatabaseException {
963 if(procedure != null) procedure.execute(graph, result);
964 this.result = result;
968 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
969 if(procedure != null) procedure.exception(graph, throwable);
970 this.throwable = throwable;
973 public T get() throws DatabaseException {
974 if(throwable != null) {
975 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
976 else throw new DatabaseException(throwable);
984 static class IntSetWrapper implements IntProcedure {
986 private IntProcedure procedure;
987 private final IntSet result;
988 private Throwable throwable = null;
990 IntSetWrapper(ReadGraphImpl graph, IntProcedure procedure) {
991 this.procedure = procedure;
992 result = new IntSet(graph.processor.querySupport);
996 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
997 if(procedure != null) procedure.execute(graph, i);
1002 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1003 if(procedure != null) procedure.exception(graph, throwable);
1004 this.throwable = throwable;
1008 public void finished(ReadGraphImpl graph) throws DatabaseException {
1009 if(procedure != null) procedure.finished(graph);
1012 public IntSet get() throws DatabaseException {
1013 if(throwable != null) {
1014 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1015 else throw new DatabaseException(throwable);
1023 static class TripleIntProcedureWrapper implements TripleIntProcedure {
1025 private TripleIntProcedure procedure;
1026 private IntArray result = new IntArray();
1027 private Throwable throwable = null;
1029 TripleIntProcedureWrapper(TripleIntProcedure procedure) {
1030 this.procedure = procedure;
1034 public void execute(ReadGraphImpl graph, int i1, int i2, int i3) throws DatabaseException {
1035 if(procedure != null) procedure.execute(graph, i1, i2, i3);
1042 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1043 if(procedure != null) procedure.exception(graph, throwable);
1044 this.throwable = throwable;
1048 public void finished(ReadGraphImpl graph) throws DatabaseException {
1049 if(procedure != null) procedure.finished(graph);
1052 public IntArray get() throws DatabaseException {
1053 if(throwable != null) {
1054 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1055 else throw new DatabaseException(throwable);
1063 public static <T> T resultExternalReadEntry(ReadGraphImpl graph, ExternalRead r, CacheEntry parent, ListenerBase listener, Procedure<T> procedure) throws DatabaseException {
1064 ExternalProcedureWrapper<T> wrap = new ExternalProcedureWrapper<>(procedure);
1065 QueryCache.runnerExternalReadEntry(graph, r, parent, listener, wrap);
1069 public static <T> T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1070 return (T)QueryCache.runnerReadEntry(graph, r, parent, listener, procedure, true);
1073 public static <T> T resultAsyncReadEntry(ReadGraphImpl graph, AsyncRead r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1074 return (T)QueryCache.runnerAsyncReadEntry(graph, r, parent, listener, procedure, true);
1077 public static byte[] resultValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1078 InternalProcedureWrapper<byte[]> wrap = new InternalProcedureWrapper<>(null);
1079 QueryCache.runnerValueQuery(graph, r, parent, listener, wrap);
1083 public static RelationInfo resultRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1084 InternalProcedureWrapper<RelationInfo> wrap = new InternalProcedureWrapper<>(null);
1085 QueryCache.runnerRelationInfoQuery(graph, r, parent, listener, wrap);
1089 public static IntSet resultSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1090 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1091 QueryCache.runnerSuperRelations(graph, r, parent, listener, wrap);
1095 public static IntSet resultSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1096 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1097 QueryCache.runnerSuperTypes(graph, r, parent, listener, wrap);
1101 public static IntSet resultTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1102 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1103 QueryCache.runnerTypes(graph, r, parent, listener, wrap);
1107 public static IntSet resultPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1108 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1109 QueryCache.runnerPredicates(graph, r, parent, listener, wrap);
1113 public static IntSet resultDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1114 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1115 QueryCache.runnerDirectPredicates(graph, r, parent, listener, wrap);
1119 public static IntArray resultAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1120 TripleIntProcedureWrapper wrap = new TripleIntProcedureWrapper(null);
1121 QueryCache.runnerAssertedStatements(graph, r1, r2, parent, listener, wrap);
1125 public static Integer resultURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1126 InternalProcedureWrapper<Integer> wrap = new InternalProcedureWrapper<Integer>(null);
1127 QueryCache.runnerURIToResource(graph, id, parent, listener, wrap);
1131 public static ObjectResourceIdMap<String> resultChildMap(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1132 InternalProcedureWrapper<ObjectResourceIdMap<String>> wrap = new InternalProcedureWrapper<ObjectResourceIdMap<String>>(null);
1133 QueryCache.runnerChildMap(graph, r, parent, listener, wrap);
1137 static boolean shouldCache(QueryProcessor processor, int r) {
1138 return processor.isImmutable(r);
1141 static boolean shouldCache(QueryProcessor processor, int r, int r2) {
1142 return processor.isImmutable(r);
1145 static boolean shouldCache(QueryProcessor processor, Object o) {