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.procedure.AsyncMultiProcedure;
17 import org.simantics.db.procedure.AsyncProcedure;
18 import org.simantics.db.procedure.Listener;
19 import org.simantics.db.procedure.ListenerBase;
20 import org.simantics.db.procedure.Procedure;
21 import org.simantics.db.request.AsyncMultiRead;
22 import org.simantics.db.request.AsyncRead;
23 import org.simantics.db.request.ExternalRead;
24 import org.simantics.db.request.MultiRead;
25 import org.simantics.db.request.Read;
27 import gnu.trove.map.hash.THashMap;
28 import gnu.trove.map.hash.TObjectIntHashMap;
30 public class QueryCacheBase {
34 final public int THREAD_MASK;
40 volatile public boolean dirty = false;
41 public boolean collecting = false;
43 final protected THashMap<String, URIToResource> uRIToResourceMap;
44 //final protected THashMap<String, NamespaceIndex> namespaceIndexMap;
45 final protected UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>> childMapMap;
46 final protected DoubleKeyQueryHashMap<IntProcedure> objectsMap;
47 final protected DoubleKeyQueryHashMap<TripleIntProcedure> assertedStatementsMap;
48 final protected DoubleKeyQueryHashMap<IntProcedure> directObjectsMap;
49 final protected DoubleKeyQueryHashMap<TripleIntProcedure> statementsMap;
50 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> typesMap;
51 final protected UnaryQueryHashMap<IntProcedure> principalTypesMap;
52 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> predicatesMap;
53 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> superTypesMap;
54 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> typeHierarchyMap;
55 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> superRelationsMap;
57 final protected UnaryQueryHashMap<IntProcedure> orderedSetMap;
58 final protected UnaryQueryHashMap<IntProcedure> assertedPredicatesMap;
59 final protected UnaryQueryHashMap<InternalProcedure<IntSet>> directPredicatesMap;
60 final protected UnaryQueryHashMap<IntProcedure> directSuperRelationsMap;
62 final protected UnaryQueryHashMap<InternalProcedure<RelationInfo>> relationInfoQueryMap;
63 final protected UnaryQueryHashMap<InternalProcedure<byte[]>> valueQueryMap;
65 final protected StableHashMap<AsyncRead, AsyncReadEntry> asyncReadEntryMap;
66 final protected StableHashMap<Read, ReadEntry> readEntryMap;
67 final protected StableHashMap<MultiRead, MultiReadEntry> multiReadEntryMap;
68 final protected StableHashMap<AsyncMultiRead, AsyncMultiReadEntry> asyncMultiReadEntryMap;
69 final protected StableHashMap<ExternalRead, ExternalReadEntry> externalReadEntryMap;
71 final THashMap<CacheEntry, ArrayList<ListenerEntry>> listeners;
73 final public QuerySupport querySupport;
75 public QueryCacheBase(QuerySupport querySupport, int threads) {
78 THREAD_MASK = threads - 1;
80 this.querySupport = querySupport;
81 directPredicatesMap = new UnaryQueryHashMap();
82 directSuperRelationsMap = new UnaryQueryHashMap();
83 valueQueryMap = new UnaryQueryHashMap();
84 principalTypesMap = new UnaryQueryHashMap();
85 uRIToResourceMap = new THashMap<String, URIToResource>();
86 //namespaceIndexMap = new THashMap<String, NamespaceIndex>();
87 childMapMap = new UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>>();
88 relationInfoQueryMap = new UnaryQueryHashMap();
89 typeHierarchyMap = new UnaryQueryHashMap();
90 superTypesMap = new UnaryQueryHashMap();
91 superRelationsMap = new UnaryQueryHashMap();
92 typesMap = new UnaryQueryHashMap();
93 objectsMap = new DoubleKeyQueryHashMap();
94 orderedSetMap = new UnaryQueryHashMap();
95 predicatesMap = new UnaryQueryHashMap();
96 statementsMap = new DoubleKeyQueryHashMap();
97 directObjectsMap = new DoubleKeyQueryHashMap();
98 assertedPredicatesMap = new UnaryQueryHashMap();
99 assertedStatementsMap = new DoubleKeyQueryHashMap();
100 asyncReadEntryMap = new StableHashMap<AsyncRead, AsyncReadEntry>();
101 readEntryMap = new StableHashMap<Read, ReadEntry>();
102 asyncMultiReadEntryMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>();
103 multiReadEntryMap = new StableHashMap<MultiRead, MultiReadEntry>();
104 externalReadEntryMap = new StableHashMap<ExternalRead, ExternalReadEntry>();
105 listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
108 // public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
110 // AsyncReadEntry<T> entry = (AsyncReadEntry<T>)entry_;
111 // AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
113 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
117 // query.perform(queryGraph, new AsyncProcedure<T>() {
120 // public void execute(AsyncReadGraph returnGraph, T result) {
121 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
122 // entry.addOrSet(parentGraph, result);
124 // procedure.execute(parentGraph, result);
125 // } catch (Throwable t) {
126 // t.printStackTrace();
128 //// parentBarrier.dec(query);
132 // public void exception(AsyncReadGraph returnGraph, Throwable t) {
133 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
134 //// AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
135 // entry.except(parentGraph, t);
137 // procedure.exception(parentGraph, t);
138 // } catch (Throwable t2) {
139 // t2.printStackTrace();
141 //// parentBarrier.dec(query);
145 // public String toString() {
146 // return procedure.toString();
151 // } catch (Throwable t) {
155 // procedure.exception(parentGraph, t);
156 // } catch (Throwable t2) {
157 // t2.printStackTrace();
159 //// parentBarrier.dec(query);
167 // public <T> Object performQuery(ReadGraphImpl parentGraph, final Read<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
169 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
171 // ReadEntry entry = (ReadEntry)entry_;
175 // T result = (T)query.perform(queryGraph);
176 // entry.addOrSet(queryGraph, result);
178 // return (T)entry.get(parentGraph, procedure_);
180 // } catch (Throwable t) {
183 // return (T)entry.get(parentGraph, procedure_);
189 public <T> Object performQuery(ReadGraphImpl parentGraph, final ExternalRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
191 ExternalReadEntry entry = (ExternalReadEntry)entry_;
192 AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
196 query.register(parentGraph, new Listener<T>() {
198 AtomicBoolean used = new AtomicBoolean(false);
201 public void execute(T result) {
204 if(entry.isDiscarded()) return;
206 if(used.compareAndSet(false, true)) {
207 //entry.setPending();
208 entry.addOrSet(parentGraph.processor, result);
209 procedure.execute(parentGraph, result);
212 parentGraph.processor.updatePrimitive(query);
218 public void exception(Throwable t) {
222 if(used.compareAndSet(false, true)) {
223 procedure.exception(parentGraph, t);
225 // entry.queue(result);
226 parentGraph.processor.updatePrimitive(query);
232 public String toString() {
233 return procedure.toString();
237 public boolean isDisposed() {
238 return entry.isDiscarded() || !parentGraph.processor.isBound(entry);
243 return entry.getResult();
245 } catch (Throwable t) {
248 procedure.exception(parentGraph, t);
249 return entry.getResult();
255 public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncMultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
257 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
259 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
260 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
264 query.perform(queryGraph, new AsyncMultiProcedure<T>() {
267 public void execute(AsyncReadGraph 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(AsyncReadGraph graph) {
279 ReadGraphImpl impl = (ReadGraphImpl)graph;
280 entry.finish(parentGraph);
282 procedure.finished(parentGraph);
283 } catch (Throwable t) {
289 public void exception(AsyncReadGraph graph, Throwable t) {
290 ReadGraphImpl impl = (ReadGraphImpl)graph;
291 entry.except(parentGraph, 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 <T> Object performQuery(ReadGraphImpl parentGraph, final MultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
320 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
322 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
323 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
327 query.perform(queryGraph, new AsyncMultiProcedure<T>() {
330 public void execute(AsyncReadGraph graph, T result) {
331 ReadGraphImpl impl = (ReadGraphImpl)graph;
332 entry.addOrSet(result);
334 procedure.execute(parentGraph, result);
335 } catch (Throwable t) {
341 public void finished(AsyncReadGraph graph) {
342 ReadGraphImpl impl = (ReadGraphImpl)graph;
343 entry.finish(parentGraph);
345 procedure.finished(parentGraph);
346 } catch (Throwable t) {
352 public void exception(AsyncReadGraph graph, Throwable t) {
353 ReadGraphImpl impl = (ReadGraphImpl)graph;
354 entry.except(parentGraph, t);
356 procedure.exception(parentGraph, t);
357 } catch (Throwable t2) {
358 t2.printStackTrace();
364 return entry.getResult();
366 } catch (Throwable t) {
370 procedure.exception(parentGraph, t);
371 } catch (Throwable t2) {
372 t2.printStackTrace();
375 return entry.getResult();
381 public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) {
383 if (parent != null && !inferred) {
385 if(!child.isImmutable(graph)) {
386 synchronized(child) {
387 child.addParent(parent);
390 } catch (DatabaseException e) {
391 Logger.defaultLogError(e);
393 if(DebugPolicy.DEPENDENCIES) System.out.println(child + " -> " + parent);
396 if (listener != null) {
397 return registerListener(child, listener, procedure);
404 public synchronized ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) {
406 assert (entry != null);
408 if (base.isDisposed())
411 return addListener(entry, base, procedure);
415 protected void primeListenerEntry(final ListenerEntry entry, final Object result) {
416 entry.setLastKnown(result);
419 private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) {
421 assert (entry != null);
422 assert (procedure != null);
424 ArrayList<ListenerEntry> list = listeners.get(entry);
426 list = new ArrayList<ListenerEntry>(1);
427 listeners.put(entry, list);
430 ListenerEntry result = new ListenerEntry(entry, base, procedure);
431 int currentIndex = list.indexOf(result);
432 // There was already a listener
433 if(currentIndex > -1) {
434 ListenerEntry current = list.get(currentIndex);
435 if(!current.base.isDisposed()) return null;
436 list.set(currentIndex, result);
441 if(DebugPolicy.LISTENER) {
442 new Exception().printStackTrace();
443 System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
451 public Collection<CacheEntry> getRootList() {
453 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
455 for (Object e : valueQueryMap.values()) {
456 result.add((CacheEntry) e);
458 for (Object e : directPredicatesMap.values()) {
459 result.add((CacheEntry) e);
461 for (Object e : directSuperRelationsMap.values()) {
462 result.add((CacheEntry) e);
464 for (Object e : objectsMap.values()) {
465 result.add((CacheEntry) e);
467 for (Object e : directObjectsMap.values()) {
468 result.add((CacheEntry) e);
470 for (Object e : principalTypesMap.values()) {
471 result.add((CacheEntry) e);
473 for (Object e : superRelationsMap.values()) {
474 result.add((CacheEntry) e);
476 for (Object e : superTypesMap.values()) {
477 result.add((CacheEntry) e);
479 for (Object e : typesMap.values()) {
480 result.add((CacheEntry) e);
482 for (Object e : objectsMap.values()) {
483 result.add((CacheEntry) e);
485 for (Object e : assertedStatementsMap.values()) {
486 result.add((CacheEntry) e);
488 for (Object e : readEntryMap.values()) {
489 if(e instanceof CacheEntry) {
490 result.add((CacheEntry) e);
492 System.err.println("e=" + e);
495 for (Object e : asyncReadEntryMap.values()) {
496 if(e instanceof CacheEntry) {
497 result.add((CacheEntry) e);
499 System.err.println("e=" + e);
502 for (Object e : externalReadEntryMap.values()) {
503 result.add((CacheEntry) e);
505 for (Object e : orderedSetMap.values()) {
506 result.add((CacheEntry) e);
513 public int calculateCurrentSize() {
517 realSize += directPredicatesMap.size();
518 realSize += directSuperRelationsMap.size();
519 realSize += principalTypesMap.size();
520 realSize += uRIToResourceMap.size();
521 //realSize += namespaceIndexMap.size();
522 realSize += childMapMap.size();
524 realSize += relationInfoQueryMap.size();
525 realSize += superTypesMap.size();
526 realSize += typeHierarchyMap.size();
527 realSize += superRelationsMap.size();
528 realSize += typesMap.size();
530 realSize += valueQueryMap.size();
531 realSize += directObjectsMap.size();
532 realSize += objectsMap.size();
533 realSize += orderedSetMap.size();
534 realSize += predicatesMap.size();
536 realSize += statementsMap.size();
537 realSize += assertedPredicatesMap.size();
538 realSize += assertedStatementsMap.size();
539 realSize += externalReadEntryMap.size();
540 realSize += asyncReadEntryMap.size();
542 realSize += readEntryMap.size();
543 realSize += asyncMultiReadEntryMap.size();
544 realSize += multiReadEntryMap.size();
550 CacheCollectionResult allCaches(CacheCollectionResult result) {
552 int level = Integer.MAX_VALUE;
553 directPredicatesMap.values(level, result);
554 directSuperRelationsMap.values(level, result);
555 principalTypesMap.values(level, result);
556 for(CacheEntryBase e : uRIToResourceMap.values())
557 if(e.getLevel() <= level)
559 // for(CacheEntryBase e : namespaceIndexMap.values())
560 // if(e.getLevel() <= level)
563 childMapMap.values(level, result);
565 relationInfoQueryMap.values(level, result);
566 superTypesMap.values(level, result);
567 typeHierarchyMap.values(level, result);
568 superRelationsMap.values(level, result);
569 typesMap.values(level, result);
571 valueQueryMap.values(level, result);
572 directObjectsMap.values(level, result);
573 objectsMap.values(level, result);
574 orderedSetMap.values(level, result);
575 predicatesMap.values(level, result);
577 statementsMap.values(level, result);
578 assertedPredicatesMap.values(level, result);
579 assertedStatementsMap.values(level, result);
580 externalReadEntryMap.values(level, result);
581 asyncReadEntryMap.values(level, result);
583 readEntryMap.values(level, result);
584 asyncMultiReadEntryMap.values(level, result);
585 multiReadEntryMap.values(level, result);
591 public void scanPending() {
593 ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
595 entries.addAll(directPredicatesMap.values());
596 entries.addAll(directSuperRelationsMap.values());
597 entries.addAll(principalTypesMap.values());
598 entries.addAll(uRIToResourceMap.values());
599 //entries.addAll(namespaceIndexMap.values());
600 entries.addAll(childMapMap.values());
601 entries.addAll(relationInfoQueryMap.values());
602 entries.addAll(superTypesMap.values());
603 entries.addAll(superRelationsMap.values());
604 entries.addAll(typesMap.values());
605 entries.addAll(valueQueryMap.values());
606 entries.addAll(directObjectsMap.values());
607 entries.addAll(objectsMap.values());
608 entries.addAll(orderedSetMap.values());
609 entries.addAll(predicatesMap.values());
610 entries.addAll(orderedSetMap.values());
611 entries.addAll(statementsMap.values());
612 // entries.addAll(assertedObjectsMap.values());
613 entries.addAll(assertedPredicatesMap.values());
614 entries.addAll(assertedStatementsMap.values());
615 entries.addAll(externalReadEntryMap.values());
616 entries.addAll(asyncReadEntryMap.values());
617 entries.addAll(externalReadEntryMap.values());
618 entries.addAll(readEntryMap.values());
619 entries.addAll(asyncMultiReadEntryMap.values());
620 entries.addAll(multiReadEntryMap.values());
621 entries.addAll(readEntryMap.values());
622 System.out.println(entries.size() + " entries.");
623 for(Object e : entries) {
624 if(e instanceof CacheEntry) {
625 CacheEntry en = (CacheEntry)e;
626 if(en.isPending()) System.out.println("pending " + e);
627 if(en.isExcepted()) System.out.println("excepted " + e);
628 if(en.isDiscarded()) System.out.println("discarded " + e);
629 if(en.isRefuted()) System.out.println("refuted " + e);
630 if(en.isFresh()) System.out.println("fresh " + e);
632 //System.out.println("Unknown object " + e);
637 public static void waitPending(CacheEntry entry) throws DatabaseException {
640 while(entry.isPending()) {
645 CacheEntryBase base = ((CacheEntryBase)entry);
646 // if(base.created != null) {
647 // System.err.println("created:");
648 // base.created.printStackTrace();
650 // if(base.performed != null) {
651 // System.err.println("performed:");
652 // base.performed.printStackTrace();
654 // if(base.ready != null) {
655 // System.err.println("ready:");
656 // base.ready.printStackTrace();
658 new Exception("Timeout waiting for request to complete: " + entry.getOriginalRequest().toString()).printStackTrace();
659 throw new DatabaseException("Timeout waiting for request to complete.");
660 //System.err.println("asd");
661 //base.getQuery().recompute(null, null, entry);
663 } catch (InterruptedException e) {
669 //////////////////////////////////////
671 static public Collection<Objects> entriesObjects(QueryProcessor processor, int r1) {
672 synchronized(processor.cache.objectsMap) {
673 return processor.cache.objectsMap.values(r1);
677 static public Collection<Objects> entriesObjects(QueryProcessor processor) {
678 synchronized(processor.cache.objectsMap) {
679 return processor.cache.objectsMap.values();
683 static public Collection<CacheEntry> entriesDirectPredicates(QueryProcessor processor) {
684 synchronized(processor.cache.directPredicatesMap) {
685 return processor.cache.directPredicatesMap.values();
689 final static Collection<DirectObjects> entriesDirectObjects(final QueryProcessor processor, final int r1) {
690 DoubleKeyQueryHashMap<IntProcedure> hash = processor.cache.directObjectsMap;
691 return hash.values(r1);
694 final static Collection<Statements> entriesStatements(final QueryProcessor processor, final int r1) {
695 return processor.cache.statementsMap.values(r1);
698 final static Types entryTypes(final QueryProcessor processor, final int r) {
699 return (Types)processor.cache.typesMap.get(r);
702 final static PrincipalTypes entryPrincipalTypes(final QueryProcessor processor, final int r) {
703 return (PrincipalTypes)processor.cache.principalTypesMap.get(r);
706 final static OrderedSet entryOrderedSet(final QueryProcessor processor, final int r) {
707 return (OrderedSet)processor.cache.orderedSetMap.get(r);
710 final static ValueQuery entryValueQuery(final QueryProcessor processor, final int r) {
711 return (ValueQuery)processor.cache.valueQueryMap.get(r);
714 final static DirectPredicates entryDirectPredicates(final QueryProcessor processor, final int r) {
715 return (DirectPredicates)processor.cache.directPredicatesMap.get(r);
718 public final static ReadEntry entryRead(final QueryProcessor processor, final Read request) {
719 return (ReadEntry)processor.cache.readEntryMap.get(request);
722 public final static MultiReadEntry entryMultiRead(final QueryProcessor processor, final MultiRead request) {
723 return (MultiReadEntry)processor.cache.multiReadEntryMap.get(request);
726 public final static AsyncReadEntry entryAsyncRead(final QueryProcessor processor, final AsyncRead request) {
727 return (AsyncReadEntry)processor.cache.asyncReadEntryMap.get(request);
730 public final static AsyncMultiReadEntry entryAsyncMultiRead(final QueryProcessor processor, final AsyncMultiRead request) {
731 return (AsyncMultiReadEntry)processor.cache.asyncMultiReadEntryMap.get(request);
734 final protected static long keyR2(long r1, long r2) {
735 long result = (r1<<32) | (r2 & 0xffffffffL);
739 final protected static <T> T id(T o) {
743 final protected static int keyR(int r) {
747 final protected static String keyID(String id) {
751 protected static InternalProcedure<IntSet> emptyIntSetProcedure = new InternalProcedure<IntSet>() {
754 public void execute(ReadGraphImpl graph, IntSet result) {
758 public void exception(ReadGraphImpl graph, Throwable throwable) {
763 protected static InternalProcedure<byte[]> emptyBytesProcedure = new InternalProcedure<byte[]>() {
766 public void execute(ReadGraphImpl graph, byte[] bytes) {
770 public void exception(ReadGraphImpl graph, Throwable throwable) {
775 protected static InternalProcedure<Integer> emptyIntegerProcedure = new InternalProcedure<Integer>() {
778 public void execute(ReadGraphImpl graph, Integer i) {
782 public void exception(ReadGraphImpl graph, Throwable throwable) {
788 protected static InternalProcedure<TObjectIntHashMap<String>> emptyNamespaceProcedure = new InternalProcedure<TObjectIntHashMap<String>>() {
791 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> i) {
795 public void exception(ReadGraphImpl graph, Throwable throwable) {
801 protected static InternalProcedure<RelationInfo> emptyRelationInfoProcedure = new InternalProcedure<RelationInfo>() {
804 public void execute(ReadGraphImpl graph, RelationInfo i) {
808 public void exception(ReadGraphImpl graph, Throwable throwable) {
813 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyChildMapProcedure = new InternalProcedure<ObjectResourceIdMap<String>>() {
816 public void execute(ReadGraphImpl graph, ObjectResourceIdMap<String> i) {
820 public void exception(ReadGraphImpl graph, Throwable throwable) {
827 protected static IntProcedure emptyIntProcedure = new IntProcedure() {
830 public void finished(ReadGraphImpl graph) {
834 public void execute(ReadGraphImpl graph, int i) {
838 public void exception(ReadGraphImpl graph, Throwable throwable) {
842 protected static TripleIntProcedure emptyTripleIntProcedure = new TripleIntProcedure() {
845 public void execute(ReadGraphImpl graph, int s, int p, int o) {
849 public void finished(ReadGraphImpl graph) {
853 public void exception(ReadGraphImpl graph, Throwable throwable) {
858 protected static AsyncProcedure<Object> emptyAsyncProcedure = new AsyncProcedure<Object>() {
861 public void execute(AsyncReadGraph graph, Object result) {
865 public void exception(AsyncReadGraph graph, Throwable throwable) {
870 protected static AsyncMultiProcedure<Object> emptyAsyncMultiProcedure = new AsyncMultiProcedure<Object>() {
873 public void execute(AsyncReadGraph graph, Object result) {
877 public void finished(AsyncReadGraph graph) {
881 public void exception(AsyncReadGraph graph, Throwable throwable) {
887 protected static InternalProcedure<IntSet> emptyProcedureTypes = emptyIntSetProcedure;
888 protected static InternalProcedure<IntSet> emptyProcedureSuperTypes = emptyIntSetProcedure;
889 protected static InternalProcedure<IntSet> emptyProcedureTypeHierarchy = emptyIntSetProcedure;
890 protected static InternalProcedure<IntSet> emptyProcedureSuperRelations = emptyIntSetProcedure;
891 protected static InternalProcedure<IntSet> emptyProcedurePredicates = emptyIntSetProcedure;
892 protected static InternalProcedure<IntSet> emptyProcedureDirectPredicates = emptyIntSetProcedure;
894 protected static IntProcedure emptyProcedureObjects = emptyIntProcedure;
895 protected static IntProcedure emptyProcedureDirectObjects = emptyIntProcedure;
896 protected static IntProcedure emptyProcedurePrincipalTypes = emptyIntProcedure;
897 protected static IntProcedure emptyProcedureDirectSuperRelations = emptyIntProcedure;
898 protected static IntProcedure emptyProcedureAssertedPredicates = emptyIntProcedure;
899 protected static IntProcedure emptyProcedureOrderedSet = emptyIntProcedure;
901 protected static TripleIntProcedure emptyProcedureStatements = emptyTripleIntProcedure;
902 protected static TripleIntProcedure emptyProcedureAssertedStatements = emptyTripleIntProcedure;
904 protected static InternalProcedure<byte[]> emptyProcedureValueQuery = emptyBytesProcedure;
906 protected static InternalProcedure<Integer> emptyProcedureURIToResource = emptyIntegerProcedure;
907 protected static InternalProcedure<TObjectIntHashMap<String>> emptyProcedureNamespaceIndex = emptyNamespaceProcedure;
908 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyProcedureChildMap = emptyChildMapProcedure;
909 protected static InternalProcedure<RelationInfo> emptyProcedureRelationInfoQuery = emptyRelationInfoProcedure;
911 protected static AsyncProcedure emptyProcedureReadEntry = emptyAsyncProcedure;
912 protected static AsyncProcedure emptyProcedureAsyncReadEntry = emptyAsyncProcedure;
913 protected static AsyncMultiProcedure emptyProcedureMultiReadEntry = emptyAsyncMultiProcedure;
914 protected static AsyncMultiProcedure emptyProcedureAsyncMultiReadEntry = emptyAsyncMultiProcedure;
915 protected static AsyncProcedure emptyProcedureExternalReadEntry = emptyAsyncProcedure;
917 static class AsyncProcedureWrapper<T> implements AsyncProcedure<T> {
919 private AsyncProcedure<T> procedure;
920 private T result = null;
921 private Throwable throwable = null;
922 private Semaphore s = new Semaphore(0);
924 AsyncProcedureWrapper(AsyncProcedure<T> procedure) {
925 this.procedure = procedure;
929 public void execute(AsyncReadGraph graph, T result) {
930 if(procedure != null) procedure.execute(graph, result);
931 this.result = result;
936 public void exception(AsyncReadGraph graph, Throwable throwable) {
937 if(procedure != null) procedure.exception(graph, throwable);
938 this.throwable = throwable;
942 public T get() throws DatabaseException {
945 } catch (InterruptedException e) {
948 if(throwable != null) {
949 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
950 else throw new DatabaseException(throwable);
958 static class ExternalProcedureWrapper<T> implements AsyncProcedure<T> {
960 private Procedure<T> procedure;
961 private T result = null;
962 private Throwable throwable = null;
964 ExternalProcedureWrapper(Procedure<T> procedure) {
965 this.procedure = procedure;
969 public void execute(AsyncReadGraph graph, T result) {
970 if(procedure != null) procedure.execute(result);
971 this.result = result;
975 public void exception(AsyncReadGraph graph, Throwable throwable) {
976 if(procedure != null) procedure.exception(throwable);
977 this.throwable = throwable;
980 public T get() throws DatabaseException {
981 if(throwable != null) {
982 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
983 else throw new DatabaseException(throwable);
992 static class InternalProcedureWrapper<T> implements InternalProcedure<T> {
994 private InternalProcedure<T> procedure;
995 private T result = null;
996 private Throwable throwable = null;
998 InternalProcedureWrapper(InternalProcedure<T> procedure) {
999 this.procedure = procedure;
1003 public void execute(ReadGraphImpl graph, T result) throws DatabaseException {
1004 if(procedure != null) procedure.execute(graph, result);
1005 this.result = result;
1009 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1010 if(procedure != null) procedure.exception(graph, throwable);
1011 this.throwable = throwable;
1014 public T get() throws DatabaseException {
1015 if(throwable != null) {
1016 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1017 else throw new DatabaseException(throwable);
1025 static class IntSetWrapper implements IntProcedure {
1027 private IntProcedure procedure;
1028 final private IntSet result;
1029 private Throwable throwable = null;
1031 IntSetWrapper(ReadGraphImpl graph, IntProcedure procedure) {
1032 this.procedure = procedure;
1033 result = new IntSet(graph.processor.querySupport);
1037 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
1038 if(procedure != null) procedure.execute(graph, i);
1043 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1044 if(procedure != null) procedure.exception(graph, throwable);
1045 this.throwable = throwable;
1049 public void finished(ReadGraphImpl graph) throws DatabaseException {
1050 if(procedure != null) procedure.finished(graph);
1053 public IntSet get() throws DatabaseException {
1054 if(throwable != null) {
1055 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1056 else throw new DatabaseException(throwable);
1064 static class TripleIntProcedureWrapper implements TripleIntProcedure {
1066 private TripleIntProcedure procedure;
1067 private IntArray result = new IntArray();
1068 private Throwable throwable = null;
1070 TripleIntProcedureWrapper(TripleIntProcedure procedure) {
1071 this.procedure = procedure;
1075 public void execute(ReadGraphImpl graph, int i1, int i2, int i3) throws DatabaseException {
1076 if(procedure != null) procedure.execute(graph, i1, i2, i3);
1083 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1084 if(procedure != null) procedure.exception(graph, throwable);
1085 this.throwable = throwable;
1089 public void finished(ReadGraphImpl graph) throws DatabaseException {
1090 if(procedure != null) procedure.finished(graph);
1093 public IntArray get() throws DatabaseException {
1094 if(throwable != null) {
1095 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1096 else throw new DatabaseException(throwable);
1104 public static <T> T resultExternalReadEntry(ReadGraphImpl graph, ExternalRead r, CacheEntry parent, ListenerBase listener, Procedure<T> procedure) throws DatabaseException {
1105 ExternalProcedureWrapper<T> wrap = new ExternalProcedureWrapper<>(procedure);
1106 QueryCache.runnerExternalReadEntry(graph, r, parent, listener, wrap);
1110 public static <T> T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1111 AsyncProcedureWrapper<T> wrap = new AsyncProcedureWrapper<>(procedure);
1112 QueryCache.runnerReadEntry(graph, r, parent, listener, wrap, true);
1116 public static <T> T resultAsyncReadEntry(ReadGraphImpl graph, AsyncRead r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1117 AsyncProcedureWrapper<T> wrap = new AsyncProcedureWrapper<>(procedure);
1118 QueryCache.runnerAsyncReadEntry(graph, r, parent, listener, wrap, true);
1122 public static byte[] resultValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1123 InternalProcedureWrapper<byte[]> wrap = new InternalProcedureWrapper<>(null);
1124 QueryCache.runnerValueQuery(graph, r, parent, listener, wrap);
1128 public static RelationInfo resultRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1129 InternalProcedureWrapper<RelationInfo> wrap = new InternalProcedureWrapper<>(null);
1130 QueryCache.runnerRelationInfoQuery(graph, r, parent, listener, wrap);
1134 public static IntSet resultSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1135 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1136 QueryCache.runnerSuperRelations(graph, r, parent, listener, wrap);
1140 public static IntSet resultSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1141 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1142 QueryCache.runnerSuperTypes(graph, r, parent, listener, wrap);
1146 public static IntSet resultTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1147 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1148 QueryCache.runnerTypes(graph, r, parent, listener, wrap);
1152 public static IntSet resultPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1153 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1154 QueryCache.runnerPredicates(graph, r, parent, listener, wrap);
1158 public static IntSet resultDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1159 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1160 QueryCache.runnerDirectPredicates(graph, r, parent, listener, wrap);
1164 public static IntArray resultAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1165 TripleIntProcedureWrapper wrap = new TripleIntProcedureWrapper(null);
1166 QueryCache.runnerAssertedStatements(graph, r1, r2, parent, listener, wrap);
1170 public static Integer resultURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1171 InternalProcedureWrapper<Integer> wrap = new InternalProcedureWrapper<Integer>(null);
1172 QueryCache.runnerURIToResource(graph, id, parent, listener, wrap);
1176 public static ObjectResourceIdMap<String> resultChildMap(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1177 InternalProcedureWrapper<ObjectResourceIdMap<String>> wrap = new InternalProcedureWrapper<ObjectResourceIdMap<String>>(null);
1178 QueryCache.runnerChildMap(graph, r, parent, listener, wrap);
1182 static boolean shouldCache(QueryProcessor processor, int r) {
1183 return processor.isImmutable(r);
1186 static boolean shouldCache(QueryProcessor processor, int r, int r2) {
1187 return processor.isImmutable(r);
1190 static boolean shouldCache(QueryProcessor processor, Object o) {