]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectObjects.java
Trying to remove synchronization problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / DirectObjects.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.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     final public 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     static public 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 //      final public void add(int add) {
69 //
70 //              assert(isPending());
71 //
72 //              IntArray value = (IntArray)getResult();
73 //              value.add(add);
74 //
75 //      }
76
77     @Override
78     public Object performFromCache(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
79
80         assert (isReady());
81
82         if (handleException(graph, procedure))
83             return getResult();
84
85         final IntArray value = (IntArray) getResult();
86         if (value.data == null) {
87             if (value.sizeOrData != IntArray.NO_DATA)
88                 procedure.execute(graph, value.sizeOrData);
89         } else {
90             for (int i = 0; i < value.sizeOrData; i++)
91                 procedure.execute(graph, value.data[i]);
92         }
93
94         procedure.finished(graph);
95
96         return value;
97
98     }
99
100     @Override
101     public void recompute(ReadGraphImpl graph) throws DatabaseException {
102
103         compute(graph, new IntProcedure() {
104
105             @Override
106             public void finished(ReadGraphImpl graph) {
107             }
108
109             @Override
110             public void exception(ReadGraphImpl graph, Throwable t) {
111                 throw new Error("Error in recompute.", t);
112             }
113
114             @Override
115             public void execute(ReadGraphImpl graphd, int i) {
116             }
117
118         });
119
120     }
121
122     @Override
123     boolean isImmutable(ReadGraphImpl graph) {
124         return graph.processor.isImmutable(r1());
125     }
126
127     @Override
128     public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
129         IntArray value = (IntArray) getResult();
130         value.add(i);
131     }
132
133     @Override
134     public void finished(ReadGraphImpl graph) throws DatabaseException {
135         setReady();
136     }
137
138     @Override
139     public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
140         except(throwable);
141     }
142
143 }