]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheEntryBase.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / CacheEntryBase.java
index 3a94e52b6f9dd58f55031b852df02776fc044874..98e7f0d00554f1c3f351339d8aa4b891028966ae 100644 (file)
@@ -12,6 +12,7 @@
 package org.simantics.db.impl.query;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
 
 import org.simantics.databoard.Bindings;
@@ -145,8 +146,9 @@ public abstract class CacheEntryBase<Procedure> extends CacheEntry<Procedure> {
     }
 
     @Override
-    public void setPending() {
-       statusOrException = PENDING;
+    public void setPending(QuerySupport querySupport) {
+        statusOrException = PENDING;
+        clearResult(querySupport);
     }
     
     @Override
@@ -276,7 +278,31 @@ public abstract class CacheEntryBase<Procedure> extends CacheEntry<Procedure> {
        }
         
     }
-    
+
+    @Override
+    void pruneParentSet() {
+        // First parent is discarded => look for more parents
+        if(p2OrParents instanceof QueryIdentityHashSet) {
+
+            QueryIdentityHashSet set = (QueryIdentityHashSet)p2OrParents;
+            set.removeDiscardedReally();
+            if(set.isEmpty()) p2OrParents = null;
+
+        } else if(p2OrParents instanceof CacheEntry) {
+
+            CacheEntry entry = (CacheEntry)p2OrParents;
+            if(entry.isDiscarded()) {
+                // Second entry is also discarded => all empty
+                p2OrParents = null;
+            }
+
+        } else {
+
+            // Nothing left
+
+        }
+    }
+
     @Override
     final public void removeParent(CacheEntry entry) {
        
@@ -338,7 +364,7 @@ public abstract class CacheEntryBase<Procedure> extends CacheEntry<Procedure> {
     }
     
     @Override
-       final public Iterable<CacheEntry<?>> getParents(QueryProcessor processor) {
+       final public Collection<CacheEntry<?>> getParents(QueryProcessor processor) {
 
                ArrayList<CacheEntry<?>> result = new ArrayList<CacheEntry<?>>();
                if(p1 != null) result.add(p1);
@@ -439,8 +465,7 @@ public abstract class CacheEntryBase<Procedure> extends CacheEntry<Procedure> {
     
     @Override
     void prepareRecompute(QuerySupport querySupport) {
-               setPending();
-               clearResult(querySupport);
+        setPending(querySupport);
     }
     
     @Override
@@ -471,5 +496,44 @@ public abstract class CacheEntryBase<Procedure> extends CacheEntry<Procedure> {
 
     public CacheEntryBase() {
     }
+    
+    public String classId() {
+        return getClass().getName();
+    }
+
+    public void serializeKey(QuerySerializer serializer) {
+        throw new IllegalStateException("Cannot serialize query key for " + this);
+    }
+    
+    public void serializeValue(QuerySerializer serializer) {
+        throw new IllegalStateException("Cannot serialize query value for " + this);
+    }
+    
+    public void serializeParents(QuerySerializer serializer) {
+        Collection<CacheEntry<?>> ps = getParents(serializer.getQueryProcessor());
+        int sizePos = serializer.writeUnknownSize();
+        int actual = 0;
+        for(CacheEntry<?> entry : ps) {
+            CacheEntryBase b = (CacheEntryBase)entry;
+            String cid = b.classId();
+            if(cid == null) 
+                continue;
+            serializer.serializeId(b.classId());
+            b.serializeKey(serializer);
+            actual++;
+        }
+        serializer.setUnknownSize(sizePos, actual);
+    }
+
+    public long cluster(QueryProcessor processor) {
+        throw new IllegalStateException("Cannot compute query cluster for " + this);
+    }
+
+    public void serialize(QuerySerializer serializer) {
+        serializer.serializeId(classId());
+        serializeKey(serializer);
+        serializeValue(serializer);
+        serializeParents(serializer);
+    }
 
 }