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.databoard.Bindings;
18 import org.simantics.db.DevelopmentKeys;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.impl.procedure.InternalProcedure;
22 import org.simantics.utils.Development;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public abstract class CacheEntryBase<Procedure> extends CacheEntry<Procedure> {
28 private static final Logger LOGGER = LoggerFactory.getLogger(CacheEntryBase.class);
30 // Default level is something that is not quite a prospect but still allows for ordering within CacheCollectionResult
31 public static final short UNDEFINED_LEVEL = 5;
33 public short level = UNDEFINED_LEVEL;
35 public int GCStatus = 0;
37 final public static CacheEntryBase[] NONE = new CacheEntryBase[0];
39 static Object NO_RESULT = new Object() { public String toString() { return "NO_RESULT"; }};
40 static protected Object INVALID_RESULT = new Object() { public String toString() { return "INVALID_RESULT"; }};
43 // static protected Object FRESH = new Object() { public String toString() { return "CREATED"; }};
44 // Result is computed - no exception
45 static protected Object READY = new Object() { public String toString() { return "READY"; }};
46 // Computation is under way
47 static protected Object PENDING = new Object() { public String toString() { return "PENDING"; }};
48 // Entry is discarded and is waiting for garbage collect
49 static protected Object DISCARDED = new Object() { public String toString() { return "DISCARDED"; }};
50 // The result has been invalidated
51 static protected Object REQUIRES_COMPUTATION = new Object() { public String toString() { return "REFUTED"; }};
52 // The computation has excepted - the exception is in the result
53 static protected Object EXCEPTED = new Object() { public String toString() { return "EXCEPTED"; }};
55 // This indicates the status of the entry
56 public Object statusOrException = REQUIRES_COMPUTATION;
58 private CacheEntry p1 = null;
59 private Object p2OrParents = null;
64 final public int hashCode() {
65 if(hash == 0) hash = makeHash();
69 abstract int makeHash();
71 // This can be tested to see if the result is finished
72 Object result = NO_RESULT;
74 final public boolean isFresh() {
75 return REQUIRES_COMPUTATION == statusOrException;
78 public void setReady() {
79 assert(result != NO_RESULT);
80 statusOrException = READY;
84 final public boolean isReady() {
85 return READY == statusOrException || EXCEPTED == statusOrException;
89 public void discard() {
90 if (Development.DEVELOPMENT) {
91 if(Development.<Boolean>getProperty(DevelopmentKeys.CACHE_ENTRY_STATE, Bindings.BOOLEAN)) {
92 System.err.println("[QUERY STATE]: discarded " + this);
95 statusOrException = DISCARDED;
99 final public boolean isDiscarded() {
100 return DISCARDED == statusOrException;
104 final public void refute() {
105 if (Development.DEVELOPMENT) {
106 if(Development.<Boolean>getProperty(DevelopmentKeys.CACHE_ENTRY_STATE, Bindings.BOOLEAN)) {
107 System.err.println("[QUERY STATE]: refuted " + this);
110 statusOrException = REQUIRES_COMPUTATION;
114 final public boolean isRefuted() {
115 return REQUIRES_COMPUTATION == statusOrException;
119 public void except(Throwable throwable) {
120 if (Development.DEVELOPMENT) {
121 if(Development.<Boolean>getProperty(DevelopmentKeys.CACHE_ENTRY_STATE, Bindings.BOOLEAN)) {
122 System.err.println("[QUERY STATE]: excepted " + this);
125 if(statusOrException != DISCARDED) {
126 statusOrException = EXCEPTED;
129 LOGGER.warn("Cache entry got excepted status after being discarded: " + getClass().getSimpleName(), throwable);
134 final public void checkAndThrow() throws DatabaseException {
136 Throwable throwable = (Throwable)result;
137 if(throwable instanceof DatabaseException) throw (DatabaseException)throwable;
138 else throw new DatabaseException(throwable);
143 final public boolean isExcepted() {
144 return EXCEPTED == statusOrException;
148 public void setPending() {
149 statusOrException = PENDING;
153 final public boolean isPending() {
154 return PENDING == statusOrException;
157 final public boolean requiresComputation() {
158 return REQUIRES_COMPUTATION == statusOrException;
161 final public boolean assertPending() {
162 boolean result = isPending();
164 LOGGER.warn("Assertion failed, expected pending, got " + statusOrException);
169 final public boolean assertNotPending() {
170 boolean result = !isPending();
172 new Exception(this + ": Assertion failed, expected not pending, got " + statusOrException).printStackTrace();
177 final public boolean assertNotDiscarded() {
178 boolean result = !isDiscarded();
180 new Exception(this + ": Assertion failed, expected not discarded, got " + statusOrException).printStackTrace();
186 public void setResult(Object result) {
187 this.result = result;
190 @SuppressWarnings("unchecked")
192 final public <T> T getResult() {
193 assert(statusOrException != DISCARDED);
198 public void clearResult(QuerySupport support) {
199 setResult(NO_RESULT);
203 final public void addParent(CacheEntry entry) {
205 assert(entry != null);
210 if(p2OrParents == entry) {
215 } else if(p2OrParents == null) {
217 } else if(p2OrParents instanceof QueryIdentityHashSet) {
218 ((QueryIdentityHashSet)p2OrParents).add(entry);
219 ((QueryIdentityHashSet)p2OrParents).purge();
221 CacheEntry tmp = (CacheEntry)p2OrParents;
222 p2OrParents = new QueryIdentityHashSet(2);
223 ((QueryIdentityHashSet)p2OrParents).add(tmp);
224 ((QueryIdentityHashSet)p2OrParents).add(entry);
230 CacheEntry pruneFirstParents() {
237 if(!p1.isDiscarded()) {
239 // First parent is still active
247 // First parent is discarded => look for more parents
248 if(p2OrParents instanceof QueryIdentityHashSet) {
250 QueryIdentityHashSet set = (QueryIdentityHashSet)p2OrParents;
251 CacheEntry entry = set.removeDiscarded();
252 if(entry == null) p2OrParents = null;
256 } else if(p2OrParents instanceof CacheEntry) {
258 CacheEntry entry = (CacheEntry)p2OrParents;
259 if(entry.isDiscarded()) {
260 // Second entry is also discarded => all empty
281 final public void removeParent(CacheEntry entry) {
284 if(p2OrParents != null) throw new Error("CacheEntryBase.removeParent: corrupted parents (p1 == null, while p2OrParents != null).");
285 else throw new Error("CacheEntryBase.removeParent: no parents.");
288 if(p2OrParents == null) {
290 } else if(p2OrParents instanceof QueryIdentityHashSet) {
291 QueryIdentityHashSet set = (QueryIdentityHashSet)p2OrParents;
292 int size = set.size();
296 } else if (size == 1) {
297 CacheEntry next = set.iterator().next();
300 } else if(set.size() == 2) {
301 Iterator<CacheEntry> iterator = set.iterator();
302 p1 = iterator.next();
303 p2OrParents = iterator.next();
305 p1 = set.iterator().next();
309 p1 = (CacheEntry)p2OrParents;
313 } else if(p2OrParents.getClass() == QueryIdentityHashSet.class) {
315 QueryIdentityHashSet set = (QueryIdentityHashSet)p2OrParents;
316 boolean success = set.remove(entry);
318 throw new Error("CacheEntryBase.removeParent: parent was not found.");
320 assert(set.size() >= 1);
321 if(set.size() == 1) {
322 p2OrParents = set.iterator().next();
326 if(p2OrParents == entry) {
329 throw new Error("CacheEntryBase.removeParent: had 2 parents but neither was removed.");
335 final public boolean hasParents() {
336 assert(statusOrException != DISCARDED);
341 final public Iterable<CacheEntry<?>> getParents(QueryProcessor processor) {
343 ArrayList<CacheEntry<?>> result = new ArrayList<CacheEntry<?>>();
344 if(p1 != null) result.add(p1);
345 if(p2OrParents != null) {
346 if(p2OrParents instanceof QueryIdentityHashSet) {
347 for(CacheEntry entry : (QueryIdentityHashSet)p2OrParents) {
351 result.add((CacheEntry)p2OrParents);
354 fillImpliedParents(processor, result);
360 CacheEntry getFirstParent(QueryProcessor processor) {
365 boolean moreThanOneParent(QueryProcessor processor) {
366 return p2OrParents != null;
370 int parentCount(QueryProcessor processor) {
371 if(p2OrParents != null) {
372 if(p2OrParents instanceof QueryIdentityHashSet) {
373 return ((QueryIdentityHashSet)p2OrParents).size()+1;
378 return p1 != null ? 1 : 0;
383 protected void fillImpliedParents(QueryProcessor processor, ArrayList<CacheEntry<?>> result) {
386 protected String internalError() {
387 return toString() + " " + statusOrException + " " + result;
391 protected boolean handleException(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
393 procedure.exception(graph, (Throwable)getResult());
400 protected boolean handleException(ReadGraphImpl graph, TripleIntProcedure procedure) throws DatabaseException {
402 procedure.exception(graph, (Throwable)getResult());
409 protected <T> boolean handleException(ReadGraphImpl graph, InternalProcedure<T> procedure) throws DatabaseException {
411 procedure.exception(graph, (Throwable)getResult());
419 boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
424 boolean shouldBeCollected() {
434 short setLevel(short level) {
435 short existing = this.level;
441 void prepareRecompute(QuerySupport querySupport) {
443 clearResult(querySupport);
452 int setGCStatus(int status) {
458 void setGCStatusFlag(int flag, boolean value) {
467 public Object getOriginalRequest() {
468 // This is the original request for all built-in queries
472 public CacheEntryBase() {