]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ExternalReadEntry.java
Fixed all line endings of the repository
[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.impl.graph.ReadGraphImpl;
18 import org.simantics.db.procedure.Procedure;
19 import org.simantics.db.request.ExternalRead;
20 import org.simantics.db.request.RequestFlags;
21
22 final public class ExternalReadEntry<T> extends CacheEntryBase {
23
24     final LinkedList<T> items = new LinkedList<T>();
25     
26 //    public ArrayList<Procedure<T>> procs;
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, Object provider, CacheEntry entry) {
99
100                             final QueryProcessor qp = (QueryProcessor)provider;
101                             synchronized(items) {
102
103                                 if(entry.isExcepted()) {
104                                         
105                                         // Exception persists
106                                         
107                                 } else {
108                                 
109                                         // Update
110                                         if(!items.isEmpty()) {
111                                                 setResult(items.removeFirst());
112                                         }
113                                         // Reschedule
114                                             if(!items.isEmpty()) {
115                                                 qp.updatePrimitive(request);
116                                             }
117                                             
118                                             setReady();
119                                             
120                                 }
121                                 
122                             }
123                                 
124                         }
125
126                         @Override
127                         public void removeEntry(QueryProcessor processor) {
128                                 processor.externalReadMap.remove(request);
129                         }
130
131                         @Override
132                         public int type() {
133                                 return RequestFlags.IMMEDIATE_UPDATE;
134                         }
135                         
136                         @Override
137                         public String toString() {
138                                 if(request == null) return "DISCARDED ExternalRead";
139                                 else return request.toString();
140                         }
141                 
142         };
143         
144     }
145
146         public void performFromCache(Object procedure) {
147                 
148         Procedure<T> proc = (Procedure<T>)procedure;
149
150             if(isExcepted()) {
151             
152             proc.exception((Throwable)getResult());
153             
154         } else {
155             
156             proc.execute((T)getResult());
157
158         }
159                 
160         }
161
162         @Override
163         public String toString() {
164                 if(request == null) return "DISCARDED ExternalRead " + System.identityHashCode(this);
165                 else return request.toString() + " " + + System.identityHashCode(this);
166         }
167
168     @Override
169     public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
170         performFromCache(procedure);
171     }
172     
173     @Override
174     public void setReady() {
175         super.setReady();
176     }
177     
178     @Override
179     void prepareRecompute(QuerySupport querySupport) {
180         // Do nothing - the state is already set and cannot be recomputed on demand
181     }
182
183 }