1 package org.simantics.db.impl.query;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.concurrent.Semaphore;
6 import java.util.concurrent.atomic.AtomicBoolean;
8 import org.simantics.db.AsyncReadGraph;
9 import org.simantics.db.ObjectResourceIdMap;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.RelationInfo;
12 import org.simantics.db.common.utils.Logger;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.impl.DebugPolicy;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.impl.procedure.InternalProcedure;
17 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
18 import org.simantics.db.procedure.AsyncMultiProcedure;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.db.procedure.Listener;
21 import org.simantics.db.procedure.ListenerBase;
22 import org.simantics.db.procedure.Procedure;
23 import org.simantics.db.procedure.SyncMultiProcedure;
24 import org.simantics.db.request.AsyncMultiRead;
25 import org.simantics.db.request.AsyncRead;
26 import org.simantics.db.request.ExternalRead;
27 import org.simantics.db.request.MultiRead;
28 import org.simantics.db.request.Read;
30 import gnu.trove.map.hash.THashMap;
31 import gnu.trove.map.hash.TObjectIntHashMap;
33 public class QueryCacheBase {
37 public final int THREAD_MASK;
43 public volatile boolean dirty = false;
44 public boolean collecting = false;
46 protected final THashMap<String, URIToResource> uRIToResourceMap;
47 //protected final THashMap<String, NamespaceIndex> namespaceIndexMap;
48 protected final UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>> childMapMap;
49 protected final DoubleKeyQueryHashMap<IntProcedure> objectsMap;
50 protected final DoubleKeyQueryHashMap<TripleIntProcedure> assertedStatementsMap;
51 protected final DoubleKeyQueryHashMap<IntProcedure> directObjectsMap;
52 protected final DoubleKeyQueryHashMap<TripleIntProcedure> statementsMap;
53 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> typesMap;
54 protected final UnaryQueryHashMap<IntProcedure> principalTypesMap;
55 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> predicatesMap;
56 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> superTypesMap;
57 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> typeHierarchyMap;
58 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> superRelationsMap;
60 protected final UnaryQueryHashMap<IntProcedure> orderedSetMap;
61 protected final UnaryQueryHashMap<IntProcedure> assertedPredicatesMap;
62 protected final UnaryQueryHashMap<InternalProcedure<IntSet>> directPredicatesMap;
63 protected final UnaryQueryHashMap<IntProcedure> directSuperRelationsMap;
65 protected final UnaryQueryHashMap<InternalProcedure<RelationInfo>> relationInfoQueryMap;
66 protected final UnaryQueryHashMap<InternalProcedure<byte[]>> valueQueryMap;
68 protected final StableHashMap<AsyncRead, AsyncReadEntry> asyncReadEntryMap;
69 protected final StableHashMap<Read, ReadEntry> readEntryMap;
70 protected final StableHashMap<MultiRead, MultiReadEntry> multiReadEntryMap;
71 protected final StableHashMap<AsyncMultiRead, AsyncMultiReadEntry> asyncMultiReadEntryMap;
72 protected final StableHashMap<ExternalRead, ExternalReadEntry> externalReadEntryMap;
74 final THashMap<CacheEntry, ArrayList<ListenerEntry>> listeners;
76 public final QuerySupport querySupport;
78 public QueryCacheBase(QuerySupport querySupport, int threads) {
81 THREAD_MASK = threads - 1;
83 this.querySupport = querySupport;
84 directPredicatesMap = new UnaryQueryHashMap();
85 directSuperRelationsMap = new UnaryQueryHashMap();
86 valueQueryMap = new UnaryQueryHashMap();
87 principalTypesMap = new UnaryQueryHashMap();
88 uRIToResourceMap = new THashMap<String, URIToResource>();
89 //namespaceIndexMap = new THashMap<String, NamespaceIndex>();
90 childMapMap = new UnaryQueryHashMap<InternalProcedure<ObjectResourceIdMap<String>>>();
91 relationInfoQueryMap = new UnaryQueryHashMap();
92 typeHierarchyMap = new UnaryQueryHashMap();
93 superTypesMap = new UnaryQueryHashMap();
94 superRelationsMap = new UnaryQueryHashMap();
95 typesMap = new UnaryQueryHashMap();
96 objectsMap = new DoubleKeyQueryHashMap();
97 orderedSetMap = new UnaryQueryHashMap();
98 predicatesMap = new UnaryQueryHashMap();
99 statementsMap = new DoubleKeyQueryHashMap();
100 directObjectsMap = new DoubleKeyQueryHashMap();
101 assertedPredicatesMap = new UnaryQueryHashMap();
102 assertedStatementsMap = new DoubleKeyQueryHashMap();
103 asyncReadEntryMap = new StableHashMap<AsyncRead, AsyncReadEntry>();
104 readEntryMap = new StableHashMap<Read, ReadEntry>();
105 asyncMultiReadEntryMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>();
106 multiReadEntryMap = new StableHashMap<MultiRead, MultiReadEntry>();
107 externalReadEntryMap = new StableHashMap<ExternalRead, ExternalReadEntry>();
108 listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
111 // public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncRead<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
113 // AsyncReadEntry<T> entry = (AsyncReadEntry<T>)entry_;
114 // AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
116 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
120 // query.perform(queryGraph, new AsyncProcedure<T>() {
123 // public void execute(AsyncReadGraph returnGraph, T result) {
124 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
125 // entry.addOrSet(parentGraph, result);
127 // procedure.execute(parentGraph, result);
128 // } catch (Throwable t) {
129 // t.printStackTrace();
131 //// parentBarrier.dec(query);
135 // public void exception(AsyncReadGraph returnGraph, Throwable t) {
136 // ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
137 //// AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
138 // entry.except(parentGraph, t);
140 // procedure.exception(parentGraph, t);
141 // } catch (Throwable t2) {
142 // t2.printStackTrace();
144 //// parentBarrier.dec(query);
148 // public String toString() {
149 // return procedure.toString();
154 // } catch (Throwable t) {
158 // procedure.exception(parentGraph, t);
159 // } catch (Throwable t2) {
160 // t2.printStackTrace();
162 //// parentBarrier.dec(query);
170 // public <T> Object performQuery(ReadGraphImpl parentGraph, final Read<T> query, final CacheEntryBase entry_, AsyncProcedure procedure_) throws DatabaseException {
172 // ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
174 // ReadEntry entry = (ReadEntry)entry_;
178 // T result = (T)query.perform(queryGraph);
179 // entry.addOrSet(queryGraph, result);
181 // return (T)entry.get(parentGraph, procedure_);
183 // } catch (Throwable t) {
186 // return (T)entry.get(parentGraph, procedure_);
193 public <T> Object performQuery(ReadGraphImpl parentGraph, final AsyncMultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
195 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
197 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
198 AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
202 query.perform(queryGraph, new AsyncMultiProcedure<T>() {
205 public void execute(AsyncReadGraph graph, T result) {
206 ReadGraphImpl impl = (ReadGraphImpl)graph;
207 entry.addOrSet(result);
209 procedure.execute(parentGraph, result);
210 } catch (Throwable t) {
216 public void finished(AsyncReadGraph graph) {
217 ReadGraphImpl impl = (ReadGraphImpl)graph;
218 entry.finish(parentGraph);
220 procedure.finished(parentGraph);
221 } catch (Throwable t) {
227 public void exception(AsyncReadGraph graph, Throwable t) {
228 ReadGraphImpl impl = (ReadGraphImpl)graph;
229 entry.except(parentGraph, t);
231 procedure.exception(parentGraph, t);
232 } catch (Throwable t2) {
233 t2.printStackTrace();
239 return entry.getResult();
241 } catch (Throwable t) {
245 procedure.exception(parentGraph, t);
246 } catch (Throwable t2) {
247 t2.printStackTrace();
250 return entry.getResult();
256 public <T> Object performQuery(ReadGraphImpl parentGraph, final MultiRead<T> query, final CacheEntryBase entry_, Object procedure_) throws DatabaseException {
258 ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
260 MultiReadEntry entry = (MultiReadEntry)entry_;
261 SyncMultiProcedure<T> procedure = (SyncMultiProcedure<T>)procedure_;
265 query.perform(queryGraph, new SyncMultiProcedure<T>() {
268 public void execute(ReadGraph 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(ReadGraph graph) {
280 ReadGraphImpl impl = (ReadGraphImpl)graph;
281 entry.finish(parentGraph);
283 procedure.finished(parentGraph);
284 } catch (Throwable t) {
290 public void exception(ReadGraph graph, Throwable t) {
291 ReadGraphImpl impl = (ReadGraphImpl)graph;
292 entry.except((DatabaseException)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 ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) {
321 if (parent != null && !inferred) {
323 if(!child.isImmutable(graph)) {
324 synchronized(child) {
325 child.addParent(parent);
328 } catch (DatabaseException e) {
329 Logger.defaultLogError(e);
331 if(DebugPolicy.DEPENDENCIES) System.out.println(child + " -> " + parent);
334 if (listener != null) {
335 return registerListener(child, listener, procedure);
342 public synchronized ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) {
344 assert (entry != null);
346 if (base.isDisposed())
349 return addListener(entry, base, procedure);
353 protected void primeListenerEntry(final ListenerEntry entry, final Object result) {
354 entry.setLastKnown(result);
357 private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) {
359 assert (entry != null);
360 assert (procedure != null);
362 ArrayList<ListenerEntry> list = listeners.get(entry);
364 list = new ArrayList<ListenerEntry>(1);
365 listeners.put(entry, list);
368 ListenerEntry result = new ListenerEntry(entry, base, procedure);
369 int currentIndex = list.indexOf(result);
370 // There was already a listener
371 if(currentIndex > -1) {
372 ListenerEntry current = list.get(currentIndex);
373 if(!current.base.isDisposed()) return null;
374 list.set(currentIndex, result);
379 if(DebugPolicy.LISTENER) {
380 new Exception().printStackTrace();
381 System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
389 public Collection<CacheEntry> getRootList() {
391 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
393 for (Object e : valueQueryMap.values()) {
394 result.add((CacheEntry) e);
396 for (Object e : directPredicatesMap.values()) {
397 result.add((CacheEntry) e);
399 for (Object e : directSuperRelationsMap.values()) {
400 result.add((CacheEntry) e);
402 for (Object e : objectsMap.values()) {
403 result.add((CacheEntry) e);
405 for (Object e : directObjectsMap.values()) {
406 result.add((CacheEntry) e);
408 for (Object e : principalTypesMap.values()) {
409 result.add((CacheEntry) e);
411 for (Object e : superRelationsMap.values()) {
412 result.add((CacheEntry) e);
414 for (Object e : superTypesMap.values()) {
415 result.add((CacheEntry) e);
417 for (Object e : typesMap.values()) {
418 result.add((CacheEntry) e);
420 for (Object e : objectsMap.values()) {
421 result.add((CacheEntry) e);
423 for (Object e : assertedStatementsMap.values()) {
424 result.add((CacheEntry) e);
426 for (Object e : readEntryMap.values()) {
427 if(e instanceof CacheEntry) {
428 result.add((CacheEntry) e);
430 System.err.println("e=" + e);
433 for (Object e : asyncReadEntryMap.values()) {
434 if(e instanceof CacheEntry) {
435 result.add((CacheEntry) e);
437 System.err.println("e=" + e);
440 for (Object e : externalReadEntryMap.values()) {
441 result.add((CacheEntry) e);
443 for (Object e : orderedSetMap.values()) {
444 result.add((CacheEntry) e);
451 public int calculateCurrentSize() {
455 realSize += directPredicatesMap.size();
456 realSize += directSuperRelationsMap.size();
457 realSize += principalTypesMap.size();
458 realSize += uRIToResourceMap.size();
459 //realSize += namespaceIndexMap.size();
460 realSize += childMapMap.size();
462 realSize += relationInfoQueryMap.size();
463 realSize += superTypesMap.size();
464 realSize += typeHierarchyMap.size();
465 realSize += superRelationsMap.size();
466 realSize += typesMap.size();
468 realSize += valueQueryMap.size();
469 realSize += directObjectsMap.size();
470 realSize += objectsMap.size();
471 realSize += orderedSetMap.size();
472 realSize += predicatesMap.size();
474 realSize += statementsMap.size();
475 realSize += assertedPredicatesMap.size();
476 realSize += assertedStatementsMap.size();
477 realSize += externalReadEntryMap.size();
478 realSize += asyncReadEntryMap.size();
480 realSize += readEntryMap.size();
481 realSize += asyncMultiReadEntryMap.size();
482 realSize += multiReadEntryMap.size();
488 CacheCollectionResult allCaches(CacheCollectionResult result) {
490 int level = Integer.MAX_VALUE;
491 directPredicatesMap.values(level, result);
492 directSuperRelationsMap.values(level, result);
493 principalTypesMap.values(level, result);
494 for(CacheEntryBase e : uRIToResourceMap.values())
495 if(e.getLevel() <= level)
497 // for(CacheEntryBase e : namespaceIndexMap.values())
498 // if(e.getLevel() <= level)
501 childMapMap.values(level, result);
503 relationInfoQueryMap.values(level, result);
504 superTypesMap.values(level, result);
505 typeHierarchyMap.values(level, result);
506 superRelationsMap.values(level, result);
507 typesMap.values(level, result);
509 valueQueryMap.values(level, result);
510 directObjectsMap.values(level, result);
511 objectsMap.values(level, result);
512 orderedSetMap.values(level, result);
513 predicatesMap.values(level, result);
515 statementsMap.values(level, result);
516 assertedPredicatesMap.values(level, result);
517 assertedStatementsMap.values(level, result);
518 externalReadEntryMap.values(level, result);
519 asyncReadEntryMap.values(level, result);
521 readEntryMap.values(level, result);
522 asyncMultiReadEntryMap.values(level, result);
523 multiReadEntryMap.values(level, result);
529 public void scanPending() {
531 ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
533 entries.addAll(directPredicatesMap.values());
534 entries.addAll(directSuperRelationsMap.values());
535 entries.addAll(principalTypesMap.values());
536 entries.addAll(uRIToResourceMap.values());
537 //entries.addAll(namespaceIndexMap.values());
538 entries.addAll(childMapMap.values());
539 entries.addAll(relationInfoQueryMap.values());
540 entries.addAll(superTypesMap.values());
541 entries.addAll(superRelationsMap.values());
542 entries.addAll(typesMap.values());
543 entries.addAll(valueQueryMap.values());
544 entries.addAll(directObjectsMap.values());
545 entries.addAll(objectsMap.values());
546 entries.addAll(orderedSetMap.values());
547 entries.addAll(predicatesMap.values());
548 entries.addAll(orderedSetMap.values());
549 entries.addAll(statementsMap.values());
550 // entries.addAll(assertedObjectsMap.values());
551 entries.addAll(assertedPredicatesMap.values());
552 entries.addAll(assertedStatementsMap.values());
553 entries.addAll(externalReadEntryMap.values());
554 entries.addAll(asyncReadEntryMap.values());
555 entries.addAll(externalReadEntryMap.values());
556 entries.addAll(readEntryMap.values());
557 entries.addAll(asyncMultiReadEntryMap.values());
558 entries.addAll(multiReadEntryMap.values());
559 entries.addAll(readEntryMap.values());
560 System.out.println(entries.size() + " entries.");
561 for(Object e : entries) {
562 if(e instanceof CacheEntry) {
563 CacheEntry en = (CacheEntry)e;
564 if(en.isPending()) System.out.println("pending " + e);
565 if(en.isExcepted()) System.out.println("excepted " + e);
566 if(en.isDiscarded()) System.out.println("discarded " + e);
567 if(en.isRefuted()) System.out.println("refuted " + e);
568 if(en.isFresh()) System.out.println("fresh " + e);
570 //System.out.println("Unknown object " + e);
575 public static void waitPending(ReadGraphImpl graph, CacheEntry entry) throws DatabaseException {
578 while(entry.isPending()) {
580 boolean performed = graph.performPending();
584 if(counter > 30000) {
585 CacheEntryBase base = ((CacheEntryBase)entry);
586 // if(base.created != null) {
587 // System.err.println("created:");
588 // base.created.printStackTrace();
590 // if(base.performed != null) {
591 // System.err.println("performed:");
592 // base.performed.printStackTrace();
594 // if(base.ready != null) {
595 // System.err.println("ready:");
596 // base.ready.printStackTrace();
598 new Exception("Timeout waiting for request to complete: " + entry.getOriginalRequest()).printStackTrace();
599 throw new DatabaseException("Timeout waiting for request to complete." + entry.getOriginalRequest());
600 //System.err.println("asd");
601 //base.getQuery().recompute(null, null, entry);
604 } catch (InterruptedException e) {
610 //////////////////////////////////////
612 public static Collection<Objects> entriesObjects(QueryProcessor processor, int r1) {
613 synchronized(processor.cache.objectsMap) {
614 return processor.cache.objectsMap.values(r1);
618 public static Collection<Objects> entriesObjects(QueryProcessor processor) {
619 synchronized(processor.cache.objectsMap) {
620 return processor.cache.objectsMap.values();
624 public static Collection<CacheEntry> entriesDirectPredicates(QueryProcessor processor) {
625 synchronized(processor.cache.directPredicatesMap) {
626 return processor.cache.directPredicatesMap.values();
630 static final Collection<DirectObjects> entriesDirectObjects(final QueryProcessor processor, final int r1) {
631 DoubleKeyQueryHashMap<IntProcedure> hash = processor.cache.directObjectsMap;
632 return hash.values(r1);
635 static final Collection<Statements> entriesStatements(final QueryProcessor processor, final int r1) {
636 return processor.cache.statementsMap.values(r1);
639 static final Types entryTypes(final QueryProcessor processor, final int r) {
640 return (Types)processor.cache.typesMap.get(r);
643 static final PrincipalTypes entryPrincipalTypes(final QueryProcessor processor, final int r) {
644 return (PrincipalTypes)processor.cache.principalTypesMap.get(r);
647 static final OrderedSet entryOrderedSet(final QueryProcessor processor, final int r) {
648 return (OrderedSet)processor.cache.orderedSetMap.get(r);
651 static final ValueQuery entryValueQuery(final QueryProcessor processor, final int r) {
652 return (ValueQuery)processor.cache.valueQueryMap.get(r);
655 static final DirectPredicates entryDirectPredicates(final QueryProcessor processor, final int r) {
656 return (DirectPredicates)processor.cache.directPredicatesMap.get(r);
659 public static final ReadEntry entryRead(final QueryProcessor processor, final Read request) {
660 return (ReadEntry)processor.cache.readEntryMap.get(request);
663 public static final MultiReadEntry entryMultiRead(final QueryProcessor processor, final MultiRead request) {
664 return (MultiReadEntry)processor.cache.multiReadEntryMap.get(request);
667 public static final AsyncReadEntry entryAsyncRead(final QueryProcessor processor, final AsyncRead request) {
668 return (AsyncReadEntry)processor.cache.asyncReadEntryMap.get(request);
671 public static final AsyncMultiReadEntry entryAsyncMultiRead(final QueryProcessor processor, final AsyncMultiRead request) {
672 return (AsyncMultiReadEntry)processor.cache.asyncMultiReadEntryMap.get(request);
675 protected static final long keyR2(long r1, long r2) {
676 long result = (r1<<32) | (r2 & 0xffffffffL);
680 protected static final <T> T id(T o) {
684 protected static final int keyR(int r) {
688 protected static final String keyID(String id) {
692 protected static InternalProcedure<IntSet> emptyIntSetProcedure = new InternalProcedure<IntSet>() {
695 public void execute(ReadGraphImpl graph, IntSet result) {
699 public void exception(ReadGraphImpl graph, Throwable throwable) {
704 protected static InternalProcedure<byte[]> emptyBytesProcedure = new InternalProcedure<byte[]>() {
707 public void execute(ReadGraphImpl graph, byte[] bytes) {
711 public void exception(ReadGraphImpl graph, Throwable throwable) {
716 protected static InternalProcedure<Integer> emptyIntegerProcedure = new InternalProcedure<Integer>() {
719 public void execute(ReadGraphImpl graph, Integer i) {
723 public void exception(ReadGraphImpl graph, Throwable throwable) {
729 protected static InternalProcedure<TObjectIntHashMap<String>> emptyNamespaceProcedure = new InternalProcedure<TObjectIntHashMap<String>>() {
732 public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> i) {
736 public void exception(ReadGraphImpl graph, Throwable throwable) {
742 protected static InternalProcedure<RelationInfo> emptyRelationInfoProcedure = new InternalProcedure<RelationInfo>() {
745 public void execute(ReadGraphImpl graph, RelationInfo i) {
749 public void exception(ReadGraphImpl graph, Throwable throwable) {
754 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyChildMapProcedure = new InternalProcedure<ObjectResourceIdMap<String>>() {
757 public void execute(ReadGraphImpl graph, ObjectResourceIdMap<String> i) {
761 public void exception(ReadGraphImpl graph, Throwable throwable) {
766 protected static IntProcedure emptyIntProcedure = new IntProcedure() {
769 public void finished(ReadGraphImpl graph) {
773 public void execute(ReadGraphImpl graph, int i) {
777 public void exception(ReadGraphImpl graph, Throwable throwable) {
781 protected static TripleIntProcedure emptyTripleIntProcedure = new TripleIntProcedure() {
784 public void execute(ReadGraphImpl graph, int s, int p, int o) {
788 public void finished(ReadGraphImpl graph) {
792 public void exception(ReadGraphImpl graph, Throwable throwable) {
797 protected static AsyncProcedure<Object> emptyAsyncProcedure = new AsyncProcedure<Object>() {
800 public void execute(AsyncReadGraph graph, Object result) {
804 public void exception(AsyncReadGraph graph, Throwable throwable) {
809 protected static AsyncMultiProcedure<Object> emptyAsyncMultiProcedure = new AsyncMultiProcedure<Object>() {
812 public void execute(AsyncReadGraph graph, Object result) {
816 public void finished(AsyncReadGraph graph) {
820 public void exception(AsyncReadGraph graph, Throwable throwable) {
825 protected static SyncMultiProcedure<Object> emptySyncMultiProcedure = new SyncMultiProcedure<Object>() {
828 public void execute(ReadGraph graph, Object result) {
832 public void finished(ReadGraph graph) {
836 public void exception(ReadGraph graph, Throwable throwable) {
841 protected static InternalProcedure<IntSet> emptyProcedureTypes = emptyIntSetProcedure;
842 protected static InternalProcedure<IntSet> emptyProcedureSuperTypes = emptyIntSetProcedure;
843 protected static InternalProcedure<IntSet> emptyProcedureTypeHierarchy = emptyIntSetProcedure;
844 protected static InternalProcedure<IntSet> emptyProcedureSuperRelations = emptyIntSetProcedure;
845 protected static InternalProcedure<IntSet> emptyProcedurePredicates = emptyIntSetProcedure;
846 protected static InternalProcedure<IntSet> emptyProcedureDirectPredicates = emptyIntSetProcedure;
848 protected static IntProcedure emptyProcedureObjects = emptyIntProcedure;
849 protected static IntProcedure emptyProcedureDirectObjects = emptyIntProcedure;
850 protected static IntProcedure emptyProcedurePrincipalTypes = emptyIntProcedure;
851 protected static IntProcedure emptyProcedureDirectSuperRelations = emptyIntProcedure;
852 protected static IntProcedure emptyProcedureAssertedPredicates = emptyIntProcedure;
853 protected static IntProcedure emptyProcedureOrderedSet = emptyIntProcedure;
855 protected static TripleIntProcedure emptyProcedureStatements = emptyTripleIntProcedure;
856 protected static TripleIntProcedure emptyProcedureAssertedStatements = emptyTripleIntProcedure;
858 protected static InternalProcedure<byte[]> emptyProcedureValueQuery = emptyBytesProcedure;
860 protected static InternalProcedure<Integer> emptyProcedureURIToResource = emptyIntegerProcedure;
861 protected static InternalProcedure<TObjectIntHashMap<String>> emptyProcedureNamespaceIndex = emptyNamespaceProcedure;
862 protected static InternalProcedure<ObjectResourceIdMap<String>> emptyProcedureChildMap = emptyChildMapProcedure;
863 protected static InternalProcedure<RelationInfo> emptyProcedureRelationInfoQuery = emptyRelationInfoProcedure;
865 protected static AsyncProcedure emptyProcedureReadEntry = emptyAsyncProcedure;
866 protected static AsyncProcedure emptyProcedureAsyncReadEntry = emptyAsyncProcedure;
867 protected static SyncMultiProcedure emptyProcedureMultiReadEntry = emptySyncMultiProcedure;
868 protected static AsyncMultiProcedure emptyProcedureAsyncMultiReadEntry = emptyAsyncMultiProcedure;
869 protected static AsyncProcedure emptyProcedureExternalReadEntry = emptyAsyncProcedure;
871 static class AsyncProcedureWrapper<T> implements AsyncProcedure<T> {
873 private AsyncProcedure<T> procedure;
874 private T result = null;
875 private Throwable throwable = null;
876 private Semaphore s = new Semaphore(0);
878 AsyncProcedureWrapper(AsyncProcedure<T> procedure) {
879 this.procedure = procedure;
883 public void execute(AsyncReadGraph graph, T result) {
884 if(procedure != null) procedure.execute(graph, result);
885 this.result = result;
890 public void exception(AsyncReadGraph graph, Throwable throwable) {
891 if(procedure != null) procedure.exception(graph, throwable);
892 this.throwable = throwable;
896 public T get() throws DatabaseException {
899 } catch (InterruptedException e) {
902 if(throwable != null) {
903 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
904 else throw new DatabaseException(throwable);
912 static class ExternalProcedureWrapper<T> implements AsyncProcedure<T> {
914 private Procedure<T> procedure;
915 private T result = null;
916 private Throwable throwable = null;
918 ExternalProcedureWrapper(Procedure<T> procedure) {
919 this.procedure = procedure;
923 public void execute(AsyncReadGraph graph, T result) {
924 if(procedure != null) procedure.execute(result);
925 this.result = result;
929 public void exception(AsyncReadGraph graph, Throwable throwable) {
930 if(procedure != null) procedure.exception(throwable);
931 this.throwable = throwable;
934 public T get() throws DatabaseException {
935 if(throwable != null) {
936 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
937 else throw new DatabaseException(throwable);
946 static class InternalProcedureWrapper<T> implements InternalProcedure<T> {
948 private InternalProcedure<T> procedure;
949 private T result = null;
950 private Throwable throwable = null;
952 InternalProcedureWrapper(InternalProcedure<T> procedure) {
953 this.procedure = procedure;
957 public void execute(ReadGraphImpl graph, T result) throws DatabaseException {
958 if(procedure != null) procedure.execute(graph, result);
959 this.result = result;
963 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
964 if(procedure != null) procedure.exception(graph, throwable);
965 this.throwable = throwable;
968 public T get() throws DatabaseException {
969 if(throwable != null) {
970 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
971 else throw new DatabaseException(throwable);
979 static class IntSetWrapper implements IntProcedure {
981 private IntProcedure procedure;
982 private final IntSet result;
983 private Throwable throwable = null;
985 IntSetWrapper(ReadGraphImpl graph, IntProcedure procedure) {
986 this.procedure = procedure;
987 result = new IntSet(graph.processor.querySupport);
991 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
992 if(procedure != null) procedure.execute(graph, i);
997 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
998 if(procedure != null) procedure.exception(graph, throwable);
999 this.throwable = throwable;
1003 public void finished(ReadGraphImpl graph) throws DatabaseException {
1004 if(procedure != null) procedure.finished(graph);
1007 public IntSet get() throws DatabaseException {
1008 if(throwable != null) {
1009 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1010 else throw new DatabaseException(throwable);
1018 static class TripleIntProcedureWrapper implements TripleIntProcedure {
1020 private TripleIntProcedure procedure;
1021 private IntArray result = new IntArray();
1022 private Throwable throwable = null;
1024 TripleIntProcedureWrapper(TripleIntProcedure procedure) {
1025 this.procedure = procedure;
1029 public void execute(ReadGraphImpl graph, int i1, int i2, int i3) throws DatabaseException {
1030 if(procedure != null) procedure.execute(graph, i1, i2, i3);
1037 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
1038 if(procedure != null) procedure.exception(graph, throwable);
1039 this.throwable = throwable;
1043 public void finished(ReadGraphImpl graph) throws DatabaseException {
1044 if(procedure != null) procedure.finished(graph);
1047 public IntArray get() throws DatabaseException {
1048 if(throwable != null) {
1049 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
1050 else throw new DatabaseException(throwable);
1058 public static <T> T resultExternalReadEntry(ReadGraphImpl graph, ExternalRead r, CacheEntry parent, ListenerBase listener, Procedure<T> procedure) throws DatabaseException {
1059 ExternalProcedureWrapper<T> wrap = new ExternalProcedureWrapper<>(procedure);
1060 QueryCache.runnerExternalReadEntry(graph, r, parent, listener, wrap);
1064 public static <T> T resultReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1065 return (T)QueryCache.runnerReadEntry(graph, r, parent, listener, procedure, true);
1068 public static <T> T resultAsyncReadEntry(ReadGraphImpl graph, AsyncRead r, CacheEntry parent, ListenerBase listener, AsyncProcedure<T> procedure) throws DatabaseException {
1069 return (T)QueryCache.runnerAsyncReadEntry(graph, r, parent, listener, procedure, true);
1072 public static byte[] resultValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1073 InternalProcedureWrapper<byte[]> wrap = new InternalProcedureWrapper<>(null);
1074 QueryCache.runnerValueQuery(graph, r, parent, listener, wrap);
1078 public static RelationInfo resultRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1079 InternalProcedureWrapper<RelationInfo> wrap = new InternalProcedureWrapper<>(null);
1080 QueryCache.runnerRelationInfoQuery(graph, r, parent, listener, wrap);
1084 public static IntSet resultSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1085 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1086 QueryCache.runnerSuperRelations(graph, r, parent, listener, wrap);
1090 public static IntSet resultSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1091 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1092 QueryCache.runnerSuperTypes(graph, r, parent, listener, wrap);
1096 public static IntSet resultTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1097 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1098 QueryCache.runnerTypes(graph, r, parent, listener, wrap);
1102 public static IntSet resultPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1103 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1104 QueryCache.runnerPredicates(graph, r, parent, listener, wrap);
1108 public static IntSet resultDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1109 InternalProcedureWrapper<IntSet> wrap = new InternalProcedureWrapper<>(null);
1110 QueryCache.runnerDirectPredicates(graph, r, parent, listener, wrap);
1114 public static IntArray resultAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1115 TripleIntProcedureWrapper wrap = new TripleIntProcedureWrapper(null);
1116 QueryCache.runnerAssertedStatements(graph, r1, r2, parent, listener, wrap);
1120 public static Integer resultURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1121 InternalProcedureWrapper<Integer> wrap = new InternalProcedureWrapper<Integer>(null);
1122 QueryCache.runnerURIToResource(graph, id, parent, listener, wrap);
1126 public static ObjectResourceIdMap<String> resultChildMap(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener) throws DatabaseException {
1127 InternalProcedureWrapper<ObjectResourceIdMap<String>> wrap = new InternalProcedureWrapper<ObjectResourceIdMap<String>>(null);
1128 QueryCache.runnerChildMap(graph, r, parent, listener, wrap);
1132 static boolean shouldCache(QueryProcessor processor, int r) {
1133 return processor.isImmutable(r);
1136 static boolean shouldCache(QueryProcessor processor, int r, int r2) {
1137 return processor.isImmutable(r);
1140 static boolean shouldCache(QueryProcessor processor, Object o) {