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 java.util.Collection;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import gnu.trove.impl.hash.THash;
23 * An open addressed hashing implementation for Object types.
25 * Created: Sun Nov 4 08:56:06 2001
27 * @author Eric D. Friedman
28 * @version $Id: QueryIdentityHash.java,v 1.1 2008/03/14 11:32:01 tuoksk Exp $
30 abstract public class QueryIdentityHash extends THash {
32 static final long serialVersionUID = -3461112548087185871L;
34 /** the set of Objects */
35 protected transient CacheEntry[] _set;
37 protected static final CacheEntry REMOVED = new CacheEntry() {
40 public void addParent(CacheEntry entry) {
41 // TODO Auto-generated method stub
46 public void clearResult(QuerySupport support) {
47 // TODO Auto-generated method stub
52 public void discard() {
53 // TODO Auto-generated method stub
58 public void except(Throwable t) {
59 // TODO Auto-generated method stub
64 public Collection<CacheEntry> getParents(QueryProcessor processor) {
65 // TODO Auto-generated method stub
70 public Query getQuery() {
71 // TODO Auto-generated method stub
76 public boolean hasParents() {
77 // TODO Auto-generated method stub
82 public boolean isDiscarded() {
83 // TODO Auto-generated method stub
88 public boolean isExcepted() {
89 // TODO Auto-generated method stub
94 public boolean isFresh() {
95 // TODO Auto-generated method stub
100 public boolean isPending() {
101 // TODO Auto-generated method stub
106 public boolean isReady() {
107 // TODO Auto-generated method stub
112 public boolean isRefuted() {
113 // TODO Auto-generated method stub
118 public void refute() {
119 // TODO Auto-generated method stub
124 CacheEntry pruneFirstParents() {
125 // TODO Auto-generated method stub
130 public void removeParent(CacheEntry entry) {
131 // TODO Auto-generated method stub
136 public void setPending(QuerySupport querySupport) {
137 // TODO Auto-generated method stub
142 public void setReady() {
143 // TODO Auto-generated method stub
148 public void setResult(Object result) {
149 // TODO Auto-generated method stub
154 boolean isImmutable(ReadGraphImpl graph) {
159 boolean shouldBeCollected() {
160 // TODO Auto-generated method stub
165 CacheEntry getFirstParent(QueryProcessor processor) {
166 // TODO Auto-generated method stub
171 boolean moreThanOneParent(QueryProcessor processor) {
172 // TODO Auto-generated method stub
177 int parentCount(QueryProcessor processor) {
178 // TODO Auto-generated method stub
184 // TODO Auto-generated method stub
189 short setLevel(short level) {
190 // TODO Auto-generated method stub
195 void prepareRecompute(QuerySupport querySupport) {
196 // TODO Auto-generated method stub
201 // TODO Auto-generated method stub
206 int setGCStatus(int status) {
207 // TODO Auto-generated method stub
212 void setGCStatusFlag(int flag, boolean value) {
213 // TODO Auto-generated method stub
217 public Object getOriginalRequest() {
218 throw new UnsupportedOperationException();
222 Object performFromCache(ReadGraphImpl graph, Object procedure) throws DatabaseException {
223 // TODO Auto-generated method stub
228 public Object getResult() {
229 // TODO Auto-generated method stub
234 void pruneParentSet() {
240 * Creates a new <code>TObjectHash</code> instance with the
241 * default capacity and load factor.
243 public QueryIdentityHash() {
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.
252 * @param initialCapacity an <code>int</code> value
254 public QueryIdentityHash(int initialCapacity) {
255 super(initialCapacity, 0.75f);
258 final public int capacity() {
262 final protected void removeAt(int index) {
263 _set[index] = REMOVED;
264 super.removeAt(index);
268 * initializes the Object set of this hash table.
270 * @param initialCapacity an <code>int</code> value
271 * @return an <code>int</code> value
273 final protected int setUp(int initialCapacity) {
276 capacity = super.setUp(initialCapacity);
277 _set = new CacheEntry[capacity];
283 * Searches the set for <tt>obj</tt>
285 * @param obj an <code>Object</code> value
286 * @return a <code>boolean</code> value
288 final public boolean contains(Object obj) {
289 return index(obj) >= 0;
293 * Locates the index of <tt>obj</tt>.
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.
298 final protected int index(Object obj) {
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];
307 if ( cur == null ) return -1;
309 // NOTE: here it has to be REMOVED or FULL (some user-given value)
310 if ( cur == REMOVED || (cur != obj)) {
312 final int probe = 1 + (hash % (length - 2));
321 && (cur == REMOVED || (cur != obj)));
324 return cur == null ? -1 : index;
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>.
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.
338 final protected int insertionIndex(Object obj) {
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];
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));
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)
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.
380 // if the index we found was removed: continue probing until we
381 // locate a free location or an element which equal()s the
383 if (cur == REMOVED) {
384 int firstRemoved = index;
386 && (cur == REMOVED || (cur != obj))) {
393 // NOTE: cur cannot == REMOVED in this block
394 return (cur != null) ? -index -1 : firstRemoved;
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;