]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Predicates.java
QueryListening sync is slow
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Predicates.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import org.simantics.db.common.exception.DebugException;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.impl.procedure.InternalProcedure;
18 import org.simantics.db.request.RequestFlags;
19
20 import gnu.trove.procedure.TIntProcedure;
21
22 final public class Predicates extends UnaryQueryP<IntSet> {
23
24         Predicates(final int r) {
25         super(r);
26     }
27
28         @Override
29         final public void removeEntry(QueryProcessor provider) {
30         provider.cache.remove(this);
31         }
32     
33     final static private void forAssertions(ReadGraphImpl graph, int r, Predicates parent, final IntSet set) throws DatabaseException {
34
35         QueryCache.runnerPrincipalTypes(graph, r, parent, null, new SyncIntProcedure() {
36             
37             @Override
38             public void run(ReadGraphImpl graph) throws DatabaseException {
39             }
40             
41             IntProcedure proc = new IntProcedure() {
42
43                 @Override
44                 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
45                         set.add(i);
46                 }
47
48                 @Override
49                 public void finished(ReadGraphImpl graph) throws DatabaseException {
50                     dec(graph);
51                 }
52                                 
53                                 @Override
54                                 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
55                     if(DebugException.DEBUG) new DebugException(t).printStackTrace();
56                     }
57
58             }; 
59
60             @Override
61             public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
62
63                 inc();
64                 QueryCache.runnerAssertedPredicates(graph, type, parent, null, proc);
65                 
66             }
67             
68             @Override
69             public void finished(ReadGraphImpl graph) throws DatabaseException {
70                 dec(graph);       
71             }
72             
73         });
74         
75
76     }
77
78         @Override
79         public void compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
80                 computeForEach(graph, id, this, procedure);
81         }
82
83         public static void computeForEach(ReadGraphImpl graph, final int r, final Predicates entry, final InternalProcedure<IntSet> procedure_) throws DatabaseException {
84             
85             InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
86         
87         IntSet direct = QueryCache.resultDirectPredicates(graph, r, entry, null);
88                 
89                 IntSet result = new IntSet(graph.processor.querySupport);
90         forAssertions(graph, r, entry, result);
91         
92         if(result.isEmpty()) {
93                 procedure.execute(graph, direct);
94         } else {
95                 direct.forEach(new TIntProcedure() {
96                         @Override
97                         public boolean execute(int value) {
98                                 result.add(value);
99                                 return true;
100                         }
101                 });
102                 procedure.execute(graph, result);
103         }
104         
105         if(entry != null) entry.performFromCache(graph, procedure_);
106         
107     }
108     
109     @Override
110     public String toString() {
111         return "Predicates[" + id + "]";
112     }
113
114     @Override
115     public void clearResult(QuerySupport support) {
116         setResult(new IntSet(support));
117     }
118     
119     @Override
120     public int type() {
121         return RequestFlags.IMMEDIATE_UPDATE;
122     }
123     
124 }