]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHash.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryIdentityHash.java
index 8f684736ede693b79f21e93211a453a873e768ea..2db67c67970b165f836317cbbaaabea40fa6994d 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.impl.query;\r
-\r
-import gnu.trove.impl.hash.THash;\r
-\r
-import org.simantics.db.impl.graph.ReadGraphImpl;\r
-\r
-\r
-/**\r
- * An open addressed hashing implementation for Object types.\r
- *\r
- * Created: Sun Nov  4 08:56:06 2001\r
- *\r
- * @author Eric D. Friedman\r
- * @version $Id: QueryIdentityHash.java,v 1.1 2008/03/14 11:32:01 tuoksk Exp $\r
- */\r
-abstract public class QueryIdentityHash extends THash {\r
-\r
-       static final long serialVersionUID = -3461112548087185871L;\r
-\r
-    /** the set of Objects */\r
-    protected transient CacheEntry[] _set;\r
-\r
-    protected static final CacheEntry REMOVED = new CacheEntry() {\r
-\r
-        @Override\r
-        public void addParent(CacheEntry entry) {\r
-            // TODO Auto-generated method stub\r
-            \r
-        }\r
-\r
-        @Override\r
-        public void clearResult(QuerySupport support) {\r
-            // TODO Auto-generated method stub\r
-            \r
-        }\r
-\r
-        @Override\r
-        public void discard() {\r
-            // TODO Auto-generated method stub\r
-            \r
-        }\r
-\r
-        @Override\r
-        public void except(Throwable t) {\r
-            // TODO Auto-generated method stub\r
-            \r
-        }\r
-\r
-        @Override\r
-        public Iterable<CacheEntry> getParents(QueryProcessor processor) {\r
-            // TODO Auto-generated method stub\r
-            return null;\r
-        }\r
-\r
-        @Override\r
-        public Query getQuery() {\r
-            // TODO Auto-generated method stub\r
-            return null;\r
-        }\r
-\r
-        @Override\r
-        public <T> T getResult() {\r
-            // TODO Auto-generated method stub\r
-            return null;\r
-        }\r
-\r
-        @Override\r
-        public boolean hasParents() {\r
-            // TODO Auto-generated method stub\r
-            return false;\r
-        }\r
-\r
-        @Override\r
-        public boolean isDiscarded() {\r
-            // TODO Auto-generated method stub\r
-            return false;\r
-        }\r
-\r
-        @Override\r
-        public boolean isExcepted() {\r
-            // TODO Auto-generated method stub\r
-            return false;\r
-        }\r
-\r
-        @Override\r
-        public boolean isFresh() {\r
-            // TODO Auto-generated method stub\r
-            return false;\r
-        }\r
-\r
-        @Override\r
-        public boolean isPending() {\r
-            // TODO Auto-generated method stub\r
-            return false;\r
-        }\r
-\r
-        @Override\r
-        public boolean isReady() {\r
-            // TODO Auto-generated method stub\r
-            return false;\r
-        }\r
-\r
-        @Override\r
-        public boolean isRefuted() {\r
-            // TODO Auto-generated method stub\r
-            return false;\r
-        }\r
-\r
-        @Override\r
-        public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {\r
-            // TODO Auto-generated method stub\r
-            \r
-        }\r
-\r
-        @Override\r
-        public void refute() {\r
-            // TODO Auto-generated method stub\r
-            \r
-        }\r
-\r
-        @Override\r
-        CacheEntry pruneFirstParents() {\r
-            // TODO Auto-generated method stub\r
-               return null;\r
-        }\r
-        \r
-        @Override\r
-        public void removeParent(CacheEntry entry) {\r
-            // TODO Auto-generated method stub\r
-            \r
-        }\r
-\r
-        @Override\r
-        public void setPending() {\r
-            // TODO Auto-generated method stub\r
-            \r
-        }\r
-\r
-        @Override\r
-        public void setReady() {\r
-            // TODO Auto-generated method stub\r
-            \r
-        }\r
-\r
-        @Override\r
-        public void setResult(Object result) {\r
-            // TODO Auto-generated method stub\r
-            \r
-        }\r
-        \r
-        @Override\r
-        boolean isImmutable(ReadGraphImpl graph) {\r
-               return true;\r
-        }\r
-\r
-               @Override\r
-               boolean shouldBeCollected() {\r
-                       // TODO Auto-generated method stub\r
-                       return false;\r
-               }\r
-\r
-               @Override\r
-               CacheEntry getFirstParent(QueryProcessor processor) {\r
-                       // TODO Auto-generated method stub\r
-                       return null;\r
-               }\r
-               \r
-               @Override\r
-               boolean moreThanOneParent(QueryProcessor processor) {\r
-                       // TODO Auto-generated method stub\r
-                       return false;\r
-               }\r
-\r
-               @Override\r
-               int parentCount(QueryProcessor processor) {\r
-                       // TODO Auto-generated method stub\r
-                       return 0;\r
-               }\r
-               \r
-               @Override\r
-               short getLevel() {\r
-                       // TODO Auto-generated method stub\r
-                       return 0;\r
-               }\r
-\r
-               @Override\r
-               short setLevel(short level) {\r
-                       // TODO Auto-generated method stub\r
-                       return level;\r
-               }\r
-               \r
-               @Override\r
-               void prepareRecompute(QuerySupport querySupport) {\r
-                       // TODO Auto-generated method stub\r
-               }\r
-\r
-               @Override\r
-               int getGCStatus() {\r
-                       // TODO Auto-generated method stub\r
-                       return 0;\r
-               }\r
-\r
-               @Override\r
-               int setGCStatus(int status) {\r
-                       // TODO Auto-generated method stub\r
-                       return 0;\r
-               }\r
-\r
-               @Override\r
-               void setGCStatusFlag(int flag, boolean value) {\r
-                       // TODO Auto-generated method stub\r
-                       \r
-               }\r
-               \r
-               public Object getOriginalRequest() {\r
-                       throw new UnsupportedOperationException();\r
-               }\r
-        \r
-    };\r
-\r
-    /**\r
-     * Creates a new <code>TObjectHash</code> instance with the\r
-     * default capacity and load factor.\r
-     */\r
-    public QueryIdentityHash() {\r
-        super();\r
-    }\r
-\r
-    /**\r
-     * Creates a new <code>TObjectHash</code> instance whose capacity\r
-     * is the next highest prime above <tt>initialCapacity + 1</tt>\r
-     * unless that value is already prime.\r
-     *\r
-     * @param initialCapacity an <code>int</code> value\r
-     */\r
-    public QueryIdentityHash(int initialCapacity) {\r
-        super(initialCapacity, 0.75f);\r
-    }\r
-\r
-    final public int capacity() {\r
-        return _set.length;\r
-    }\r
-\r
-    final protected void removeAt(int index) {\r
-        _set[index] = REMOVED;\r
-        super.removeAt(index);\r
-    }\r
-\r
-    /**\r
-     * initializes the Object set of this hash table.\r
-     *\r
-     * @param initialCapacity an <code>int</code> value\r
-     * @return an <code>int</code> value\r
-     */\r
-    final protected int setUp(int initialCapacity) {\r
-        int capacity;\r
-\r
-        capacity = super.setUp(initialCapacity);\r
-        _set = new CacheEntry[capacity];\r
-        return capacity;\r
-        \r
-    }\r
-\r
-    /**\r
-     * Searches the set for <tt>obj</tt>\r
-     *\r
-     * @param obj an <code>Object</code> value\r
-     * @return a <code>boolean</code> value\r
-     */\r
-    final public boolean contains(Object obj) {\r
-        return index(obj) >= 0;\r
-    }\r
-\r
-    /**\r
-     * Locates the index of <tt>obj</tt>.\r
-     *\r
-     * @param obj an <code>Object</code> value\r
-     * @return the index of <tt>obj</tt> or -1 if it isn't in the set.\r
-     */\r
-    final protected int index(Object obj) {\r
-\r
-        final Object[] set = _set;\r
-        final int length = set.length;\r
-        //final int hash = System.identityHashCode(obj) & 0x7fffffff;\r
-        final int hash = obj.hashCode() & 0x7fffffff;\r
-        int index = hash % length;\r
-        Object cur = set[index];\r
-\r
-        if ( cur == null ) return -1;\r
-\r
-        // NOTE: here it has to be REMOVED or FULL (some user-given value)\r
-        if ( cur == REMOVED || (cur != obj)) {\r
-            // see Knuth, p. 529\r
-            final int probe = 1 + (hash % (length - 2));\r
-\r
-            do {\r
-                index -= probe;\r
-                if (index < 0) {\r
-                    index += length;\r
-                }\r
-                cur = set[index];\r
-            } while (cur != null\r
-                 && (cur == REMOVED || (cur != obj)));\r
-        }\r
-\r
-        return cur == null ? -1 : index;\r
-    }\r
-    \r
-\r
-    /**\r
-     * Locates the index at which <tt>obj</tt> can be inserted.  if\r
-     * there is already a value equal()ing <tt>obj</tt> in the set,\r
-     * returns that value's index as <tt>-index - 1</tt>.\r
-     *\r
-     * @param obj an <code>Object</code> value\r
-     * @return the index of a FREE slot at which obj can be inserted\r
-     * or, if obj is already stored in the hash, the negative value of\r
-     * that index, minus 1: -index -1.\r
-     */\r
-    final protected int insertionIndex(Object obj) {\r
-\r
-        final Object[] set = _set;\r
-        final int length = set.length;\r
-        //final int hash = System.identityHashCode(obj) & 0x7fffffff;\r
-        final int hash = obj.hashCode() & 0x7fffffff;\r
-        int index = hash % length;\r
-        Object cur = set[index];\r
-\r
-        if (cur == null) {\r
-            return index;       // empty, all done\r
-        } else if (cur != REMOVED && (cur == obj)) {\r
-            return -index -1;   // already stored\r
-        } else {                // already FULL or REMOVED, must probe\r
-            // compute the double hash\r
-            final int probe = 1 + (hash % (length - 2));\r
-\r
-            // if the slot we landed on is FULL (but not removed), probe\r
-            // until we find an empty slot, a REMOVED slot, or an element\r
-            // equal to the one we are trying to insert.\r
-            // finding an empty slot means that the value is not present\r
-            // and that we should use that slot as the insertion point;\r
-            // finding a REMOVED slot means that we need to keep searching,\r
-            // however we want to remember the offset of that REMOVED slot\r
-            // so we can reuse it in case a "new" insertion (i.e. not an update)\r
-            // is possible.\r
-            // finding a matching value means that we've found that our desired\r
-            // key is already in the table\r
-            if (cur != REMOVED) {\r
-                // starting at the natural offset, probe until we find an\r
-                // offset that isn't full.\r
-                do {\r
-                    index -= probe;\r
-                    if (index < 0) {\r
-                        index += length;\r
-                    }\r
-                    cur = set[index];\r
-                } while (cur != null\r
-                         && cur != REMOVED\r
-                         && (cur != obj));\r
-            }\r
-\r
-            // if the index we found was removed: continue probing until we\r
-            // locate a free location or an element which equal()s the\r
-            // one we have.\r
-            if (cur == REMOVED) {\r
-                int firstRemoved = index;\r
-                while (cur != null\r
-                       && (cur == REMOVED || (cur != obj))) {\r
-                    index -= probe;\r
-                    if (index < 0) {\r
-                        index += length;\r
-                    }\r
-                    cur = set[index];\r
-                }\r
-                // NOTE: cur cannot == REMOVED in this block\r
-                return (cur != null) ? -index -1 : firstRemoved;\r
-            }\r
-            // if it's full, the key is already stored\r
-            // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)\r
-            return (cur != null) ? -index -1 : index;\r
-        }\r
-    }\r
-\r
-} // TObjectHash\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.impl.query;
+
+import java.util.Collection;
+
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.impl.graph.ReadGraphImpl;
+
+import gnu.trove.impl.hash.THash;
+
+
+/**
+ * An open addressed hashing implementation for Object types.
+ *
+ * Created: Sun Nov  4 08:56:06 2001
+ *
+ * @author Eric D. Friedman
+ * @version $Id: QueryIdentityHash.java,v 1.1 2008/03/14 11:32:01 tuoksk Exp $
+ */
+abstract public class QueryIdentityHash extends THash {
+
+       static final long serialVersionUID = -3461112548087185871L;
+
+    /** the set of Objects */
+    protected transient CacheEntry[] _set;
+
+    protected static final CacheEntry REMOVED = new CacheEntry() {
+
+        @Override
+        public void addParent(CacheEntry entry) {
+            // TODO Auto-generated method stub
+            
+        }
+
+        @Override
+        public void clearResult(QuerySupport support) {
+            // TODO Auto-generated method stub
+            
+        }
+
+        @Override
+        public void discard() {
+            // TODO Auto-generated method stub
+            
+        }
+
+        @Override
+        public void except(Throwable t) {
+            // TODO Auto-generated method stub
+            
+        }
+
+        @Override
+        public Collection<CacheEntry> getParents(QueryProcessor processor) {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        @Override
+        public Query getQuery() {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        @Override
+        public boolean hasParents() {
+            // TODO Auto-generated method stub
+            return false;
+        }
+
+        @Override
+        public boolean isDiscarded() {
+            // TODO Auto-generated method stub
+            return false;
+        }
+
+        @Override
+        public boolean isExcepted() {
+            // TODO Auto-generated method stub
+            return false;
+        }
+
+        @Override
+        public boolean isFresh() {
+            // TODO Auto-generated method stub
+            return false;
+        }
+
+        @Override
+        public boolean isPending() {
+            // TODO Auto-generated method stub
+            return false;
+        }
+
+        @Override
+        public boolean isReady() {
+            // TODO Auto-generated method stub
+            return false;
+        }
+
+        @Override
+        public boolean isRefuted() {
+            // TODO Auto-generated method stub
+            return false;
+        }
+
+        @Override
+        public void refute() {
+            // TODO Auto-generated method stub
+            
+        }
+
+        @Override
+        CacheEntry pruneFirstParents() {
+            // TODO Auto-generated method stub
+               return null;
+        }
+        
+        @Override
+        public void removeParent(CacheEntry entry) {
+            // TODO Auto-generated method stub
+            
+        }
+
+        @Override
+        public void setPending(QuerySupport querySupport) {
+            // TODO Auto-generated method stub
+            
+        }
+
+        @Override
+        public void setReady() {
+            // TODO Auto-generated method stub
+            
+        }
+
+        @Override
+        public void setResult(Object result) {
+            // TODO Auto-generated method stub
+            
+        }
+        
+        @Override
+        boolean isImmutable(ReadGraphImpl graph) {
+               return true;
+        }
+
+               @Override
+               boolean shouldBeCollected() {
+                       // TODO Auto-generated method stub
+                       return false;
+               }
+
+               @Override
+               CacheEntry getFirstParent(QueryProcessor processor) {
+                       // TODO Auto-generated method stub
+                       return null;
+               }
+               
+               @Override
+               boolean moreThanOneParent(QueryProcessor processor) {
+                       // TODO Auto-generated method stub
+                       return false;
+               }
+
+               @Override
+               int parentCount(QueryProcessor processor) {
+                       // TODO Auto-generated method stub
+                       return 0;
+               }
+               
+               @Override
+               short getLevel() {
+                       // TODO Auto-generated method stub
+                       return 0;
+               }
+
+               @Override
+               short setLevel(short level) {
+                       // TODO Auto-generated method stub
+                       return level;
+               }
+               
+               @Override
+               void prepareRecompute(QuerySupport querySupport) {
+                       // TODO Auto-generated method stub
+               }
+
+               @Override
+               int getGCStatus() {
+                       // TODO Auto-generated method stub
+                       return 0;
+               }
+
+               @Override
+               int setGCStatus(int status) {
+                       // TODO Auto-generated method stub
+                       return 0;
+               }
+
+               @Override
+               void setGCStatusFlag(int flag, boolean value) {
+                       // TODO Auto-generated method stub
+                       
+               }
+               
+               public Object getOriginalRequest() {
+                       throw new UnsupportedOperationException();
+               }
+
+               @Override
+               Object performFromCache(ReadGraphImpl graph, Object procedure) throws DatabaseException {
+                       // TODO Auto-generated method stub
+                       return null;
+               }
+
+               @Override
+               public Object getResult() {
+                       // TODO Auto-generated method stub
+                       return null;
+               }
+
+        @Override
+            void pruneParentSet() {
+        }
+
+    };
+
+    /**
+     * Creates a new <code>TObjectHash</code> instance with the
+     * default capacity and load factor.
+     */
+    public QueryIdentityHash() {
+        super();
+    }
+
+    /**
+     * Creates a new <code>TObjectHash</code> instance whose capacity
+     * is the next highest prime above <tt>initialCapacity + 1</tt>
+     * unless that value is already prime.
+     *
+     * @param initialCapacity an <code>int</code> value
+     */
+    public QueryIdentityHash(int initialCapacity) {
+        super(initialCapacity, 0.75f);
+    }
+
+    final public int capacity() {
+        return _set.length;
+    }
+
+    final protected void removeAt(int index) {
+        _set[index] = REMOVED;
+        super.removeAt(index);
+    }
+
+    /**
+     * initializes the Object set of this hash table.
+     *
+     * @param initialCapacity an <code>int</code> value
+     * @return an <code>int</code> value
+     */
+    final protected int setUp(int initialCapacity) {
+        int capacity;
+
+        capacity = super.setUp(initialCapacity);
+        _set = new CacheEntry[capacity];
+        return capacity;
+        
+    }
+
+    /**
+     * Searches the set for <tt>obj</tt>
+     *
+     * @param obj an <code>Object</code> value
+     * @return a <code>boolean</code> value
+     */
+    final public boolean contains(Object obj) {
+        return index(obj) >= 0;
+    }
+
+    /**
+     * Locates the index of <tt>obj</tt>.
+     *
+     * @param obj an <code>Object</code> value
+     * @return the index of <tt>obj</tt> or -1 if it isn't in the set.
+     */
+    final protected int index(Object obj) {
+
+        final Object[] set = _set;
+        final int length = set.length;
+        //final int hash = System.identityHashCode(obj) & 0x7fffffff;
+        final int hash = obj.hashCode() & 0x7fffffff;
+        int index = hash % length;
+        Object cur = set[index];
+
+        if ( cur == null ) return -1;
+
+        // NOTE: here it has to be REMOVED or FULL (some user-given value)
+        if ( cur == REMOVED || (cur != obj)) {
+            // see Knuth, p. 529
+            final int probe = 1 + (hash % (length - 2));
+
+            do {
+                index -= probe;
+                if (index < 0) {
+                    index += length;
+                }
+                cur = set[index];
+            } while (cur != null
+                 && (cur == REMOVED || (cur != obj)));
+        }
+
+        return cur == null ? -1 : index;
+    }
+    
+
+    /**
+     * Locates the index at which <tt>obj</tt> can be inserted.  if
+     * there is already a value equal()ing <tt>obj</tt> in the set,
+     * returns that value's index as <tt>-index - 1</tt>.
+     *
+     * @param obj an <code>Object</code> value
+     * @return the index of a FREE slot at which obj can be inserted
+     * or, if obj is already stored in the hash, the negative value of
+     * that index, minus 1: -index -1.
+     */
+    final protected int insertionIndex(Object obj) {
+
+        final Object[] set = _set;
+        final int length = set.length;
+        //final int hash = System.identityHashCode(obj) & 0x7fffffff;
+        final int hash = obj.hashCode() & 0x7fffffff;
+        int index = hash % length;
+        Object cur = set[index];
+
+        if (cur == null) {
+            return index;       // empty, all done
+        } else if (cur != REMOVED && (cur == obj)) {
+            return -index -1;   // already stored
+        } else {                // already FULL or REMOVED, must probe
+            // compute the double hash
+            final int probe = 1 + (hash % (length - 2));
+
+            // if the slot we landed on is FULL (but not removed), probe
+            // until we find an empty slot, a REMOVED slot, or an element
+            // equal to the one we are trying to insert.
+            // finding an empty slot means that the value is not present
+            // and that we should use that slot as the insertion point;
+            // finding a REMOVED slot means that we need to keep searching,
+            // however we want to remember the offset of that REMOVED slot
+            // so we can reuse it in case a "new" insertion (i.e. not an update)
+            // is possible.
+            // finding a matching value means that we've found that our desired
+            // key is already in the table
+            if (cur != REMOVED) {
+                // starting at the natural offset, probe until we find an
+                // offset that isn't full.
+                do {
+                    index -= probe;
+                    if (index < 0) {
+                        index += length;
+                    }
+                    cur = set[index];
+                } while (cur != null
+                         && cur != REMOVED
+                         && (cur != obj));
+            }
+
+            // if the index we found was removed: continue probing until we
+            // locate a free location or an element which equal()s the
+            // one we have.
+            if (cur == REMOVED) {
+                int firstRemoved = index;
+                while (cur != null
+                       && (cur == REMOVED || (cur != obj))) {
+                    index -= probe;
+                    if (index < 0) {
+                        index += length;
+                    }
+                    cur = set[index];
+                }
+                // NOTE: cur cannot == REMOVED in this block
+                return (cur != null) ? -index -1 : firstRemoved;
+            }
+            // if it's full, the key is already stored
+            // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
+            return (cur != null) ? -index -1 : index;
+        }
+    }
+
+} // TObjectHash