package org.simantics.db.impl.query; import org.simantics.db.ObjectResourceIdMap; import org.simantics.db.RelationInfo; import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.DatabaseException; import org.simantics.db.impl.graph.ReadGraphImpl; import org.simantics.db.impl.procedure.InternalProcedure; import org.simantics.db.impl.query.QueryProcessor.SessionTask; import org.simantics.db.procedure.AsyncMultiProcedure; import org.simantics.db.procedure.AsyncProcedure; import org.simantics.db.procedure.ListenerBase; import org.simantics.db.procedure.SyncMultiProcedure; import org.simantics.db.request.AsyncMultiRead; import org.simantics.db.request.AsyncRead; import org.simantics.db.request.ExternalRead; import org.simantics.db.request.MultiRead; import org.simantics.db.request.Read; public class QueryCache extends QueryCacheBase { private static final boolean SINGLE = true; public QueryCache(QuerySupport querySupport, int threads) { super(querySupport, threads); } Objects getOrCreateObjects(ReadGraphImpl graph, int r1, int r2) throws DatabaseException { Objects existing = null; synchronized(objectsMap) { existing = (Objects)objectsMap.get(r1,r2); if(existing == null) { existing = new Objects(r1,r2); existing.setPending(querySupport); objectsMap.put(keyR2(r1,r2), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(Objects entry) { synchronized(objectsMap) { objectsMap.remove(entry.id); } } public static void runnerObjects(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r1,r2)) { if (SINGLE) { Objects e = cache.peekObjects(r1,r2); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } Objects.computeForEach(graph, r1,r2, null, procedure); return; } Objects entry = (Objects)cache.getOrCreateObjects(graph, r1,r2); IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureObjects; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); Objects.computeForEach(graph, r1,r2, entry, procedure_); } } private Objects peekObjects(int r1, int r2) { synchronized(objectsMap) { return (Objects) objectsMap.get(r1,r2); } } Statements getOrCreateStatements(ReadGraphImpl graph, int r1, int r2) throws DatabaseException { Statements existing = null; synchronized(statementsMap) { existing = (Statements)statementsMap.get(r1,r2); if(existing == null) { existing = new Statements(r1,r2); existing.setPending(querySupport); statementsMap.put(keyR2(r1,r2), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(Statements entry) { synchronized(statementsMap) { statementsMap.remove(entry.id); } } public static void runnerStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r1,r2)) { if (SINGLE) { Statements e = cache.peekStatements(r1,r2); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } Statements.computeForEach(graph, r1,r2, null, procedure); return; } Statements entry = (Statements)cache.getOrCreateStatements(graph, r1,r2); TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureStatements; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); Statements.computeForEach(graph, r1,r2, entry, procedure_); } } private Statements peekStatements(int r1, int r2) { synchronized(statementsMap) { return (Statements) statementsMap.get(r1,r2); } } DirectObjects getOrCreateDirectObjects(ReadGraphImpl graph, int r1, int r2) throws DatabaseException { DirectObjects existing = null; synchronized(directObjectsMap) { existing = (DirectObjects)directObjectsMap.get(r1,r2); if(existing == null) { existing = new DirectObjects(r1,r2); existing.setPending(querySupport); directObjectsMap.put(keyR2(r1,r2), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(DirectObjects entry) { synchronized(directObjectsMap) { directObjectsMap.remove(entry.id); } } public static void runnerDirectObjects(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r1,r2)) { if (SINGLE) { DirectObjects e = cache.peekDirectObjects(r1,r2); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } DirectObjects.computeForEach(graph, r1,r2, null, procedure); return; } DirectObjects entry = (DirectObjects)cache.getOrCreateDirectObjects(graph, r1,r2); IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectObjects; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); DirectObjects.computeForEach(graph, r1,r2, entry, procedure_); } } private DirectObjects peekDirectObjects(int r1, int r2) { synchronized(directObjectsMap) { return (DirectObjects) directObjectsMap.get(r1,r2); } } RelationInfoQuery getOrCreateRelationInfoQuery(ReadGraphImpl graph, int r) throws DatabaseException { RelationInfoQuery existing = null; synchronized(relationInfoQueryMap) { existing = (RelationInfoQuery)relationInfoQueryMap.get(r); if(existing == null) { existing = new RelationInfoQuery(r); existing.setPending(querySupport); relationInfoQueryMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(RelationInfoQuery entry) { synchronized(relationInfoQueryMap) { relationInfoQueryMap.remove(entry.id); } } public static void runnerRelationInfoQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { RelationInfoQuery e = cache.peekRelationInfoQuery(r); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } RelationInfoQuery.computeForEach(graph, r, null, procedure); return; } RelationInfoQuery entry = (RelationInfoQuery)cache.getOrCreateRelationInfoQuery(graph, r); InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureRelationInfoQuery; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); RelationInfoQuery.computeForEach(graph, r, entry, procedure_); } } private RelationInfoQuery peekRelationInfoQuery(int r) { synchronized(relationInfoQueryMap) { return (RelationInfoQuery) relationInfoQueryMap.get(r); } } URIToResource getOrCreateURIToResource(ReadGraphImpl graph, String id) throws DatabaseException { URIToResource existing = null; synchronized(uRIToResourceMap) { existing = (URIToResource)uRIToResourceMap.get(id); if(existing == null) { existing = new URIToResource(id); existing.setPending(querySupport); uRIToResourceMap.put(keyID(id), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(URIToResource entry) { synchronized(uRIToResourceMap) { uRIToResourceMap.remove(entry.id); } } public static void runnerURIToResource(ReadGraphImpl graph, String id, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, id)) { if (SINGLE) { URIToResource e = cache.peekURIToResource(id); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } URIToResource.computeForEach(graph, id, null, procedure); return; } URIToResource entry = (URIToResource)cache.getOrCreateURIToResource(graph, id); InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureURIToResource; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); URIToResource.computeForEach(graph, id, entry, procedure_); } } private URIToResource peekURIToResource(String id) { synchronized(uRIToResourceMap) { return (URIToResource) uRIToResourceMap.get(id); } } ValueQuery getOrCreateValueQuery(ReadGraphImpl graph, int r) throws DatabaseException { ValueQuery existing = null; synchronized(valueQueryMap) { existing = (ValueQuery)valueQueryMap.get(r); if(existing == null) { existing = new ValueQuery(r); existing.setPending(querySupport); valueQueryMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(ValueQuery entry) { synchronized(valueQueryMap) { valueQueryMap.remove(entry.id); } } public static void runnerValueQuery(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { ValueQuery e = cache.peekValueQuery(r); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } ValueQuery.computeForEach(graph, r, null, procedure); return; } ValueQuery entry = (ValueQuery)cache.getOrCreateValueQuery(graph, r); InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureValueQuery; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); ValueQuery.computeForEach(graph, r, entry, procedure_); } } private ValueQuery peekValueQuery(int r) { synchronized(valueQueryMap) { return (ValueQuery) valueQueryMap.get(r); } } OrderedSet getOrCreateOrderedSet(ReadGraphImpl graph, int r) throws DatabaseException { OrderedSet existing = null; synchronized(orderedSetMap) { existing = (OrderedSet)orderedSetMap.get(r); if(existing == null) { existing = new OrderedSet(r); existing.setPending(querySupport); orderedSetMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(OrderedSet entry) { synchronized(orderedSetMap) { orderedSetMap.remove(entry.id); } } public static void runnerOrderedSet(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { OrderedSet e = cache.peekOrderedSet(r); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } OrderedSet.computeForEach(graph, r, null, procedure); return; } OrderedSet entry = (OrderedSet)cache.getOrCreateOrderedSet(graph, r); IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureOrderedSet; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); OrderedSet.computeForEach(graph, r, entry, procedure_); } } private OrderedSet peekOrderedSet(int r) { synchronized(orderedSetMap) { return (OrderedSet) orderedSetMap.get(r); } } PrincipalTypes getOrCreatePrincipalTypes(ReadGraphImpl graph, int r) throws DatabaseException { PrincipalTypes existing = null; synchronized(principalTypesMap) { existing = (PrincipalTypes)principalTypesMap.get(r); if(existing == null) { existing = new PrincipalTypes(r); existing.setPending(querySupport); principalTypesMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(PrincipalTypes entry) { synchronized(principalTypesMap) { principalTypesMap.remove(entry.id); } } public static void runnerPrincipalTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { PrincipalTypes e = cache.peekPrincipalTypes(r); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } PrincipalTypes.computeForEach(graph, r, null, procedure); return; } PrincipalTypes entry = (PrincipalTypes)cache.getOrCreatePrincipalTypes(graph, r); IntProcedure procedure_ = procedure != null ? procedure : emptyProcedurePrincipalTypes; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); PrincipalTypes.computeForEach(graph, r, entry, procedure_); } } private PrincipalTypes peekPrincipalTypes(int r) { synchronized(principalTypesMap) { return (PrincipalTypes) principalTypesMap.get(r); } } DirectPredicates getOrCreateDirectPredicates(ReadGraphImpl graph, int r) throws DatabaseException { DirectPredicates existing = null; synchronized(directPredicatesMap) { existing = (DirectPredicates)directPredicatesMap.get(r); if(existing == null) { existing = new DirectPredicates(r); existing.setPending(querySupport); directPredicatesMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(DirectPredicates entry) { synchronized(directPredicatesMap) { directPredicatesMap.remove(entry.id); } } public static void runnerDirectPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { DirectPredicates e = cache.peekDirectPredicates(r); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } DirectPredicates.computeForEach(graph, r, null, procedure); return; } DirectPredicates entry = (DirectPredicates)cache.getOrCreateDirectPredicates(graph, r); InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectPredicates; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); DirectPredicates.computeForEach(graph, r, entry, procedure_); } } private DirectPredicates peekDirectPredicates(int r) { synchronized(directPredicatesMap) { return (DirectPredicates) directPredicatesMap.get(r); } } Predicates getOrCreatePredicates(ReadGraphImpl graph, int r) throws DatabaseException { Predicates existing = null; synchronized(predicatesMap) { existing = (Predicates)predicatesMap.get(r); if(existing == null) { existing = new Predicates(r); existing.setPending(querySupport); predicatesMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(Predicates entry) { synchronized(predicatesMap) { predicatesMap.remove(entry.id); } } public static void runnerPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { Predicates e = cache.peekPredicates(r); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } Predicates.computeForEach(graph, r, null, procedure); return; } Predicates entry = (Predicates)cache.getOrCreatePredicates(graph, r); InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedurePredicates; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); Predicates.computeForEach(graph, r, entry, procedure_); } } private Predicates peekPredicates(int r) { synchronized(predicatesMap) { return (Predicates) predicatesMap.get(r); } } private final ReadEntry getOrCreateReadEntry(ReadGraphImpl graph, Read r, boolean needsToBlock) throws DatabaseException { ReadEntry existing = null; synchronized(readEntryMap) { existing = (ReadEntry)readEntryMap.get(r); if(existing == null) { existing = new ReadEntry(r); existing.setPending(querySupport); readEntryMap.put(id(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { if(needsToBlock) waitPending(graph, existing); else { return null; } } return existing; } void remove(ReadEntry entry) { synchronized(readEntryMap) { readEntryMap.remove(entry.id); } } public static Object runnerReadEntry(ReadGraphImpl graph, Read r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, boolean needsToBlock) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { ReadEntry e = cache.peekReadEntry(r); if (e != null && e.isReady()) { return e.performFromCache(graph, procedure); } } return ReadEntry.computeForEach(graph, r, null, procedure, needsToBlock); } ReadEntry entry = (ReadEntry)cache.getOrCreateReadEntry(graph, r, needsToBlock); if(entry == null) { graph.asyncBarrier.inc(); graph.processor.scheduleNow(new SessionTask() { @Override public void run0(int thread) { try { runnerReadEntry(graph, r, parent, listener, procedure, needsToBlock); graph.asyncBarrier.dec(); } catch (DatabaseException e) { Logger.defaultLogError(e); } } }); return null; } AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureReadEntry; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); Object result = entry.performFromCache(graph, procedure_); graph.processor.listening.registerFirstKnown(listener, result); return result; } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); Object result = ReadEntry.computeForEach(graph, r, entry, procedure_, needsToBlock); graph.processor.listening.registerFirstKnown(listener, result); return result; } } private ReadEntry peekReadEntry(Read r) { synchronized(readEntryMap) { return (ReadEntry) readEntryMap.get(r); } } AsyncReadEntry getOrCreateAsyncReadEntry(ReadGraphImpl graph, AsyncRead r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, boolean needsToBlock) throws DatabaseException { AsyncReadEntry existing = null; synchronized(asyncReadEntryMap) { existing = (AsyncReadEntry)asyncReadEntryMap.get(r); if(existing == null) { existing = new AsyncReadEntry(r); existing.setPending(querySupport); asyncReadEntryMap.put(id(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { if(needsToBlock) waitPending(graph, existing); else { existing.executeWhenResultIsAvailable(graph.processor, new SessionTask(graph) { @Override public void run0(int thread) { try { runnerAsyncReadEntry(graph, r, parent, listener, procedure, needsToBlock); } catch (DatabaseException e) { Logger.defaultLogError(e); } } }); return null; } } return existing; } void remove(AsyncReadEntry entry) { synchronized(asyncReadEntryMap) { asyncReadEntryMap.remove(entry.id); } } public static Object runnerAsyncReadEntry(ReadGraphImpl graph, AsyncRead r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, boolean needsToBlock) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { AsyncReadEntry e = cache.peekAsyncReadEntry(r); if (e != null && e.isReady()) { return e.performFromCache(graph, procedure); } } return AsyncReadEntry.computeForEach(graph, r, null, procedure, needsToBlock); } AsyncReadEntry entry = (AsyncReadEntry)cache.getOrCreateAsyncReadEntry(graph, r, parent, listener, procedure, needsToBlock); if(entry == null) { // Entry was pending and this request has been queued return null; } AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureAsyncReadEntry; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); Object result = entry.performFromCache(graph, procedure_); graph.processor.listening.registerFirstKnown(listener, result); return result; } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); Object result = AsyncReadEntry.computeForEach(graph, r, entry, procedure_, needsToBlock); graph.processor.listening.registerFirstKnown(listener, result); return result; } } private AsyncReadEntry peekAsyncReadEntry(AsyncRead r) { synchronized(asyncReadEntryMap) { return (AsyncReadEntry) asyncReadEntryMap.get(r); } } Types getOrCreateTypes(ReadGraphImpl graph, int r) throws DatabaseException { Types existing = null; synchronized(typesMap) { existing = (Types)typesMap.get(r); if(existing == null) { existing = new Types(r); existing.setPending(querySupport); typesMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(Types entry) { synchronized(typesMap) { typesMap.remove(entry.id); } } public static void runnerTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { Types e = cache.peekTypes(r); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } Types.computeForEach(graph, r, null, procedure); return; } Types entry = (Types)cache.getOrCreateTypes(graph, r); InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureTypes; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); Types.computeForEach(graph, r, entry, procedure_); } } private Types peekTypes(int r) { synchronized(typesMap) { return (Types) typesMap.get(r); } } ChildMap getOrCreateChildMap(ReadGraphImpl graph, int r) throws DatabaseException { ChildMap existing = null; synchronized(childMapMap) { existing = (ChildMap)childMapMap.get(r); if(existing == null) { existing = new ChildMap(r); existing.setPending(querySupport); childMapMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(ChildMap entry) { synchronized(childMapMap) { childMapMap.remove(entry.id); } } public static void runnerChildMap(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure> procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { ChildMap e = cache.peekChildMap(r); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } ChildMap.computeForEach(graph, r, null, procedure); return; } ChildMap entry = (ChildMap)cache.getOrCreateChildMap(graph, r); InternalProcedure> procedure_ = procedure != null ? procedure : emptyProcedureChildMap; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); ChildMap.computeForEach(graph, r, entry, procedure_); } } private ChildMap peekChildMap(int r) { synchronized(childMapMap) { return (ChildMap) childMapMap.get(r); } } TypeHierarchy getOrCreateTypeHierarchy(ReadGraphImpl graph, int r) throws DatabaseException { TypeHierarchy existing = null; synchronized(typeHierarchyMap) { existing = (TypeHierarchy)typeHierarchyMap.get(r); if(existing == null) { existing = new TypeHierarchy(r); existing.setPending(querySupport); typeHierarchyMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(TypeHierarchy entry) { synchronized(typeHierarchyMap) { typeHierarchyMap.remove(entry.id); } } public static void runnerTypeHierarchy(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { TypeHierarchy e = cache.peekTypeHierarchy(r); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } TypeHierarchy.computeForEach(graph, r, null, procedure); return; } TypeHierarchy entry = (TypeHierarchy)cache.getOrCreateTypeHierarchy(graph, r); InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureTypeHierarchy; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); TypeHierarchy.computeForEach(graph, r, entry, procedure_); } } private TypeHierarchy peekTypeHierarchy(int r) { synchronized(typeHierarchyMap) { return (TypeHierarchy) typeHierarchyMap.get(r); } } SuperTypes getOrCreateSuperTypes(ReadGraphImpl graph, int r) throws DatabaseException { SuperTypes existing = null; synchronized(superTypesMap) { existing = (SuperTypes)superTypesMap.get(r); if(existing == null) { existing = new SuperTypes(r); existing.setPending(querySupport); superTypesMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(SuperTypes entry) { synchronized(superTypesMap) { superTypesMap.remove(entry.id); } } public static void runnerSuperTypes(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { SuperTypes e = cache.peekSuperTypes(r); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } SuperTypes.computeForEach(graph, r, null, procedure); return; } SuperTypes entry = (SuperTypes)cache.getOrCreateSuperTypes(graph, r); InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureSuperTypes; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); SuperTypes.computeForEach(graph, r, entry, procedure_); } } private SuperTypes peekSuperTypes(int r) { synchronized(superTypesMap) { return (SuperTypes) superTypesMap.get(r); } } SuperRelations getOrCreateSuperRelations(ReadGraphImpl graph, int r) throws DatabaseException { SuperRelations existing = null; synchronized(superRelationsMap) { existing = (SuperRelations)superRelationsMap.get(r); if(existing == null) { existing = new SuperRelations(r); existing.setPending(querySupport); superRelationsMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(SuperRelations entry) { synchronized(superRelationsMap) { superRelationsMap.remove(entry.id); } } public static void runnerSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final InternalProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) { if (SINGLE) { SuperRelations e = cache.peekSuperRelations(r); if (e != null && e.isReady()) { e.performFromCache(graph, procedure); return; } } SuperRelations.computeForEach(graph, r, null, procedure); return; } SuperRelations entry = (SuperRelations)cache.getOrCreateSuperRelations(graph, r); InternalProcedure procedure_ = procedure != null ? procedure : emptyProcedureSuperRelations; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); SuperRelations.computeForEach(graph, r, entry, procedure_); } } private SuperRelations peekSuperRelations(int r) { synchronized(superRelationsMap) { return (SuperRelations) superRelationsMap.get(r); } } AssertedPredicates getOrCreateAssertedPredicates(ReadGraphImpl graph, int r) throws DatabaseException { AssertedPredicates existing = null; synchronized(assertedPredicatesMap) { existing = (AssertedPredicates)assertedPredicatesMap.get(r); if(existing == null) { existing = new AssertedPredicates(r); existing.setPending(querySupport); assertedPredicatesMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(AssertedPredicates entry) { synchronized(assertedPredicatesMap) { assertedPredicatesMap.remove(entry.id); } } public static void runnerAssertedPredicates(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; AssertedPredicates entry = (AssertedPredicates)cache.getOrCreateAssertedPredicates(graph, r); IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedPredicates; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.compute(graph, procedure_); } } private AssertedPredicates peekAssertedPredicates(int r) { synchronized(assertedPredicatesMap) { return (AssertedPredicates) assertedPredicatesMap.get(r); } } AssertedStatements getOrCreateAssertedStatements(ReadGraphImpl graph, int r1, int r2) throws DatabaseException { AssertedStatements existing = null; synchronized(assertedStatementsMap) { existing = (AssertedStatements)assertedStatementsMap.get(r1,r2); if(existing == null) { existing = new AssertedStatements(r1,r2); existing.setPending(querySupport); assertedStatementsMap.put(keyR2(r1,r2), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(AssertedStatements entry) { synchronized(assertedStatementsMap) { assertedStatementsMap.remove(entry.id); } } public static void runnerAssertedStatements(ReadGraphImpl graph, int r1, int r2, CacheEntry parent, ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; AssertedStatements entry = (AssertedStatements)cache.getOrCreateAssertedStatements(graph, r1,r2); TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedStatements; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.compute(graph, procedure_); } } private AssertedStatements peekAssertedStatements(int r1, int r2) { synchronized(assertedStatementsMap) { return (AssertedStatements) assertedStatementsMap.get(r1,r2); } } DirectSuperRelations getOrCreateDirectSuperRelations(ReadGraphImpl graph, int r) throws DatabaseException { DirectSuperRelations existing = null; synchronized(directSuperRelationsMap) { existing = (DirectSuperRelations)directSuperRelationsMap.get(r); if(existing == null) { existing = new DirectSuperRelations(r); existing.setPending(querySupport); directSuperRelationsMap.put(keyR(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(DirectSuperRelations entry) { synchronized(directSuperRelationsMap) { directSuperRelationsMap.remove(entry.id); } } public static void runnerDirectSuperRelations(ReadGraphImpl graph, int r, CacheEntry parent, ListenerBase listener, final IntProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; DirectSuperRelations entry = (DirectSuperRelations)cache.getOrCreateDirectSuperRelations(graph, r); IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectSuperRelations; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.compute(graph, procedure_); } } private DirectSuperRelations peekDirectSuperRelations(int r) { synchronized(directSuperRelationsMap) { return (DirectSuperRelations) directSuperRelationsMap.get(r); } } MultiReadEntry getOrCreateMultiReadEntry(ReadGraphImpl graph, MultiRead r) throws DatabaseException { MultiReadEntry existing = null; synchronized(multiReadEntryMap) { existing = (MultiReadEntry)multiReadEntryMap.get(r); if(existing == null) { existing = new MultiReadEntry(r); existing.setPending(querySupport); multiReadEntryMap.put(id(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(MultiReadEntry entry) { synchronized(multiReadEntryMap) { multiReadEntryMap.remove(entry.id); } } public static void runnerMultiReadEntry(ReadGraphImpl graph, MultiRead r, CacheEntry parent, ListenerBase listener, final SyncMultiProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; MultiReadEntry entry = (MultiReadEntry)cache.getOrCreateMultiReadEntry(graph, r); SyncMultiProcedure procedure_ = procedure != null ? procedure : emptyProcedureMultiReadEntry; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.compute(graph, procedure_); } } private MultiReadEntry peekMultiReadEntry(MultiRead r) { synchronized(multiReadEntryMap) { return (MultiReadEntry) multiReadEntryMap.get(r); } } AsyncMultiReadEntry getOrCreateAsyncMultiReadEntry(ReadGraphImpl graph, AsyncMultiRead r) throws DatabaseException { AsyncMultiReadEntry existing = null; synchronized(asyncMultiReadEntryMap) { existing = (AsyncMultiReadEntry)asyncMultiReadEntryMap.get(r); if(existing == null) { existing = new AsyncMultiReadEntry(r); existing.setPending(querySupport); asyncMultiReadEntryMap.put(id(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(AsyncMultiReadEntry entry) { synchronized(asyncMultiReadEntryMap) { asyncMultiReadEntryMap.remove(entry.id); } } public static void runnerAsyncMultiReadEntry(ReadGraphImpl graph, AsyncMultiRead r, CacheEntry parent, ListenerBase listener, final AsyncMultiProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; AsyncMultiReadEntry entry = (AsyncMultiReadEntry)cache.getOrCreateAsyncMultiReadEntry(graph, r); AsyncMultiProcedure procedure_ = procedure != null ? procedure : emptyProcedureAsyncMultiReadEntry; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.compute(graph, procedure_); } } private AsyncMultiReadEntry peekAsyncMultiReadEntry(AsyncMultiRead r) { synchronized(asyncMultiReadEntryMap) { return (AsyncMultiReadEntry) asyncMultiReadEntryMap.get(r); } } ExternalReadEntry getOrCreateExternalReadEntry(ReadGraphImpl graph, ExternalRead r) throws DatabaseException { ExternalReadEntry existing = null; synchronized(externalReadEntryMap) { existing = (ExternalReadEntry)externalReadEntryMap.get(r); if(existing == null) { existing = new ExternalReadEntry(r, graph); existing.setPending(querySupport); externalReadEntryMap.put(id(r), existing); size++; return existing; } if(existing.requiresComputation()) { existing.setPending(querySupport); return existing; } } if(existing.isPending()) { waitPending(graph, existing); } return existing; } void remove(ExternalReadEntry entry) { synchronized(externalReadEntryMap) { externalReadEntryMap.remove(entry.id); } } public static void runnerExternalReadEntry(ReadGraphImpl graph, ExternalRead r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure) throws DatabaseException { QueryCache cache = graph.processor.cache; ExternalReadEntry entry = (ExternalReadEntry)cache.getOrCreateExternalReadEntry(graph, r); AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureExternalReadEntry; if(entry.isReady()) { graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.performFromCache(graph, procedure_); } else { assert(entry.isPending()); graph.processor.listening.registerDependencies(graph, entry, parent, listener, procedure_, false); entry.compute(graph, procedure_); } } private ExternalReadEntry peekExternalReadEntry(ExternalRead r) { synchronized(externalReadEntryMap) { return (ExternalReadEntry) externalReadEntryMap.get(r); } } }