1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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 gnu.trove.impl.hash.THash;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
20 * An open addressed hashing implementation for Object types.
22 * Created: Sun Nov 4 08:56:06 2001
24 * @author Eric D. Friedman
25 * @version $Id: QueryIdentityHash.java,v 1.1 2008/03/14 11:32:01 tuoksk Exp $
27 abstract public class QueryIdentityHash extends THash {
29 static final long serialVersionUID = -3461112548087185871L;
31 /** the set of Objects */
32 protected transient CacheEntry[] _set;
34 protected static final CacheEntry REMOVED = new CacheEntry() {
37 public void addParent(CacheEntry entry) {
38 // TODO Auto-generated method stub
43 public void clearResult(QuerySupport support) {
44 // TODO Auto-generated method stub
49 public void discard() {
50 // TODO Auto-generated method stub
55 public void except(Throwable t) {
56 // TODO Auto-generated method stub
61 public Iterable<CacheEntry> getParents(QueryProcessor processor) {
62 // TODO Auto-generated method stub
67 public Query getQuery() {
68 // TODO Auto-generated method stub
73 public <T> T getResult() {
74 // TODO Auto-generated method stub
79 public boolean hasParents() {
80 // TODO Auto-generated method stub
85 public boolean isDiscarded() {
86 // TODO Auto-generated method stub
91 public boolean isExcepted() {
92 // TODO Auto-generated method stub
97 public boolean isFresh() {
98 // TODO Auto-generated method stub
103 public boolean isPending() {
104 // TODO Auto-generated method stub
109 public boolean isReady() {
110 // TODO Auto-generated method stub
115 public boolean isRefuted() {
116 // TODO Auto-generated method stub
121 public Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
126 public void refute() {
127 // TODO Auto-generated method stub
132 CacheEntry pruneFirstParents() {
133 // TODO Auto-generated method stub
138 public void removeParent(CacheEntry entry) {
139 // TODO Auto-generated method stub
144 public void setPending() {
145 // TODO Auto-generated method stub
150 public void setReady() {
151 // TODO Auto-generated method stub
156 public void setResult(Object result) {
157 // TODO Auto-generated method stub
162 boolean isImmutable(ReadGraphImpl graph) {
167 boolean shouldBeCollected() {
168 // TODO Auto-generated method stub
173 CacheEntry getFirstParent(QueryProcessor processor) {
174 // TODO Auto-generated method stub
179 boolean moreThanOneParent(QueryProcessor processor) {
180 // TODO Auto-generated method stub
185 int parentCount(QueryProcessor processor) {
186 // TODO Auto-generated method stub
192 // TODO Auto-generated method stub
197 short setLevel(short level) {
198 // TODO Auto-generated method stub
203 void prepareRecompute(QuerySupport querySupport) {
204 // TODO Auto-generated method stub
209 // TODO Auto-generated method stub
214 int setGCStatus(int status) {
215 // TODO Auto-generated method stub
220 void setGCStatusFlag(int flag, boolean value) {
221 // TODO Auto-generated method stub
225 public Object getOriginalRequest() {
226 throw new UnsupportedOperationException();
232 * Creates a new <code>TObjectHash</code> instance with the
233 * default capacity and load factor.
235 public QueryIdentityHash() {
240 * Creates a new <code>TObjectHash</code> instance whose capacity
241 * is the next highest prime above <tt>initialCapacity + 1</tt>
242 * unless that value is already prime.
244 * @param initialCapacity an <code>int</code> value
246 public QueryIdentityHash(int initialCapacity) {
247 super(initialCapacity, 0.75f);
250 final public int capacity() {
254 final protected void removeAt(int index) {
255 _set[index] = REMOVED;
256 super.removeAt(index);
260 * initializes the Object set of this hash table.
262 * @param initialCapacity an <code>int</code> value
263 * @return an <code>int</code> value
265 final protected int setUp(int initialCapacity) {
268 capacity = super.setUp(initialCapacity);
269 _set = new CacheEntry[capacity];
275 * Searches the set for <tt>obj</tt>
277 * @param obj an <code>Object</code> value
278 * @return a <code>boolean</code> value
280 final public boolean contains(Object obj) {
281 return index(obj) >= 0;
285 * Locates the index of <tt>obj</tt>.
287 * @param obj an <code>Object</code> value
288 * @return the index of <tt>obj</tt> or -1 if it isn't in the set.
290 final protected int index(Object obj) {
292 final Object[] set = _set;
293 final int length = set.length;
294 //final int hash = System.identityHashCode(obj) & 0x7fffffff;
295 final int hash = obj.hashCode() & 0x7fffffff;
296 int index = hash % length;
297 Object cur = set[index];
299 if ( cur == null ) return -1;
301 // NOTE: here it has to be REMOVED or FULL (some user-given value)
302 if ( cur == REMOVED || (cur != obj)) {
304 final int probe = 1 + (hash % (length - 2));
313 && (cur == REMOVED || (cur != obj)));
316 return cur == null ? -1 : index;
321 * Locates the index at which <tt>obj</tt> can be inserted. if
322 * there is already a value equal()ing <tt>obj</tt> in the set,
323 * returns that value's index as <tt>-index - 1</tt>.
325 * @param obj an <code>Object</code> value
326 * @return the index of a FREE slot at which obj can be inserted
327 * or, if obj is already stored in the hash, the negative value of
328 * that index, minus 1: -index -1.
330 final protected int insertionIndex(Object obj) {
332 final Object[] set = _set;
333 final int length = set.length;
334 //final int hash = System.identityHashCode(obj) & 0x7fffffff;
335 final int hash = obj.hashCode() & 0x7fffffff;
336 int index = hash % length;
337 Object cur = set[index];
340 return index; // empty, all done
341 } else if (cur != REMOVED && (cur == obj)) {
342 return -index -1; // already stored
343 } else { // already FULL or REMOVED, must probe
344 // compute the double hash
345 final int probe = 1 + (hash % (length - 2));
347 // if the slot we landed on is FULL (but not removed), probe
348 // until we find an empty slot, a REMOVED slot, or an element
349 // equal to the one we are trying to insert.
350 // finding an empty slot means that the value is not present
351 // and that we should use that slot as the insertion point;
352 // finding a REMOVED slot means that we need to keep searching,
353 // however we want to remember the offset of that REMOVED slot
354 // so we can reuse it in case a "new" insertion (i.e. not an update)
356 // finding a matching value means that we've found that our desired
357 // key is already in the table
358 if (cur != REMOVED) {
359 // starting at the natural offset, probe until we find an
360 // offset that isn't full.
372 // if the index we found was removed: continue probing until we
373 // locate a free location or an element which equal()s the
375 if (cur == REMOVED) {
376 int firstRemoved = index;
378 && (cur == REMOVED || (cur != obj))) {
385 // NOTE: cur cannot == REMOVED in this block
386 return (cur != null) ? -index -1 : firstRemoved;
388 // if it's full, the key is already stored
389 // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
390 return (cur != null) ? -index -1 : index;