1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 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 public abstract class CacheEntryBase<Procedure> extends CacheEntry<Procedure> {
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 Object NO_RESULT = new Object() { public String toString() { return "NO_RESULT"; }};
38 static protected Object INVALID_RESULT = new Object() { public String toString() { return "INVALID_RESULT"; }};
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 REQUIRES_COMPUTATION = 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 = REQUIRES_COMPUTATION;
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 Object result = NO_RESULT;
72 final public boolean isFresh() {
73 return REQUIRES_COMPUTATION == statusOrException;
76 public void setReady() {
77 assert(result != NO_RESULT);
78 statusOrException = READY;
82 final public boolean isReady() {
83 return READY == statusOrException || EXCEPTED == statusOrException;
87 public void discard() {
88 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: discarded " + this);
89 statusOrException = DISCARDED;
93 final public boolean isDiscarded() {
94 return DISCARDED == statusOrException;
98 final public void refute() {
99 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: refuted " + this);
100 statusOrException = REQUIRES_COMPUTATION;
104 final public boolean isRefuted() {
105 return REQUIRES_COMPUTATION == statusOrException;
109 public void except(Throwable throwable) {
110 if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: excepted " + this);
111 if(statusOrException != DISCARDED) {
112 statusOrException = EXCEPTED;
115 LOGGER.warn("Cache entry got excepted status after being discarded: " + getClass().getSimpleName(), throwable);
120 final public void checkAndThrow() throws DatabaseException {
122 Throwable throwable = (Throwable)result;
123 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
124 else throw new DatabaseException(throwable);
129 final public boolean isExcepted() {
130 return EXCEPTED == statusOrException;
134 public void setPending() {
135 statusOrException = PENDING;
139 final public boolean isPending() {
140 return PENDING == statusOrException;
143 final public boolean requiresComputation() {
144 return REQUIRES_COMPUTATION == statusOrException;
147 final public boolean assertPending() {
148 boolean result = isPending();
150 LOGGER.warn("Assertion failed, expected pending, got " + statusOrException);
155 final public boolean assertNotPending() {
156 boolean result = !isPending();
158 new Exception(this + ": Assertion failed, expected not pending, got " + statusOrException).printStackTrace();
163 final public boolean assertNotDiscarded() {
164 boolean result = !isDiscarded();
166 new Exception(this + ": Assertion failed, expected not discarded, got " + statusOrException).printStackTrace();
172 public void setResult(Object result) {
173 this.result = result;
176 @SuppressWarnings("unchecked")
178 final public <T> T getResult() {
179 assert(statusOrException != DISCARDED);
184 public void clearResult(QuerySupport support) {
185 setResult(NO_RESULT);
189 final public void addParent(CacheEntry entry) {
191 assert(entry != null);
196 if(p2OrParents == entry) {
201 } else if(p2OrParents == null) {
203 } else if(p2OrParents instanceof QueryIdentityHashSet) {
204 ((QueryIdentityHashSet)p2OrParents).add(entry);
205 ((QueryIdentityHashSet)p2OrParents).purge();
207 CacheEntry tmp = (CacheEntry)p2OrParents;
208 p2OrParents = new QueryIdentityHashSet(2);
209 ((QueryIdentityHashSet)p2OrParents).add(tmp);
210 ((QueryIdentityHashSet)p2OrParents).add(entry);
216 CacheEntry pruneFirstParents() {
223 if(!p1.isDiscarded()) {
225 // First parent is still active
233 // First parent is discarded => look for more parents
234 if(p2OrParents instanceof QueryIdentityHashSet) {
236 QueryIdentityHashSet set = (QueryIdentityHashSet)p2OrParents;
237 CacheEntry entry = set.removeDiscarded();
238 if(entry == null) p2OrParents = null;
242 } else if(p2OrParents instanceof CacheEntry) {
244 CacheEntry entry = (CacheEntry)p2OrParents;
245 if(entry.isDiscarded()) {
246 // Second entry is also discarded => all empty
267 final public void removeParent(CacheEntry entry) {
270 if(p2OrParents != null) throw new Error("CacheEntryBase.removeParent: corrupted parents (p1 == null, while p2OrParents != null).");
271 else throw new Error("CacheEntryBase.removeParent: no parents.");
274 if(p2OrParents == null) {
276 } else if(p2OrParents instanceof QueryIdentityHashSet) {
277 QueryIdentityHashSet set = (QueryIdentityHashSet)p2OrParents;
278 int size = set.size();
282 } else if (size == 1) {
283 CacheEntry next = set.iterator().next();
286 } else if(set.size() == 2) {
287 Iterator<CacheEntry> iterator = set.iterator();
288 p1 = iterator.next();
289 p2OrParents = iterator.next();
291 p1 = set.iterator().next();
295 p1 = (CacheEntry)p2OrParents;
299 } else if(p2OrParents.getClass() == QueryIdentityHashSet.class) {
301 QueryIdentityHashSet set = (QueryIdentityHashSet)p2OrParents;
302 boolean success = set.remove(entry);
304 throw new Error("CacheEntryBase.removeParent: parent was not found.");
306 assert(set.size() >= 1);
307 if(set.size() == 1) {
308 p2OrParents = set.iterator().next();
312 if(p2OrParents == entry) {
315 throw new Error("CacheEntryBase.removeParent: had 2 parents but neither was removed.");
321 final public boolean hasParents() {
322 assert(statusOrException != DISCARDED);
327 final public Iterable<CacheEntry<?>> getParents(QueryProcessor processor) {
329 ArrayList<CacheEntry<?>> result = new ArrayList<CacheEntry<?>>();
330 if(p1 != null) result.add(p1);
331 if(p2OrParents != null) {
332 if(p2OrParents instanceof QueryIdentityHashSet) {
333 for(CacheEntry entry : (QueryIdentityHashSet)p2OrParents) {
337 result.add((CacheEntry)p2OrParents);
340 fillImpliedParents(processor, result);
346 CacheEntry getFirstParent(QueryProcessor processor) {
351 boolean moreThanOneParent(QueryProcessor processor) {
352 return p2OrParents != null;
356 int parentCount(QueryProcessor processor) {
357 if(p2OrParents != null) {
358 if(p2OrParents instanceof QueryIdentityHashSet) {
359 return ((QueryIdentityHashSet)p2OrParents).size()+1;
364 return p1 != null ? 1 : 0;
369 protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {
372 protected String internalError() {
373 return toString() + " " + statusOrException + " " + result;
377 protected boolean handleException(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
379 procedure.exception(graph, (Throwable)getResult());
386 protected boolean handleException(ReadGraphImpl graph, TripleIntProcedure procedure) throws DatabaseException {
388 procedure.exception(graph, (Throwable)getResult());
395 protected <T> boolean handleException(ReadGraphImpl graph, InternalProcedure<T> procedure) throws DatabaseException {
397 procedure.exception(graph, (Throwable)getResult());
405 boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
410 boolean shouldBeCollected() {
420 short setLevel(short level) {
421 short existing = this.level;
427 void prepareRecompute(QuerySupport querySupport) {
429 clearResult(querySupport);
438 int setGCStatus(int status) {
444 void setGCStatusFlag(int flag, boolean value) {
453 public Object getOriginalRequest() {
454 // This is the original request for all built-in queries
458 public CacheEntryBase() {