]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ExternalReadEntry.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 java.util.ArrayList;
15 import java.util.LinkedList;
16
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.db.procedure.Procedure;
21 import org.simantics.db.request.ExternalRead;
22 import org.simantics.db.request.RequestFlags;
23
24 final public class ExternalReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
25
26     final LinkedList<T> items = new LinkedList<T>();
27
28     protected ExternalRead<T> request;
29
30     @Override
31     int makeHash() {
32         return request.hashCode();
33     }
34     
35     @Override
36     public Object getOriginalRequest() {
37         return request;
38     }
39     
40     @Override
41     public void clearResult(QuerySupport support) {
42     }
43     
44     @Override
45     public void discard() {
46         request.unregistered();
47         request = null;
48         super.discard();
49     }
50     
51     public ExternalReadEntry(ExternalRead<T> request) {
52         assert request != null;
53         this.request = request;
54     }
55     
56     final public void queue(T item) {
57         synchronized(items) {
58                 items.addLast(item);
59                 // TODO: implement flags/logic in ExternalRead to state that all but the latest request result can be evaporated
60                 // In some cases where data is produced really fast this might be necessary but currently this queueing will do.
61         }
62     }
63     
64     final public void addOrSet(QueryProcessor processor, Object item) {
65
66         try {
67         
68             assert(isPending());
69
70             ArrayList<Procedure<T>> p = null;
71
72             synchronized(this) {
73
74                 setResult(item);
75                 setReady();
76 //                p = procs;
77 //                procs = null;
78
79             }
80
81 //            if(p != null)
82 //                for(Procedure proc : p) {
83 //                    proc.execute((T)item);
84 //                }
85
86         } catch (Throwable t) {
87             t.printStackTrace();
88         }
89         
90     }
91     
92     @Override
93     final public Query getQuery() {
94         
95         return new Query() {
96
97                         @Override
98                         public void recompute(ReadGraphImpl graph) {
99
100                                 synchronized(items) {
101
102
103                                         // Update
104                                         if(!items.isEmpty()) {
105                                                 setResult(items.removeFirst());
106                                         }
107                                         // Reschedule
108                                         if(!items.isEmpty()) {
109                                                 graph.processor.updatePrimitive(request);
110                                         }
111
112                                 }
113                                 
114                         }
115
116                         @Override
117                         public void removeEntry(QueryProcessor processor) {
118                                 processor.cache.remove(ExternalReadEntry.this);
119                         }
120
121                         @Override
122                         public int type() {
123                                 return RequestFlags.IMMEDIATE_UPDATE;
124                         }
125                         
126                         @Override
127                         public String toString() {
128                                 if(request == null) return "DISCARDED ExternalRead";
129                                 else return request.toString();
130                         }
131                 
132         };
133         
134     }
135
136         @Override
137         public String toString() {
138                 if(request == null) return "DISCARDED ExternalRead " + System.identityHashCode(this);
139                 else return request.toString() + " " + + System.identityHashCode(this);
140         }
141
142     @Override
143     public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> procedure) {
144         
145         AsyncProcedure<T> proc = (AsyncProcedure<T>)procedure;
146
147             if(isExcepted()) {
148             
149             proc.exception(graph, (Throwable)getResult());
150             
151         } else {
152             
153             proc.execute(graph, (T)getResult());
154
155         }
156             
157             return getResult();
158         
159     }
160     
161     @Override
162     void prepareRecompute(QuerySupport querySupport) {
163         // Do nothing - the state is already set and cannot be recomputed on demand
164     }
165
166         @Override
167         public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
168         return graph.processor.cache.performQuery(graph, request, this, procedure);
169         }
170
171 }