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 void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
122 // TODO Auto-generated method stub
127 public void refute() {
128 // TODO Auto-generated method stub
133 CacheEntry pruneFirstParents() {
134 // TODO Auto-generated method stub
139 public void removeParent(CacheEntry entry) {
140 // TODO Auto-generated method stub
145 public void setPending() {
146 // TODO Auto-generated method stub
151 public void setReady() {
152 // TODO Auto-generated method stub
157 public void setResult(Object result) {
158 // TODO Auto-generated method stub
163 boolean isImmutable(ReadGraphImpl graph) {
168 boolean shouldBeCollected() {
169 // TODO Auto-generated method stub
174 CacheEntry getFirstParent(QueryProcessor processor) {
175 // TODO Auto-generated method stub
180 boolean moreThanOneParent(QueryProcessor processor) {
181 // TODO Auto-generated method stub
186 int parentCount(QueryProcessor processor) {
187 // TODO Auto-generated method stub
193 // TODO Auto-generated method stub
198 short setLevel(short level) {
199 // TODO Auto-generated method stub
204 void prepareRecompute(QuerySupport querySupport) {
205 // TODO Auto-generated method stub
210 // TODO Auto-generated method stub
215 int setGCStatus(int status) {
216 // TODO Auto-generated method stub
221 void setGCStatusFlag(int flag, boolean value) {
222 // TODO Auto-generated method stub
226 public Object getOriginalRequest() {
227 throw new UnsupportedOperationException();
233 * Creates a new <code>TObjectHash</code> instance with the
234 * default capacity and load factor.
236 public QueryIdentityHash() {
241 * Creates a new <code>TObjectHash</code> instance whose capacity
242 * is the next highest prime above <tt>initialCapacity + 1</tt>
243 * unless that value is already prime.
245 * @param initialCapacity an <code>int</code> value
247 public QueryIdentityHash(int initialCapacity) {
248 super(initialCapacity, 0.75f);
251 final public int capacity() {
255 final protected void removeAt(int index) {
256 _set[index] = REMOVED;
257 super.removeAt(index);
261 * initializes the Object set of this hash table.
263 * @param initialCapacity an <code>int</code> value
264 * @return an <code>int</code> value
266 final protected int setUp(int initialCapacity) {
269 capacity = super.setUp(initialCapacity);
270 _set = new CacheEntry[capacity];
276 * Searches the set for <tt>obj</tt>
278 * @param obj an <code>Object</code> value
279 * @return a <code>boolean</code> value
281 final public boolean contains(Object obj) {
282 return index(obj) >= 0;
286 * Locates the index of <tt>obj</tt>.
288 * @param obj an <code>Object</code> value
289 * @return the index of <tt>obj</tt> or -1 if it isn't in the set.
291 final protected int index(Object obj) {
293 final Object[] set = _set;
294 final int length = set.length;
295 //final int hash = System.identityHashCode(obj) & 0x7fffffff;
296 final int hash = obj.hashCode() & 0x7fffffff;
297 int index = hash % length;
298 Object cur = set[index];
300 if ( cur == null ) return -1;
302 // NOTE: here it has to be REMOVED or FULL (some user-given value)
303 if ( cur == REMOVED || (cur != obj)) {
305 final int probe = 1 + (hash % (length - 2));
314 && (cur == REMOVED || (cur != obj)));
317 return cur == null ? -1 : index;
322 * Locates the index at which <tt>obj</tt> can be inserted. if
323 * there is already a value equal()ing <tt>obj</tt> in the set,
324 * returns that value's index as <tt>-index - 1</tt>.
326 * @param obj an <code>Object</code> value
327 * @return the index of a FREE slot at which obj can be inserted
328 * or, if obj is already stored in the hash, the negative value of
329 * that index, minus 1: -index -1.
331 final protected int insertionIndex(Object obj) {
333 final Object[] set = _set;
334 final int length = set.length;
335 //final int hash = System.identityHashCode(obj) & 0x7fffffff;
336 final int hash = obj.hashCode() & 0x7fffffff;
337 int index = hash % length;
338 Object cur = set[index];
341 return index; // empty, all done
342 } else if (cur != REMOVED && (cur == obj)) {
343 return -index -1; // already stored
344 } else { // already FULL or REMOVED, must probe
345 // compute the double hash
346 final int probe = 1 + (hash % (length - 2));
348 // if the slot we landed on is FULL (but not removed), probe
349 // until we find an empty slot, a REMOVED slot, or an element
350 // equal to the one we are trying to insert.
351 // finding an empty slot means that the value is not present
352 // and that we should use that slot as the insertion point;
353 // finding a REMOVED slot means that we need to keep searching,
354 // however we want to remember the offset of that REMOVED slot
355 // so we can reuse it in case a "new" insertion (i.e. not an update)
357 // finding a matching value means that we've found that our desired
358 // key is already in the table
359 if (cur != REMOVED) {
360 // starting at the natural offset, probe until we find an
361 // offset that isn't full.
373 // if the index we found was removed: continue probing until we
374 // locate a free location or an element which equal()s the
376 if (cur == REMOVED) {
377 int firstRemoved = index;
379 && (cur == REMOVED || (cur != obj))) {
386 // NOTE: cur cannot == REMOVED in this block
387 return (cur != null) ? -index -1 : firstRemoved;
389 // if it's full, the key is already stored
390 // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
391 return (cur != null) ? -index -1 : index;