-import gnu.trove.map.hash.THashMap;
-
-public class QueryCache {
-
- final public UnaryQueryHashMap<IntProcedure> directPredicatesMap;
- final public UnaryQueryHashMap<IntProcedure> principalTypesMap;
- final public THashMap<String, URIToResource> uriToResourceMap;
- final public THashMap<String, NamespaceIndex> namespaceIndexMap22;
- final public UnaryQueryHashMap<IntProcedure> projectsMap;
- final public UnaryQueryHashMap<InternalProcedure<RelationInfo>> relationInfoMap;
- final public UnaryQueryHashMap<InternalProcedure<IntSet>> superTypesMap;
- final public UnaryQueryHashMap<InternalProcedure<IntSet>> typeHierarchyMap;
- final public UnaryQueryHashMap<InternalProcedure<IntSet>> superRelationsMap;
- final public UnaryQueryHashMap<InternalProcedure<IntSet>> typesMap;
- final public UnaryQueryHashMap<InternalProcedure<byte[]>> valueMap;
- final public DoubleKeyQueryHashMap<IntProcedure> directObjectsMap;
- final public DoubleKeyQueryHashMap<IntProcedure> objectsMap;
- final public UnaryQueryHashMap<IntProcedure> orderedSetMap;
- final public UnaryQueryHashMap<IntProcedure> predicatesMap;
- final public DoubleKeyQueryHashMap<TripleIntProcedure> statementsMap;
- final public UnaryQueryHashMap<IntProcedure> assertedPredicatesMap;
- final public BinaryQueryHashMap<TripleIntProcedure> assertedStatementsMap;
- final public StableHashMap<ExternalRead, ExternalReadEntry> externalReadMap;
- final public StableHashMap<AsyncRead, AsyncReadEntry> asyncReadMap;
- final public StableHashMap<Read, ReadEntry> readMap;
- final public StableHashMap<AsyncMultiRead, AsyncMultiReadEntry> asyncMultiReadMap;
- final public StableHashMap<MultiRead, MultiReadEntry> multiReadMap;
-
- final THashMap<CacheEntry, ArrayList<ListenerEntry>> listeners;
-
- public QueryCache() {
- directPredicatesMap = new UnaryQueryHashMap();
- valueMap = new UnaryQueryHashMap();
- principalTypesMap = new UnaryQueryHashMap();
- uriToResourceMap = new THashMap<String, URIToResource>();
- namespaceIndexMap22 = new THashMap<String, NamespaceIndex>();
- projectsMap = new UnaryQueryHashMap();
- relationInfoMap = new UnaryQueryHashMap();
- typeHierarchyMap = new UnaryQueryHashMap();
- superTypesMap = new UnaryQueryHashMap();
- superRelationsMap = new UnaryQueryHashMap();
- typesMap = new UnaryQueryHashMap();
- objectsMap = new DoubleKeyQueryHashMap();
- orderedSetMap = new UnaryQueryHashMap();
- predicatesMap = new UnaryQueryHashMap();
- statementsMap = new DoubleKeyQueryHashMap();
- directObjectsMap = new DoubleKeyQueryHashMap();
- assertedPredicatesMap = new UnaryQueryHashMap();
- assertedStatementsMap = new BinaryQueryHashMap();
- asyncReadMap = new StableHashMap<AsyncRead, AsyncReadEntry>();
- readMap = new StableHashMap<Read, ReadEntry>();
- asyncMultiReadMap = new StableHashMap<AsyncMultiRead, AsyncMultiReadEntry>();
- multiReadMap = new StableHashMap<MultiRead, MultiReadEntry>();
- externalReadMap = new StableHashMap<ExternalRead, ExternalReadEntry>();
- listeners = new THashMap<CacheEntry, ArrayList<ListenerEntry>>(10, 0.75f);
- }
-
- public int requestHash(Object object) {
- try {
- return object.hashCode();
- } catch (Throwable t) {
- Logger.defaultLogError(t);
- return 0;
- }
- }
-
- private CacheEntryBase getCached(Object query, int hash) {
- if (query instanceof AsyncRead)
- return asyncReadMap.get(query, hash);
- else if (query instanceof Read)
- return readMap.get(query, hash);
- else if (query instanceof ExternalRead)
- return externalReadMap.get(query, hash);
- else if (query instanceof AsyncMultiRead)
- return asyncMultiReadMap.get(query, hash);
- throw new IllegalStateException();
- }
-
- private CacheEntryBase createEntry(QuerySupport support, Object query, int hash) {
-
- CacheEntryBase result;
- if (query instanceof AsyncRead) {
- AsyncReadEntry entry = new AsyncReadEntry((AsyncRead)query);
- asyncReadMap.put((AsyncRead)query, entry, hash);
- result = entry;
- } else if (query instanceof Read) {
- ReadEntry entry = new ReadEntry((Read)query);
- readMap.put((Read)query, entry, hash);
- result = entry;
- } else if (query instanceof ExternalRead) {
- ExternalReadEntry entry = new ExternalReadEntry((ExternalRead)query);
- externalReadMap.put((ExternalRead)query, entry, hash);
- result = entry;
- } else if (query instanceof AsyncMultiRead) {
- AsyncMultiReadEntry entry = new AsyncMultiReadEntry((AsyncMultiRead)query);
- asyncMultiReadMap.put((AsyncMultiRead)query, entry, hash);
- result = entry;
- } else {
- throw new IllegalStateException();
- }
-
- result.setPending();
- result.clearResult(support);
-
- return result;
-
- }
-
- public final <T> Object runQuery(final ReadGraphImpl graph, final Object query, final CacheEntry parent, final ListenerBase listener, final Object procedure) throws DatabaseException {
-
- int hash = requestHash(query);
-
- CacheEntryBase entry = getCached(query, hash);
-
- if(parent == null && listener == null) {
- if(entry != null && (entry.isReady() || entry.isExcepted())) {
- return entry.performFromCache(graph, this, procedure);
- } else {
- return performQuery(graph, query, entry, null, procedure);
- }
- }
-
- if(entry == null) {
- entry = createEntry(graph.processor.querySupport, query, hash);
- return performForEach(graph, query, entry, parent, listener, procedure, false);
-
- } else {
-
- if(entry.isPending()) {
- synchronized(entry) {
- if(entry.isPending()) {
- throw new IllegalStateException();
- // final AsyncBarrierImpl parentBarrier = graph.state.barrier;
- // if(entry.procs == null) entry.procs = new ArrayList<AsyncProcedure<T>>();
- // entry.procs.add(new AsyncProcedure<T>() {
- //
- // @Override
- // public void execute(AsyncReadGraph graph, T result) {
- // procedure.execute(graph, result);
- // parentBarrier.dec(query);
- // }
- //
- // @Override
- // public void exception(AsyncReadGraph graph, Throwable throwable) {
- // procedure.exception(graph, throwable);
- // parentBarrier.dec(query);
- // }
- //
- // });
-// if(graph.parent != null || listener != null) {
-// registerDependencies(graph, entry, parent, listener, procedure, false);
-// }
-//
-// query.perform(graph, procedure);
-//
-// return;
-
- }
- }
- }
-
- if(entry.isReady()) {
- Object result = entry.performFromCache(graph, this, procedure);
- registerDependencies(graph, entry, parent, listener, procedure, false);
- return result;
- } else {
- return performForEach(graph, query, entry, parent, listener, procedure, false);
- }
-
- }
-
- }
-
- public <T> Object performQuery(ReadGraphImpl parentGraph, final Object query_, final CacheEntryBase entry_, ListenerEntry listenerEntry, Object procedure_) throws DatabaseException {
-
- ReadGraphImpl queryGraph = parentGraph.withParent(entry_);
-
- if(query_ instanceof AsyncRead) {
-
- AsyncRead<T> query = (AsyncRead<T>)query_;
- AsyncReadEntry<T> entry = (AsyncReadEntry<T>)entry_;
- AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
-
- try {
-
- query.perform(queryGraph, new AsyncProcedure<T>() {
-
- @Override
- public void execute(AsyncReadGraph returnGraph, T result) {
- ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
- entry.addOrSet(parentGraph, result);
- if(listenerEntry != null) {
- primeListenerEntry(listenerEntry, result);
- }
- try {
- procedure.execute(parentGraph, result);
- } catch (Throwable t) {
- t.printStackTrace();
- }
- // parentBarrier.dec(query);
- }
-
- @Override
- public void exception(AsyncReadGraph returnGraph, Throwable t) {
- ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
- // AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
- entry.except(parentGraph, t);
- try {
- procedure.exception(parentGraph, t);
- } catch (Throwable t2) {
- t2.printStackTrace();
- }
- // parentBarrier.dec(query);
- }
-
- @Override
- public String toString() {
- return procedure.toString();
- }
-
- });
-
- } catch (Throwable t) {
-
- entry.except(t);
- try {
- procedure.exception(parentGraph, t);
- } catch (Throwable t2) {
- t2.printStackTrace();
- }
- // parentBarrier.dec(query);
-
- }
-
- return null;
-
- } else if (query_ instanceof Read) {
-
- Read query = (Read)query_;
- ReadEntry entry = (ReadEntry)entry_;
-
- entry.setPending();
-
- try {
-
- T result = (T)query.perform(queryGraph);
- entry.addOrSet(queryGraph, result);
-
- if(listenerEntry != null) primeListenerEntry(listenerEntry, result);
-
- return (T)entry.get(parentGraph, parentGraph.processor, procedure_);
-
- } catch (Throwable t) {
-
- entry.except(t);
- return (T)entry.get(parentGraph, parentGraph.processor, procedure_);
-
- }
-
- } else if (query_ instanceof ExternalRead) {
-
- ExternalRead query = (ExternalRead)query_;
- ExternalReadEntry entry = (ExternalReadEntry)entry_;
- Procedure<T> procedure = (Procedure<T>)procedure_;
-
- try {
-
- query.register(parentGraph, new Listener<T>() {
-
- AtomicBoolean used = new AtomicBoolean(false);
-
- @Override
- public void execute(T result) {
-
- // Just for safety
- if(entry.isDiscarded()) return;
- if(entry.isExcepted()) entry.setPending();
-
- if(used.compareAndSet(false, true)) {
- entry.addOrSet(parentGraph.processor, result);
- procedure.execute(result);
- } else {
- entry.queue(result);
- parentGraph.processor.updatePrimitive(query);
- }
-
- }
-
- @Override
- public void exception(Throwable t) {
-
- entry.except(t);
-
- if(used.compareAndSet(false, true)) {
- procedure.exception(t);
- } else {
-// entry.queue(result);
- parentGraph.processor.updatePrimitive(query);
- }
-
- }
-
- @Override
- public String toString() {
- return procedure.toString();
- }
-
- @Override
- public boolean isDisposed() {
- return entry.isDiscarded() || !parentGraph.processor.isBound(entry);
- }
-
- });
-
- return entry.getResult();
-
- } catch (Throwable t) {
-
- entry.except(t);
- procedure.exception(t);
- return entry.getResult();
-
- }
-
- } else if (query_ instanceof AsyncMultiRead) {
-
- AsyncMultiRead query = (AsyncMultiRead)query_;
- AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
- AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
-
- try {
-
- query.perform(queryGraph, new AsyncMultiProcedure<T>() {
-
- @Override
- public void execute(AsyncReadGraph graph, T result) {
- ReadGraphImpl impl = (ReadGraphImpl)graph;
- entry.addOrSet(result);
- try {
- procedure.execute(parentGraph, result);
- } catch (Throwable t) {
- t.printStackTrace();
- }
- }
-
- @Override
- public void finished(AsyncReadGraph graph) {
- ReadGraphImpl impl = (ReadGraphImpl)graph;
- entry.finish(parentGraph);
- try {
- procedure.finished(parentGraph);
- } catch (Throwable t) {
- t.printStackTrace();
- }
- }
-
- @Override
- public void exception(AsyncReadGraph graph, Throwable t) {
- ReadGraphImpl impl = (ReadGraphImpl)graph;
- entry.except(parentGraph, t);
- try {
- procedure.exception(parentGraph, t);
- } catch (Throwable t2) {
- t2.printStackTrace();
- }
- }
-
- });
-
- return entry.getResult();
-
- } catch (Throwable t) {
-
- entry.except(t);
- try {
- procedure.exception(parentGraph, t);
- } catch (Throwable t2) {
- t2.printStackTrace();
- }
-
- return entry.getResult();
-
- }
-
- } else {
-
- throw new IllegalStateException();
-
- }
-
- }
-
- public <T> Object performFromCache(ReadGraphImpl parentGraph, Object query, CacheEntryBase entry_, ListenerEntry listenerEntry, Object procedure_) throws DatabaseException {
-
- Object result = entry_.performFromCache(parentGraph, this, procedure_);
- if(listenerEntry != null) {
- primeListenerEntry(listenerEntry, result);
- }
- return result;
-
-// if(query instanceof AsyncRead) {
-//
-// AsyncProcedure<T> procedure = (AsyncProcedure<T>)procedure_;
-//
-//
-// return null;
-//
-// } else if(query instanceof Read) {
-//
-// ReadEntry entry = (ReadEntry)entry_;
-//
-// T result = (T)entry.get(parentGraph, parentGraph.processor, procedure_);
-//
-// if(listenerEntry != null) primeListenerEntry(listenerEntry, result);
-//
-// return result;
-//
-// } else if(query instanceof ExternalRead) {
-//
-// ExternalReadEntry entry = (ExternalReadEntry)entry_;
-// Procedure<T> procedure = (Procedure<T>)procedure_;
-//
-// return entry.performFromCache(procedure);
-//
-// } else if (query instanceof AsyncMultiRead) {
-//
-// AsyncMultiReadEntry entry = (AsyncMultiReadEntry)entry_;
-// AsyncMultiProcedure<T> procedure = (AsyncMultiProcedure<T>)procedure_;
-//
-// return entry.performFromCache(parentGraph, this, procedure);
-//
-// } else {
-//
-// throw new IllegalStateException();
-//
-// }
-
- }
-
- public <T> Object performForEach(ReadGraphImpl parentGraph, final Object query, final CacheEntryBase entry, final CacheEntry parent, final ListenerBase base, final Object procedure,
- boolean inferredDependency) throws DatabaseException {
-
-// if (DebugPolicy.PERFORM)
-// System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
-
-// assert (!dirty);
-// assert (!collecting);
-
- assert(!entry.isDiscarded());
-
- final ListenerEntry listenerEntry = registerDependencies(parentGraph, entry, parent, base, procedure, inferredDependency);
-
- // FRESH, REFUTED, EXCEPTED go here
- if (!entry.isReady()) {
-
- entry.setPending();
-
-// size++;
-
- return performQuery(parentGraph, query, entry, listenerEntry, procedure);
-
-// misses++;
-
- } else {
-
- return performFromCache(parentGraph, query, entry, listenerEntry, procedure);
-
-// parentBarrier.dec(query);
-
-// hits++;
-
- }
-
- }
-
- synchronized public ListenerEntry registerDependencies(ReadGraphImpl graph, CacheEntry child, CacheEntry parent, ListenerBase listener, Object procedure, boolean inferred) {
-
- if (parent != null && !inferred) {
- try {
- if(!child.isImmutable(graph))
- child.addParent(parent);
- } catch (DatabaseException e) {
- Logger.defaultLogError(e);
- }
- if(DebugPolicy.DEPENDENCIES) System.out.println(child + " -> " + parent);
- }
-
- if (listener != null) {
- return registerListener(child, listener, procedure);
- } else {
- return null;
- }
-
- }
-
- public ListenerEntry registerListener(final CacheEntry entry, final ListenerBase base, final Object procedure) {
-
- assert (entry != null);
-
- if (base.isDisposed())
- return null;
-
- return addListener(entry, base, procedure);
-
- }
-
- private void primeListenerEntry(final ListenerEntry entry, final Object result) {
- entry.setLastKnown(result);
- }
-
- private ListenerEntry addListener(CacheEntry entry, ListenerBase base, Object procedure) {
-
- assert (entry != null);
- assert (procedure != null);
-
- ArrayList<ListenerEntry> list = listeners.get(entry);
- if (list == null) {
- list = new ArrayList<ListenerEntry>(1);
- listeners.put(entry, list);
- }
-
- ListenerEntry result = new ListenerEntry(entry, base, procedure);
- int currentIndex = list.indexOf(result);
- // There was already a listener
- if(currentIndex > -1) {
- ListenerEntry current = list.get(currentIndex);
- if(!current.base.isDisposed()) return null;
- list.set(currentIndex, result);
- } else {
- list.add(result);
- }
-
- if(DebugPolicy.LISTENER) {
- new Exception().printStackTrace();
- System.out.println("addListener -> " + list.size() + " " + entry + " " + base + " " + procedure);
- }
-
- return result;
-
- }
-
-
- public Collection<CacheEntry> getRootList() {
-
- ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
-
- for (Object e : valueMap.values()) {
- result.add((CacheEntry) e);
- }
- for (Object e : directPredicatesMap.values()) {
- result.add((CacheEntry) e);
- }
- for (Object e : objectsMap.values()) {
- result.add((CacheEntry) e);
- }
- for (Object e : directObjectsMap.values()) {
- result.add((CacheEntry) e);
- }
- for (Object e : principalTypesMap.values()) {
- result.add((CacheEntry) e);
- }
- for (Object e : superRelationsMap.values()) {
- result.add((CacheEntry) e);
- }
- for (Object e : superTypesMap.values()) {
- result.add((CacheEntry) e);
- }
- for (Object e : typesMap.values()) {
- result.add((CacheEntry) e);
- }
- for (Object e : objectsMap.values()) {
- result.add((CacheEntry) e);
- }
- for (Object e : assertedStatementsMap.values()) {
- result.add((CacheEntry) e);
- }
- for (Object e : readMap.values()) {
- if(e instanceof CacheEntry) {
- result.add((CacheEntry) e);
- } else {
- System.err.println("e=" + e);
- }
- }
- for (Object e : asyncReadMap.values()) {
- if(e instanceof CacheEntry) {
- result.add((CacheEntry) e);
- } else {
- System.err.println("e=" + e);
- }
- }
- for (Object e : externalReadMap.values()) {
- result.add((CacheEntry) e);
- }
- for (Object e : orderedSetMap.values()) {
- result.add((CacheEntry) e);
- }
-
- return result;
-
- }
-
- public int calculateCurrentSize() {
-
- int realSize = 0;
-
- realSize += directPredicatesMap.size();
- realSize += principalTypesMap.size();
- realSize += uriToResourceMap.size();
- realSize += namespaceIndexMap22.size();
- realSize += projectsMap.size();
-
- realSize += relationInfoMap.size();
- realSize += superTypesMap.size();
- realSize += typeHierarchyMap.size();
- realSize += superRelationsMap.size();
- realSize += typesMap.size();
-
- realSize += valueMap.size();
- realSize += directObjectsMap.size();
- realSize += objectsMap.size();
- realSize += orderedSetMap.size();
- realSize += predicatesMap.size();
-
- realSize += statementsMap.size();
- realSize += assertedPredicatesMap.size();
- realSize += assertedStatementsMap.size();
- realSize += externalReadMap.size();
- realSize += asyncReadMap.size();
-
- realSize += readMap.size();
- realSize += asyncMultiReadMap.size();
- realSize += multiReadMap.size();
-
- return realSize;
-
- }
-
- CacheCollectionResult allCaches(CacheCollectionResult result) {
-
- int level = Integer.MAX_VALUE;
- directPredicatesMap.values(level, result);
- principalTypesMap.values(level, result);
- for(CacheEntryBase e : uriToResourceMap.values())
- if(e.getLevel() <= level)
- result.add(e);
- for(CacheEntryBase e : namespaceIndexMap22.values())
- if(e.getLevel() <= level)
- result.add(e);
- projectsMap.values(level, result);
-
- relationInfoMap.values(level, result);
- superTypesMap.values(level, result);
- typeHierarchyMap.values(level, result);
- superRelationsMap.values(level, result);
- typesMap.values(level, result);
-
- valueMap.values(level, result);
- directObjectsMap.values(level, result);
- objectsMap.values(level, result);
- orderedSetMap.values(level, result);
- predicatesMap.values(level, result);
-
- statementsMap.values(level, result);
- assertedPredicatesMap.values(level, result);
- assertedStatementsMap.values(level, result);
- externalReadMap.values(level, result);
- asyncReadMap.values(level, result);
-
- readMap.values(level, result);
- asyncMultiReadMap.values(level, result);
- multiReadMap.values(level, result);
-
- return result;
-
- }
-
- public void scanPending() {
-
- ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
-
- entries.addAll(directPredicatesMap.values());
- entries.addAll(principalTypesMap.values());
- entries.addAll(uriToResourceMap.values());
- entries.addAll(namespaceIndexMap22.values());
- entries.addAll(projectsMap.values());
- entries.addAll(relationInfoMap.values());
- entries.addAll(superTypesMap.values());
- entries.addAll(superRelationsMap.values());
- entries.addAll(typesMap.values());
- entries.addAll(valueMap.values());
- entries.addAll(directObjectsMap.values());
- entries.addAll(objectsMap.values());
- entries.addAll(orderedSetMap.values());
- entries.addAll(predicatesMap.values());
- entries.addAll(orderedSetMap.values());
- entries.addAll(statementsMap.values());
- // entries.addAll(assertedObjectsMap.values());
- entries.addAll(assertedPredicatesMap.values());
- entries.addAll(assertedStatementsMap.values());
- entries.addAll(externalReadMap.values());
- entries.addAll(asyncReadMap.values());
- entries.addAll(externalReadMap.values());
- entries.addAll(readMap.values());
- entries.addAll(asyncMultiReadMap.values());
- entries.addAll(multiReadMap.values());
- entries.addAll(readMap.values());
- System.out.println(entries.size() + " entries.");
- for(Object e : entries) {
- if(e instanceof CacheEntry) {
- CacheEntry en = (CacheEntry)e;
- if(en.isPending()) System.out.println("pending " + e);
- if(en.isExcepted()) System.out.println("excepted " + e);
- if(en.isDiscarded()) System.out.println("discarded " + e);
- if(en.isRefuted()) System.out.println("refuted " + e);
- if(en.isFresh()) System.out.println("fresh " + e);
- } else {
- //System.out.println("Unknown object " + e);
- }
- }
- }
-
+import gnu.trove.map.hash.TObjectIntHashMap;
+
+public class QueryCache extends QueryCacheBase {
+
+ public QueryCache(QuerySupport querySupport, int threads) {
+ super(querySupport, threads);
+ }
+
+ Objects getOrCreateObjects(QueryProcessor processor, 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.clearResult(querySupport);
+ existing.setPending();
+ objectsMap.put(keyR2(r1,r2), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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)) {
+ Objects.computeForEach(graph, r1,r2, null, procedure);
+ return;
+ }
+ Objects entry = (Objects)cache.getOrCreateObjects(graph.processor, r1,r2);
+ IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureObjects;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ Objects.computeForEach(graph, r1,r2, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ Statements getOrCreateStatements(QueryProcessor processor, 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.clearResult(querySupport);
+ existing.setPending();
+ statementsMap.put(keyR2(r1,r2), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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)) {
+ Statements.computeForEach(graph, r1,r2, null, procedure);
+ return;
+ }
+ Statements entry = (Statements)cache.getOrCreateStatements(graph.processor, r1,r2);
+ TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureStatements;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ Statements.computeForEach(graph, r1,r2, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ DirectObjects getOrCreateDirectObjects(QueryProcessor processor, 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.clearResult(querySupport);
+ existing.setPending();
+ directObjectsMap.put(keyR2(r1,r2), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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)) {
+ DirectObjects.computeForEach(graph, r1,r2, null, procedure);
+ return;
+ }
+ DirectObjects entry = (DirectObjects)cache.getOrCreateDirectObjects(graph.processor, r1,r2);
+ IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectObjects;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ DirectObjects.computeForEach(graph, r1,r2, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ RelationInfoQuery getOrCreateRelationInfoQuery(QueryProcessor processor, int r) throws DatabaseException {
+ RelationInfoQuery existing = null;
+ synchronized(relationInfoQueryMap) {
+ existing = (RelationInfoQuery)relationInfoQueryMap.get(r);
+ if(existing == null) {
+ existing = new RelationInfoQuery(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ relationInfoQueryMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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<RelationInfo> procedure) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+ RelationInfoQuery.computeForEach(graph, r, null, procedure);
+ return;
+ }
+ RelationInfoQuery entry = (RelationInfoQuery)cache.getOrCreateRelationInfoQuery(graph.processor, r);
+ InternalProcedure<RelationInfo> procedure_ = procedure != null ? procedure : emptyProcedureRelationInfoQuery;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ RelationInfoQuery.computeForEach(graph, r, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ URIToResource getOrCreateURIToResource(QueryProcessor processor, String id) throws DatabaseException {
+ URIToResource existing = null;
+ synchronized(uRIToResourceMap) {
+ existing = (URIToResource)uRIToResourceMap.get(id);
+ if(existing == null) {
+ existing = new URIToResource(id);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ uRIToResourceMap.put(keyID(id), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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<Integer> procedure) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ if(parent == null && listener == null && !cache.shouldCache(graph.processor, id)) {
+ URIToResource.computeForEach(graph, id, null, procedure);
+ return;
+ }
+ URIToResource entry = (URIToResource)cache.getOrCreateURIToResource(graph.processor, id);
+ InternalProcedure<Integer> procedure_ = procedure != null ? procedure : emptyProcedureURIToResource;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ URIToResource.computeForEach(graph, id, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ ValueQuery getOrCreateValueQuery(QueryProcessor processor, int r) throws DatabaseException {
+ ValueQuery existing = null;
+ synchronized(valueQueryMap) {
+ existing = (ValueQuery)valueQueryMap.get(r);
+ if(existing == null) {
+ existing = new ValueQuery(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ valueQueryMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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<byte[]> procedure) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+ ValueQuery.computeForEach(graph, r, null, procedure);
+ return;
+ }
+ ValueQuery entry = (ValueQuery)cache.getOrCreateValueQuery(graph.processor, r);
+ InternalProcedure<byte[]> procedure_ = procedure != null ? procedure : emptyProcedureValueQuery;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ ValueQuery.computeForEach(graph, r, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ OrderedSet getOrCreateOrderedSet(QueryProcessor processor, int r) throws DatabaseException {
+ OrderedSet existing = null;
+ synchronized(orderedSetMap) {
+ existing = (OrderedSet)orderedSetMap.get(r);
+ if(existing == null) {
+ existing = new OrderedSet(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ orderedSetMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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)) {
+ OrderedSet.computeForEach(graph, r, null, procedure);
+ return;
+ }
+ OrderedSet entry = (OrderedSet)cache.getOrCreateOrderedSet(graph.processor, r);
+ IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureOrderedSet;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ OrderedSet.computeForEach(graph, r, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ PrincipalTypes getOrCreatePrincipalTypes(QueryProcessor processor, int r) throws DatabaseException {
+ PrincipalTypes existing = null;
+ synchronized(principalTypesMap) {
+ existing = (PrincipalTypes)principalTypesMap.get(r);
+ if(existing == null) {
+ existing = new PrincipalTypes(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ principalTypesMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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)) {
+ PrincipalTypes.computeForEach(graph, r, null, procedure);
+ return;
+ }
+ PrincipalTypes entry = (PrincipalTypes)cache.getOrCreatePrincipalTypes(graph.processor, r);
+ IntProcedure procedure_ = procedure != null ? procedure : emptyProcedurePrincipalTypes;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ PrincipalTypes.computeForEach(graph, r, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ DirectPredicates getOrCreateDirectPredicates(QueryProcessor processor, int r) throws DatabaseException {
+ DirectPredicates existing = null;
+ synchronized(directPredicatesMap) {
+ existing = (DirectPredicates)directPredicatesMap.get(r);
+ if(existing == null) {
+ existing = new DirectPredicates(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ directPredicatesMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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<IntSet> procedure) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+ DirectPredicates.computeForEach(graph, r, null, procedure);
+ return;
+ }
+ DirectPredicates entry = (DirectPredicates)cache.getOrCreateDirectPredicates(graph.processor, r);
+ InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureDirectPredicates;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ DirectPredicates.computeForEach(graph, r, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ Predicates getOrCreatePredicates(QueryProcessor processor, int r) throws DatabaseException {
+ Predicates existing = null;
+ synchronized(predicatesMap) {
+ existing = (Predicates)predicatesMap.get(r);
+ if(existing == null) {
+ existing = new Predicates(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ predicatesMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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<IntSet> procedure) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+ Predicates.computeForEach(graph, r, null, procedure);
+ return;
+ }
+ Predicates entry = (Predicates)cache.getOrCreatePredicates(graph.processor, r);
+ InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedurePredicates;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ Predicates.computeForEach(graph, r, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ ReadEntry getOrCreateReadEntry(QueryProcessor processor, Read<?> r, boolean isSync) throws DatabaseException {
+ ReadEntry existing = null;
+ synchronized(readEntryMap) {
+ existing = (ReadEntry)readEntryMap.get(r);
+ if(existing == null) {
+ existing = new ReadEntry(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ readEntryMap.put(id(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) {
+ if(isSync) waitPending(processor, existing);
+ else return null;
+ }
+ return existing;
+ }
+
+ void remove(ReadEntry entry) {
+ synchronized(readEntryMap) {
+ readEntryMap.remove(entry.request);
+ }
+ }
+
+ public static void runnerReadEntry(ReadGraphImpl graph, Read<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, boolean isSync) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+ ReadEntry.computeForEach(graph, r, null, procedure);
+ return;
+ }
+ ReadEntry entry = (ReadEntry)cache.getOrCreateReadEntry(graph.processor, r, isSync);
+ if(entry == null) {
+ graph.processor.schedule(new SessionTask(false) {
+ @Override
+ public void run(int thread) {
+ try {
+ assert(!isSync);
+ runnerReadEntry(graph, r, parent, listener, procedure, isSync);
+ } catch (DatabaseException e) {
+ Logger.defaultLogError(e);
+ }
+ }
+ });
+ return;
+ }
+ AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureReadEntry;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ ReadEntry.computeForEach(graph, r, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ AsyncReadEntry getOrCreateAsyncReadEntry(QueryProcessor processor, AsyncRead<?> r, boolean isSync) throws DatabaseException {
+ AsyncReadEntry existing = null;
+ synchronized(asyncReadEntryMap) {
+ existing = (AsyncReadEntry)asyncReadEntryMap.get(r);
+ if(existing == null) {
+ existing = new AsyncReadEntry(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ asyncReadEntryMap.put(id(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) {
+ if(isSync) waitPending(processor, existing);
+ else return null;
+ }
+ return existing;
+ }
+
+ void remove(AsyncReadEntry entry) {
+ synchronized(asyncReadEntryMap) {
+ asyncReadEntryMap.remove(entry.request);
+ }
+ }
+
+ public static void runnerAsyncReadEntry(ReadGraphImpl graph, AsyncRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncProcedure procedure, boolean isSync) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+ AsyncReadEntry.computeForEach(graph, r, null, procedure);
+ return;
+ }
+ AsyncReadEntry entry = (AsyncReadEntry)cache.getOrCreateAsyncReadEntry(graph.processor, r, isSync);
+ if(entry == null) {
+ graph.processor.schedule(new SessionTask(false) {
+ @Override
+ public void run(int thread) {
+ try {
+ assert(!isSync);
+ runnerAsyncReadEntry(graph, r, parent, listener, procedure, isSync);
+ } catch (DatabaseException e) {
+ Logger.defaultLogError(e);
+ }
+ }
+ });
+ return;
+ }
+ AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureAsyncReadEntry;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ AsyncReadEntry.computeForEach(graph, r, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ Types getOrCreateTypes(QueryProcessor processor, int r) throws DatabaseException {
+ Types existing = null;
+ synchronized(typesMap) {
+ existing = (Types)typesMap.get(r);
+ if(existing == null) {
+ existing = new Types(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ typesMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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<IntSet> procedure) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+ Types.computeForEach(graph, r, null, procedure);
+ return;
+ }
+ Types entry = (Types)cache.getOrCreateTypes(graph.processor, r);
+ InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureTypes;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ Types.computeForEach(graph, r, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ ChildMap getOrCreateChildMap(QueryProcessor processor, int r) throws DatabaseException {
+ ChildMap existing = null;
+ synchronized(childMapMap) {
+ existing = (ChildMap)childMapMap.get(r);
+ if(existing == null) {
+ existing = new ChildMap(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ childMapMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ if(parent == null && listener == null && !cache.shouldCache(graph.processor, r)) {
+ ChildMap.computeForEach(graph, r, null, procedure);
+ return;
+ }
+ ChildMap entry = (ChildMap)cache.getOrCreateChildMap(graph.processor, r);
+ InternalProcedure<ObjectResourceIdMap<String>> procedure_ = procedure != null ? procedure : emptyProcedureChildMap;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ ChildMap.computeForEach(graph, r, entry, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ AssertedStatements getOrCreateAssertedStatements(QueryProcessor processor, 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.clearResult(querySupport);
+ existing.setPending();
+ assertedStatementsMap.put(keyR2(r1,r2), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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.processor, r1,r2);
+ TripleIntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedStatements;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ entry.compute(graph, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ AssertedPredicates getOrCreateAssertedPredicates(QueryProcessor processor, int r) throws DatabaseException {
+ AssertedPredicates existing = null;
+ synchronized(assertedPredicatesMap) {
+ existing = (AssertedPredicates)assertedPredicatesMap.get(r);
+ if(existing == null) {
+ existing = new AssertedPredicates(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ assertedPredicatesMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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.processor, r);
+ IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureAssertedPredicates;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ entry.compute(graph, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ DirectSuperRelations getOrCreateDirectSuperRelations(QueryProcessor processor, int r) throws DatabaseException {
+ DirectSuperRelations existing = null;
+ synchronized(directSuperRelationsMap) {
+ existing = (DirectSuperRelations)directSuperRelationsMap.get(r);
+ if(existing == null) {
+ existing = new DirectSuperRelations(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ directSuperRelationsMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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.processor, r);
+ IntProcedure procedure_ = procedure != null ? procedure : emptyProcedureDirectSuperRelations;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ entry.compute(graph, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ SuperTypes getOrCreateSuperTypes(QueryProcessor processor, int r) throws DatabaseException {
+ SuperTypes existing = null;
+ synchronized(superTypesMap) {
+ existing = (SuperTypes)superTypesMap.get(r);
+ if(existing == null) {
+ existing = new SuperTypes(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ superTypesMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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<IntSet> procedure) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ SuperTypes entry = (SuperTypes)cache.getOrCreateSuperTypes(graph.processor, r);
+ InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureSuperTypes;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ entry.compute(graph, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ TypeHierarchy getOrCreateTypeHierarchy(QueryProcessor processor, int r) throws DatabaseException {
+ TypeHierarchy existing = null;
+ synchronized(typeHierarchyMap) {
+ existing = (TypeHierarchy)typeHierarchyMap.get(r);
+ if(existing == null) {
+ existing = new TypeHierarchy(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ typeHierarchyMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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<IntSet> procedure) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ TypeHierarchy entry = (TypeHierarchy)cache.getOrCreateTypeHierarchy(graph.processor, r);
+ InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureTypeHierarchy;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ entry.compute(graph, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ SuperRelations getOrCreateSuperRelations(QueryProcessor processor, int r) throws DatabaseException {
+ SuperRelations existing = null;
+ synchronized(superRelationsMap) {
+ existing = (SuperRelations)superRelationsMap.get(r);
+ if(existing == null) {
+ existing = new SuperRelations(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ superRelationsMap.put(keyR(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, 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<IntSet> procedure) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ SuperRelations entry = (SuperRelations)cache.getOrCreateSuperRelations(graph.processor, r);
+ InternalProcedure<IntSet> procedure_ = procedure != null ? procedure : emptyProcedureSuperRelations;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ entry.compute(graph, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ MultiReadEntry getOrCreateMultiReadEntry(QueryProcessor processor, MultiRead<?> r) throws DatabaseException {
+ MultiReadEntry existing = null;
+ synchronized(multiReadEntryMap) {
+ existing = (MultiReadEntry)multiReadEntryMap.get(r);
+ if(existing == null) {
+ existing = new MultiReadEntry(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ multiReadEntryMap.put(id(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, existing);
+ return existing;
+ }
+
+ void remove(MultiReadEntry entry) {
+ synchronized(multiReadEntryMap) {
+ multiReadEntryMap.remove(entry.request);
+ }
+ }
+
+ public static void runnerMultiReadEntry(ReadGraphImpl graph, MultiRead<?> r, CacheEntry parent, ListenerBase listener, final AsyncMultiProcedure procedure) throws DatabaseException {
+ QueryCache cache = graph.processor.cache;
+ MultiReadEntry entry = (MultiReadEntry)cache.getOrCreateMultiReadEntry(graph.processor, r);
+ AsyncMultiProcedure procedure_ = procedure != null ? procedure : emptyProcedureMultiReadEntry;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ entry.compute(graph, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ AsyncMultiReadEntry getOrCreateAsyncMultiReadEntry(QueryProcessor processor, AsyncMultiRead<?> r) throws DatabaseException {
+ AsyncMultiReadEntry existing = null;
+ synchronized(asyncMultiReadEntryMap) {
+ existing = (AsyncMultiReadEntry)asyncMultiReadEntryMap.get(r);
+ if(existing == null) {
+ existing = new AsyncMultiReadEntry(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ asyncMultiReadEntryMap.put(id(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, existing);
+ return existing;
+ }
+
+ void remove(AsyncMultiReadEntry entry) {
+ synchronized(asyncMultiReadEntryMap) {
+ asyncMultiReadEntryMap.remove(entry.request);
+ }
+ }
+
+ 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.processor, r);
+ AsyncMultiProcedure procedure_ = procedure != null ? procedure : emptyProcedureAsyncMultiReadEntry;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ entry.compute(graph, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+
+ ExternalReadEntry getOrCreateExternalReadEntry(QueryProcessor processor, ExternalRead<?> r) throws DatabaseException {
+ ExternalReadEntry existing = null;
+ synchronized(externalReadEntryMap) {
+ existing = (ExternalReadEntry)externalReadEntryMap.get(r);
+ if(existing == null) {
+ existing = new ExternalReadEntry(r);
+ existing.clearResult(querySupport);
+ existing.setPending();
+ externalReadEntryMap.put(id(r), existing);
+ size++;
+ return existing;
+ }
+ if(existing.requiresComputation()) {
+ existing.setPending();
+ return existing;
+ }
+ }
+ if(existing.isPending()) waitPending(processor, existing);
+ return existing;
+ }
+
+ void remove(ExternalReadEntry entry) {
+ synchronized(externalReadEntryMap) {
+ externalReadEntryMap.remove(entry.request);
+ }
+ }
+
+ 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.processor, r);
+ AsyncProcedure procedure_ = procedure != null ? procedure : emptyProcedureExternalReadEntry;
+ ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure_, false);
+ if(entry.isReady()) entry.performFromCache(graph, procedure_);
+ else {
+ assert(entry.isPending());
+ entry.compute(graph, procedure_);
+ if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());
+ }
+ }
+