]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/QueryControlImpl.java
Merge "Save cluster sets only when creating DB snapshots"
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / QueryControlImpl.java
1 package fi.vtt.simantics.procore.internal;
2
3 import java.util.Collection;
4
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;
18
19 public class QueryControlImpl implements QueryControl {
20         
21         final private SessionImplSocket session;
22         
23         QueryControlImpl(SessionImplSocket session) {
24                 this.session = session;
25         }
26
27         @Override
28         public int getAmountOfQueryThreads() {
29                 return session.getAmountOfQueryThreads();
30         }
31         
32     @Override
33     public int getGraphThread(AsyncReadGraph graph) {
34         return 0;
35     }
36
37     @Override
38     public int flush() {
39         final DataContainer<Integer> result = new DataContainer<Integer>(); 
40         try {
41             session.syncRequest(new WriteRequest() {
42
43                 @Override
44                 public void perform(WriteGraph graph) throws DatabaseException {
45                     result.set(session.queryProvider2.clean());
46                 }
47                 
48             });
49         } catch (DatabaseException e) {
50             e.printStackTrace();
51         }
52         return result.get();
53     }
54
55     @Override
56     public int flush(ReadGraph graph) {
57         return session.queryProvider2.clean();
58     }
59     
60     @Override
61     public int count() {
62         return session.queryProvider2.querySize();
63     }
64     
65     @Override
66     public void gc(ReadGraph graph, int allowedTimeInMs) {
67         // 20% young target
68         session.queryProvider2.gc(20, allowedTimeInMs);
69     }
70     
71     @Override
72     public void gc(final Collection<ExternalRead<?>> requests) {
73         try {
74                 session.syncRequest(new WriteRequest() {
75                 @Override
76                 public void perform(WriteGraph graph) throws DatabaseException {
77                     gc(graph, requests);
78                 }
79             });
80         } catch (DatabaseException e) {
81             e.printStackTrace();
82         }
83     }
84
85     @Override
86     public void gc(WriteGraph graph, final Collection<ExternalRead<?>> requests) {
87         if (graph == null)
88             throw new IllegalArgumentException("null WriteGraph");
89         if (requests == null)
90             throw new IllegalArgumentException("null requests");
91         session.queryProvider2.clean(requests);
92     }
93     
94         @Override
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();
102 //              
103 //              AsyncReadGraph targetGraph = impl.newAsync();
104                 procedure.execute(impl, resource);
105 //              impl.state.barrier.dec();
106                 
107                 return false;
108                 
109         }
110
111         @Override
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();
119 //              
120 //              AsyncReadGraph targetGraph = impl.newAsync();
121                 procedure.execute(impl, context, resource);
122 //              impl.state.barrier.dec();
123                 
124                 return false;
125         }
126
127         @Override
128         public void schedule(AsyncReadGraph graph, int targetThread, final ControlProcedure procedure) {
129                 final ReadGraphImpl impl = (ReadGraphImpl)graph;
130
131 //              impl.state.barrier.inc();
132 //
133 //              AsyncReadGraph targetGraph = impl.newAsync();
134                 procedure.execute(impl);
135 //              impl.state.barrier.dec();
136
137         }
138         
139         @Override
140         public ReadGraph getIndependentGraph(ReadGraph graph) {
141                 ReadGraphImpl impl = (ReadGraphImpl)graph;
142                 return impl.withParent(null);
143         }
144
145         @Override
146         public boolean hasParentRequest(ReadGraph graph) {
147                 ReadGraphImpl impl = (ReadGraphImpl)graph;
148                 return impl.parent != null;
149         }
150         
151         @Override
152         public boolean resume(AsyncReadGraph graph) {
153                 ReadGraphImpl impl = (ReadGraphImpl)graph;
154                 return impl.processor.querySupport.resume(impl);
155         }
156
157 }