1 package fi.vtt.simantics.procore.internal;
3 import java.util.Collection;
5 import org.simantics.db.AsyncReadGraph;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.common.request.WriteRequest;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.impl.ResourceImpl;
12 import org.simantics.db.impl.graph.ReadGraphImpl;
13 import org.simantics.db.procedure.AsyncContextMultiProcedure;
14 import org.simantics.db.procedure.AsyncMultiProcedure;
15 import org.simantics.db.request.ExternalRead;
16 import org.simantics.db.service.QueryControl;
17 import org.simantics.utils.DataContainer;
19 public class QueryControlImpl implements QueryControl {
21 final private SessionImplSocket session;
23 QueryControlImpl(SessionImplSocket session) {
24 this.session = session;
28 public int getAmountOfQueryThreads() {
29 return session.getAmountOfQueryThreads();
33 public int getGraphThread(AsyncReadGraph graph) {
39 final DataContainer<Integer> result = new DataContainer<Integer>();
41 session.syncRequest(new WriteRequest() {
44 public void perform(WriteGraph graph) throws DatabaseException {
45 result.set(session.queryProvider2.clean());
49 } catch (DatabaseException e) {
56 public int flush(ReadGraph graph) {
57 return session.queryProvider2.clean();
62 return session.queryProvider2.querySize();
66 public void gc(ReadGraph graph, int allowedTimeInMs) {
68 session.queryProvider2.gc(20, allowedTimeInMs);
72 public void gc(final Collection<ExternalRead<?>> requests) {
74 session.syncRequest(new WriteRequest() {
76 public void perform(WriteGraph graph) throws DatabaseException {
80 } catch (DatabaseException e) {
86 public void gc(WriteGraph graph, final Collection<ExternalRead<?>> requests) {
88 throw new IllegalArgumentException("null WriteGraph");
90 throw new IllegalArgumentException("null requests");
91 session.queryProvider2.clean(requests);
95 public boolean scheduleByCluster(AsyncReadGraph graph, final Resource resource, final AsyncMultiProcedure<Resource> procedure) {
96 final ReadGraphImpl impl = (ReadGraphImpl)graph;
97 ResourceImpl res = (ResourceImpl)resource;
98 int targetThread = ((res.id >>> 16) & session.queryProvider2.THREAD_MASK);
99 if(0 == targetThread) return true;
100 //System.err.println("scheduleByCluster[" + res.id + "|" + (res.id>>>16) + "] " + impl.callerThread + " -> " + targetThread);
101 // impl.state.barrier.inc();
103 // AsyncReadGraph targetGraph = impl.newAsync();
104 procedure.execute(impl, resource);
105 // impl.state.barrier.dec();
112 public <C> boolean scheduleByCluster(AsyncReadGraph graph, final Resource resource, final C context, final AsyncContextMultiProcedure<C, Resource> procedure) {
113 final ReadGraphImpl impl = (ReadGraphImpl)graph;
114 ResourceImpl res = (ResourceImpl)resource;
115 int targetThread = ((res.id >>> 16) & session.queryProvider2.THREAD_MASK);
116 if(0 == targetThread) return true;
117 //System.err.println("scheduleByCluster[" + res.id + "|" + (res.id>>>16) + "] " + impl.callerThread + " -> " + targetThread);
118 // impl.state.barrier.inc();
120 // AsyncReadGraph targetGraph = impl.newAsync();
121 procedure.execute(impl, context, resource);
122 // impl.state.barrier.dec();
128 public void schedule(AsyncReadGraph graph, int targetThread, final ControlProcedure procedure) {
129 final ReadGraphImpl impl = (ReadGraphImpl)graph;
131 // impl.state.barrier.inc();
133 // AsyncReadGraph targetGraph = impl.newAsync();
134 procedure.execute(impl);
135 // impl.state.barrier.dec();
140 public ReadGraph getIndependentGraph(ReadGraph graph) {
141 ReadGraphImpl impl = (ReadGraphImpl)graph;
142 return impl.withParent(null);
146 public boolean hasParentRequest(ReadGraph graph) {
147 ReadGraphImpl impl = (ReadGraphImpl)graph;
148 return impl.parent != null;
152 public boolean resume(AsyncReadGraph graph) {
153 ReadGraphImpl impl = (ReadGraphImpl)graph;
154 return impl.processor.querySupport.resume(impl);