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