1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.query;
14 import java.io.BufferedOutputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.PrintStream;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.IdentityHashMap;
26 import java.util.Iterator;
27 import java.util.LinkedList;
28 import java.util.List;
31 import java.util.concurrent.Semaphore;
32 import java.util.concurrent.atomic.AtomicBoolean;
33 import java.util.concurrent.atomic.AtomicInteger;
35 import org.simantics.databoard.Bindings;
36 import org.simantics.db.AsyncReadGraph;
37 import org.simantics.db.DevelopmentKeys;
38 import org.simantics.db.DirectStatements;
39 import org.simantics.db.ReadGraph;
40 import org.simantics.db.RelationInfo;
41 import org.simantics.db.Resource;
42 import org.simantics.db.Session;
43 import org.simantics.db.Statement;
44 import org.simantics.db.VirtualGraph;
45 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
46 import org.simantics.db.common.utils.Logger;
47 import org.simantics.db.exception.DatabaseException;
48 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
49 import org.simantics.db.exception.NoInverseException;
50 import org.simantics.db.exception.ResourceNotFoundException;
51 import org.simantics.db.impl.ResourceImpl;
52 import org.simantics.db.impl.graph.BarrierTracing;
53 import org.simantics.db.impl.graph.ReadGraphImpl;
54 import org.simantics.db.impl.graph.ReadGraphSupport;
55 import org.simantics.db.impl.procedure.IntProcedureAdapter;
56 import org.simantics.db.impl.procedure.InternalProcedure;
57 import org.simantics.db.impl.procedure.TripleIntProcedureAdapter;
58 import org.simantics.db.impl.support.ResourceSupport;
59 import org.simantics.db.procedure.AsyncMultiListener;
60 import org.simantics.db.procedure.AsyncMultiProcedure;
61 import org.simantics.db.procedure.AsyncProcedure;
62 import org.simantics.db.procedure.AsyncSetListener;
63 import org.simantics.db.procedure.ListenerBase;
64 import org.simantics.db.procedure.MultiProcedure;
65 import org.simantics.db.procedure.StatementProcedure;
66 import org.simantics.db.procedure.SyncMultiProcedure;
67 import org.simantics.db.request.AsyncMultiRead;
68 import org.simantics.db.request.ExternalRead;
69 import org.simantics.db.request.MultiRead;
70 import org.simantics.db.request.RequestFlags;
71 import org.simantics.layer0.Layer0;
72 import org.simantics.utils.DataContainer;
73 import org.simantics.utils.Development;
74 import org.simantics.utils.datastructures.Pair;
75 import org.simantics.utils.datastructures.collections.CollectionUtils;
76 import org.simantics.utils.datastructures.disposable.AbstractDisposable;
78 import gnu.trove.procedure.TIntProcedure;
79 import gnu.trove.procedure.TLongProcedure;
80 import gnu.trove.procedure.TObjectProcedure;
81 import gnu.trove.set.hash.THashSet;
82 import gnu.trove.set.hash.TIntHashSet;
84 @SuppressWarnings({"rawtypes", "unchecked"})
85 final public class QueryProcessor extends AbstractDisposable implements ReadGraphSupport {
87 public static int indent = 0;
92 public int boundQueries = 0;
95 final private int functionalRelation;
97 final private int superrelationOf;
99 final private int instanceOf;
101 final private int inverseOf;
103 final private int asserts;
105 final private int hasPredicate;
107 final private int hasPredicateInverse;
109 final private int hasObject;
111 final private int inherits;
113 final private int subrelationOf;
115 final private int rootLibrary;
118 * A cache for the root library resource. Initialized in
119 * {@link #getRootLibraryResource()}.
121 private volatile ResourceImpl rootLibraryResource;
123 final private int library;
125 final private int consistsOf;
127 final private int hasName;
129 AtomicInteger sleepers = new AtomicInteger(0);
131 boolean updating = false;
134 final public QueryCache cache;
135 final public QuerySupport querySupport;
136 final public Session session;
137 final public ResourceSupport resourceSupport;
139 final public Semaphore requests = new Semaphore(1);
141 final public QueryListening listening = new QueryListening(this);
143 QueryThread[] executors;
147 INIT, RUN, SLEEP, DISPOSED
151 final Scheduling scheduling;
153 public ThreadState[] threadStates;
155 final Object querySupportLock;
157 public Long modificationCounter = 0L;
159 public void close() {
164 * We are running errands while waiting for requests to complete.
165 * We can only run work that is part of the current root request to avoid any deadlocks
167 public boolean performPending(ReadGraphImpl under) {
168 SessionTask task = scheduling.getSubTask(under);
170 task.run(thread.get());
176 final public void scheduleNow(SessionTask request) {
177 SessionTask toExecute = scheduleOrReturnForExecution(request);
178 if(toExecute != null)
179 toExecute.run(thread.get());
182 final public SessionTask scheduleOrReturnForExecution(SessionTask request) {
184 return scheduling.scheduleOrReturnForExecution(request);
190 final public int THREAD_MASK;
192 final public static ThreadGroup QueryThreadGroup = new ThreadGroup("Query Thread Group");
194 public static abstract class SessionTask {
196 final protected ReadGraphImpl rootGraph;
197 private int counter = 0;
198 protected int position = 1;
199 private Exception trace;
201 public SessionTask() {
205 public SessionTask(ReadGraphImpl rootGraph) {
206 this.rootGraph = rootGraph;
209 public boolean isSubtask(ReadGraphImpl graph) {
210 return graph.isParent(rootGraph);
213 public abstract void run0(int thread);
215 public final void run(int thread) {
217 if(BarrierTracing.BOOKKEEPING) {
218 trace.printStackTrace();
219 new Exception().printStackTrace();
221 throw new IllegalStateException("Multiple invocations of SessionTask!");
223 if(BarrierTracing.BOOKKEEPING) {
224 trace = new Exception();
229 public boolean maybeReady() {
234 public String toString() {
235 if(rootGraph == null)
236 return "SessionTask[no graph]";
238 return "SessionTask[" + rootGraph.parent + "]";
243 public static abstract class SessionRead extends SessionTask {
245 final public Semaphore notify;
246 final public DataContainer<Throwable> throwable;
248 public SessionRead(DataContainer<Throwable> throwable, Semaphore notify) {
250 this.throwable = throwable;
251 this.notify = notify;
256 public boolean resume(ReadGraphImpl graph) {
257 return executors[0].runSynchronized();
260 public QueryProcessor(final int threads, QuerySupport core, Set<Thread> threadSet)
261 throws DatabaseException {
264 THREAD_MASK = threads - 1;
266 scheduling = new Scheduling(requests);
269 cache = new QueryCache(core, threads);
270 session = querySupport.getSession();
271 resourceSupport = querySupport.getSupport();
272 querySupportLock = core.getLock();
274 executors = new QueryThread[THREADS];
275 threadStates = new ThreadState[THREADS];
277 for (int i = 0; i < THREADS; i++) {
278 threadStates[i] = ThreadState.INIT;
281 for (int i = 0; i < THREADS; i++) {
285 executors[i] = new QueryThread(session, this, index, "Query Thread " + index);
287 threadSet.add(executors[i]);
292 for (int i = 0; i < THREADS; i++) {
293 executors[i].start();
296 // Make sure that query threads are up and running
297 while(sleepers.get() != THREADS) {
300 } catch (InterruptedException e) {
305 rootLibrary = core.getBuiltin("http:/");
306 boolean builtinsInstalled = rootLibrary != 0;
308 if (builtinsInstalled) {
309 functionalRelation = core.getBuiltin(Layer0.URIs.FunctionalRelation);
310 assert (functionalRelation != 0);
312 functionalRelation = 0;
314 if (builtinsInstalled) {
315 instanceOf = core.getBuiltin(Layer0.URIs.InstanceOf);
316 assert (instanceOf != 0);
320 if (builtinsInstalled) {
321 inverseOf = core.getBuiltin(Layer0.URIs.InverseOf);
322 assert (inverseOf != 0);
327 if (builtinsInstalled) {
328 inherits = core.getBuiltin(Layer0.URIs.Inherits);
329 assert (inherits != 0);
333 if (builtinsInstalled) {
334 asserts = core.getBuiltin(Layer0.URIs.Asserts);
335 assert (asserts != 0);
339 if (builtinsInstalled) {
340 hasPredicate = core.getBuiltin(Layer0.URIs.HasPredicate);
341 assert (hasPredicate != 0);
345 if (builtinsInstalled) {
346 hasPredicateInverse = core.getBuiltin(Layer0.URIs.HasPredicateInverse);
347 assert (hasPredicateInverse != 0);
349 hasPredicateInverse = 0;
351 if (builtinsInstalled) {
352 hasObject = core.getBuiltin(Layer0.URIs.HasObject);
353 assert (hasObject != 0);
357 if (builtinsInstalled) {
358 subrelationOf = core.getBuiltin(Layer0.URIs.SubrelationOf);
359 assert (subrelationOf != 0);
363 if (builtinsInstalled) {
364 superrelationOf = core.getBuiltin(Layer0.URIs.SuperrelationOf);
365 assert (superrelationOf != 0);
369 if (builtinsInstalled) {
370 library = core.getBuiltin(Layer0.URIs.Library);
371 assert (library != 0);
375 if (builtinsInstalled) {
376 consistsOf = core.getBuiltin(Layer0.URIs.ConsistsOf);
377 assert (consistsOf != 0);
381 if (builtinsInstalled) {
382 hasName = core.getBuiltin(Layer0.URIs.HasName);
383 assert (hasName != 0);
389 final public void releaseWrite(ReadGraphImpl graph) {
390 propagateChangesInQueryCache(graph);
391 modificationCounter++;
394 final public int getId(final Resource r) {
395 return querySupport.getId(r);
398 public QuerySupport getCore() {
402 public int getFunctionalRelation() {
403 return functionalRelation;
406 public int getInherits() {
410 public int getInstanceOf() {
414 public int getInverseOf() {
418 public int getSubrelationOf() {
419 return subrelationOf;
422 public int getSuperrelationOf() {
423 return superrelationOf;
426 public int getAsserts() {
430 public int getHasPredicate() {
434 public int getHasPredicateInverse() {
435 return hasPredicateInverse;
438 public int getHasObject() {
442 public int getRootLibrary() {
446 public Resource getRootLibraryResource() {
447 if (rootLibraryResource == null) {
448 // Synchronization is not needed here, it doesn't matter if multiple
449 // threads simultaneously set rootLibraryResource once.
450 int root = getRootLibrary();
452 throw new UnsupportedOperationException("database is not initialized, cannot get root library resource");
453 this.rootLibraryResource = new ResourceImpl(querySupport.getSupport(), root);
455 return rootLibraryResource;
458 public int getLibrary() {
462 public int getConsistsOf() {
466 public int getHasName() {
470 public void forResource(ReadGraphImpl graph, final String id, CacheEntry parent, final InternalProcedure<Integer> procedure) {
474 QueryCache.runnerURIToResource(graph, id, parent, null, new InternalProcedure<Integer>() {
477 public void execute(ReadGraphImpl graph, Integer result) throws DatabaseException {
479 if (result != null && result != 0) {
480 procedure.execute(graph, result);
484 // Fall back to using the fixed builtins.
485 // result = querySupport.getBuiltin(id);
486 // if (result != 0) {
487 // procedure.execute(graph, result);
492 // result = querySupport.getRandomAccessReference(id);
493 // } catch (ResourceNotFoundException e) {
494 // procedure.exception(graph, e);
499 procedure.execute(graph, result);
501 procedure.exception(graph, new ResourceNotFoundException(id));
507 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
508 procedure.exception(graph, t);
512 } catch (DatabaseException e) {
516 procedure.exception(graph, e);
518 } catch (DatabaseException e1) {
520 Logger.defaultLogError(e1);
528 public void forBuiltin(ReadGraphImpl graph, final String id, CacheEntry parent, final InternalProcedure<Integer> procedure) throws DatabaseException {
530 Integer result = querySupport.getBuiltin(id);
532 procedure.execute(graph, result);
534 procedure.exception(graph, new ResourceNotFoundException(id));
539 final <T> void runMultiRead(final ReadGraphImpl graph, MultiReadEntry cached, final MultiRead<T> query, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final SyncMultiProcedure<T> procedure) {
542 QueryCache.runnerMultiReadEntry(graph, query, parent, listener, procedure);
543 } catch (DatabaseException e) {
544 throw new IllegalStateException(e);
549 public final <T> void runAsyncMultiRead(final ReadGraphImpl graph, final AsyncMultiRead<T> query, final CacheEntry parent, final ListenerBase listener, final AsyncMultiProcedure<T> procedure) {
553 QueryCache.runnerAsyncMultiReadEntry(graph, query, parent, listener, procedure);
554 } catch (DatabaseException e) {
555 throw new IllegalStateException(e);
560 final <T> void runPrimitiveRead(ReadGraphImpl graph, ExternalReadEntry cached, final ExternalRead<T> query, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final AsyncProcedure<T> procedure) throws DatabaseException {
561 QueryCache.runnerExternalReadEntry(graph, query, parent, listener, procedure);
565 // public <T> T query(final ReadGraphImpl graph, final Read<T> query, final CacheEntry parent, final AsyncProcedure<T> procedure, final ListenerBase listener) throws DatabaseException {
567 // return QueryCache.resultReadEntry(graph, query, parent, listener, procedure);
571 public <T> void queryMultiRead(final ReadGraphImpl graph, final MultiRead<T> query, final CacheEntry parent, final ListenerBase listener, final SyncMultiProcedure<T> procedure) throws DatabaseException {
573 QueryCache.runnerMultiReadEntry(graph, query, parent, listener, procedure);
577 public <T> void queryPrimitiveRead(final ReadGraphImpl graph, final ExternalRead<T> query, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure<T> procedure) throws DatabaseException {
579 QueryCache.runnerExternalReadEntry(graph, query, parent, listener, procedure);
583 boolean isBound(ExternalReadEntry<?> entry) {
584 if(entry.hasParents()) return true;
585 else if(listening.hasListener(entry)) return true;
589 static class Dummy implements InternalProcedure<Object>, IntProcedure {
592 public void execute(ReadGraphImpl graph, int i) {
596 public void finished(ReadGraphImpl graph) {
600 public void execute(ReadGraphImpl graph, Object result) {
604 public void exception(ReadGraphImpl graph, Throwable throwable) {
609 private static final Dummy dummy = new Dummy();
612 public <Procedure> Object performForEach2(ReadGraphImpl graph, UnaryQuery<Procedure> query, CacheEntry parent, ListenerBase listener, Procedure procedure) throws Throwable {
614 if (DebugPolicy.PERFORM)
615 System.out.println("PE[ " + (query.hashCode() & THREAD_MASK) + "] " + query);
618 assert (!collecting);
620 assert(query.assertNotDiscarded());
622 registerDependencies(graph, query, parent, listener, procedure, false);
624 // FRESH, REFUTED, EXCEPTED go here
625 if (!query.isReady()) {
630 query.computeForEach(graph, this, (Procedure)dummy, true);
631 return query.get(graph, this, null);
637 return query.get(graph, this, procedure);
645 interface QueryCollectorSupport {
646 public CacheCollectionResult allCaches();
647 public Collection<CacheEntry> getRootList();
648 public int getCurrentSize();
649 public int calculateCurrentSize();
650 public CacheEntryBase iterate(int level);
651 public void remove();
652 public void setLevel(CacheEntryBase entry, int level);
653 public boolean start(boolean flush);
656 interface QueryCollector {
658 public void collect(int youngTarget, int allowedTimeInMs);
662 class QueryCollectorSupportImpl implements QueryCollectorSupport {
664 private static final boolean DEBUG = false;
665 private static final double ITERATION_RATIO = 0.2;
667 private CacheCollectionResult iteration = new CacheCollectionResult();
668 private boolean fresh = true;
669 private boolean needDataInStart = true;
671 QueryCollectorSupportImpl() {
675 public CacheCollectionResult allCaches() {
676 CacheCollectionResult result = new CacheCollectionResult();
677 QueryProcessor.this.allCaches(result);
682 public boolean start(boolean flush) {
683 // We need new data from query maps
685 if(needDataInStart || flush) {
686 // Last run ended after processing all queries => refresh data
687 restart(flush ? 0.0 : ITERATION_RATIO);
689 // continue with previous big data
691 // Notify caller about iteration situation
692 return iteration.isAtStart();
695 private void restart(double targetRatio) {
697 needDataInStart = true;
699 long start = System.nanoTime();
702 // We need new data from query maps
704 int iterationSize = iteration.size()+1;
705 int diff = calculateCurrentSize()-iterationSize;
707 double ratio = (double)diff / (double)iterationSize;
708 boolean dirty = Math.abs(ratio) >= targetRatio;
711 iteration = allCaches();
713 System.err.print("iterate: allCaches in " + 1e-9*(System.nanoTime()-start) + "s. (" + iteration.size() + ") ");
714 for(int i=0;i<CacheCollectionResult.LEVELS;i++)
715 System.err.print(" " + iteration.levels[i].size());
716 System.err.println("");
723 needDataInStart = false;
725 // We are returning here within the same GC round - reuse the cache table
734 public CacheEntryBase iterate(int level) {
736 CacheEntryBase entry = iteration.next(level);
738 restart(ITERATION_RATIO);
742 while(entry != null && entry.isDiscarded()) {
743 entry = iteration.next(level);
751 public void remove() {
756 public void setLevel(CacheEntryBase entry, int level) {
757 iteration.setLevel(entry, level);
760 public Collection<CacheEntry> getRootList() {
761 return cache.getRootList();
765 public int calculateCurrentSize() {
766 return cache.calculateCurrentSize();
770 public int getCurrentSize() {
775 // final private static int MINIMUM_SIZE = (int)(Runtime.getRuntime().maxMemory() / 600);
777 private QueryCollectorSupport collectorSupport = new QueryCollectorSupportImpl();
778 private QueryCollector collector = new QueryCollectorImpl(this, collectorSupport);
780 public int querySize() {
784 public void gc(int youngTarget, int allowedTimeInMs) {
786 collector.collect(youngTarget, allowedTimeInMs);
791 void processParentReport(CacheEntry entry, Map<CacheEntry, Set<CacheEntry>> workarea) {
793 if(entry.isDiscarded()) return;
794 if(workarea.containsKey(entry)) return;
796 Iterable<CacheEntry> parents = entry.getParents(this);
797 HashSet<CacheEntry> ps = new HashSet<CacheEntry>();
798 for(CacheEntry e : parents) {
799 if(e.isDiscarded()) continue;
801 processParentReport(e, workarea);
803 workarea.put(entry, ps);
807 public synchronized String reportQueryActivity(File file) throws IOException {
809 System.err.println("reportQueries " + file.getAbsolutePath());
814 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
816 List<Pair<String,Integer>> entries = CollectionUtils.valueSortedEntries(Development.histogram);
817 Collections.reverse(entries);
819 for(Pair<String,Integer> entry : entries) {
820 b.println(entry.first + ": " + entry.second);
825 Development.histogram.clear();
831 public synchronized String reportQueries(File file) throws IOException {
833 System.err.println("reportQueries " + file.getAbsolutePath());
838 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
840 long start = System.nanoTime();
842 // ArrayList<CacheEntry> all = ;
844 Map<CacheEntry, Set<CacheEntry>> workarea = new HashMap<CacheEntry, Set<CacheEntry>>();
845 Collection<CacheEntryBase> caches = allCaches(new CacheCollectionResult()).toCollection();
846 for(CacheEntryBase entry : caches) {
847 processParentReport(entry, workarea);
850 // for(CacheEntry e : all) System.err.println("entry: " + e);
852 long duration = System.nanoTime() - start;
853 System.err.println("Query root set in " + 1e-9*duration + "s.");
855 start = System.nanoTime();
857 HashMap<CacheEntry, Integer> flagMap = new HashMap<CacheEntry, Integer>();
861 for(CacheEntry entry : workarea.keySet()) {
862 boolean listener = listening.hasListenerAfterDisposing(entry);
863 boolean hasParents = entry.getParents(this).iterator().hasNext();
866 flagMap.put(entry, 0);
867 } else if (!hasParents) {
869 flagMap.put(entry, 1);
872 flagMap.put(entry, 2);
885 long start2 = System.nanoTime();
887 int boundCounter = 0;
888 int unboundCounter = 0;
889 int unknownCounter = 0;
891 for(CacheEntry<?> entry : workarea.keySet()) {
893 //System.err.println("process " + entry);
895 int flags = flagMap.get(entry);
896 int bindStatus = flags & 3;
898 if(bindStatus == 0) boundCounter++;
899 else if(bindStatus == 1) unboundCounter++;
900 else if(bindStatus == 2) unknownCounter++;
902 if(bindStatus < 2) continue;
905 for(CacheEntry parent : entry.getParents(this)) {
907 if(parent.isDiscarded()) flagMap.put(parent, 1);
909 int flags2 = flagMap.get(parent);
910 int bindStatus2 = flags2 & 3;
911 // Parent is bound => child is bound
912 if(bindStatus2 == 0) {
916 // Parent is unknown => child is unknown
917 else if (bindStatus2 == 2) {
924 flagMap.put(entry, newStatus);
928 duration = System.nanoTime() - start2;
929 System.err.println("Query analysis pass (" + boundCounter + "/" + unboundCounter + "/" + unknownCounter + ") in "+ 1e-9*duration + "s.");
930 b.println("Query analysis pass (" + boundCounter + "/" + unboundCounter + "/" + unknownCounter + ") in "+ 1e-9*duration + "s.");
932 } while(!done && loops++ < 20);
936 for(CacheEntry entry : workarea.keySet()) {
938 int bindStatus = flagMap.get(entry);
939 if(bindStatus == 2) System.err.println("Undefined bind status for " + entry);
945 duration = System.nanoTime() - start;
946 System.err.println("Query analysis in " + 1e-9*duration + "s.");
948 Map<Class<?>, Integer> counts = new HashMap<Class<?>, Integer>();
950 for(CacheEntry entry : workarea.keySet()) {
951 Class<?> clazz = entry.getClass();
952 if(entry instanceof ReadEntry) clazz = ((ReadEntry)entry).id.getClass();
953 else if(entry instanceof MultiReadEntry) clazz = ((MultiReadEntry)entry).id.getClass();
954 else if(entry instanceof AsyncReadEntry) clazz = ((AsyncReadEntry)entry).id.getClass();
955 else if(entry instanceof AsyncMultiReadEntry) clazz = ((AsyncMultiReadEntry)entry).id.getClass();
956 else if(entry instanceof ExternalReadEntry) clazz = ((ExternalReadEntry)entry).id.getClass();
957 Integer c = counts.get(clazz);
958 if(c == null) counts.put(clazz, -1);
959 else counts.put(clazz, c-1);
962 b.print("// Simantics DB client query report file\n");
963 b.print("// This file contains the following information\n");
964 b.print("// -The amount of cached query instances per query class\n");
965 b.print("// -The sizes of retained child sets\n");
966 b.print("// -List of parents for each query (search for 'P <query name>')\n");
967 b.print("// -Followed by status, where\n");
968 b.print("// -0=bound\n");
969 b.print("// -1=free\n");
970 b.print("// -2=unknown\n");
971 b.print("// -L=has listener\n");
972 b.print("// -List of children for each query (search for 'C <query name>')\n");
974 b.print("----------------------------------------\n");
976 b.print("// Queries by class\n");
977 for(Pair<Class<?>, Integer> p : CollectionUtils.valueSortedEntries(counts)) {
978 b.print(-p.second + " " + p.first.getName() + "\n");
981 Map<CacheEntry, Integer> hist = new HashMap<CacheEntry, Integer>();
982 for(CacheEntry e : workarea.keySet())
985 boolean changed = true;
987 while(changed && iter++<50) {
991 Map<CacheEntry, Integer> newHist = new HashMap<CacheEntry, Integer>();
992 for(CacheEntry e : workarea.keySet())
995 for(Map.Entry<CacheEntry, Set<CacheEntry>> e : workarea.entrySet()) {
996 Integer c = hist.get(e.getKey());
997 for(CacheEntry p : e.getValue()) {
998 Integer i = newHist.get(p);
1002 for(CacheEntry e : workarea.keySet()) {
1003 Integer value = newHist.get(e);
1004 Integer old = hist.get(e);
1005 if(!value.equals(old)) {
1007 // System.err.println("hist " + e + ": " + old + " => " + value);
1012 System.err.println("Retained set iteration " + iter);
1016 b.print("// Queries by retained set\n");
1017 for(Pair<CacheEntry, Integer> p : CollectionUtils.valueSortedEntries(hist)) {
1018 b.print("" + -p.second + " " + p.first + "\n");
1021 HashMap<CacheEntry, Collection<CacheEntry>> inverse = new HashMap<CacheEntry, Collection<CacheEntry>>();
1023 b.print("// Entry parent listing\n");
1024 for(CacheEntry entry : workarea.keySet()) {
1025 int status = flagMap.get(entry);
1026 boolean hasListener = listening.hasListenerAfterDisposing(entry);
1027 b.print("Q " + entry.toString());
1029 b.print(" (L" + status + ")");
1032 b.print(" (" + status + ")");
1035 for(CacheEntry parent : workarea.get(entry)) {
1036 Collection<CacheEntry> inv = inverse.get(parent);
1038 inv = new ArrayList<CacheEntry>();
1039 inverse.put(parent, inv);
1042 b.print(" " + parent.toString());
1047 b.print("// Entry child listing\n");
1048 for(Map.Entry<CacheEntry, Collection<CacheEntry>> entry : inverse.entrySet()) {
1049 b.print("C " + entry.getKey().toString());
1051 for(CacheEntry child : entry.getValue()) {
1052 Integer h = hist.get(child);
1056 b.print(" <no children>");
1058 b.print(" " + child.toString());
1063 b.print("#queries: " + workarea.keySet().size() + "\n");
1064 b.print("#listeners: " + listeners + "\n");
1068 return "Dumped " + workarea.keySet().size() + " queries.";
1072 boolean removeQuery(CacheEntry entry) {
1074 // This entry has been removed before. No need to do anything here.
1075 if(entry.isDiscarded()) return false;
1077 assert (!entry.isDiscarded());
1079 Query query = entry.getQuery();
1081 query.removeEntry(this);
1086 if((entry.getGCStatus() & CacheEntry.HAS_BEEN_BOUND) != 0)
1097 * @return true if this entry is being listened
1099 private boolean updateQuery(UpdateEntry e, LinkedList<UpdateEntry> todo, IdentityHashMap<CacheEntry, CacheEntry> immediates) throws DatabaseException {
1103 CacheEntry entry = e.entry;
1106 * If the dependency graph forms a DAG, some entries are inserted in the
1107 * todo list many times. They only need to be processed once though.
1109 if (entry.isDiscarded()) {
1110 if (Development.DEVELOPMENT) {
1111 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
1112 System.err.print("D");
1113 for (int i = 0; i < e.indent; i++)
1114 System.err.print(" ");
1115 System.err.println(entry.getQuery());
1118 // System.err.println(" => DISCARDED");
1122 // if (entry.isRefuted()) {
1123 // if (Development.DEVELOPMENT) {
1124 // if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
1125 // System.err.print("R");
1126 // for (int i = 0; i < e.indent; i++)
1127 // System.err.print(" ");
1128 // System.err.println(entry.getQuery());
1134 if (entry.isExcepted()) {
1135 if (Development.DEVELOPMENT) {
1136 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
1137 System.err.print("E");
1142 if (entry.isPending()) {
1143 if (Development.DEVELOPMENT) {
1144 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
1145 System.err.print("P");
1152 if (Development.DEVELOPMENT) {
1153 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
1154 System.err.print("U ");
1155 for (int i = 0; i < e.indent; i++)
1156 System.err.print(" ");
1157 System.err.print(entry.getQuery());
1161 Query query = entry.getQuery();
1162 int type = query.type();
1164 boolean hasListener = listening.hasListener(entry);
1166 if (Development.DEVELOPMENT) {
1167 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
1168 if(listening.hasListener(entry)) {
1169 System.err.println(" (L)");
1171 System.err.println("");
1176 if(entry.isPending() || entry.isExcepted()) {
1179 if ((type & RequestFlags.UPDATE_MASK) == RequestFlags.IMMEDIATE_UPDATE) {
1181 immediates.put(entry, entry);
1196 if ((type & RequestFlags.UPDATE_MASK) == RequestFlags.IMMEDIATE_UPDATE) {
1198 immediates.put(entry, entry);
1212 // System.err.println(" => FOO " + type);
1215 ArrayList<ListenerEntry> entries = listening.listeners.get(entry);
1216 if(entries != null) {
1217 for (ListenerEntry le : entries) {
1218 listening.scheduleListener(le);
1223 // If invalid, update parents
1224 if (type == RequestFlags.INVALIDATE) {
1225 listening.updateParents(e.indent, entry, todo);
1233 * @param av1 an array (guaranteed)
1234 * @param av2 any object
1235 * @return <code>true</code> if the two arrays are equal
1237 private final boolean arrayEquals(Object av1, Object av2) {
1240 Class<?> c1 = av1.getClass().getComponentType();
1241 Class<?> c2 = av2.getClass().getComponentType();
1242 if (c2 == null || !c1.equals(c2))
1244 boolean p1 = c1.isPrimitive();
1245 boolean p2 = c2.isPrimitive();
1249 return Arrays.equals((Object[]) av1, (Object[]) av2);
1250 if (boolean.class.equals(c1))
1251 return Arrays.equals((boolean[]) av1, (boolean[]) av2);
1252 else if (byte.class.equals(c1))
1253 return Arrays.equals((byte[]) av1, (byte[]) av2);
1254 else if (int.class.equals(c1))
1255 return Arrays.equals((int[]) av1, (int[]) av2);
1256 else if (long.class.equals(c1))
1257 return Arrays.equals((long[]) av1, (long[]) av2);
1258 else if (float.class.equals(c1))
1259 return Arrays.equals((float[]) av1, (float[]) av2);
1260 else if (double.class.equals(c1))
1261 return Arrays.equals((double[]) av1, (double[]) av2);
1262 throw new RuntimeException("??? Contact application querySupport.");
1267 final Object compareTo(ReadGraphImpl graph, final CacheEntry entry, final Object oldValue) {
1271 Query query = entry.getQuery();
1273 if (Development.DEVELOPMENT) {
1274 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_RECOMPUTE, Bindings.BOOLEAN)) {
1275 System.err.println("R " + query);
1279 entry.prepareRecompute(querySupport);
1281 ReadGraphImpl parentGraph = graph.forRecompute(entry);
1282 parentGraph.asyncBarrier.inc();
1283 query.recompute(parentGraph);
1284 parentGraph.asyncBarrier.dec();
1286 if(entry.isExcepted()) return ListenerEntry.NO_VALUE;
1288 Object newValue = entry.getResult();
1290 if (ListenerEntry.NO_VALUE == oldValue) {
1291 if (Development.DEVELOPMENT) {
1292 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_CHANGES, Bindings.BOOLEAN)) {
1293 System.out.println("C " + query);
1294 System.out.println("- " + oldValue);
1295 System.out.println("- " + newValue);
1301 boolean changed = false;
1303 if (newValue != null) {
1304 if (newValue.getClass().isArray()) {
1305 changed = !arrayEquals(newValue, oldValue);
1307 changed = !newValue.equals(oldValue);
1310 changed = (oldValue != null);
1312 if (Development.DEVELOPMENT) {
1313 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_CHANGES, Bindings.BOOLEAN)) {
1314 System.err.println("C " + query);
1315 System.err.println("- " + oldValue);
1316 System.err.println("- " + newValue);
1320 return changed ? newValue : ListenerEntry.NOT_CHANGED;
1322 } catch (Throwable t) {
1324 Logger.defaultLogError(t);
1326 return ListenerEntry.NO_VALUE;
1335 * @return true if this entry still has listeners
1337 public boolean update(final ReadGraphImpl graph, final CacheEntry entry) {
1339 assert (!cache.collecting);
1343 boolean hadListeners = false;
1344 boolean listenersUnknown = false;
1348 assert(entry != null);
1349 LinkedList<UpdateEntry> todo = new LinkedList<UpdateEntry>();
1350 IdentityHashMap<CacheEntry, CacheEntry> immediates = new IdentityHashMap<CacheEntry, CacheEntry>();
1351 todo.add(new UpdateEntry(null, entry, 0));
1355 // Walk the tree and collect immediate updates
1356 while (!todo.isEmpty()) {
1357 UpdateEntry e = todo.pop();
1358 hadListeners |= updateQuery(e, todo, immediates);
1361 if(immediates.isEmpty()) break;
1363 // Evaluate all immediate updates and collect parents to update
1364 for(CacheEntry immediate : immediates.values()) {
1366 if(immediate.isDiscarded()) {
1370 if(immediate.isExcepted()) {
1372 Object newValue = compareTo(graph, immediate, ListenerEntry.NO_VALUE);
1373 if (newValue != ListenerEntry.NOT_CHANGED)
1374 listening.updateParents(0, immediate, todo);
1378 Object oldValue = immediate.getResult();
1379 Object newValue = compareTo(graph, immediate, oldValue);
1381 if (newValue != ListenerEntry.NOT_CHANGED) {
1382 listening.updateParents(0, immediate, todo);
1384 // If not changed, keep the old value
1385 immediate.setResult(oldValue);
1386 immediate.setReady();
1387 listenersUnknown = true;
1397 } catch (Throwable t) {
1398 Logger.defaultLogError(t);
1404 return hadListeners | listenersUnknown;
1408 private ObjectUpdateSet scheduledObjectUpdates = new ObjectUpdateSet();
1409 private ValueUpdateSet scheduledValueUpdates = new ValueUpdateSet();
1410 private ValueUpdateSet scheduledInvalidates = new ValueUpdateSet();
1411 // Maybe use a mutex from util.concurrent?
1412 private Object primitiveUpdateLock = new Object();
1413 private THashSet scheduledPrimitiveUpdates = new THashSet();
1415 private ArrayList<CacheEntry> refutations = new ArrayList<>();
1417 private void markForUpdate(ReadGraphImpl graph, CacheEntry e) {
1422 private void updateRefutations(ReadGraphImpl graph) {
1424 for(CacheEntry e : refutations)
1427 refutations.clear();
1431 public void propagateChangesInQueryCache(final ReadGraphImpl graph) {
1433 // Make sure that listening has performed its work
1436 cache.dirty = false;
1439 if (Development.DEVELOPMENT) {
1440 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
1441 System.err.println("== Query update ==");
1445 // Special case - one statement
1446 if(scheduledObjectUpdates.size() == 1 && scheduledValueUpdates.size() == 0 && scheduledPrimitiveUpdates.size() == 0 && scheduledInvalidates.size() == 0) {
1448 long arg0 = scheduledObjectUpdates.getFirst();
1450 final int subject = (int)(arg0 >>> 32);
1451 final int predicate = (int)(arg0 & 0xffffffff);
1453 for(Objects o : QueryCache.entriesObjects(QueryProcessor.this, subject)) markForUpdate(graph, o);
1454 for(DirectObjects o : QueryCache.entriesDirectObjects(QueryProcessor.this, subject)) markForUpdate(graph, o);
1455 for(Statements o : QueryCache.entriesStatements(QueryProcessor.this, subject)) markForUpdate(graph, o);
1457 if(predicate == instanceOf || predicate == inherits || predicate == subrelationOf) {
1458 PrincipalTypes principalTypes = QueryCache.entryPrincipalTypes(QueryProcessor.this, subject);
1459 if(principalTypes != null) markForUpdate(graph, principalTypes);
1460 Types types = QueryCache.entryTypes(QueryProcessor.this, subject);
1461 if(types != null) markForUpdate(graph, types);
1464 if(predicate == subrelationOf) {
1465 SuperRelations superRelations = SuperRelations.entry(QueryProcessor.this, subject);
1466 if(superRelations != null) markForUpdate(graph, superRelations);
1469 DirectPredicates dp = QueryCache.entryDirectPredicates(QueryProcessor.this, subject);
1470 if(dp != null) markForUpdate(graph, dp);
1471 OrderedSet os = QueryCache.entryOrderedSet(QueryProcessor.this, predicate);
1472 if(os != null) markForUpdate(graph, os);
1474 updateRefutations(graph);
1476 scheduledObjectUpdates.clear();
1478 if (Development.DEVELOPMENT) {
1479 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
1480 System.err.println("== Query update ends ==");
1488 // Special case - one value
1489 if(scheduledObjectUpdates.size() == 0 && scheduledValueUpdates.size() == 1 && scheduledPrimitiveUpdates.size() == 0 && scheduledInvalidates.size() == 0) {
1491 int arg0 = scheduledValueUpdates.getFirst();
1493 ValueQuery valueQuery = QueryCache.entryValueQuery(QueryProcessor.this, arg0);
1494 if(valueQuery != null) markForUpdate(graph, valueQuery);
1496 updateRefutations(graph);
1498 scheduledValueUpdates.clear();
1500 if (Development.DEVELOPMENT) {
1501 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
1502 System.err.println("== Query update ends ==");
1510 final TIntHashSet predicates = new TIntHashSet();
1511 final TIntHashSet orderedSets = new TIntHashSet();
1513 THashSet primitiveUpdates;
1514 synchronized (primitiveUpdateLock) {
1515 primitiveUpdates = scheduledPrimitiveUpdates;
1516 scheduledPrimitiveUpdates = new THashSet();
1519 scheduledValueUpdates.forEach(new TIntProcedure() {
1522 public boolean execute(int arg0) {
1523 ValueQuery valueQuery = QueryCache.entryValueQuery(QueryProcessor.this, arg0);
1524 if(valueQuery != null) markForUpdate(graph, valueQuery);
1530 scheduledInvalidates.forEach(new TIntProcedure() {
1533 public boolean execute(int resource) {
1535 ValueQuery valueQuery = QueryCache.entryValueQuery(QueryProcessor.this, resource);
1536 if(valueQuery != null) markForUpdate(graph, valueQuery);
1538 PrincipalTypes principalTypes = QueryCache.entryPrincipalTypes(QueryProcessor.this, resource);
1539 if(principalTypes != null) markForUpdate(graph, principalTypes);
1540 Types types = QueryCache.entryTypes(QueryProcessor.this, resource);
1541 if(types != null) markForUpdate(graph, types);
1543 SuperRelations superRelations = SuperRelations.entry(QueryProcessor.this, resource);
1544 if(superRelations != null) markForUpdate(graph, superRelations);
1546 predicates.add(resource);
1553 scheduledObjectUpdates.forEach(new TLongProcedure() {
1556 public boolean execute(long arg0) {
1558 final int subject = (int)(arg0 >>> 32);
1559 final int predicate = (int)(arg0 & 0xffffffff);
1561 if(predicate == instanceOf || predicate == inherits || predicate == subrelationOf) {
1562 PrincipalTypes principalTypes = QueryCache.entryPrincipalTypes(QueryProcessor.this, subject);
1563 if(principalTypes != null) markForUpdate(graph, principalTypes);
1564 Types types = QueryCache.entryTypes(QueryProcessor.this, subject);
1565 if(types != null) markForUpdate(graph, types);
1568 if(predicate == subrelationOf) {
1569 SuperRelations superRelations = SuperRelations.entry(QueryProcessor.this, subject);
1570 if(superRelations != null) markForUpdate(graph, superRelations);
1573 predicates.add(subject);
1574 orderedSets.add(predicate);
1582 predicates.forEach(new TIntProcedure() {
1585 public boolean execute(final int subject) {
1587 for(Objects o : QueryCache.entriesObjects(QueryProcessor.this, subject)) markForUpdate(graph, o);
1588 for(DirectObjects o : QueryCache.entriesDirectObjects(QueryProcessor.this, subject)) markForUpdate(graph, o);
1589 for(Statements o : QueryCache.entriesStatements(QueryProcessor.this, subject)) markForUpdate(graph, o);
1591 DirectPredicates entry = QueryCache.entryDirectPredicates(QueryProcessor.this, subject);
1592 if(entry != null) markForUpdate(graph, entry);
1600 orderedSets.forEach(new TIntProcedure() {
1603 public boolean execute(int orderedSet) {
1605 OrderedSet entry = QueryCache.entryOrderedSet(QueryProcessor.this, orderedSet);
1606 if(entry != null) markForUpdate(graph, entry);
1614 updateRefutations(graph);
1616 primitiveUpdates.forEach(new TObjectProcedure() {
1619 public boolean execute(Object arg0) {
1621 ExternalReadEntry query = (ExternalReadEntry)cache.externalReadEntryMap.get(arg0);
1622 if (query != null) {
1623 boolean listening = update(graph, query);
1624 if (!listening && !query.hasParents()) {
1625 cache.externalReadEntryMap.remove(arg0);
1634 scheduledValueUpdates.clear();
1635 scheduledObjectUpdates.clear();
1636 scheduledInvalidates.clear();
1638 if (Development.DEVELOPMENT) {
1639 if(Development.<Boolean>getProperty(DevelopmentKeys.QUERYPROCESSOR_UPDATE, Bindings.BOOLEAN)) {
1640 System.err.println("== Query update ends ==");
1646 public void updateValue(final int resource) {
1647 scheduledValueUpdates.add(resource);
1651 public void updateStatements(final int resource, final int predicate) {
1652 scheduledObjectUpdates.add((((long)resource) << 32) + predicate);
1656 private int lastInvalidate = 0;
1658 public void invalidateResource(final int resource) {
1659 if(lastInvalidate == resource) return;
1660 scheduledValueUpdates.add(resource);
1661 lastInvalidate = resource;
1665 public void updatePrimitive(final ExternalRead primitive) {
1667 // External reads may be updated from arbitrary threads.
1668 // Synchronize to prevent race-conditions.
1669 synchronized (primitiveUpdateLock) {
1670 scheduledPrimitiveUpdates.add(primitive);
1672 querySupport.dirtyPrimitives();
1677 public synchronized String toString() {
1678 return "QueryProvider [size = " + cache.size + ", hits = " + cache.hits + " misses = " + cache.misses + ", updates = " + cache.updates + "]";
1682 protected void doDispose() {
1684 requests.release(Integer.MAX_VALUE / 2);
1686 for(int index = 0; index < THREADS; index++) {
1687 executors[index].dispose();
1691 for(int i=0;i<100;i++) {
1693 boolean alive = false;
1694 for(int index = 0; index < THREADS; index++) {
1695 alive |= executors[index].isAlive();
1700 } catch (InterruptedException e) {
1701 Logger.defaultLogError(e);
1706 // Then start interrupting
1707 for(int i=0;i<100;i++) {
1709 boolean alive = false;
1710 for(int index = 0; index < THREADS; index++) {
1711 alive |= executors[index].isAlive();
1714 for(int index = 0; index < THREADS; index++) {
1715 executors[index].interrupt();
1719 // // Then just destroy
1720 // for(int index = 0; index < THREADS; index++) {
1721 // executors[index].destroy();
1724 for(int index = 0; index < THREADS; index++) {
1726 executors[index].join(5000);
1727 } catch (InterruptedException e) {
1728 Logger.defaultLogError("QueryThread " + index + " will not die.", e);
1730 executors[index] = null;
1735 public int getHits() {
1739 public int getMisses() {
1740 return cache.misses;
1743 public int getSize() {
1747 public Set<Long> getReferencedClusters() {
1748 HashSet<Long> result = new HashSet<Long>();
1749 for (CacheEntry entry : QueryCache.entriesObjects(this)) {
1750 Objects query = (Objects) entry.getQuery();
1751 result.add(querySupport.getClusterId(query.r1()));
1753 for (CacheEntry entry : QueryCache.entriesDirectPredicates(this)) {
1754 DirectPredicates query = (DirectPredicates) entry.getQuery();
1755 result.add(querySupport.getClusterId(query.id));
1757 for (CacheEntry entry : cache.valueQueryMap.values()) {
1758 ValueQuery query = (ValueQuery) entry.getQuery();
1759 result.add(querySupport.getClusterId(query.id));
1764 public void assertDone() {
1767 CacheCollectionResult allCaches(CacheCollectionResult result) {
1769 return cache.allCaches(result);
1773 public void printDiagnostics() {
1776 public void requestCluster(ReadGraphImpl graph, long clusterId, Runnable runnable) {
1777 querySupport.requestCluster(graph, clusterId, runnable);
1780 public int clean() {
1781 collector.collect(0, Integer.MAX_VALUE);
1785 public void clean(final Collection<ExternalRead<?>> requests) {
1786 QueryCollectorSupport collectorSupport = new QueryCollectorSupport() {
1787 Iterator<ExternalRead<?>> iterator = requests.iterator();
1789 public CacheCollectionResult allCaches() {
1790 throw new UnsupportedOperationException();
1793 public CacheEntryBase iterate(int level) {
1794 if(iterator.hasNext()) {
1795 ExternalRead<?> request = iterator.next();
1796 ExternalReadEntry entry = cache.externalReadEntryMap.get(request);
1797 if (entry != null) return entry;
1798 else return iterate(level);
1800 iterator = requests.iterator();
1805 public void remove() {
1806 throw new UnsupportedOperationException();
1809 public void setLevel(CacheEntryBase entry, int level) {
1810 throw new UnsupportedOperationException();
1813 public Collection<CacheEntry> getRootList() {
1814 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>(requests.size());
1815 for (ExternalRead<?> request : requests) {
1816 ExternalReadEntry entry = cache.externalReadEntryMap.get(request);
1823 public int getCurrentSize() {
1827 public int calculateCurrentSize() {
1828 // This tells the collector to attempt collecting everything.
1829 return Integer.MAX_VALUE;
1832 public boolean start(boolean flush) {
1836 new QueryCollectorImpl2(this, collectorSupport).collect(0, Integer.MAX_VALUE);
1839 public void scanPending() {
1841 cache.scanPending();
1845 public ReadGraphImpl graphForVirtualRequest() {
1846 return ReadGraphImpl.createAsync(this);
1850 private HashMap<Resource, Class<?>> builtinValues;
1852 public Class<?> getBuiltinValue(Resource r) {
1853 if(builtinValues == null) initBuiltinValues();
1854 return builtinValues.get(r);
1857 Exception callerException = null;
1859 public interface AsyncBarrier {
1862 public void waitBarrier(Object request, ReadGraphImpl impl);
1863 public boolean isBlocking();
1866 // final public QueryProcessor processor;
1867 // final public QuerySupport support;
1869 // boolean disposed = false;
1871 private void initBuiltinValues() {
1873 Layer0 b = getSession().peekService(Layer0.class);
1874 if(b == null) return;
1876 builtinValues = new HashMap<Resource, Class<?>>();
1878 builtinValues.put(b.String, String.class);
1879 builtinValues.put(b.Double, Double.class);
1880 builtinValues.put(b.Float, Float.class);
1881 builtinValues.put(b.Long, Long.class);
1882 builtinValues.put(b.Integer, Integer.class);
1883 builtinValues.put(b.Byte, Byte.class);
1884 builtinValues.put(b.Boolean, Boolean.class);
1886 builtinValues.put(b.StringArray, String[].class);
1887 builtinValues.put(b.DoubleArray, double[].class);
1888 builtinValues.put(b.FloatArray, float[].class);
1889 builtinValues.put(b.LongArray, long[].class);
1890 builtinValues.put(b.IntegerArray, int[].class);
1891 builtinValues.put(b.ByteArray, byte[].class);
1892 builtinValues.put(b.BooleanArray, boolean[].class);
1896 // public ReadGraphSupportImpl(final QueryProcessor provider2) {
1898 // if (null == provider2) {
1899 // this.processor = null;
1903 // this.processor = provider2;
1904 // support = provider2.getCore();
1905 // initBuiltinValues();
1909 // final static public ReadGraphSupportImpl basedOn(ReadGraphSupportImpl impl) {
1910 // return new ReadGraphSupportImpl(impl.processor);
1914 final public Session getSession() {
1918 final public ResourceSupport getResourceSupport() {
1919 return resourceSupport;
1923 final public void forEachPredicate(final ReadGraphImpl impl, final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
1927 for(Resource predicate : getPredicates(impl, subject))
1928 procedure.execute(impl, predicate);
1930 procedure.finished(impl);
1932 } catch (Throwable e) {
1933 procedure.exception(impl, e);
1939 final public void forEachPredicate(final ReadGraphImpl impl, final Resource subject, final MultiProcedure<Resource> procedure) {
1941 throw new UnsupportedOperationException();
1943 // assert(subject != null);
1944 // assert(procedure != null);
1946 // final ListenerBase listener = getListenerBase(procedure);
1949 // QueryCache.runnerPredicates(impl, querySupport.getId(subject), impl.parent, listener, new IntProcedure() {
1952 // public void execute(ReadGraphImpl graph, int i) {
1954 // procedure.execute(querySupport.getResource(i));
1955 // } catch (Throwable t2) {
1956 // Logger.defaultLogError(t2);
1961 // public void finished(ReadGraphImpl graph) {
1963 // procedure.finished();
1964 // } catch (Throwable t2) {
1965 // Logger.defaultLogError(t2);
1967 //// impl.state.barrier.dec();
1971 // public void exception(ReadGraphImpl graph, Throwable t) {
1973 // procedure.exception(t);
1974 // } catch (Throwable t2) {
1975 // Logger.defaultLogError(t2);
1977 //// impl.state.barrier.dec();
1981 // } catch (DatabaseException e) {
1982 // Logger.defaultLogError(e);
1988 final public IntSet getPredicates(final ReadGraphImpl impl, final Resource subject) throws Throwable {
1989 return QueryCacheBase.resultPredicates(impl, querySupport.getId(subject), impl.parent, null);
1993 final public void forEachStatement(final ReadGraphImpl impl, final Resource subject,
1994 final Resource predicate, final MultiProcedure<Statement> procedure) {
1996 assert(subject != null);
1997 assert(predicate != null);
1998 assert(procedure != null);
2000 final ListenerBase listener = getListenerBase(procedure);
2002 // impl.state.barrier.inc();
2005 Statements.queryEach(impl, querySupport.getId(subject), querySupport.getId(predicate), this, impl.parent, listener, new TripleIntProcedureAdapter() {
2008 public void execute(ReadGraphImpl graph, int s, int p, int o) {
2010 procedure.execute(querySupport.getStatement(s, p, o));
2011 } catch (Throwable t2) {
2012 Logger.defaultLogError(t2);
2017 public void finished(ReadGraphImpl graph) {
2019 procedure.finished();
2020 } catch (Throwable t2) {
2021 Logger.defaultLogError(t2);
2023 // impl.state.barrier.dec();
2027 public void exception(ReadGraphImpl graph, Throwable t) {
2029 procedure.exception(t);
2030 } catch (Throwable t2) {
2031 Logger.defaultLogError(t2);
2033 // impl.state.barrier.dec();
2037 } catch (DatabaseException e) {
2038 Logger.defaultLogError(e);
2044 final public void forEachStatement(final ReadGraphImpl impl, final Resource subject,
2045 final Resource predicate, final AsyncMultiProcedure<Statement> procedure) {
2047 assert(subject != null);
2048 assert(predicate != null);
2049 assert(procedure != null);
2051 final ListenerBase listener = getListenerBase(procedure);
2053 TripleIntProcedureAdapter proc = new TripleIntProcedureAdapter() {
2055 boolean first = true;
2058 public void execute(ReadGraphImpl graph, int s, int p, int o) {
2061 procedure.execute(graph, querySupport.getStatement(s, p, o));
2063 procedure.execute(impl.newRestart(graph), querySupport.getStatement(s, p, o));
2065 } catch (Throwable t2) {
2066 Logger.defaultLogError(t2);
2071 public void finished(ReadGraphImpl graph) {
2076 procedure.finished(graph);
2077 // impl.state.barrier.dec(this);
2079 procedure.finished(impl.newRestart(graph));
2081 } catch (Throwable t2) {
2082 Logger.defaultLogError(t2);
2088 public void exception(ReadGraphImpl graph, Throwable t) {
2093 procedure.exception(graph, t);
2094 // impl.state.barrier.dec(this);
2096 procedure.exception(impl.newRestart(graph), t);
2098 } catch (Throwable t2) {
2099 Logger.defaultLogError(t2);
2106 int sId = querySupport.getId(subject);
2107 int pId = querySupport.getId(predicate);
2109 // if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(proc, "#Statements" + sId + "#" + pId);
2110 // else impl.state.barrier.inc(null, null);
2113 Statements.queryEach(impl, sId, pId, this, impl.parent, listener, proc);
2114 } catch (DatabaseException e) {
2115 Logger.defaultLogError(e);
2121 final public void forEachStatement(final ReadGraphImpl impl, final Resource subject,
2122 final Resource predicate, final StatementProcedure procedure) {
2124 assert(subject != null);
2125 assert(predicate != null);
2126 assert(procedure != null);
2128 final ListenerBase listener = getListenerBase(procedure);
2130 TripleIntProcedureAdapter proc = new TripleIntProcedureAdapter() {
2132 boolean first = true;
2135 public void execute(ReadGraphImpl graph, int s, int p, int o) {
2138 procedure.execute(graph, s, p, o);
2140 procedure.execute(impl.newRestart(graph), s, p, o);
2142 } catch (Throwable t2) {
2143 Logger.defaultLogError(t2);
2148 public void finished(ReadGraphImpl graph) {
2153 procedure.finished(graph);
2154 // impl.state.barrier.dec(this);
2156 procedure.finished(impl.newRestart(graph));
2158 } catch (Throwable t2) {
2159 Logger.defaultLogError(t2);
2165 public void exception(ReadGraphImpl graph, Throwable t) {
2170 procedure.exception(graph, t);
2171 // impl.state.barrier.dec(this);
2173 procedure.exception(impl.newRestart(graph), t);
2175 } catch (Throwable t2) {
2176 Logger.defaultLogError(t2);
2183 int sId = querySupport.getId(subject);
2184 int pId = querySupport.getId(predicate);
2186 // if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(proc, "#Statements" + sId + "#" + pId);
2187 // else impl.state.barrier.inc(null, null);
2190 Statements.queryEach(impl, sId, pId, this, impl.parent, listener, proc);
2191 } catch (DatabaseException e) {
2192 Logger.defaultLogError(e);
2198 final public void forStatementSet(final ReadGraphImpl impl, final Resource subject, final Resource predicate, final AsyncSetListener<Statement> procedure) {
2200 assert(subject != null);
2201 assert(predicate != null);
2202 assert(procedure != null);
2204 forEachStatement(impl, subject, predicate, new AsyncMultiListener<Statement>() {
2206 private Set<Statement> current = null;
2207 private Set<Statement> run = new HashSet<Statement>();
2210 public void execute(AsyncReadGraph graph, Statement result) {
2212 boolean found = false;
2214 if(current != null) {
2216 found = current.remove(result);
2220 if(!found) procedure.add(graph, result);
2227 public void finished(AsyncReadGraph graph) {
2229 if(current != null) {
2230 for(Statement r : current) procedure.remove(graph, r);
2235 run = new HashSet<Statement>();
2240 public void exception(AsyncReadGraph graph, Throwable t) {
2241 procedure.exception(graph, t);
2245 public boolean isDisposed() {
2246 return procedure.isDisposed();
2254 final public void forEachAssertedStatement(final ReadGraphImpl impl, final Resource subject,
2255 final Resource predicate, final AsyncMultiProcedure<Statement> procedure) {
2257 assert(subject != null);
2258 assert(predicate != null);
2259 assert(procedure != null);
2261 final ListenerBase listener = getListenerBase(procedure);
2263 // impl.state.barrier.inc();
2266 QueryCache.runnerAssertedStatements(impl, querySupport.getId(subject), querySupport.getId(predicate), impl.parent, listener, new TripleIntProcedureAdapter() {
2269 public void execute(ReadGraphImpl graph, int s, int p, int o) {
2271 procedure.execute(graph, querySupport.getStatement(s, p, o));
2272 } catch (Throwable t2) {
2273 Logger.defaultLogError(t2);
2278 public void finished(ReadGraphImpl graph) {
2280 procedure.finished(graph);
2281 } catch (Throwable t2) {
2282 Logger.defaultLogError(t2);
2284 // impl.state.barrier.dec();
2288 public void exception(ReadGraphImpl graph, Throwable t) {
2290 procedure.exception(graph, t);
2291 } catch (Throwable t2) {
2292 Logger.defaultLogError(t2);
2294 // impl.state.barrier.dec();
2298 } catch (DatabaseException e) {
2299 Logger.defaultLogError(e);
2304 private static ListenerBase getListenerBase(Object procedure) {
2305 if(procedure instanceof ListenerBase) return (ListenerBase)procedure;
2310 final public void forEachObject(final ReadGraphImpl impl, final Resource subject, final Resource predicate, final MultiProcedure<Resource> procedure) {
2312 assert(subject != null);
2313 assert(predicate != null);
2314 assert(procedure != null);
2316 final ListenerBase listener = getListenerBase(procedure);
2318 // impl.state.barrier.inc();
2321 QueryCache.runnerObjects(impl, querySupport.getId(subject), querySupport.getId(predicate), impl.parent, listener, new IntProcedure() {
2324 public void execute(ReadGraphImpl graph, int i) {
2326 procedure.execute(querySupport.getResource(i));
2327 } catch (Throwable t2) {
2328 Logger.defaultLogError(t2);
2333 public void finished(ReadGraphImpl graph) {
2335 procedure.finished();
2336 } catch (Throwable t2) {
2337 Logger.defaultLogError(t2);
2339 // impl.state.barrier.dec();
2343 public void exception(ReadGraphImpl graph, Throwable t) {
2344 System.out.println("forEachObject exception " + t);
2346 procedure.exception(t);
2347 } catch (Throwable t2) {
2348 Logger.defaultLogError(t2);
2350 // impl.state.barrier.dec();
2354 } catch (DatabaseException e) {
2355 Logger.defaultLogError(e);
2361 final public void forEachDirectPredicate(final ReadGraphImpl impl, final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
2363 assert(subject != null);
2364 assert(procedure != null);
2366 final ListenerBase listener = getListenerBase(procedure);
2368 int sId = querySupport.getId(subject);
2371 QueryCache.runnerDirectPredicates(impl, sId, impl.parent, listener, new InternalProcedure<IntSet>() {
2374 public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
2375 procedure.execute(graph, result);
2379 public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
2380 procedure.exception(graph, throwable);
2384 } catch (DatabaseException e) {
2385 Logger.defaultLogError(e);
2390 final public DirectStatements getDirectStatements(final ReadGraphImpl impl, final Resource subject, final boolean ignoreVirtual) {
2392 // assert(subject != null);
2393 // assert(procedure != null);
2395 // final ListenerBase listener = getListenerBase(procedure);
2397 // org.simantics.db.impl.query.DirectStatements.queryEach(impl, querySupport.getId(subject), this, impl.parent, listener, procedure);
2399 return querySupport.getStatements(impl, querySupport.getId(subject), this, ignoreVirtual);
2404 // final public void forEachDirectStatement(final ReadGraphImpl impl, final Resource subject, final SyncProcedure<DirectStatements> procedure, boolean ignoreVirtual) {
2406 // assert(subject != null);
2407 // assert(procedure != null);
2409 // final ListenerBase listener = getListenerBase(procedure);
2411 // org.simantics.db.impl.query.DirectStatements.queryEach(impl, querySupport.getId(subject), this, impl.parent, listener, procedure, ignoreVirtual);
2415 private static final Resource INVALID_RESOURCE = new ResourceImpl(null, Integer.MIN_VALUE);
2418 final public void forPossibleObject(final ReadGraphImpl impl, final Resource subject, final Resource predicate, final AsyncProcedure<Resource> procedure) {
2420 forEachObject(impl, subject, predicate, new AsyncMultiProcedure<Resource>() {
2422 private Resource single = null;
2425 public synchronized void execute(AsyncReadGraph graph, Resource result) {
2426 if(single == null) {
2429 single = INVALID_RESOURCE;
2434 public synchronized void finished(AsyncReadGraph graph) {
2435 if(single == null || single == INVALID_RESOURCE) procedure.execute(graph, null);
2436 else procedure.execute(graph, single);
2440 public synchronized void exception(AsyncReadGraph graph, Throwable throwable) {
2441 procedure.exception(graph, throwable);
2448 final public void forEachObject(final ReadGraphImpl impl, final Resource subject, final Resource predicate, final ListenerBase listener, final IntProcedure procedure) {
2450 final int sId = querySupport.getId(subject);
2451 final int pId = querySupport.getId(predicate);
2454 QueryCache.runnerObjects(impl, sId, pId, impl.parent, listener, procedure);
2455 } catch (DatabaseException e) {
2456 Logger.defaultLogError(e);
2461 static class Runner2Procedure implements IntProcedure {
2463 public int single = 0;
2464 public Throwable t = null;
2466 public void clear() {
2472 public void execute(ReadGraphImpl graph, int i) {
2473 if(single == 0) single = i;
2478 public void finished(ReadGraphImpl graph) {
2479 if(single == -1) single = 0;
2483 public void exception(ReadGraphImpl graph, Throwable throwable) {
2488 public int get() throws DatabaseException {
2490 if(t instanceof DatabaseException) throw (DatabaseException)t;
2491 else throw new DatabaseException(t);
2498 final public int getSingleObject(final ReadGraphImpl impl, final Resource subject, final Resource predicate) throws DatabaseException {
2500 final int sId = querySupport.getId(subject);
2501 final int pId = querySupport.getId(predicate);
2503 Runner2Procedure proc = new Runner2Procedure();
2504 QueryCache.runnerObjects(impl, sId, pId, impl.parent, null, proc);
2509 final public void forEachObject(final ReadGraphImpl impl, final Resource subject, final Resource predicate, final AsyncMultiProcedure<Resource> procedure) {
2511 assert(subject != null);
2512 assert(predicate != null);
2514 final ListenerBase listener = getListenerBase(procedure);
2516 if(impl.parent != null || listener != null) {
2518 IntProcedure ip = new IntProcedure() {
2520 AtomicBoolean first = new AtomicBoolean(true);
2523 public void execute(ReadGraphImpl graph, int i) {
2526 procedure.execute(impl, querySupport.getResource(i));
2528 procedure.execute(impl.newRestart(graph), querySupport.getResource(i));
2530 } catch (Throwable t2) {
2531 Logger.defaultLogError(t2);
2537 public void finished(ReadGraphImpl graph) {
2539 if(first.compareAndSet(true, false)) {
2540 procedure.finished(impl);
2541 // impl.state.barrier.dec(this);
2543 procedure.finished(impl.newRestart(graph));
2545 } catch (Throwable t2) {
2546 Logger.defaultLogError(t2);
2551 public void exception(ReadGraphImpl graph, Throwable t) {
2553 procedure.exception(graph, t);
2554 } catch (Throwable t2) {
2555 Logger.defaultLogError(t2);
2557 // impl.state.barrier.dec(this);
2561 public String toString() {
2562 return "forEachObject with " + procedure;
2567 // if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#Objects" + subject + "#" + predicate);
2568 // else impl.state.barrier.inc(null, null);
2570 forEachObject(impl, subject, predicate, listener, ip);
2574 IntProcedure ip = new IntProcedure() {
2577 public void execute(ReadGraphImpl graph, int i) {
2578 procedure.execute(graph, querySupport.getResource(i));
2582 public void finished(ReadGraphImpl graph) {
2583 procedure.finished(graph);
2587 public void exception(ReadGraphImpl graph, Throwable t) {
2588 procedure.exception(graph, t);
2592 public String toString() {
2593 return "forEachObject with " + procedure;
2598 forEachObject(impl, subject, predicate, listener, ip);
2605 final public void forObjectSet(final ReadGraphImpl impl, final Resource subject, final Resource predicate, final AsyncSetListener<Resource> procedure) {
2607 assert(subject != null);
2608 assert(predicate != null);
2609 assert(procedure != null);
2611 forEachObject(impl, subject, predicate, new AsyncMultiListener<Resource>() {
2613 private Set<Resource> current = null;
2614 private Set<Resource> run = new HashSet<Resource>();
2617 public void execute(AsyncReadGraph graph, Resource result) {
2619 boolean found = false;
2621 if(current != null) {
2623 found = current.remove(result);
2627 if(!found) procedure.add(graph, result);
2634 public void finished(AsyncReadGraph graph) {
2636 if(current != null) {
2637 for(Resource r : current) procedure.remove(graph, r);
2642 run = new HashSet<Resource>();
2647 public boolean isDisposed() {
2648 return procedure.isDisposed();
2652 public void exception(AsyncReadGraph graph, Throwable t) {
2653 procedure.exception(graph, t);
2657 public String toString() {
2658 return "forObjectSet " + procedure;
2666 final public void forPredicateSet(final ReadGraphImpl impl, final Resource subject, final AsyncSetListener<Resource> procedure) {
2668 assert(subject != null);
2669 assert(procedure != null);
2671 forEachPredicate(impl, subject, new AsyncMultiListener<Resource>() {
2673 private Set<Resource> current = null;
2674 private Set<Resource> run = new HashSet<Resource>();
2677 public void execute(AsyncReadGraph graph, Resource result) {
2679 boolean found = false;
2681 if(current != null) {
2683 found = current.remove(result);
2687 if(!found) procedure.add(graph, result);
2694 public void finished(AsyncReadGraph graph) {
2696 if(current != null) {
2697 for(Resource r : current) procedure.remove(graph, r);
2702 run = new HashSet<Resource>();
2707 public boolean isDisposed() {
2708 return procedure.isDisposed();
2712 public void exception(AsyncReadGraph graph, Throwable t) {
2713 procedure.exception(graph, t);
2717 public String toString() {
2718 return "forPredicateSet " + procedure;
2726 final public void forPrincipalTypeSet(final ReadGraphImpl impl, final Resource subject, final AsyncSetListener<Resource> procedure) {
2728 assert(subject != null);
2729 assert(procedure != null);
2731 forEachPrincipalType(impl, subject, new AsyncMultiListener<Resource>() {
2733 private Set<Resource> current = null;
2734 private Set<Resource> run = new HashSet<Resource>();
2737 public void execute(AsyncReadGraph graph, Resource result) {
2739 boolean found = false;
2741 if(current != null) {
2743 found = current.remove(result);
2747 if(!found) procedure.add(graph, result);
2754 public void finished(AsyncReadGraph graph) {
2756 if(current != null) {
2757 for(Resource r : current) procedure.remove(graph, r);
2762 run = new HashSet<Resource>();
2767 public boolean isDisposed() {
2768 return procedure.isDisposed();
2772 public void exception(AsyncReadGraph graph, Throwable t) {
2773 procedure.exception(graph, t);
2777 public String toString() {
2778 return "forPrincipalTypeSet " + procedure;
2786 final public void forAssertedObjectSet(final ReadGraphImpl impl, final Resource subject, final Resource predicate, final AsyncSetListener<Resource> procedure) {
2788 assert(subject != null);
2789 assert(predicate != null);
2790 assert(procedure != null);
2792 forEachAssertedObject(impl, subject, predicate, new AsyncMultiListener<Resource>() {
2794 private Set<Resource> current = null;
2795 private Set<Resource> run = new HashSet<Resource>();
2798 public void execute(AsyncReadGraph graph, Resource result) {
2800 boolean found = false;
2802 if(current != null) {
2804 found = current.remove(result);
2808 if(!found) procedure.add(graph, result);
2815 public void finished(AsyncReadGraph graph) {
2817 if(current != null) {
2818 for(Resource r : current) procedure.remove(graph, r);
2823 run = new HashSet<Resource>();
2828 public boolean isDisposed() {
2829 return procedure.isDisposed();
2833 public void exception(AsyncReadGraph graph, Throwable t) {
2834 procedure.exception(graph, t);
2838 public String toString() {
2839 return "forObjectSet " + procedure;
2847 final public void forAssertedStatementSet(final ReadGraphImpl impl, final Resource subject, final Resource predicate, final AsyncSetListener<Statement> procedure) {
2849 assert(subject != null);
2850 assert(predicate != null);
2851 assert(procedure != null);
2853 forEachAssertedStatement(impl, subject, predicate, new AsyncMultiListener<Statement>() {
2855 private Set<Statement> current = null;
2856 private Set<Statement> run = new HashSet<Statement>();
2859 public void execute(AsyncReadGraph graph, Statement result) {
2861 boolean found = false;
2863 if(current != null) {
2865 found = current.remove(result);
2869 if(!found) procedure.add(graph, result);
2876 public void finished(AsyncReadGraph graph) {
2878 if(current != null) {
2879 for(Statement s : current) procedure.remove(graph, s);
2884 run = new HashSet<Statement>();
2889 public boolean isDisposed() {
2890 return procedure.isDisposed();
2894 public void exception(AsyncReadGraph graph, Throwable t) {
2895 procedure.exception(graph, t);
2899 public String toString() {
2900 return "forStatementSet " + procedure;
2908 final public void forEachAssertedObject(final ReadGraphImpl impl, final Resource subject,
2909 final Resource predicate, final AsyncMultiProcedure<Resource> procedure) {
2911 assert(subject != null);
2912 assert(predicate != null);
2913 assert(procedure != null);
2915 final ListenerBase listener = getListenerBase(procedure);
2918 QueryCache.runnerAssertedStatements(impl, querySupport.getId(subject), querySupport.getId(predicate), impl.parent, listener, new TripleIntProcedure() {
2921 public void execute(ReadGraphImpl graph, int s, int p, int o) {
2923 procedure.execute(graph, querySupport.getResource(o));
2924 } catch (Throwable t2) {
2925 Logger.defaultLogError(t2);
2930 public void finished(ReadGraphImpl graph) {
2932 procedure.finished(graph);
2933 } catch (Throwable t2) {
2934 Logger.defaultLogError(t2);
2936 // impl.state.barrier.dec();
2940 public void exception(ReadGraphImpl graph, Throwable t) {
2942 procedure.exception(graph, t);
2943 } catch (Throwable t2) {
2944 Logger.defaultLogError(t2);
2946 // impl.state.barrier.dec();
2950 } catch (DatabaseException e) {
2951 Logger.defaultLogError(e);
2957 final public void forEachPrincipalType(final ReadGraphImpl impl, final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
2959 assert(subject != null);
2960 assert(procedure != null);
2962 final ListenerBase listener = getListenerBase(procedure);
2964 IntProcedure ip = new IntProcedure() {
2967 public void execute(ReadGraphImpl graph, int i) {
2969 procedure.execute(graph, querySupport.getResource(i));
2970 } catch (Throwable t2) {
2971 Logger.defaultLogError(t2);
2976 public void finished(ReadGraphImpl graph) {
2978 procedure.finished(graph);
2979 } catch (Throwable t2) {
2980 Logger.defaultLogError(t2);
2982 // impl.state.barrier.dec(this);
2986 public void exception(ReadGraphImpl graph, Throwable t) {
2988 procedure.exception(graph, t);
2989 } catch (Throwable t2) {
2990 Logger.defaultLogError(t2);
2992 // impl.state.barrier.dec(this);
2997 int sId = querySupport.getId(subject);
2999 // if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#PrincipalTypes#" + sId);
3000 // else impl.state.barrier.inc(null, null);
3003 QueryCache.runnerPrincipalTypes(impl, sId, impl.parent, listener, ip);
3004 } catch (DatabaseException e) {
3005 Logger.defaultLogError(e);
3011 final public void forEachPrincipalType(final ReadGraphImpl impl, final Resource subject, final MultiProcedure<Resource> procedure) {
3013 assert(subject != null);
3014 assert(procedure != null);
3016 final ListenerBase listener = getListenerBase(procedure);
3018 // impl.state.barrier.inc();
3021 QueryCache.runnerPrincipalTypes(impl, querySupport.getId(subject), impl.parent, listener, new IntProcedure() {
3024 public void execute(ReadGraphImpl graph, int i) {
3026 procedure.execute(querySupport.getResource(i));
3027 } catch (Throwable t2) {
3028 Logger.defaultLogError(t2);
3033 public void finished(ReadGraphImpl graph) {
3035 procedure.finished();
3036 } catch (Throwable t2) {
3037 Logger.defaultLogError(t2);
3039 // impl.state.barrier.dec();
3043 public void exception(ReadGraphImpl graph, Throwable t) {
3045 procedure.exception(t);
3046 } catch (Throwable t2) {
3047 Logger.defaultLogError(t2);
3049 // impl.state.barrier.dec();
3053 } catch (DatabaseException e) {
3054 Logger.defaultLogError(e);
3058 final public void forTypes(final ReadGraphImpl impl, final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3060 assert(subject != null);
3061 assert(procedure != null);
3063 final ListenerBase listener = getListenerBase(procedure);
3064 assert(listener == null);
3066 InternalProcedure<IntSet> ip = new InternalProcedure<IntSet>() {
3069 public void execute(final ReadGraphImpl graph, IntSet set) {
3070 procedure.execute(graph, set);
3074 public void exception(ReadGraphImpl graph, Throwable t) {
3075 procedure.exception(graph, t);
3080 int sId = querySupport.getId(subject);
3083 QueryCache.runnerTypes(impl, sId, impl.parent, listener, ip);
3084 } catch (DatabaseException e) {
3085 Logger.defaultLogError(e);
3091 final public IntSet getTypes(final ReadGraphImpl impl, final Resource subject) throws Throwable {
3093 assert(subject != null);
3095 return QueryCacheBase.resultTypes(impl, querySupport.getId(subject), impl.parent, null);
3100 final public RelationInfo getRelationInfo(final ReadGraphImpl impl, final Resource subject) throws DatabaseException {
3102 assert(subject != null);
3104 return QueryCache.resultRelationInfoQuery(impl, querySupport.getId(subject), impl.parent, null);
3109 final public void forSupertypes(final ReadGraphImpl impl, final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3111 assert(subject != null);
3112 assert(procedure != null);
3114 final ListenerBase listener = getListenerBase(procedure);
3117 QueryCache.runnerSuperTypes(impl, querySupport.getId(subject), impl.parent, listener, new InternalProcedure<IntSet>() {
3119 AtomicBoolean first = new AtomicBoolean(true);
3122 public void execute(final ReadGraphImpl graph, IntSet set) {
3123 // final HashSet<Resource> result = new HashSet<Resource>();
3124 // set.forEach(new TIntProcedure() {
3127 // public boolean execute(int type) {
3128 // result.add(querySupport.getResource(type));
3134 if(first.compareAndSet(true, false)) {
3135 procedure.execute(graph, set);
3136 // impl.state.barrier.dec();
3138 procedure.execute(impl.newRestart(graph), set);
3140 } catch (Throwable t2) {
3141 Logger.defaultLogError(t2);
3146 public void exception(ReadGraphImpl graph, Throwable t) {
3148 if(first.compareAndSet(true, false)) {
3149 procedure.exception(graph, t);
3150 // impl.state.barrier.dec();
3152 procedure.exception(impl.newRestart(graph), t);
3154 } catch (Throwable t2) {
3155 Logger.defaultLogError(t2);
3160 } catch (DatabaseException e) {
3161 Logger.defaultLogError(e);
3167 final public void forDirectSuperrelations(final ReadGraphImpl impl, final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
3169 assert(subject != null);
3170 assert(procedure != null);
3172 final ListenerBase listener = getListenerBase(procedure);
3174 IntProcedure ip = new IntProcedureAdapter() {
3177 public void execute(final ReadGraphImpl graph, int superRelation) {
3179 procedure.execute(graph, querySupport.getResource(superRelation));
3180 } catch (Throwable t2) {
3181 Logger.defaultLogError(t2);
3186 public void finished(final ReadGraphImpl graph) {
3188 procedure.finished(graph);
3189 } catch (Throwable t2) {
3190 Logger.defaultLogError(t2);
3192 // impl.state.barrier.dec(this);
3197 public void exception(ReadGraphImpl graph, Throwable t) {
3199 procedure.exception(graph, t);
3200 } catch (Throwable t2) {
3201 Logger.defaultLogError(t2);
3203 // impl.state.barrier.dec(this);
3208 int sId = querySupport.getId(subject);
3210 // if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#DirectSuperRelations#" + sId);
3211 // else impl.state.barrier.inc(null, null);
3214 QueryCache.runnerDirectSuperRelations(impl, sId, impl.parent, listener, ip);
3215 } catch (DatabaseException e) {
3216 Logger.defaultLogError(e);
3219 // DirectSuperRelations.queryEach(impl, sId, this, impl.parent, listener, ip);
3224 final public void forPossibleSuperrelation(final ReadGraphImpl impl, final Resource subject, final AsyncProcedure<Resource> procedure) {
3226 assert(subject != null);
3227 assert(procedure != null);
3229 final ListenerBase listener = getListenerBase(procedure);
3231 // impl.state.barrier.inc();
3233 PossibleSuperRelation.queryEach(impl, querySupport.getId(subject), this, impl.parent, listener, procedure);
3238 final public void forSuperrelations(final ReadGraphImpl impl, final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3240 assert(subject != null);
3241 assert(procedure != null);
3243 final ListenerBase listener = getListenerBase(procedure);
3245 InternalProcedure<IntSet> ip = new InternalProcedure<IntSet>() {
3248 public void execute(final ReadGraphImpl graph, IntSet set) {
3249 // final HashSet<Resource> result = new HashSet<Resource>();
3250 // set.forEach(new TIntProcedure() {
3253 // public boolean execute(int type) {
3254 // result.add(querySupport.getResource(type));
3260 procedure.execute(graph, set);
3261 } catch (Throwable t2) {
3262 Logger.defaultLogError(t2);
3264 // impl.state.barrier.dec(this);
3268 public void exception(ReadGraphImpl graph, Throwable t) {
3270 procedure.exception(graph, t);
3271 } catch (Throwable t2) {
3272 Logger.defaultLogError(t2);
3274 // impl.state.barrier.dec(this);
3279 int sId = querySupport.getId(subject);
3281 // if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#SuperRelations#" + sId);
3282 // else impl.state.barrier.inc(null, null);
3285 QueryCache.runnerSuperRelations(impl, sId, impl.parent, listener, ip);
3286 } catch (DatabaseException e) {
3287 Logger.defaultLogError(e);
3292 final public byte[] getValue(final ReadGraphImpl impl, final Resource subject) throws DatabaseException {
3293 return getValue(impl, querySupport.getId(subject));
3296 final public byte[] getValue(final ReadGraphImpl impl, final int subject) throws DatabaseException {
3297 return QueryCache.resultValueQuery(impl, subject, impl.parent, null);
3301 final public void forValue(final ReadGraphImpl impl, final Resource subject, final AsyncProcedure<byte[]> procedure) {
3303 assert(subject != null);
3304 assert(procedure != null);
3306 int sId = querySupport.getId(subject);
3308 // if(procedure != null) {
3310 final ListenerBase listener = getListenerBase(procedure);
3312 InternalProcedure<byte[]> ip = new InternalProcedure<byte[]>() {
3314 AtomicBoolean first = new AtomicBoolean(true);
3317 public void execute(ReadGraphImpl graph, byte[] result) {
3319 if(first.compareAndSet(true, false)) {
3320 procedure.execute(graph, result);
3321 // impl.state.barrier.dec(this);
3323 procedure.execute(impl.newRestart(graph), result);
3325 } catch (Throwable t2) {
3326 Logger.defaultLogError(t2);
3331 public void exception(ReadGraphImpl graph, Throwable t) {
3333 if(first.compareAndSet(true, false)) {
3334 procedure.exception(graph, t);
3335 // impl.state.barrier.dec(this);
3337 procedure.exception(impl.newRestart(graph), t);
3339 } catch (Throwable t2) {
3340 Logger.defaultLogError(t2);
3346 // if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#Value" + sId);
3347 // else impl.state.barrier.inc(null, null);
3350 QueryCache.runnerValueQuery(impl, sId, impl.parent, listener, ip);
3351 } catch (DatabaseException e) {
3352 throw new IllegalStateException("Internal error");
3357 // return QueryCacheBase.runnerValueQuery(impl, sId, impl.parent, null, null);
3361 // throw new IllegalStateException("Internal error");
3366 final public void forPossibleValue(final ReadGraphImpl impl, final Resource subject, final AsyncProcedure<byte[]> procedure) {
3368 assert(subject != null);
3369 assert(procedure != null);
3371 final ListenerBase listener = getListenerBase(procedure);
3373 if(impl.parent != null || listener != null) {
3375 InternalProcedure<byte[]> ip = new InternalProcedure<byte[]>() {
3377 AtomicBoolean first = new AtomicBoolean(true);
3380 public void execute(ReadGraphImpl graph, byte[] result) {
3382 if(first.compareAndSet(true, false)) {
3383 procedure.execute(graph, result);
3384 // impl.state.barrier.dec(this);
3386 procedure.execute(impl.newRestart(graph), result);
3388 } catch (Throwable t2) {
3389 Logger.defaultLogError(t2);
3394 public void exception(ReadGraphImpl graph, Throwable t) {
3396 if(first.compareAndSet(true, false)) {
3397 procedure.exception(graph, t);
3398 // impl.state.barrier.dec(this);
3400 procedure.exception(impl.newRestart(graph), t);
3402 } catch (Throwable t2) {
3403 Logger.defaultLogError(t2);
3409 int sId = querySupport.getId(subject);
3411 // if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#Value" + sId);
3412 // else impl.state.barrier.inc(null, null);
3415 QueryCache.runnerValueQuery(impl, sId, impl.parent, listener, ip);
3416 } catch (DatabaseException e) {
3417 Logger.defaultLogError(e);
3422 InternalProcedure<byte[]> ip = new InternalProcedure<byte[]>() {
3425 public void execute(ReadGraphImpl graph, byte[] result) {
3427 procedure.execute(graph, result);
3432 public void exception(ReadGraphImpl graph, Throwable t) {
3434 procedure.exception(graph, t);
3440 int sId = querySupport.getId(subject);
3443 QueryCache.runnerValueQuery(impl, sId, impl.parent, listener, ip);
3444 } catch (DatabaseException e) {
3445 Logger.defaultLogError(e);
3453 final public void forInverse(final ReadGraphImpl impl, final Resource relation, final AsyncProcedure<Resource> procedure) {
3455 assert(relation != null);
3456 assert(procedure != null);
3458 final ListenerBase listener = getListenerBase(procedure);
3460 IntProcedure ip = new IntProcedure() {
3462 private int result = 0;
3464 final AtomicBoolean found = new AtomicBoolean(false);
3465 final AtomicBoolean done = new AtomicBoolean(false);
3468 public void finished(ReadGraphImpl graph) {
3470 // Shall fire exactly once!
3471 if(done.compareAndSet(false, true)) {
3474 procedure.exception(graph, new NoInverseException(""));
3475 // impl.state.barrier.dec(this);
3477 procedure.execute(graph, querySupport.getResource(result));
3478 // impl.state.barrier.dec(this);
3480 } catch (Throwable t) {
3481 Logger.defaultLogError(t);
3488 public void execute(ReadGraphImpl graph, int i) {
3490 if(found.compareAndSet(false, true)) {
3493 // Shall fire exactly once!
3494 if(done.compareAndSet(false, true)) {
3496 procedure.exception(graph, new ManyObjectsForFunctionalRelationException("Multiple items e.g. " + this.result + " and " + result));
3497 // impl.state.barrier.dec(this);
3498 } catch (Throwable t) {
3499 Logger.defaultLogError(t);
3507 public void exception(ReadGraphImpl graph, Throwable t) {
3508 // Shall fire exactly once!
3509 if(done.compareAndSet(false, true)) {
3511 procedure.exception(graph, t);
3512 // impl.state.barrier.dec(this);
3513 } catch (Throwable t2) {
3514 Logger.defaultLogError(t2);
3521 int sId = querySupport.getId(relation);
3523 // if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#DirectObjects#" + sId);
3524 // else impl.state.barrier.inc(null, null);
3527 QueryCache.runnerObjects(impl, sId, getInverseOf(), impl.parent, listener, ip);
3528 } catch (DatabaseException e) {
3529 Logger.defaultLogError(e);
3535 final public void forResource(final ReadGraphImpl impl, final String id, final AsyncProcedure<Resource> procedure) {
3538 assert(procedure != null);
3540 InternalProcedure<Integer> ip = new InternalProcedure<Integer>() {
3543 public void execute(ReadGraphImpl graph, Integer result) {
3545 procedure.execute(graph, querySupport.getResource(result));
3546 } catch (Throwable t2) {
3547 Logger.defaultLogError(t2);
3549 // impl.state.barrier.dec(this);
3553 public void exception(ReadGraphImpl graph, Throwable t) {
3556 procedure.exception(graph, t);
3557 } catch (Throwable t2) {
3558 Logger.defaultLogError(t2);
3560 // impl.state.barrier.dec(this);
3565 // if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "Resource");
3566 // else impl.state.barrier.inc(null, null);
3568 forResource(impl, id, impl.parent, ip);
3573 final public void forBuiltin(final ReadGraphImpl impl, final String id, final AsyncProcedure<Resource> procedure) {
3576 assert(procedure != null);
3578 // impl.state.barrier.inc();
3581 forBuiltin(impl, id, impl.parent, new InternalProcedure<Integer>() {
3584 public void execute(ReadGraphImpl graph, Integer result) {
3586 procedure.execute(graph, querySupport.getResource(result));
3587 } catch (Throwable t2) {
3588 Logger.defaultLogError(t2);
3590 // impl.state.barrier.dec();
3594 public void exception(ReadGraphImpl graph, Throwable t) {
3596 procedure.exception(graph, t);
3597 } catch (Throwable t2) {
3598 Logger.defaultLogError(t2);
3600 // impl.state.barrier.dec();
3604 } catch (DatabaseException e) {
3605 Logger.defaultLogError(e);
3611 final public void forHasStatement(final ReadGraphImpl impl, final Resource subject, final AsyncProcedure<Boolean> procedure) {
3613 assert(subject != null);
3614 assert(procedure != null);
3616 final ListenerBase listener = getListenerBase(procedure);
3619 IntSet result = QueryCache.resultDirectPredicates(impl, querySupport.getId(subject), impl.parent, listener);
3620 procedure.execute(impl, !result.isEmpty());
3621 } catch (DatabaseException e) {
3622 procedure.exception(impl, e);
3628 final public void forHasStatement(final ReadGraphImpl impl, final Resource subject, final Resource predicate, final AsyncProcedure<Boolean> procedure) {
3630 assert(subject != null);
3631 assert(predicate != null);
3632 assert(procedure != null);
3634 AsyncMultiProcedure<Resource> ip = new AsyncMultiProcedureAdapter<Resource>() {
3636 boolean found = false;
3639 synchronized public void execute(AsyncReadGraph graph, Resource resource) {
3644 synchronized public void finished(AsyncReadGraph graph) {
3646 procedure.execute(graph, found);
3647 } catch (Throwable t2) {
3648 Logger.defaultLogError(t2);
3650 // impl.state.barrier.dec(this);
3654 public void exception(AsyncReadGraph graph, Throwable t) {
3656 procedure.exception(graph, t);
3657 } catch (Throwable t2) {
3658 Logger.defaultLogError(t2);
3660 // impl.state.barrier.dec(this);
3665 // if(AsyncBarrierImpl.BOOKKEEPING) impl.state.barrier.inc(ip, "#ForEachObject#" + subject + "#" + predicate);
3666 // else impl.state.barrier.inc(null, null);
3668 forEachObject(impl, subject, predicate, ip);
3673 final public void forHasStatement(final ReadGraphImpl impl, final Resource subject, final Resource predicate, final Resource object, final AsyncProcedure<Boolean> procedure) {
3675 assert(subject != null);
3676 assert(predicate != null);
3677 assert(procedure != null);
3679 // impl.state.barrier.inc();
3681 forEachObject(impl, subject, predicate, new AsyncMultiProcedureAdapter<Resource>() {
3683 boolean found = false;
3686 synchronized public void execute(AsyncReadGraph graph, Resource resource) {
3687 if(resource.equals(object)) found = true;
3691 synchronized public void finished(AsyncReadGraph graph) {
3693 procedure.execute(graph, found);
3694 } catch (Throwable t2) {
3695 Logger.defaultLogError(t2);
3697 // impl.state.barrier.dec();
3701 public void exception(AsyncReadGraph graph, Throwable t) {
3703 procedure.exception(graph, t);
3704 } catch (Throwable t2) {
3705 Logger.defaultLogError(t2);
3707 // impl.state.barrier.dec();
3715 final public void forHasValue(final ReadGraphImpl impl, final Resource subject, final AsyncProcedure<Boolean> procedure) {
3717 assert(subject != null);
3718 assert(procedure != null);
3720 final ListenerBase listener = getListenerBase(procedure);
3722 // impl.state.barrier.inc();
3725 QueryCache.runnerValueQuery(impl, querySupport.getId(subject), impl.parent, listener, new InternalProcedure<byte[]>() {
3728 public void execute(ReadGraphImpl graph, byte[] object) {
3729 boolean result = object != null;
3731 procedure.execute(graph, result);
3732 } catch (Throwable t2) {
3733 Logger.defaultLogError(t2);
3735 // impl.state.barrier.dec();
3739 public void exception(ReadGraphImpl graph, Throwable t) {
3741 procedure.exception(graph, t);
3742 } catch (Throwable t2) {
3743 Logger.defaultLogError(t2);
3745 // impl.state.barrier.dec();
3749 } catch (DatabaseException e) {
3750 Logger.defaultLogError(e);
3756 final public void forOrderedSet(final ReadGraphImpl impl, final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
3758 assert(subject != null);
3759 assert(procedure != null);
3761 final ListenerBase listener = getListenerBase(procedure);
3765 QueryCache.runnerOrderedSet(impl, querySupport.getId(subject), impl.parent, listener, new IntProcedure() {
3768 public void exception(ReadGraphImpl graph, Throwable t) {
3770 procedure.exception(graph, t);
3771 } catch (Throwable t2) {
3772 Logger.defaultLogError(t2);
3774 // impl.state.barrier.dec();
3778 public void execute(ReadGraphImpl graph, int i) {
3780 procedure.execute(graph, querySupport.getResource(i));
3781 } catch (Throwable t2) {
3782 Logger.defaultLogError(t2);
3787 public void finished(ReadGraphImpl graph) {
3789 procedure.finished(graph);
3790 } catch (Throwable t2) {
3791 Logger.defaultLogError(t2);
3793 // impl.state.barrier.dec();
3797 } catch (DatabaseException e) {
3798 Logger.defaultLogError(e);
3804 // final public <T> void query(final ReadGraphImpl impl, final AsyncRead<T> request, final CacheEntry parent, final AsyncProcedure<T> procedure, ListenerBase listener) throws DatabaseException {
3806 // assert(request != null);
3807 // assert(procedure != null);
3809 // QueryCache.runnerAsyncReadEntry(impl, request, parent, listener, procedure);
3814 // final public <T> T tryQuery(final ReadGraphImpl graph, final Read<T> request) throws DatabaseException {
3816 // assert(graph != null);
3817 // assert(request != null);
3819 // final ReadEntry entry = (ReadEntry)cache.getCached(request);
3820 // if(entry != null && entry.isReady()) {
3821 // return (T)entry.get(graph, this, null);
3823 // return request.perform(graph);
3828 // final public <T> T tryQuery(final ReadGraphImpl graph, final ExternalRead<T> request) throws DatabaseException {
3830 // assert(graph != null);
3831 // assert(request != null);
3833 // final ExternalReadEntry<T> entry = cache.externalReadMap.get(request);
3834 // if(entry != null && entry.isReady()) {
3835 // if(entry.isExcepted()) {
3836 // Throwable t = (Throwable)entry.getResult();
3837 // if(t instanceof DatabaseException) throw (DatabaseException)t;
3838 // else throw new DatabaseException(t);
3840 // return (T)entry.getResult();
3844 // final DataContainer<T> result = new DataContainer<T>();
3845 // final DataContainer<Throwable> exception = new DataContainer<Throwable>();
3847 // request.register(graph, new Listener<T>() {
3850 // public void exception(Throwable t) {
3851 // exception.set(t);
3855 // public void execute(T t) {
3860 // public boolean isDisposed() {
3866 // Throwable t = exception.get();
3868 // if(t instanceof DatabaseException) throw (DatabaseException)t;
3869 // else throw new DatabaseException(t);
3872 // return result.get();
3879 // final public <T> void tryQuery(final ReadGraphImpl graph, final AsyncRead<T> request, AsyncProcedure<T> procedure) {
3881 // assert(graph != null);
3882 // assert(request != null);
3884 // final AsyncReadEntry entry = cache.asyncReadMap.get(request);
3885 // if(entry != null && entry.isReady()) {
3886 // if(entry.isExcepted()) {
3887 // procedure.exception(graph, (Throwable)entry.getResult());
3889 // procedure.execute(graph, (T)entry.getResult());
3892 // request.perform(graph, procedure);
3898 final public <T> void query(final ReadGraphImpl impl, final MultiRead<T> request, final CacheEntry parent, final SyncMultiProcedure<T> procedure, ListenerBase listener) {
3900 assert(request != null);
3901 assert(procedure != null);
3905 queryMultiRead(impl, request, parent, listener, procedure);
3907 } catch (DatabaseException e) {
3909 throw new IllegalStateException(e);
3916 final public <T> void query(final ReadGraphImpl impl, final AsyncMultiRead<T> request, final CacheEntry parent, final AsyncMultiProcedure<T> procedure, ListenerBase listener) {
3918 assert(request != null);
3919 assert(procedure != null);
3921 // impl.state.barrier.inc();
3923 runAsyncMultiRead(impl, request, parent, listener, new AsyncMultiProcedure<T>() {
3925 public void execute(AsyncReadGraph graph, T result) {
3928 procedure.execute(graph, result);
3929 } catch (Throwable t2) {
3930 Logger.defaultLogError(t2);
3935 public void finished(AsyncReadGraph graph) {
3938 procedure.finished(graph);
3939 } catch (Throwable t2) {
3940 Logger.defaultLogError(t2);
3943 // impl.state.barrier.dec();
3948 public String toString() {
3949 return procedure.toString();
3953 public void exception(AsyncReadGraph graph, Throwable t) {
3956 procedure.exception(graph, t);
3957 } catch (Throwable t2) {
3958 Logger.defaultLogError(t2);
3961 // impl.state.barrier.dec();
3970 // final public <T> void query(final ReadGraphImpl impl, final ExternalRead<T> request, final CacheEntry parent, final Procedure<T> procedure, ListenerBase listener) throws DatabaseException {
3972 // assert(request != null);
3973 // assert(procedure != null);
3977 // queryPrimitiveRead(impl, request, parent, listener, new AsyncProcedure<T>() {
3980 // public String toString() {
3981 // return procedure.toString();
3985 // public void execute(AsyncReadGraph graph, T result) {
3987 // procedure.execute(result);
3988 // } catch (Throwable t2) {
3989 // Logger.defaultLogError(t2);
3994 // public void exception(AsyncReadGraph graph, Throwable throwable) {
3996 // procedure.exception(throwable);
3997 // } catch (Throwable t2) {
3998 // Logger.defaultLogError(t2);
4004 // } catch (DatabaseException e) {
4006 // throw new IllegalStateException(e);
4013 public VirtualGraph getProvider(Resource subject, Resource predicate, Resource object) {
4015 return querySupport.getProvider(querySupport.getId(subject), querySupport.getId(predicate), querySupport.getId(object));
4020 public VirtualGraph getProvider(Resource subject, Resource predicate) {
4022 return querySupport.getProvider(querySupport.getId(subject), querySupport.getId(predicate));
4027 public VirtualGraph getValueProvider(Resource subject) {
4029 return querySupport.getValueProvider(querySupport.getId(subject));
4033 public boolean resumeTasks(ReadGraphImpl graph) {
4035 return querySupport.resume(graph);
4039 public boolean isImmutable(int resourceId) {
4040 return querySupport.isImmutable(resourceId);
4043 public boolean isImmutable(Resource resource) {
4044 ResourceImpl impl = (ResourceImpl)resource;
4045 return isImmutable(impl.id);
4050 public Layer0 getL0(ReadGraph graph) {
4052 L0 = Layer0.getInstance(graph);
4057 public Layer0 getL0() {
4061 public static ThreadLocal<Integer> thread = new ThreadLocal<Integer>() {
4062 protected Integer initialValue() {