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.RelationInfo;
11 import org.simantics.db.common.utils.Logger;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.impl.DebugPolicy;
14 import org.simantics.db.impl.graph.ReadGraphImpl;
15 import org.simantics.db.impl.procedure.InternalProcedure;
16 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
17 import org.simantics.db.procedure.AsyncMultiProcedure;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.db.procedure.Listener;
20 import org.simantics.db.procedure.ListenerBase;
21 import org.simantics.db.procedure.Procedure;
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;
28 import gnu.trove.map.hash.THashMap;
29 import gnu.trove.map.hash.TObjectIntHashMap;
31 public class QueryCacheBase {
35 final public int THREAD_MASK;
41 volatile public boolean dirty = false;
42 public boolean collecting = false;
44 final protected THashMap<String, URIToResource> uRIToResourceMap;
45 //final protected THashMap<String, NamespaceIndex> namespaceIndexMap;
46 final protected UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>> childMapMap;
47 final protected DoubleKeyQueryHashMap<IntProcedure> objectsMap;
48 final protected DoubleKeyQueryHashMap<TripleIntProcedure> assertedStatementsMap;
49 final protected DoubleKeyQueryHashMap<IntProcedure> directObjectsMap;
50 final protected DoubleKeyQueryHashMap<TripleIntProcedure> statementsMap;
51 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> typesMap;
52 final protected UnaryQueryHashMap<IntProcedure> principalTypesMap;
53 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> predicatesMap;
54 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> superTypesMap;
55 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> typeHierarchyMap;
56 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> superRelationsMap;
58 final protected UnaryQueryHashMap<IntProcedure> orderedSetMap;
59 final protected UnaryQueryHashMap<IntProcedure> assertedPredicatesMap;
60 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> directPredicatesMap;
61 final protected UnaryQueryHashMap<IntProcedure> directSuperRelationsMap;
63 final protected UnaryQueryHashMap<InternalProcedure<RelationInfo>> relationInfoQueryMap;
64 final protected UnaryQueryHashMap<InternalProcedure<byte[]>> valueQueryMap;
66 final protected StableHashMap<AsyncRead, AsyncReadEntry> asyncReadEntryMap;
67 final protected StableHashMap<Read, ReadEntry> readEntryMap;
68 final protected StableHashMap<MultiRead, MultiReadEntry> multiReadEntryMap;
69 final protected StableHashMap<AsyncMultiRead, AsyncMultiReadEntry> asyncMultiReadEntryMap;
70 final protected StableHashMap<ExternalRead, ExternalReadEntry> externalReadEntryMap;
72 final THashMap<CacheEntry, ArrayList<ListenerEntry>> listeners;
74 final public QuerySupport querySupport;
76 public QueryCacheBase(QuerySupport querySupport, int threads) {
79 THREAD_MASK = threads - 1;
81 this.querySupport = querySupport;
82 directPredicatesMap = new UnaryQueryHashMap();
83 directSuperRelationsMap = new UnaryQueryHashMap();
84 valueQueryMap = new UnaryQueryHashMap();
85 principalTypesMap = new UnaryQueryHashMap();
86 uRIToResourceMap = new THashMap<String, URIToResource>();
87 //namespaceIndexMap = new THashMap<String, NamespaceIndex>();
88 childMapMap = new UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>>();
89 relationInfoQueryMap = new UnaryQueryHashMap();
90 typeHierarchyMap = new UnaryQueryHashMap();
91 superTypesMap = new UnaryQueryHashMap();
92 superRelationsMap = new UnaryQueryHashMap();
93 typesMap = new UnaryQueryHashMap();
94 objectsMap = new DoubleKeyQueryHashMap();
95 orderedSetMap = new UnaryQueryHashMap();
96 predicatesMap = new UnaryQueryHashMap();
97 statementsMap = new DoubleKeyQueryHashMap();
98 directObjectsMap = new DoubleKeyQueryHashMap();
99 assertedPredicatesMap = new UnaryQueryHashMap();
100 assertedStatementsMap = new DoubleKeyQueryHashMap();
101 asyncReadEntryMap = new StableHashMap<AsyncRead, AsyncReadEntry>();
102 readEntryMap = new StableHashMap<Read, ReadEntry>();
103 asyncMultiReadEntryMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>();
104 multiReadEntryMap = new StableHashMap<MultiRead, MultiReadEntry>();
105 externalReadEntryMap = new StableHashMap<ExternalRead, ExternalReadEntry>();
106 listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
109 // public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
111 // AsyncReadEntry<T> entry = (AsyncReadEntry<T>)entry_;
112 // AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
114 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
118 // query.perform(queryGraph, new AsyncProcedure<T>() {
121 // public void execute(AsyncReadGraph returnGraph, T result) {
122 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
123 // entry.addOrSet(parentGraph, result);
125 // procedure.execute(parentGraph, result);
126 // } catch (Throwable t) {
127 // t.printStackTrace();
129 //// parentBarrier.dec(query);
133 // public void exception(AsyncReadGraph returnGraph, Throwable t) {
134 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
135 //// AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
136 // entry.except(parentGraph, t);
138 // procedure.exception(parentGraph, t);
139 // } catch (Throwable t2) {
140 // t2.printStackTrace();
142 //// parentBarrier.dec(query);
146 // public String toString() {
147 // return procedure.toString();
152 // } catch (Throwable t) {
156 // procedure.exception(parentGraph, t);
157 // } catch (Throwable t2) {
158 // t2.printStackTrace();
160 //// parentBarrier.dec(query);
168 // public <T> Object performQuery(ReadGraphImpl parentGraph, final Read<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
170 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
172 // ReadEntry entry = (ReadEntry)entry_;
176 // T result = (T)query.perform(queryGraph);
177 // entry.addOrSet(queryGraph, result);
179 // return (T)entry.get(parentGraph, procedure_);
181 // } catch (Throwable t) {
184 // return (T)entry.get(parentGraph, procedure_);
190 public <T> Object performQuery(ReadGraphImpl parentGraph, final ExternalRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
192 ExternalReadEntry entry = (ExternalReadEntry)entry_;
193 AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
197 query.register(parentGraph, new Listener<T>() {
199 AtomicBoolean used = new AtomicBoolean(false);
202 public void execute(T result) {
205 if(entry.isDiscarded()) return;
207 if(used.compareAndSet(false, true)) {
208 //entry.setPending();
209 entry.addOrSet(parentGraph.processor, result);
210 procedure.execute(parentGraph, result);
213 parentGraph.processor.updatePrimitive(query);
219 public void exception(Throwable t) {
223 if(used.compareAndSet(false, true)) {
224 procedure.exception(parentGraph, t);
226 // entry.queue(result);
227 parentGraph.processor.updatePrimitive(query);
233 public String toString() {
234 return procedure.toString();
238 public boolean isDisposed() {
239 return entry.isDiscarded() || !parentGraph.processor.isBound(entry);
244 return entry.getResult();
246 } catch (Throwable t) {
249 procedure.exception(parentGraph, t);
250 return entry.getResult();
256 public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncMultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
258 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
260 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
261 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
265 query.perform(queryGraph, new AsyncMultiProcedure<T>() {
268 public void execute(AsyncReadGraph graph, T result) {
269 ReadGraphImpl impl = (ReadGraphImpl)graph;
270 entry.addOrSet(result);
272 procedure.execute(parentGraph, result);
273 } catch (Throwable t) {
279 public void finished(AsyncReadGraph graph) {
280 ReadGraphImpl impl = (ReadGraphImpl)graph;
281 entry.finish(parentGraph);
283 procedure.finished(parentGraph);
284 } catch (Throwable t) {
290 public void exception(AsyncReadGraph graph, Throwable t) {
291 ReadGraphImpl impl = (ReadGraphImpl)graph;
292 entry.except(parentGraph, t);
294 procedure.exception(parentGraph, t);
295 } catch (Throwable t2) {
296 t2.printStackTrace();
302 return entry.getResult();
304 } catch (Throwable t) {
308 procedure.exception(parentGraph, t);
309 } catch (Throwable t2) {
310 t2.printStackTrace();
313 return entry.getResult();
319 public <T> Object performQuery(ReadGraphImpl parentGraph, final MultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
321 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
323 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
324 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
328 query.perform(queryGraph, new AsyncMultiProcedure<T>() {
331 public void execute(AsyncReadGraph graph, T result) {
332 ReadGraphImpl impl = (ReadGraphImpl)graph;
333 entry.addOrSet(result);
335 procedure.execute(parentGraph, result);
336 } catch (Throwable t) {
342 public void finished(AsyncReadGraph graph) {
343 ReadGraphImpl impl = (ReadGraphImpl)graph;
344 entry.finish(parentGraph);
346 procedure.finished(parentGraph);
347 } catch (Throwable t) {
353 public void exception(AsyncReadGraph graph, Throwable t) {
354 ReadGraphImpl impl = (ReadGraphImpl)graph;
355 entry.except(parentGraph, t);
357 procedure.exception(parentGraph, t);
358 } catch (Throwable t2) {
359 t2.printStackTrace();
365 return entry.getResult();
367 } catch (Throwable t) {
371 procedure.exception(parentGraph, t);
372 } catch (Throwable t2) {
373 t2.printStackTrace();
376 return entry.getResult();
382 public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) {
384 if (parent != null && !inferred) {
386 if(!child.isImmutable(graph)) {
387 synchronized(child) {
388 child.addParent(parent);
391 } catch (DatabaseException e) {
392 Logger.defaultLogError(e);
394 if(DebugPolicy.DEPENDENCIES) System.out.println(child + " -> " + parent);
397 if (listener != null) {
398 return registerListener(child, listener, procedure);
405 public synchronized ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) {
407 assert (entry != null);
409 if (base.isDisposed())
412 return addListener(entry, base, procedure);
416 protected void primeListenerEntry(final ListenerEntry entry, final Object result) {
417 entry.setLastKnown(result);
420 private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) {
422 assert (entry != null);
423 assert (procedure != null);
425 ArrayList<ListenerEntry> list = listeners.get(entry);
427 list = new ArrayList<ListenerEntry>(1);
428 listeners.put(entry, list);
431 ListenerEntry result = new ListenerEntry(entry, base, procedure);
432 int currentIndex = list.indexOf(result);
433 // There was already a listener
434 if(currentIndex > -1) {
435 ListenerEntry current = list.get(currentIndex);
436 if(!current.base.isDisposed()) return null;
437 list.set(currentIndex, result);
442 if(DebugPolicy.LISTENER) {
443 new Exception().printStackTrace();
444 System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
452 public Collection<CacheEntry> getRootList() {
454 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
456 for (Object e : valueQueryMap.values()) {
457 result.add((CacheEntry) e);
459 for (Object e : directPredicatesMap.values()) {
460 result.add((CacheEntry) e);
462 for (Object e : directSuperRelationsMap.values()) {
463 result.add((CacheEntry) e);
465 for (Object e : objectsMap.values()) {
466 result.add((CacheEntry) e);
468 for (Object e : directObjectsMap.values()) {
469 result.add((CacheEntry) e);
471 for (Object e : principalTypesMap.values()) {
472 result.add((CacheEntry) e);
474 for (Object e : superRelationsMap.values()) {
475 result.add((CacheEntry) e);
477 for (Object e : superTypesMap.values()) {
478 result.add((CacheEntry) e);
480 for (Object e : typesMap.values()) {
481 result.add((CacheEntry) e);
483 for (Object e : objectsMap.values()) {
484 result.add((CacheEntry) e);
486 for (Object e : assertedStatementsMap.values()) {
487 result.add((CacheEntry) e);
489 for (Object e : readEntryMap.values()) {
490 if(e instanceof CacheEntry) {
491 result.add((CacheEntry) e);
493 System.err.println("e=" + e);
496 for (Object e : asyncReadEntryMap.values()) {
497 if(e instanceof CacheEntry) {
498 result.add((CacheEntry) e);
500 System.err.println("e=" + e);
503 for (Object e : externalReadEntryMap.values()) {
504 result.add((CacheEntry) e);
506 for (Object e : orderedSetMap.values()) {
507 result.add((CacheEntry) e);
514 public int calculateCurrentSize() {
518 realSize += directPredicatesMap.size();
519 realSize += directSuperRelationsMap.size();
520 realSize += principalTypesMap.size();
521 realSize += uRIToResourceMap.size();
522 //realSize += namespaceIndexMap.size();
523 realSize += childMapMap.size();
525 realSize += relationInfoQueryMap.size();
526 realSize += superTypesMap.size();
527 realSize += typeHierarchyMap.size();
528 realSize += superRelationsMap.size();
529 realSize += typesMap.size();
531 realSize += valueQueryMap.size();
532 realSize += directObjectsMap.size();
533 realSize += objectsMap.size();
534 realSize += orderedSetMap.size();
535 realSize += predicatesMap.size();
537 realSize += statementsMap.size();
538 realSize += assertedPredicatesMap.size();
539 realSize += assertedStatementsMap.size();
540 realSize += externalReadEntryMap.size();
541 realSize += asyncReadEntryMap.size();
543 realSize += readEntryMap.size();
544 realSize += asyncMultiReadEntryMap.size();
545 realSize += multiReadEntryMap.size();
551 CacheCollectionResult allCaches(CacheCollectionResult result) {
553 int level = Integer.MAX_VALUE;
554 directPredicatesMap.values(level, result);
555 directSuperRelationsMap.values(level, result);
556 principalTypesMap.values(level, result);
557 for(CacheEntryBase e : uRIToResourceMap.values())
558 if(e.getLevel() <= level)
560 // for(CacheEntryBase e : namespaceIndexMap.values())
561 // if(e.getLevel() <= level)
564 childMapMap.values(level, result);
566 relationInfoQueryMap.values(level, result);
567 superTypesMap.values(level, result);
568 typeHierarchyMap.values(level, result);
569 superRelationsMap.values(level, result);
570 typesMap.values(level, result);
572 valueQueryMap.values(level, result);
573 directObjectsMap.values(level, result);
574 objectsMap.values(level, result);
575 orderedSetMap.values(level, result);
576 predicatesMap.values(level, result);
578 statementsMap.values(level, result);
579 assertedPredicatesMap.values(level, result);
580 assertedStatementsMap.values(level, result);
581 externalReadEntryMap.values(level, result);
582 asyncReadEntryMap.values(level, result);
584 readEntryMap.values(level, result);
585 asyncMultiReadEntryMap.values(level, result);
586 multiReadEntryMap.values(level, result);
592 public void scanPending() {
594 ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
596 entries.addAll(directPredicatesMap.values());
597 entries.addAll(directSuperRelationsMap.values());
598 entries.addAll(principalTypesMap.values());
599 entries.addAll(uRIToResourceMap.values());
600 //entries.addAll(namespaceIndexMap.values());
601 entries.addAll(childMapMap.values());
602 entries.addAll(relationInfoQueryMap.values());
603 entries.addAll(superTypesMap.values());
604 entries.addAll(superRelationsMap.values());
605 entries.addAll(typesMap.values());
606 entries.addAll(valueQueryMap.values());
607 entries.addAll(directObjectsMap.values());
608 entries.addAll(objectsMap.values());
609 entries.addAll(orderedSetMap.values());
610 entries.addAll(predicatesMap.values());
611 entries.addAll(orderedSetMap.values());
612 entries.addAll(statementsMap.values());
613 // entries.addAll(assertedObjectsMap.values());
614 entries.addAll(assertedPredicatesMap.values());
615 entries.addAll(assertedStatementsMap.values());
616 entries.addAll(externalReadEntryMap.values());
617 entries.addAll(asyncReadEntryMap.values());
618 entries.addAll(externalReadEntryMap.values());
619 entries.addAll(readEntryMap.values());
620 entries.addAll(asyncMultiReadEntryMap.values());
621 entries.addAll(multiReadEntryMap.values());
622 entries.addAll(readEntryMap.values());
623 System.out.println(entries.size() + " entries.");
624 for(Object e : entries) {
625 if(e instanceof CacheEntry) {
626 CacheEntry en = (CacheEntry)e;
627 if(en.isPending()) System.out.println("pending " + e);
628 if(en.isExcepted()) System.out.println("excepted " + e);
629 if(en.isDiscarded()) System.out.println("discarded " + e);
630 if(en.isRefuted()) System.out.println("refuted " + e);
631 if(en.isFresh()) System.out.println("fresh " + e);
633 //System.out.println("Unknown object " + e);
638 public static void waitPending(QueryProcessor processor, CacheEntry entry) throws DatabaseException {
641 while(entry.isPending()) {
643 SessionTask task = processor.getOwnTask(processor.thread.get());
645 task.run(processor.thread.get());
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().toString()).printStackTrace();
664 throw new DatabaseException("Timeout waiting for request to complete.");
665 //System.err.println("asd");
666 //base.getQuery().recompute(null, null, entry);
669 } catch (InterruptedException e) {
675 //////////////////////////////////////
677 static public Collection<Objects> entriesObjects(QueryProcessor processor, int r1) {
678 synchronized(processor.cache.objectsMap) {
679 return processor.cache.objectsMap.values(r1);
683 static public Collection<Objects> entriesObjects(QueryProcessor processor) {
684 synchronized(processor.cache.objectsMap) {
685 return processor.cache.objectsMap.values();
689 static public Collection<CacheEntry> entriesDirectPredicates(QueryProcessor processor) {
690 synchronized(processor.cache.directPredicatesMap) {
691 return processor.cache.directPredicatesMap.values();
695 final static Collection<DirectObjects> entriesDirectObjects(final QueryProcessor processor, final int r1) {
696 DoubleKeyQueryHashMap<IntProcedure> hash = processor.cache.directObjectsMap;
697 return hash.values(r1);
700 final static Collection<Statements> entriesStatements(final QueryProcessor processor, final int r1) {
701 return processor.cache.statementsMap.values(r1);
704 final static Types entryTypes(final QueryProcessor processor, final int r) {
705 return (Types)processor.cache.typesMap.get(r);
708 final static PrincipalTypes entryPrincipalTypes(final QueryProcessor processor, final int r) {
709 return (PrincipalTypes)processor.cache.principalTypesMap.get(r);
712 final static OrderedSet entryOrderedSet(final QueryProcessor processor, final int r) {
713 return (OrderedSet)processor.cache.orderedSetMap.get(r);
716 final static ValueQuery entryValueQuery(final QueryProcessor processor, final int r) {
717 return (ValueQuery)processor.cache.valueQueryMap.get(r);
720 final static DirectPredicates entryDirectPredicates(final QueryProcessor processor, final int r) {
721 return (DirectPredicates)processor.cache.directPredicatesMap.get(r);
724 public final static ReadEntry entryRead(final QueryProcessor processor, final Read request) {
725 return (ReadEntry)processor.cache.readEntryMap.get(request);
728 public final static MultiReadEntry entryMultiRead(final QueryProcessor processor, final MultiRead request) {
729 return (MultiReadEntry)processor.cache.multiReadEntryMap.get(request);
732 public final static AsyncReadEntry entryAsyncRead(final QueryProcessor processor, final AsyncRead request) {
733 return (AsyncReadEntry)processor.cache.asyncReadEntryMap.get(request);
736 public final static AsyncMultiReadEntry entryAsyncMultiRead(final QueryProcessor processor, final AsyncMultiRead request) {
737 return (AsyncMultiReadEntry)processor.cache.asyncMultiReadEntryMap.get(request);
740 final protected static long keyR2(long r1, long r2) {
741 long result = (r1<<32) | (r2 & 0xffffffffL);
745 final protected static <T> T id(T o) {
749 final protected static int keyR(int r) {
753 final protected static 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) {
833 protected static IntProcedure emptyIntProcedure = new IntProcedure() {
836 public void finished(ReadGraphImpl graph) {
840 public void execute(ReadGraphImpl graph, int i) {
844 public void exception(ReadGraphImpl graph, Throwable throwable) {
848 protected static TripleIntProcedure emptyTripleIntProcedure = new TripleIntProcedure() {
851 public void execute(ReadGraphImpl graph, int s, int p, int o) {
855 public void finished(ReadGraphImpl graph) {
859 public void exception(ReadGraphImpl graph, Throwable throwable) {
864 protected static AsyncProcedure<Object> emptyAsyncProcedure = new AsyncProcedure<Object>() {
867 public void execute(AsyncReadGraph graph, Object result) {
871 public void exception(AsyncReadGraph graph, Throwable throwable) {
876 protected static AsyncMultiProcedure<Object> emptyAsyncMultiProcedure = new AsyncMultiProcedure<Object>() {
879 public void execute(AsyncReadGraph graph, Object result) {
883 public void finished(AsyncReadGraph graph) {
887 public void exception(AsyncReadGraph graph, Throwable throwable) {
893 protected static InternalProcedure<IntSet> emptyProcedureTypes = emptyIntSetProcedure;
894 protected static InternalProcedure<IntSet> emptyProcedureSuperTypes = emptyIntSetProcedure;
895 protected static InternalProcedure<IntSet> emptyProcedureTypeHierarchy = emptyIntSetProcedure;
896 protected static InternalProcedure<IntSet> emptyProcedureSuperRelations = emptyIntSetProcedure;
897 protected static InternalProcedure<IntSet> emptyProcedurePredicates = emptyIntSetProcedure;
898 protected static InternalProcedure<IntSet> emptyProcedureDirectPredicates = emptyIntSetProcedure;
900 protected static IntProcedure emptyProcedureObjects = emptyIntProcedure;
901 protected static IntProcedure emptyProcedureDirectObjects = emptyIntProcedure;
902 protected static IntProcedure emptyProcedurePrincipalTypes = emptyIntProcedure;
903 protected static IntProcedure emptyProcedureDirectSuperRelations = emptyIntProcedure;
904 protected static IntProcedure emptyProcedureAssertedPredicates = emptyIntProcedure;
905 protected static IntProcedure emptyProcedureOrderedSet = emptyIntProcedure;
907 protected static TripleIntProcedure emptyProcedureStatements = emptyTripleIntProcedure;
908 protected static TripleIntProcedure emptyProcedureAssertedStatements = emptyTripleIntProcedure;
910 protected static InternalProcedure<byte[]> emptyProcedureValueQuery = emptyBytesProcedure;
912 protected static InternalProcedure<Integer> emptyProcedureURIToResource = emptyIntegerProcedure;
913 protected static InternalProcedure<TObjectIntHashMap<String>> emptyProcedureNamespaceIndex = emptyNamespaceProcedure;
914 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyProcedureChildMap = emptyChildMapProcedure;
915 protected static InternalProcedure<RelationInfo> emptyProcedureRelationInfoQuery = emptyRelationInfoProcedure;
917 protected static AsyncProcedure emptyProcedureReadEntry = emptyAsyncProcedure;
918 protected static AsyncProcedure emptyProcedureAsyncReadEntry = emptyAsyncProcedure;
919 protected static AsyncMultiProcedure emptyProcedureMultiReadEntry = emptyAsyncMultiProcedure;
920 protected static AsyncMultiProcedure emptyProcedureAsyncMultiReadEntry = emptyAsyncMultiProcedure;
921 protected static AsyncProcedure emptyProcedureExternalReadEntry = emptyAsyncProcedure;
923 static class AsyncProcedureWrapper<T> implements AsyncProcedure<T> {
925 private AsyncProcedure<T> procedure;
926 private T result = null;
927 private Throwable throwable = null;
928 private Semaphore s = new Semaphore(0);
930 AsyncProcedureWrapper(AsyncProcedure<T> procedure) {
931 this.procedure = procedure;
935 public void execute(AsyncReadGraph graph, T result) {
936 if(procedure != null) procedure.execute(graph, result);
937 this.result = result;
942 public void exception(AsyncReadGraph graph, Throwable throwable) {
943 if(procedure != null) procedure.exception(graph, throwable);
944 this.throwable = throwable;
948 public T get() throws DatabaseException {
951 } catch (InterruptedException e) {
954 if(throwable != null) {
955 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
956 else throw new DatabaseException(throwable);
964 static class ExternalProcedureWrapper<T> implements AsyncProcedure<T> {
966 private Procedure<T> procedure;
967 private T result = null;
968 private Throwable throwable = null;
970 ExternalProcedureWrapper(Procedure<T> procedure) {
971 this.procedure = procedure;
975 public void execute(AsyncReadGraph graph, T result) {
976 if(procedure != null) procedure.execute(result);
977 this.result = result;
981 public void exception(AsyncReadGraph graph, Throwable throwable) {
982 if(procedure != null) procedure.exception(throwable);
983 this.throwable = throwable;
986 public T get() throws DatabaseException {
987 if(throwable != null) {
988 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
989 else throw new DatabaseException(throwable);
998 static class InternalProcedureWrapper<T> implements InternalProcedure<T> {
1000 private InternalProcedure<T> procedure;
1001 private T result = null;
1002 private Throwable throwable = null;
1004 InternalProcedureWrapper(InternalProcedure<T> procedure) {
1005 this.procedure = procedure;
1009 public void execute(ReadGraphImpl graph, T result) throws DatabaseException {
1010 if(procedure != null) procedure.execute(graph, result);
1011 this.result = result;
1015 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1016 if(procedure != null) procedure.exception(graph, throwable);
1017 this.throwable = throwable;
1020 public T get() throws DatabaseException {
1021 if(throwable != null) {
1022 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1023 else throw new DatabaseException(throwable);
1031 static class IntSetWrapper implements IntProcedure {
1033 private IntProcedure procedure;
1034 final private IntSet result;
1035 private Throwable throwable = null;
1037 IntSetWrapper(ReadGraphImpl graph, IntProcedure procedure) {
1038 this.procedure = procedure;
1039 result = new IntSet(graph.processor.querySupport);
1043 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
1044 if(procedure != null) procedure.execute(graph, i);
1049 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1050 if(procedure != null) procedure.exception(graph, throwable);
1051 this.throwable = throwable;
1055 public void finished(ReadGraphImpl graph) throws DatabaseException {
1056 if(procedure != null) procedure.finished(graph);
1059 public IntSet get() throws DatabaseException {
1060 if(throwable != null) {
1061 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1062 else throw new DatabaseException(throwable);
1070 static class TripleIntProcedureWrapper implements TripleIntProcedure {
1072 private TripleIntProcedure procedure;
1073 private IntArray result = new IntArray();
1074 private Throwable throwable = null;
1076 TripleIntProcedureWrapper(TripleIntProcedure procedure) {
1077 this.procedure = procedure;
1081 public void execute(ReadGraphImpl graph, int i1, int i2, int i3) throws DatabaseException {
1082 if(procedure != null) procedure.execute(graph, i1, i2, i3);
1089 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1090 if(procedure != null) procedure.exception(graph, throwable);
1091 this.throwable = throwable;
1095 public void finished(ReadGraphImpl graph) throws DatabaseException {
1096 if(procedure != null) procedure.finished(graph);
1099 public IntArray get() throws DatabaseException {
1100 if(throwable != null) {
1101 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1102 else throw new DatabaseException(throwable);
1110 public static <T> T resultExternalReadEntry(ReadGraphImpl graph, ExternalRead r, CacheEntry parent, ListenerBase listener, Procedure<T> procedure) throws DatabaseException {
1111 ExternalProcedureWrapper<T> wrap = new ExternalProcedureWrapper<>(procedure);
1112 QueryCache.runnerExternalReadEntry(graph, r, parent, listener, wrap);
1116 public static <T> T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1117 AsyncProcedureWrapper<T> wrap = new AsyncProcedureWrapper<>(procedure);
1118 QueryCache.runnerReadEntry(graph, r, parent, listener, wrap, true);
1122 public static <T> T resultAsyncReadEntry(ReadGraphImpl graph, AsyncRead r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1123 AsyncProcedureWrapper<T> wrap = new AsyncProcedureWrapper<>(procedure);
1124 QueryCache.runnerAsyncReadEntry(graph, r, parent, listener, wrap, true);
1128 public static byte[] resultValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1129 InternalProcedureWrapper<byte[]> wrap = new InternalProcedureWrapper<>(null);
1130 QueryCache.runnerValueQuery(graph, r, parent, listener, wrap);
1134 public static RelationInfo resultRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1135 InternalProcedureWrapper<RelationInfo> wrap = new InternalProcedureWrapper<>(null);
1136 QueryCache.runnerRelationInfoQuery(graph, r, parent, listener, wrap);
1140 public static IntSet resultSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1141 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1142 QueryCache.runnerSuperRelations(graph, r, parent, listener, wrap);
1146 public static IntSet resultSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1147 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1148 QueryCache.runnerSuperTypes(graph, r, parent, listener, wrap);
1152 public static IntSet resultTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1153 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1154 QueryCache.runnerTypes(graph, r, parent, listener, wrap);
1158 public static IntSet resultPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1159 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1160 QueryCache.runnerPredicates(graph, r, parent, listener, wrap);
1164 public static IntSet resultDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1165 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1166 QueryCache.runnerDirectPredicates(graph, r, parent, listener, wrap);
1170 public static IntArray resultAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1171 TripleIntProcedureWrapper wrap = new TripleIntProcedureWrapper(null);
1172 QueryCache.runnerAssertedStatements(graph, r1, r2, parent, listener, wrap);
1176 public static Integer resultURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1177 InternalProcedureWrapper<Integer> wrap = new InternalProcedureWrapper<Integer>(null);
1178 QueryCache.runnerURIToResource(graph, id, parent, listener, wrap);
1182 public static ObjectResourceIdMap<String> resultChildMap(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1183 InternalProcedureWrapper<ObjectResourceIdMap<String>> wrap = new InternalProcedureWrapper<ObjectResourceIdMap<String>>(null);
1184 QueryCache.runnerChildMap(graph, r, parent, listener, wrap);
1188 static boolean shouldCache(QueryProcessor processor, int r) {
1189 return processor.isImmutable(r);
1192 static boolean shouldCache(QueryProcessor processor, int r, int r2) {
1193 return processor.isImmutable(r);
1196 static boolean shouldCache(QueryProcessor processor, Object o) {