]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java
QueryListening sync is slow
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / DirectObjects.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 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.exception.DatabaseException;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.request.RequestFlags;
17
18 final public class DirectObjects extends CollectionBinaryQuery<IntProcedure> implements IntProcedure {
19
20     DirectObjects(final int r1, final int r2) {
21         super(r1, r2);
22     }
23
24     @Override
25     public int type() {
26         return RequestFlags.INVALIDATE;
27     }
28
29     @Override
30     public final void removeEntry(QueryProcessor provider) {
31         provider.cache.remove(this);
32     }
33
34     // @Override
35     public Object compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
36         computeForEach(graph, r1(), r2(), this, procedure);
37         return getResult();
38     }
39
40     public static void computeForEach(ReadGraphImpl graph, int r1, int r2, final DirectObjects entry,
41             final IntProcedure procedure_) throws DatabaseException {
42
43         IntProcedure procedure = entry != null ? entry : procedure_;
44
45         QueryProcessor processor = graph.processor;
46
47         processor.querySupport.ensureLoaded(graph, r1, r2);
48
49         processor.querySupport.getObjects(graph, r1, r2, procedure);
50
51         procedure.finished(graph);
52
53         if (entry != null)
54             entry.performFromCache(graph, procedure_);
55
56     }
57
58     @Override
59     public String toString() {
60         return "DirectObjects[" + r1() + " - " + r2() + "]";
61     }
62
63     @Override
64     public void setReady() {
65         statusOrException = READY;
66     }
67
68     @Override
69     public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
70
71         assert (isReady());
72
73         if (handleException(graph, procedure))
74             return getResult();
75
76         final IntArray value = (IntArray) getResult();
77         if (value.data == null) {
78             if (value.sizeOrData != IntArray.NO_DATA)
79                 procedure.execute(graph, value.sizeOrData);
80         } else {
81             for (int i = 0; i < value.sizeOrData; i++)
82                 procedure.execute(graph, value.data[i]);
83         }
84
85         procedure.finished(graph);
86
87         return value;
88
89     }
90
91     @Override
92     public void recompute(ReadGraphImpl graph) throws DatabaseException {
93
94         compute(graph, new IntProcedure() {
95
96             @Override
97             public void finished(ReadGraphImpl graph) {
98             }
99
100             @Override
101             public void exception(ReadGraphImpl graph, Throwable t) {
102                 throw new Error("Error in recompute.", t);
103             }
104
105             @Override
106             public void execute(ReadGraphImpl graphd, int i) {
107             }
108
109         });
110
111     }
112
113     @Override
114     boolean isImmutable(ReadGraphImpl graph) {
115         return graph.processor.isImmutable(r1());
116     }
117
118     @Override
119     public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
120         IntArray value = (IntArray) getResult();
121         value.add(i);
122     }
123
124     @Override
125     public void finished(ReadGraphImpl graph) throws DatabaseException {
126         setReady();
127     }
128
129     @Override
130     public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
131         except(throwable);
132     }
133
134 }