1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.query;
14 import java.util.ArrayList;
15 import java.util.Iterator;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.DebugPolicy;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.impl.procedure.InternalProcedure;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 abstract public class CacheEntryBase extends CacheEntry {
26 private static final Logger LOGGER = LoggerFactory.getLogger(CacheEntryBase.class);
28 // Default level is something that is not quite a prospect but still allows for ordering within CacheCollectionResult
29 public static final short UNDEFINED_LEVEL = 5;
31 public short level = UNDEFINED_LEVEL;
33 public int GCStatus = 0;
35 final public static CacheEntryBase[] NONE = new CacheEntryBase[0];
37 static private Object NO_RESULT = new Object();
38 static protected Object INVALID_RESULT = new Object();
41 static protected Object FRESH = new Object() { public String toString() { return "CREATED"; }};
42 // Result is computed - no exception
43 static protected Object READY = new Object() { public String toString() { return "READY"; }};
44 // Computation is under way
45 static protected Object PENDING = new Object() { public String toString() { return "PENDING"; }};
46 // Entry is discarded and is waiting for garbage collect
47 static protected Object DISCARDED = new Object() { public String toString() { return "DISCARDED"; }};
48 // The result has been invalidated
49 static protected Object REFUTED = new Object() { public String toString() { return "REFUTED"; }};
50 // The computation has excepted - the exception is in the result
51 static protected Object EXCEPTED = new Object() { public String toString() { return "EXCEPTED"; }};
53 // This indicates the status of the entry
54 public Object statusOrException = FRESH;
56 private CacheEntry p1 = null;
57 private Object p2OrParents = null;
62 final public int hashCode() {
63 if(hash == 0) hash = makeHash();
67 abstract int makeHash();
69 // This can be tested to see if the result is finished
70 private Object result = NO_RESULT;
72 final public boolean isFresh() {
73 return FRESH == statusOrException;
76 public void setReady() {
77 statusOrException = READY;
81 final public boolean isReady() {
82 return READY == statusOrException || EXCEPTED == statusOrException;
86 public void discard() {
87 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: discarded " + this);
88 statusOrException = DISCARDED;
92 final public boolean isDiscarded() {
93 return DISCARDED == statusOrException;
97 final public void refute() {
98 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: refuted " + this);
99 statusOrException = REFUTED;
103 final public boolean isRefuted() {
104 return REFUTED == statusOrException;
108 final public void except(Throwable t) {
109 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: excepted " + this);
110 if(statusOrException != DISCARDED) {
111 statusOrException = EXCEPTED;
114 LOGGER.warn("Cache entry got excepted status after being discarded: " + getClass().getSimpleName(), t);
119 final public void checkAndThrow() throws DatabaseException {
121 Throwable throwable = (Throwable)result;
122 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
123 else throw new DatabaseException(throwable);
128 final public boolean isExcepted() {
129 return EXCEPTED == statusOrException;
133 final public void setPending() {
134 statusOrException = PENDING;
138 final public boolean isPending() {
139 return PENDING == statusOrException;
142 final public boolean assertPending() {
143 boolean result = isPending();
145 LOGGER.warn("Assertion failed, expected pending, got " + statusOrException);
150 final public boolean assertNotPending() {
151 boolean result = !isPending();
153 new Exception(this + ": Assertion failed, expected not pending, got " + statusOrException).printStackTrace();
158 final public boolean assertNotDiscarded() {
159 boolean result = !isDiscarded();
161 new Exception(this + ": Assertion failed, expected not discarded, got " + statusOrException).printStackTrace();
167 public void setResult(Object result) {
168 this.result = result;
172 final public <T> T getResult() {
173 assert(statusOrException != DISCARDED);
178 public void clearResult(QuerySupport support) {
179 setResult(NO_RESULT);
183 final public void addParent(CacheEntry entry) {
185 assert(entry != null);
190 if(p2OrParents == entry) {
195 } else if(p2OrParents == null) {
197 } else if(p2OrParents instanceof QueryIdentityHashSet) {
198 ((QueryIdentityHashSet)p2OrParents).add(entry);
199 ((QueryIdentityHashSet)p2OrParents).purge();
201 CacheEntry tmp = (CacheEntry)p2OrParents;
202 p2OrParents = new QueryIdentityHashSet(2);
203 ((QueryIdentityHashSet)p2OrParents).add(tmp);
204 ((QueryIdentityHashSet)p2OrParents).add(entry);
210 CacheEntry pruneFirstParents() {
217 if(!p1.isDiscarded()) {
219 // First parent is still active
227 // First parent is discarded => look for more parents
228 if(p2OrParents instanceof QueryIdentityHashSet) {
230 QueryIdentityHashSet set = (QueryIdentityHashSet)p2OrParents;
231 CacheEntry entry = set.removeDiscarded();
232 if(entry == null) p2OrParents = null;
236 } else if(p2OrParents instanceof CacheEntry) {
238 CacheEntry entry = (CacheEntry)p2OrParents;
239 if(entry.isDiscarded()) {
240 // Second entry is also discarded => all empty
261 final public void removeParent(CacheEntry entry) {
264 if(p2OrParents != null) throw new Error("CacheEntryBase.removeParent: corrupted parents (p1 == null, while p2OrParents != null).");
265 else throw new Error("CacheEntryBase.removeParent: no parents.");
268 if(p2OrParents == null) {
270 } else if(p2OrParents instanceof QueryIdentityHashSet) {
271 QueryIdentityHashSet set = (QueryIdentityHashSet)p2OrParents;
272 int size = set.size();
276 } else if (size == 1) {
277 CacheEntry next = set.iterator().next();
280 } else if(set.size() == 2) {
281 Iterator<CacheEntry> iterator = set.iterator();
282 p1 = iterator.next();
283 p2OrParents = iterator.next();
285 p1 = set.iterator().next();
289 p1 = (CacheEntry)p2OrParents;
293 } else if(p2OrParents.getClass() == QueryIdentityHashSet.class) {
295 QueryIdentityHashSet set = (QueryIdentityHashSet)p2OrParents;
296 boolean success = set.remove(entry);
298 throw new Error("CacheEntryBase.removeParent: parent was not found.");
300 assert(set.size() >= 1);
301 if(set.size() == 1) {
302 p2OrParents = set.iterator().next();
306 if(p2OrParents == entry) {
309 throw new Error("CacheEntryBase.removeParent: had 2 parents but neither was removed.");
315 final public boolean hasParents() {
316 assert(statusOrException != DISCARDED);
321 final public Iterable<CacheEntry> getParents(QueryProcessor processor) {
323 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
324 if(p1 != null) result.add(p1);
325 if(p2OrParents != null) {
326 if(p2OrParents instanceof QueryIdentityHashSet) {
327 for(CacheEntry entry : (QueryIdentityHashSet)p2OrParents) {
331 result.add((CacheEntry)p2OrParents);
334 fillImpliedParents(processor, result);
340 CacheEntry getFirstParent(QueryProcessor processor) {
345 boolean moreThanOneParent(QueryProcessor processor) {
346 return p2OrParents != null;
350 int parentCount(QueryProcessor processor) {
351 if(p2OrParents != null) {
352 if(p2OrParents instanceof QueryIdentityHashSet) {
353 return ((QueryIdentityHashSet)p2OrParents).size()+1;
358 return p1 != null ? 1 : 0;
363 protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry> result) {
367 protected String internalError() {
368 return toString() + " " + statusOrException + " " + result;
372 protected boolean handleException(ReadGraphImpl graph, IntProcedure procedure) {
374 procedure.exception(graph, (Throwable)getResult());
381 protected boolean handleException(ReadGraphImpl graph, TripleIntProcedure procedure) {
383 procedure.exception(graph, (Throwable)getResult());
390 protected <T> boolean handleException(ReadGraphImpl graph, InternalProcedure<T> procedure) {
392 procedure.exception(graph, (Throwable)getResult());
400 boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
405 boolean shouldBeCollected() {
415 short setLevel(short level) {
416 short existing = this.level;
422 void prepareRecompute(QuerySupport querySupport) {
424 clearResult(querySupport);
437 int setGCStatus(int status) {
443 void setGCStatusFlag(int flag, boolean value) {
452 public Object getOriginalRequest() {
453 // This is the original request for all built-in queries