]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import gnu.trove.impl.hash.THash;
15
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
18
19
20 /**
21  * An open addressed hashing implementation for Object types.
22  *
23  * Created: Sun Nov  4 08:56:06 2001
24  *
25  * @author Eric D. Friedman
26  * @version $Id: QueryIdentityHash.java,v 1.1 2008/03/14 11:32:01 tuoksk Exp $
27  */
28 abstract public class QueryIdentityHash extends THash {
29
30         static final long serialVersionUID = -3461112548087185871L;
31
32     /** the set of Objects */
33     protected transient CacheEntry[] _set;
34
35     protected static final CacheEntry REMOVED = new CacheEntry() {
36
37         @Override
38         public void addParent(CacheEntry entry) {
39             // TODO Auto-generated method stub
40             
41         }
42
43         @Override
44         public void clearResult(QuerySupport support) {
45             // TODO Auto-generated method stub
46             
47         }
48
49         @Override
50         public void discard() {
51             // TODO Auto-generated method stub
52             
53         }
54
55         @Override
56         public void except(Throwable t) {
57             // TODO Auto-generated method stub
58             
59         }
60
61         @Override
62         public Iterable<CacheEntry> getParents(QueryProcessor processor) {
63             // TODO Auto-generated method stub
64             return null;
65         }
66
67         @Override
68         public Query getQuery() {
69             // TODO Auto-generated method stub
70             return null;
71         }
72
73         @Override
74         public boolean hasParents() {
75             // TODO Auto-generated method stub
76             return false;
77         }
78
79         @Override
80         public boolean isDiscarded() {
81             // TODO Auto-generated method stub
82             return false;
83         }
84
85         @Override
86         public boolean isExcepted() {
87             // TODO Auto-generated method stub
88             return false;
89         }
90
91         @Override
92         public boolean isFresh() {
93             // TODO Auto-generated method stub
94             return false;
95         }
96
97         @Override
98         public boolean isPending() {
99             // TODO Auto-generated method stub
100             return false;
101         }
102
103         @Override
104         public boolean isReady() {
105             // TODO Auto-generated method stub
106             return false;
107         }
108
109         @Override
110         public boolean isRefuted() {
111             // TODO Auto-generated method stub
112             return false;
113         }
114
115         @Override
116         public void refute() {
117             // TODO Auto-generated method stub
118             
119         }
120
121         @Override
122         CacheEntry pruneFirstParents() {
123             // TODO Auto-generated method stub
124                 return null;
125         }
126         
127         @Override
128         public void removeParent(CacheEntry entry) {
129             // TODO Auto-generated method stub
130             
131         }
132
133         @Override
134         public void setPending(QuerySupport querySupport) {
135             // TODO Auto-generated method stub
136             
137         }
138
139         @Override
140         public void setReady() {
141             // TODO Auto-generated method stub
142             
143         }
144
145         @Override
146         public void setResult(Object result) {
147             // TODO Auto-generated method stub
148             
149         }
150         
151         @Override
152         boolean isImmutable(ReadGraphImpl graph) {
153                 return true;
154         }
155
156                 @Override
157                 boolean shouldBeCollected() {
158                         // TODO Auto-generated method stub
159                         return false;
160                 }
161
162                 @Override
163                 CacheEntry getFirstParent(QueryProcessor processor) {
164                         // TODO Auto-generated method stub
165                         return null;
166                 }
167                 
168                 @Override
169                 boolean moreThanOneParent(QueryProcessor processor) {
170                         // TODO Auto-generated method stub
171                         return false;
172                 }
173
174                 @Override
175                 int parentCount(QueryProcessor processor) {
176                         // TODO Auto-generated method stub
177                         return 0;
178                 }
179                 
180                 @Override
181                 short getLevel() {
182                         // TODO Auto-generated method stub
183                         return 0;
184                 }
185
186                 @Override
187                 short setLevel(short level) {
188                         // TODO Auto-generated method stub
189                         return level;
190                 }
191                 
192                 @Override
193                 void prepareRecompute(QuerySupport querySupport) {
194                         // TODO Auto-generated method stub
195                 }
196
197                 @Override
198                 int getGCStatus() {
199                         // TODO Auto-generated method stub
200                         return 0;
201                 }
202
203                 @Override
204                 int setGCStatus(int status) {
205                         // TODO Auto-generated method stub
206                         return 0;
207                 }
208
209                 @Override
210                 void setGCStatusFlag(int flag, boolean value) {
211                         // TODO Auto-generated method stub
212                         
213                 }
214                 
215                 public Object getOriginalRequest() {
216                         throw new UnsupportedOperationException();
217                 }
218
219                 @Override
220                 Object performFromCache(ReadGraphImpl graph, Object procedure) throws DatabaseException {
221                         // TODO Auto-generated method stub
222                         return null;
223                 }
224
225                 @Override
226                 public Object getResult() {
227                         // TODO Auto-generated method stub
228                         return null;
229                 }
230
231         @Override
232             void pruneParentSet() {
233         }
234
235     };
236
237     /**
238      * Creates a new <code>TObjectHash</code> instance with the
239      * default capacity and load factor.
240      */
241     public QueryIdentityHash() {
242         super();
243     }
244
245     /**
246      * Creates a new <code>TObjectHash</code> instance whose capacity
247      * is the next highest prime above <tt>initialCapacity + 1</tt>
248      * unless that value is already prime.
249      *
250      * @param initialCapacity an <code>int</code> value
251      */
252     public QueryIdentityHash(int initialCapacity) {
253         super(initialCapacity, 0.75f);
254     }
255
256     final public int capacity() {
257         return _set.length;
258     }
259
260     final protected void removeAt(int index) {
261         _set[index] = REMOVED;
262         super.removeAt(index);
263     }
264
265     /**
266      * initializes the Object set of this hash table.
267      *
268      * @param initialCapacity an <code>int</code> value
269      * @return an <code>int</code> value
270      */
271     final protected int setUp(int initialCapacity) {
272         int capacity;
273
274         capacity = super.setUp(initialCapacity);
275         _set = new CacheEntry[capacity];
276         return capacity;
277         
278     }
279
280     /**
281      * Searches the set for <tt>obj</tt>
282      *
283      * @param obj an <code>Object</code> value
284      * @return a <code>boolean</code> value
285      */
286     final public boolean contains(Object obj) {
287         return index(obj) >= 0;
288     }
289
290     /**
291      * Locates the index of <tt>obj</tt>.
292      *
293      * @param obj an <code>Object</code> value
294      * @return the index of <tt>obj</tt> or -1 if it isn't in the set.
295      */
296     final protected int index(Object obj) {
297
298         final Object[] set = _set;
299         final int length = set.length;
300         //final int hash = System.identityHashCode(obj) & 0x7fffffff;
301         final int hash = obj.hashCode() & 0x7fffffff;
302         int index = hash % length;
303         Object cur = set[index];
304
305         if ( cur == null ) return -1;
306
307         // NOTE: here it has to be REMOVED or FULL (some user-given value)
308         if ( cur == REMOVED || (cur != obj)) {
309             // see Knuth, p. 529
310             final int probe = 1 + (hash % (length - 2));
311
312             do {
313                 index -= probe;
314                 if (index < 0) {
315                     index += length;
316                 }
317                 cur = set[index];
318             } while (cur != null
319                  && (cur == REMOVED || (cur != obj)));
320         }
321
322         return cur == null ? -1 : index;
323     }
324     
325
326     /**
327      * Locates the index at which <tt>obj</tt> can be inserted.  if
328      * there is already a value equal()ing <tt>obj</tt> in the set,
329      * returns that value's index as <tt>-index - 1</tt>.
330      *
331      * @param obj an <code>Object</code> value
332      * @return the index of a FREE slot at which obj can be inserted
333      * or, if obj is already stored in the hash, the negative value of
334      * that index, minus 1: -index -1.
335      */
336     final protected int insertionIndex(Object obj) {
337
338         final Object[] set = _set;
339         final int length = set.length;
340         //final int hash = System.identityHashCode(obj) & 0x7fffffff;
341         final int hash = obj.hashCode() & 0x7fffffff;
342         int index = hash % length;
343         Object cur = set[index];
344
345         if (cur == null) {
346             return index;       // empty, all done
347         } else if (cur != REMOVED && (cur == obj)) {
348             return -index -1;   // already stored
349         } else {                // already FULL or REMOVED, must probe
350             // compute the double hash
351             final int probe = 1 + (hash % (length - 2));
352
353             // if the slot we landed on is FULL (but not removed), probe
354             // until we find an empty slot, a REMOVED slot, or an element
355             // equal to the one we are trying to insert.
356             // finding an empty slot means that the value is not present
357             // and that we should use that slot as the insertion point;
358             // finding a REMOVED slot means that we need to keep searching,
359             // however we want to remember the offset of that REMOVED slot
360             // so we can reuse it in case a "new" insertion (i.e. not an update)
361             // is possible.
362             // finding a matching value means that we've found that our desired
363             // key is already in the table
364             if (cur != REMOVED) {
365                 // starting at the natural offset, probe until we find an
366                 // offset that isn't full.
367                 do {
368                     index -= probe;
369                     if (index < 0) {
370                         index += length;
371                     }
372                     cur = set[index];
373                 } while (cur != null
374                          && cur != REMOVED
375                          && (cur != obj));
376             }
377
378             // if the index we found was removed: continue probing until we
379             // locate a free location or an element which equal()s the
380             // one we have.
381             if (cur == REMOVED) {
382                 int firstRemoved = index;
383                 while (cur != null
384                        && (cur == REMOVED || (cur != obj))) {
385                     index -= probe;
386                     if (index < 0) {
387                         index += length;
388                     }
389                     cur = set[index];
390                 }
391                 // NOTE: cur cannot == REMOVED in this block
392                 return (cur != null) ? -index -1 : firstRemoved;
393             }
394             // if it's full, the key is already stored
395             // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
396             return (cur != null) ? -index -1 : index;
397         }
398     }
399
400 } // TObjectHash