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();
231 void pruneParentSet() {
237 * Creates a new <code>TObjectHash</code> instance with the
238 * default capacity and load factor.
240 public QueryIdentityHash() {
245 * Creates a new <code>TObjectHash</code> instance whose capacity
246 * is the next highest prime above <tt>initialCapacity + 1</tt>
247 * unless that value is already prime.
249 * @param initialCapacity an <code>int</code> value
251 public QueryIdentityHash(int initialCapacity) {
252 super(initialCapacity, 0.75f);
255 final public int capacity() {
259 final protected void removeAt(int index) {
260 _set[index] = REMOVED;
261 super.removeAt(index);
265 * initializes the Object set of this hash table.
267 * @param initialCapacity an <code>int</code> value
268 * @return an <code>int</code> value
270 final protected int setUp(int initialCapacity) {
273 capacity = super.setUp(initialCapacity);
274 _set = new CacheEntry[capacity];
280 * Searches the set for <tt>obj</tt>
282 * @param obj an <code>Object</code> value
283 * @return a <code>boolean</code> value
285 final public boolean contains(Object obj) {
286 return index(obj) >= 0;
290 * Locates the index of <tt>obj</tt>.
292 * @param obj an <code>Object</code> value
293 * @return the index of <tt>obj</tt> or -1 if it isn't in the set.
295 final protected int index(Object obj) {
297 final Object[] set = _set;
298 final int length = set.length;
299 //final int hash = System.identityHashCode(obj) & 0x7fffffff;
300 final int hash = obj.hashCode() & 0x7fffffff;
301 int index = hash % length;
302 Object cur = set[index];
304 if ( cur == null ) return -1;
306 // NOTE: here it has to be REMOVED or FULL (some user-given value)
307 if ( cur == REMOVED || (cur != obj)) {
309 final int probe = 1 + (hash % (length - 2));
318 && (cur == REMOVED || (cur != obj)));
321 return cur == null ? -1 : index;
326 * Locates the index at which <tt>obj</tt> can be inserted. if
327 * there is already a value equal()ing <tt>obj</tt> in the set,
328 * returns that value's index as <tt>-index - 1</tt>.
330 * @param obj an <code>Object</code> value
331 * @return the index of a FREE slot at which obj can be inserted
332 * or, if obj is already stored in the hash, the negative value of
333 * that index, minus 1: -index -1.
335 final protected int insertionIndex(Object obj) {
337 final Object[] set = _set;
338 final int length = set.length;
339 //final int hash = System.identityHashCode(obj) & 0x7fffffff;
340 final int hash = obj.hashCode() & 0x7fffffff;
341 int index = hash % length;
342 Object cur = set[index];
345 return index; // empty, all done
346 } else if (cur != REMOVED && (cur == obj)) {
347 return -index -1; // already stored
348 } else { // already FULL or REMOVED, must probe
349 // compute the double hash
350 final int probe = 1 + (hash % (length - 2));
352 // if the slot we landed on is FULL (but not removed), probe
353 // until we find an empty slot, a REMOVED slot, or an element
354 // equal to the one we are trying to insert.
355 // finding an empty slot means that the value is not present
356 // and that we should use that slot as the insertion point;
357 // finding a REMOVED slot means that we need to keep searching,
358 // however we want to remember the offset of that REMOVED slot
359 // so we can reuse it in case a "new" insertion (i.e. not an update)
361 // finding a matching value means that we've found that our desired
362 // key is already in the table
363 if (cur != REMOVED) {
364 // starting at the natural offset, probe until we find an
365 // offset that isn't full.
377 // if the index we found was removed: continue probing until we
378 // locate a free location or an element which equal()s the
380 if (cur == REMOVED) {
381 int firstRemoved = index;
383 && (cur == REMOVED || (cur != obj))) {
390 // NOTE: cur cannot == REMOVED in this block
391 return (cur != null) ? -index -1 : firstRemoved;
393 // if it's full, the key is already stored
394 // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
395 return (cur != null) ? -index -1 : index;