1 package org.simantics.db.impl.query;
3 import org.simantics.db.ObjectResourceIdMap;
4 import org.simantics.db.RelationInfo;
5 import org.simantics.db.common.utils.Logger;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.db.impl.graph.ReadGraphImpl;
8 import org.simantics.db.impl.procedure.InternalProcedure;
9 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
10 import org.simantics.db.procedure.AsyncMultiProcedure;
11 import org.simantics.db.procedure.AsyncProcedure;
12 import org.simantics.db.procedure.ListenerBase;
13 import org.simantics.db.request.AsyncMultiRead;
14 import org.simantics.db.request.AsyncRead;
15 import org.simantics.db.request.ExternalRead;
16 import org.simantics.db.request.MultiRead;
17 import org.simantics.db.request.Read;
19 public class QueryCache extends QueryCacheBase {
21 public QueryCache(QuerySupport querySupport, int threads) {
22 super(querySupport, threads);
25 Objects getOrCreateObjects(QueryProcessor processor, int r1, int r2) throws DatabaseException {
26 Objects existing = null;
27 synchronized(objectsMap) {
28 existing = (Objects)objectsMap.get(r1,r2);
29 if(existing == null) {
30 existing = new Objects(r1,r2);
31 existing.clearResult(querySupport);
32 existing.setPending();
33 objectsMap.put(keyR2(r1,r2), existing);
37 if(existing.requiresComputation()) {
38 existing.setPending();
42 if(existing.isPending()) waitPending(processor, existing);
46 void remove(Objects entry) {
47 synchronized(objectsMap) {
48 objectsMap.remove(entry.id);
52 public static void runnerObjects(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
53 QueryCache cache = graph.processor.cache;
54 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r1,r2)) {
55 Objects.computeForEach(graph, r1,r2, null, procedure);
58 Objects entry = (Objects)cache.getOrCreateObjects(graph.processor, r1,r2);
59 IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureObjects;
60 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
61 if(entry.isReady()) entry.performFromCache(graph, procedure_);
63 assert(entry.isPending());
64 Objects.computeForEach(graph, r1,r2, entry, procedure_);
65 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
69 Statements getOrCreateStatements(QueryProcessor processor, int r1, int r2) throws DatabaseException {
70 Statements existing = null;
71 synchronized(statementsMap) {
72 existing = (Statements)statementsMap.get(r1,r2);
73 if(existing == null) {
74 existing = new Statements(r1,r2);
75 existing.clearResult(querySupport);
76 existing.setPending();
77 statementsMap.put(keyR2(r1,r2), existing);
81 if(existing.requiresComputation()) {
82 existing.setPending();
86 if(existing.isPending()) waitPending(processor, existing);
90 void remove(Statements entry) {
91 synchronized(statementsMap) {
92 statementsMap.remove(entry.id);
96 public static void runnerStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException {
97 QueryCache cache = graph.processor.cache;
98 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r1,r2)) {
99 Statements.computeForEach(graph, r1,r2, null, procedure);
102 Statements entry = (Statements)cache.getOrCreateStatements(graph.processor, r1,r2);
103 TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureStatements;
104 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
105 if(entry.isReady()) entry.performFromCache(graph, procedure_);
107 assert(entry.isPending());
108 Statements.computeForEach(graph, r1,r2, entry, procedure_);
109 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
113 DirectObjects getOrCreateDirectObjects(QueryProcessor processor, int r1, int r2) throws DatabaseException {
114 DirectObjects existing = null;
115 synchronized(directObjectsMap) {
116 existing = (DirectObjects)directObjectsMap.get(r1,r2);
117 if(existing == null) {
118 existing = new DirectObjects(r1,r2);
119 existing.clearResult(querySupport);
120 existing.setPending();
121 directObjectsMap.put(keyR2(r1,r2), existing);
125 if(existing.requiresComputation()) {
126 existing.setPending();
130 if(existing.isPending()) waitPending(processor, existing);
134 void remove(DirectObjects entry) {
135 synchronized(directObjectsMap) {
136 directObjectsMap.remove(entry.id);
140 public static void runnerDirectObjects(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
141 QueryCache cache = graph.processor.cache;
142 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r1,r2)) {
143 DirectObjects.computeForEach(graph, r1,r2, null, procedure);
146 DirectObjects entry = (DirectObjects)cache.getOrCreateDirectObjects(graph.processor, r1,r2);
147 IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectObjects;
148 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
149 if(entry.isReady()) entry.performFromCache(graph, procedure_);
151 assert(entry.isPending());
152 DirectObjects.computeForEach(graph, r1,r2, entry, procedure_);
153 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
157 RelationInfoQuery getOrCreateRelationInfoQuery(QueryProcessor processor, int r) throws DatabaseException {
158 RelationInfoQuery existing = null;
159 synchronized(relationInfoQueryMap) {
160 existing = (RelationInfoQuery)relationInfoQueryMap.get(r);
161 if(existing == null) {
162 existing = new RelationInfoQuery(r);
163 existing.clearResult(querySupport);
164 existing.setPending();
165 relationInfoQueryMap.put(keyR(r), existing);
169 if(existing.requiresComputation()) {
170 existing.setPending();
174 if(existing.isPending()) waitPending(processor, existing);
178 void remove(RelationInfoQuery entry) {
179 synchronized(relationInfoQueryMap) {
180 relationInfoQueryMap.remove(entry.id);
184 public static void runnerRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<RelationInfo> procedure) throws DatabaseException {
185 QueryCache cache = graph.processor.cache;
186 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
187 RelationInfoQuery.computeForEach(graph, r, null, procedure);
190 RelationInfoQuery entry = (RelationInfoQuery)cache.getOrCreateRelationInfoQuery(graph.processor, r);
191 InternalProcedure<RelationInfo> procedure_ = procedure != null ? procedure : emptyProcedureRelationInfoQuery;
192 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
193 if(entry.isReady()) entry.performFromCache(graph, procedure_);
195 assert(entry.isPending());
196 RelationInfoQuery.computeForEach(graph, r, entry, procedure_);
197 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
201 URIToResource getOrCreateURIToResource(QueryProcessor processor, String id) throws DatabaseException {
202 URIToResource existing = null;
203 synchronized(uRIToResourceMap) {
204 existing = (URIToResource)uRIToResourceMap.get(id);
205 if(existing == null) {
206 existing = new URIToResource(id);
207 existing.clearResult(querySupport);
208 existing.setPending();
209 uRIToResourceMap.put(keyID(id), existing);
213 if(existing.requiresComputation()) {
214 existing.setPending();
218 if(existing.isPending()) waitPending(processor, existing);
222 void remove(URIToResource entry) {
223 synchronized(uRIToResourceMap) {
224 uRIToResourceMap.remove(entry.id);
228 public static void runnerURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener, final InternalProcedure<Integer> procedure) throws DatabaseException {
229 QueryCache cache = graph.processor.cache;
230 if(parent == null && listener == null && !cache.shouldCache(graph.processor, id)) {
231 URIToResource.computeForEach(graph, id, null, procedure);
234 URIToResource entry = (URIToResource)cache.getOrCreateURIToResource(graph.processor, id);
235 InternalProcedure<Integer> procedure_ = procedure != null ? procedure : emptyProcedureURIToResource;
236 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
237 if(entry.isReady()) entry.performFromCache(graph, procedure_);
239 assert(entry.isPending());
240 URIToResource.computeForEach(graph, id, entry, procedure_);
241 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
245 ValueQuery getOrCreateValueQuery(QueryProcessor processor, int r) throws DatabaseException {
246 ValueQuery existing = null;
247 synchronized(valueQueryMap) {
248 existing = (ValueQuery)valueQueryMap.get(r);
249 if(existing == null) {
250 existing = new ValueQuery(r);
251 existing.clearResult(querySupport);
252 existing.setPending();
253 valueQueryMap.put(keyR(r), existing);
257 if(existing.requiresComputation()) {
258 existing.setPending();
262 if(existing.isPending()) waitPending(processor, existing);
266 void remove(ValueQuery entry) {
267 synchronized(valueQueryMap) {
268 valueQueryMap.remove(entry.id);
272 public static void runnerValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<byte[]> procedure) throws DatabaseException {
273 QueryCache cache = graph.processor.cache;
274 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
275 ValueQuery.computeForEach(graph, r, null, procedure);
278 ValueQuery entry = (ValueQuery)cache.getOrCreateValueQuery(graph.processor, r);
279 InternalProcedure<byte[]> procedure_ = procedure != null ? procedure : emptyProcedureValueQuery;
280 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
281 if(entry.isReady()) entry.performFromCache(graph, procedure_);
283 assert(entry.isPending());
284 ValueQuery.computeForEach(graph, r, entry, procedure_);
285 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
289 OrderedSet getOrCreateOrderedSet(QueryProcessor processor, int r) throws DatabaseException {
290 OrderedSet existing = null;
291 synchronized(orderedSetMap) {
292 existing = (OrderedSet)orderedSetMap.get(r);
293 if(existing == null) {
294 existing = new OrderedSet(r);
295 existing.clearResult(querySupport);
296 existing.setPending();
297 orderedSetMap.put(keyR(r), existing);
301 if(existing.requiresComputation()) {
302 existing.setPending();
306 if(existing.isPending()) waitPending(processor, existing);
310 void remove(OrderedSet entry) {
311 synchronized(orderedSetMap) {
312 orderedSetMap.remove(entry.id);
316 public static void runnerOrderedSet(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
317 QueryCache cache = graph.processor.cache;
318 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
319 OrderedSet.computeForEach(graph, r, null, procedure);
322 OrderedSet entry = (OrderedSet)cache.getOrCreateOrderedSet(graph.processor, r);
323 IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureOrderedSet;
324 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
325 if(entry.isReady()) entry.performFromCache(graph, procedure_);
327 assert(entry.isPending());
328 OrderedSet.computeForEach(graph, r, entry, procedure_);
329 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
333 PrincipalTypes getOrCreatePrincipalTypes(QueryProcessor processor, int r) throws DatabaseException {
334 PrincipalTypes existing = null;
335 synchronized(principalTypesMap) {
336 existing = (PrincipalTypes)principalTypesMap.get(r);
337 if(existing == null) {
338 existing = new PrincipalTypes(r);
339 existing.clearResult(querySupport);
340 existing.setPending();
341 principalTypesMap.put(keyR(r), existing);
345 if(existing.requiresComputation()) {
346 existing.setPending();
350 if(existing.isPending()) waitPending(processor, existing);
354 void remove(PrincipalTypes entry) {
355 synchronized(principalTypesMap) {
356 principalTypesMap.remove(entry.id);
360 public static void runnerPrincipalTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
361 QueryCache cache = graph.processor.cache;
362 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
363 PrincipalTypes.computeForEach(graph, r, null, procedure);
366 PrincipalTypes entry = (PrincipalTypes)cache.getOrCreatePrincipalTypes(graph.processor, r);
367 IntProcedure procedure_ = procedure != null ? procedure : emptyProcedurePrincipalTypes;
368 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
369 if(entry.isReady()) entry.performFromCache(graph, procedure_);
371 assert(entry.isPending());
372 PrincipalTypes.computeForEach(graph, r, entry, procedure_);
373 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
377 DirectPredicates getOrCreateDirectPredicates(QueryProcessor processor, int r) throws DatabaseException {
378 DirectPredicates existing = null;
379 synchronized(directPredicatesMap) {
380 existing = (DirectPredicates)directPredicatesMap.get(r);
381 if(existing == null) {
382 existing = new DirectPredicates(r);
383 existing.clearResult(querySupport);
384 existing.setPending();
385 directPredicatesMap.put(keyR(r), existing);
389 if(existing.requiresComputation()) {
390 existing.setPending();
394 if(existing.isPending()) waitPending(processor, existing);
398 void remove(DirectPredicates entry) {
399 synchronized(directPredicatesMap) {
400 directPredicatesMap.remove(entry.id);
404 public static void runnerDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
405 QueryCache cache = graph.processor.cache;
406 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
407 DirectPredicates.computeForEach(graph, r, null, procedure);
410 DirectPredicates entry = (DirectPredicates)cache.getOrCreateDirectPredicates(graph.processor, r);
411 InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureDirectPredicates;
412 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
413 if(entry.isReady()) entry.performFromCache(graph, procedure_);
415 assert(entry.isPending());
416 DirectPredicates.computeForEach(graph, r, entry, procedure_);
417 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
421 Predicates getOrCreatePredicates(QueryProcessor processor, int r) throws DatabaseException {
422 Predicates existing = null;
423 synchronized(predicatesMap) {
424 existing = (Predicates)predicatesMap.get(r);
425 if(existing == null) {
426 existing = new Predicates(r);
427 existing.clearResult(querySupport);
428 existing.setPending();
429 predicatesMap.put(keyR(r), existing);
433 if(existing.requiresComputation()) {
434 existing.setPending();
438 if(existing.isPending()) waitPending(processor, existing);
442 void remove(Predicates entry) {
443 synchronized(predicatesMap) {
444 predicatesMap.remove(entry.id);
448 public static void runnerPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
449 QueryCache cache = graph.processor.cache;
450 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
451 Predicates.computeForEach(graph, r, null, procedure);
454 Predicates entry = (Predicates)cache.getOrCreatePredicates(graph.processor, r);
455 InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedurePredicates;
456 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
457 if(entry.isReady()) entry.performFromCache(graph, procedure_);
459 assert(entry.isPending());
460 Predicates.computeForEach(graph, r, entry, procedure_);
461 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
465 ReadEntry getOrCreateReadEntry(QueryProcessor processor, Read<?> r, boolean isSync) throws DatabaseException {
466 ReadEntry existing = null;
467 synchronized(readEntryMap) {
468 existing = (ReadEntry)readEntryMap.get(r);
469 if(existing == null) {
470 existing = new ReadEntry(r);
471 existing.clearResult(querySupport);
472 existing.setPending();
473 readEntryMap.put(id(r), existing);
477 if(existing.requiresComputation()) {
478 existing.setPending();
482 if(existing.isPending()) {
483 if(isSync) waitPending(processor, existing);
489 void remove(ReadEntry entry) {
490 synchronized(readEntryMap) {
491 readEntryMap.remove(entry.request);
495 public static void runnerReadEntry(ReadGraphImpl graph, Read<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, boolean isSync) throws DatabaseException {
496 QueryCache cache = graph.processor.cache;
497 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
498 ReadEntry.computeForEach(graph, r, null, procedure);
501 ReadEntry entry = (ReadEntry)cache.getOrCreateReadEntry(graph.processor, r, isSync);
503 graph.processor.schedule(new SessionTask(false) {
505 public void run(int thread) {
508 runnerReadEntry(graph, r, parent, listener, procedure, isSync);
509 } catch (DatabaseException e) {
510 Logger.defaultLogError(e);
516 AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureReadEntry;
517 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
518 if(entry.isReady()) entry.performFromCache(graph, procedure_);
520 assert(entry.isPending());
521 ReadEntry.computeForEach(graph, r, entry, procedure_);
522 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
526 AsyncReadEntry getOrCreateAsyncReadEntry(QueryProcessor processor, AsyncRead<?> r, boolean isSync) throws DatabaseException {
527 AsyncReadEntry existing = null;
528 synchronized(asyncReadEntryMap) {
529 existing = (AsyncReadEntry)asyncReadEntryMap.get(r);
530 if(existing == null) {
531 existing = new AsyncReadEntry(r);
532 existing.clearResult(querySupport);
533 existing.setPending();
534 asyncReadEntryMap.put(id(r), existing);
538 if(existing.requiresComputation()) {
539 existing.setPending();
543 if(existing.isPending()) {
544 if(isSync) waitPending(processor, existing);
550 void remove(AsyncReadEntry entry) {
551 synchronized(asyncReadEntryMap) {
552 asyncReadEntryMap.remove(entry.request);
556 public static void runnerAsyncReadEntry(ReadGraphImpl graph, AsyncRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, boolean isSync) throws DatabaseException {
557 QueryCache cache = graph.processor.cache;
558 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
559 AsyncReadEntry.computeForEach(graph, r, null, procedure);
562 AsyncReadEntry entry = (AsyncReadEntry)cache.getOrCreateAsyncReadEntry(graph.processor, r, isSync);
564 graph.processor.schedule(new SessionTask(false) {
566 public void run(int thread) {
569 runnerAsyncReadEntry(graph, r, parent, listener, procedure, isSync);
570 } catch (DatabaseException e) {
571 Logger.defaultLogError(e);
577 AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureAsyncReadEntry;
578 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
579 if(entry.isReady()) entry.performFromCache(graph, procedure_);
581 assert(entry.isPending());
582 AsyncReadEntry.computeForEach(graph, r, entry, procedure_);
583 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
587 Types getOrCreateTypes(QueryProcessor processor, int r) throws DatabaseException {
588 Types existing = null;
589 synchronized(typesMap) {
590 existing = (Types)typesMap.get(r);
591 if(existing == null) {
592 existing = new Types(r);
593 existing.clearResult(querySupport);
594 existing.setPending();
595 typesMap.put(keyR(r), existing);
599 if(existing.requiresComputation()) {
600 existing.setPending();
604 if(existing.isPending()) waitPending(processor, existing);
608 void remove(Types entry) {
609 synchronized(typesMap) {
610 typesMap.remove(entry.id);
614 public static void runnerTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
615 QueryCache cache = graph.processor.cache;
616 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
617 Types.computeForEach(graph, r, null, procedure);
620 Types entry = (Types)cache.getOrCreateTypes(graph.processor, r);
621 InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureTypes;
622 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
623 if(entry.isReady()) entry.performFromCache(graph, procedure_);
625 assert(entry.isPending());
626 Types.computeForEach(graph, r, entry, procedure_);
627 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
631 ChildMap getOrCreateChildMap(QueryProcessor processor, int r) throws DatabaseException {
632 ChildMap existing = null;
633 synchronized(childMapMap) {
634 existing = (ChildMap)childMapMap.get(r);
635 if(existing == null) {
636 existing = new ChildMap(r);
637 existing.clearResult(querySupport);
638 existing.setPending();
639 childMapMap.put(keyR(r), existing);
643 if(existing.requiresComputation()) {
644 existing.setPending();
648 if(existing.isPending()) waitPending(processor, existing);
652 void remove(ChildMap entry) {
653 synchronized(childMapMap) {
654 childMapMap.remove(entry.id);
658 public static void runnerChildMap(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
659 QueryCache cache = graph.processor.cache;
660 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
661 ChildMap.computeForEach(graph, r, null, procedure);
664 ChildMap entry = (ChildMap)cache.getOrCreateChildMap(graph.processor, r);
665 InternalProcedure<ObjectResourceIdMap<String>> procedure_ = procedure != null ? procedure : emptyProcedureChildMap;
666 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
667 if(entry.isReady()) entry.performFromCache(graph, procedure_);
669 assert(entry.isPending());
670 ChildMap.computeForEach(graph, r, entry, procedure_);
671 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
675 TypeHierarchy getOrCreateTypeHierarchy(QueryProcessor processor, int r) throws DatabaseException {
676 TypeHierarchy existing = null;
677 synchronized(typeHierarchyMap) {
678 existing = (TypeHierarchy)typeHierarchyMap.get(r);
679 if(existing == null) {
680 existing = new TypeHierarchy(r);
681 existing.clearResult(querySupport);
682 existing.setPending();
683 typeHierarchyMap.put(keyR(r), existing);
687 if(existing.requiresComputation()) {
688 existing.setPending();
692 if(existing.isPending()) waitPending(processor, existing);
696 void remove(TypeHierarchy entry) {
697 synchronized(typeHierarchyMap) {
698 typeHierarchyMap.remove(entry.id);
702 public static void runnerTypeHierarchy(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
703 QueryCache cache = graph.processor.cache;
704 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
705 TypeHierarchy.computeForEach(graph, r, null, procedure);
708 TypeHierarchy entry = (TypeHierarchy)cache.getOrCreateTypeHierarchy(graph.processor, r);
709 InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureTypeHierarchy;
710 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
711 if(entry.isReady()) entry.performFromCache(graph, procedure_);
713 assert(entry.isPending());
714 TypeHierarchy.computeForEach(graph, r, entry, procedure_);
715 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
719 SuperTypes getOrCreateSuperTypes(QueryProcessor processor, int r) throws DatabaseException {
720 SuperTypes existing = null;
721 synchronized(superTypesMap) {
722 existing = (SuperTypes)superTypesMap.get(r);
723 if(existing == null) {
724 existing = new SuperTypes(r);
725 existing.clearResult(querySupport);
726 existing.setPending();
727 superTypesMap.put(keyR(r), existing);
731 if(existing.requiresComputation()) {
732 existing.setPending();
736 if(existing.isPending()) waitPending(processor, existing);
740 void remove(SuperTypes entry) {
741 synchronized(superTypesMap) {
742 superTypesMap.remove(entry.id);
746 public static void runnerSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
747 QueryCache cache = graph.processor.cache;
748 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
749 SuperTypes.computeForEach(graph, r, null, procedure);
752 SuperTypes entry = (SuperTypes)cache.getOrCreateSuperTypes(graph.processor, r);
753 InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureSuperTypes;
754 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
755 if(entry.isReady()) entry.performFromCache(graph, procedure_);
757 assert(entry.isPending());
758 SuperTypes.computeForEach(graph, r, entry, procedure_);
759 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
763 SuperRelations getOrCreateSuperRelations(QueryProcessor processor, int r) throws DatabaseException {
764 SuperRelations existing = null;
765 synchronized(superRelationsMap) {
766 existing = (SuperRelations)superRelationsMap.get(r);
767 if(existing == null) {
768 existing = new SuperRelations(r);
769 existing.clearResult(querySupport);
770 existing.setPending();
771 superRelationsMap.put(keyR(r), existing);
775 if(existing.requiresComputation()) {
776 existing.setPending();
780 if(existing.isPending()) waitPending(processor, existing);
784 void remove(SuperRelations entry) {
785 synchronized(superRelationsMap) {
786 superRelationsMap.remove(entry.id);
790 public static void runnerSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
791 QueryCache cache = graph.processor.cache;
792 if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
793 SuperRelations.computeForEach(graph, r, null, procedure);
796 SuperRelations entry = (SuperRelations)cache.getOrCreateSuperRelations(graph.processor, r);
797 InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureSuperRelations;
798 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
799 if(entry.isReady()) entry.performFromCache(graph, procedure_);
801 assert(entry.isPending());
802 SuperRelations.computeForEach(graph, r, entry, procedure_);
803 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
807 AssertedPredicates getOrCreateAssertedPredicates(QueryProcessor processor, int r) throws DatabaseException {
808 AssertedPredicates existing = null;
809 synchronized(assertedPredicatesMap) {
810 existing = (AssertedPredicates)assertedPredicatesMap.get(r);
811 if(existing == null) {
812 existing = new AssertedPredicates(r);
813 existing.clearResult(querySupport);
814 existing.setPending();
815 assertedPredicatesMap.put(keyR(r), existing);
819 if(existing.requiresComputation()) {
820 existing.setPending();
824 if(existing.isPending()) waitPending(processor, existing);
828 void remove(AssertedPredicates entry) {
829 synchronized(assertedPredicatesMap) {
830 assertedPredicatesMap.remove(entry.id);
834 public static void runnerAssertedPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
835 QueryCache cache = graph.processor.cache;
836 AssertedPredicates entry = (AssertedPredicates)cache.getOrCreateAssertedPredicates(graph.processor, r);
837 IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedPredicates;
838 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
839 if(entry.isReady()) entry.performFromCache(graph, procedure_);
841 assert(entry.isPending());
842 entry.compute(graph, procedure_);
843 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
847 AssertedStatements getOrCreateAssertedStatements(QueryProcessor processor, int r1, int r2) throws DatabaseException {
848 AssertedStatements existing = null;
849 synchronized(assertedStatementsMap) {
850 existing = (AssertedStatements)assertedStatementsMap.get(r1,r2);
851 if(existing == null) {
852 existing = new AssertedStatements(r1,r2);
853 existing.clearResult(querySupport);
854 existing.setPending();
855 assertedStatementsMap.put(keyR2(r1,r2), existing);
859 if(existing.requiresComputation()) {
860 existing.setPending();
864 if(existing.isPending()) waitPending(processor, existing);
868 void remove(AssertedStatements entry) {
869 synchronized(assertedStatementsMap) {
870 assertedStatementsMap.remove(entry.id);
874 public static void runnerAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException {
875 QueryCache cache = graph.processor.cache;
876 AssertedStatements entry = (AssertedStatements)cache.getOrCreateAssertedStatements(graph.processor, r1,r2);
877 TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedStatements;
878 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
879 if(entry.isReady()) entry.performFromCache(graph, procedure_);
881 assert(entry.isPending());
882 entry.compute(graph, procedure_);
883 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
887 DirectSuperRelations getOrCreateDirectSuperRelations(QueryProcessor processor, int r) throws DatabaseException {
888 DirectSuperRelations existing = null;
889 synchronized(directSuperRelationsMap) {
890 existing = (DirectSuperRelations)directSuperRelationsMap.get(r);
891 if(existing == null) {
892 existing = new DirectSuperRelations(r);
893 existing.clearResult(querySupport);
894 existing.setPending();
895 directSuperRelationsMap.put(keyR(r), existing);
899 if(existing.requiresComputation()) {
900 existing.setPending();
904 if(existing.isPending()) waitPending(processor, existing);
908 void remove(DirectSuperRelations entry) {
909 synchronized(directSuperRelationsMap) {
910 directSuperRelationsMap.remove(entry.id);
914 public static void runnerDirectSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException {
915 QueryCache cache = graph.processor.cache;
916 DirectSuperRelations entry = (DirectSuperRelations)cache.getOrCreateDirectSuperRelations(graph.processor, r);
917 IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectSuperRelations;
918 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
919 if(entry.isReady()) entry.performFromCache(graph, procedure_);
921 assert(entry.isPending());
922 entry.compute(graph, procedure_);
923 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
927 MultiReadEntry getOrCreateMultiReadEntry(QueryProcessor processor, MultiRead<?> r) throws DatabaseException {
928 MultiReadEntry existing = null;
929 synchronized(multiReadEntryMap) {
930 existing = (MultiReadEntry)multiReadEntryMap.get(r);
931 if(existing == null) {
932 existing = new MultiReadEntry(r);
933 existing.clearResult(querySupport);
934 existing.setPending();
935 multiReadEntryMap.put(id(r), existing);
939 if(existing.requiresComputation()) {
940 existing.setPending();
944 if(existing.isPending()) waitPending(processor, existing);
948 void remove(MultiReadEntry entry) {
949 synchronized(multiReadEntryMap) {
950 multiReadEntryMap.remove(entry.request);
954 public static void runnerMultiReadEntry(ReadGraphImpl graph, MultiRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncMultiProcedure procedure) throws DatabaseException {
955 QueryCache cache = graph.processor.cache;
956 MultiReadEntry entry = (MultiReadEntry)cache.getOrCreateMultiReadEntry(graph.processor, r);
957 AsyncMultiProcedure procedure_ = procedure != null ? procedure : emptyProcedureMultiReadEntry;
958 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
959 if(entry.isReady()) entry.performFromCache(graph, procedure_);
961 assert(entry.isPending());
962 entry.compute(graph, procedure_);
963 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
967 AsyncMultiReadEntry getOrCreateAsyncMultiReadEntry(QueryProcessor processor, AsyncMultiRead<?> r) throws DatabaseException {
968 AsyncMultiReadEntry existing = null;
969 synchronized(asyncMultiReadEntryMap) {
970 existing = (AsyncMultiReadEntry)asyncMultiReadEntryMap.get(r);
971 if(existing == null) {
972 existing = new AsyncMultiReadEntry(r);
973 existing.clearResult(querySupport);
974 existing.setPending();
975 asyncMultiReadEntryMap.put(id(r), existing);
979 if(existing.requiresComputation()) {
980 existing.setPending();
984 if(existing.isPending()) waitPending(processor, existing);
988 void remove(AsyncMultiReadEntry entry) {
989 synchronized(asyncMultiReadEntryMap) {
990 asyncMultiReadEntryMap.remove(entry.request);
994 public static void runnerAsyncMultiReadEntry(ReadGraphImpl graph, AsyncMultiRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncMultiProcedure procedure) throws DatabaseException {
995 QueryCache cache = graph.processor.cache;
996 AsyncMultiReadEntry entry = (AsyncMultiReadEntry)cache.getOrCreateAsyncMultiReadEntry(graph.processor, r);
997 AsyncMultiProcedure procedure_ = procedure != null ? procedure : emptyProcedureAsyncMultiReadEntry;
998 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
999 if(entry.isReady()) entry.performFromCache(graph, procedure_);
1001 assert(entry.isPending());
1002 entry.compute(graph, procedure_);
1003 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
1007 ExternalReadEntry getOrCreateExternalReadEntry(QueryProcessor processor, ExternalRead<?> r) throws DatabaseException {
1008 ExternalReadEntry existing = null;
1009 synchronized(externalReadEntryMap) {
1010 existing = (ExternalReadEntry)externalReadEntryMap.get(r);
1011 if(existing == null) {
1012 existing = new ExternalReadEntry(r);
1013 existing.clearResult(querySupport);
1014 existing.setPending();
1015 externalReadEntryMap.put(id(r), existing);
1019 if(existing.requiresComputation()) {
1020 existing.setPending();
1024 if(existing.isPending()) waitPending(processor, existing);
1028 void remove(ExternalReadEntry entry) {
1029 synchronized(externalReadEntryMap) {
1030 externalReadEntryMap.remove(entry.request);
1034 public static void runnerExternalReadEntry(ReadGraphImpl graph, ExternalRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure) throws DatabaseException {
1035 QueryCache cache = graph.processor.cache;
1036 ExternalReadEntry entry = (ExternalReadEntry)cache.getOrCreateExternalReadEntry(graph.processor, r);
1037 AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureExternalReadEntry;
1038 ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
1039 if(entry.isReady()) entry.performFromCache(graph, procedure_);
1041 assert(entry.isPending());
1042 entry.compute(graph, procedure_);
1043 if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());