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
236 * Creates a new <code>TObjectHash</code> instance with the
237 * default capacity and load factor.
239 public QueryIdentityHash() {
244 * Creates a new <code>TObjectHash</code> instance whose capacity
245 * is the next highest prime above <tt>initialCapacity + 1</tt>
246 * unless that value is already prime.
248 * @param initialCapacity an <code>int</code> value
250 public QueryIdentityHash(int initialCapacity) {
251 super(initialCapacity, 0.75f);
254 final public int capacity() {
258 final protected void removeAt(int index) {
259 _set[index] = REMOVED;
260 super.removeAt(index);
264 * initializes the Object set of this hash table.
266 * @param initialCapacity an <code>int</code> value
267 * @return an <code>int</code> value
269 final protected int setUp(int initialCapacity) {
272 capacity = super.setUp(initialCapacity);
273 _set = new CacheEntry[capacity];
279 * Searches the set for <tt>obj</tt>
281 * @param obj an <code>Object</code> value
282 * @return a <code>boolean</code> value
284 final public boolean contains(Object obj) {
285 return index(obj) >= 0;
289 * Locates the index of <tt>obj</tt>.
291 * @param obj an <code>Object</code> value
292 * @return the index of <tt>obj</tt> or -1 if it isn't in the set.
294 final protected int index(Object obj) {
296 final Object[] set = _set;
297 final int length = set.length;
298 //final int hash = System.identityHashCode(obj) & 0x7fffffff;
299 final int hash = obj.hashCode() & 0x7fffffff;
300 int index = hash % length;
301 Object cur = set[index];
303 if ( cur == null ) return -1;
305 // NOTE: here it has to be REMOVED or FULL (some user-given value)
306 if ( cur == REMOVED || (cur != obj)) {
308 final int probe = 1 + (hash % (length - 2));
317 && (cur == REMOVED || (cur != obj)));
320 return cur == null ? -1 : index;
325 * Locates the index at which <tt>obj</tt> can be inserted. if
326 * there is already a value equal()ing <tt>obj</tt> in the set,
327 * returns that value's index as <tt>-index - 1</tt>.
329 * @param obj an <code>Object</code> value
330 * @return the index of a FREE slot at which obj can be inserted
331 * or, if obj is already stored in the hash, the negative value of
332 * that index, minus 1: -index -1.
334 final protected int insertionIndex(Object obj) {
336 final Object[] set = _set;
337 final int length = set.length;
338 //final int hash = System.identityHashCode(obj) & 0x7fffffff;
339 final int hash = obj.hashCode() & 0x7fffffff;
340 int index = hash % length;
341 Object cur = set[index];
344 return index; // empty, all done
345 } else if (cur != REMOVED && (cur == obj)) {
346 return -index -1; // already stored
347 } else { // already FULL or REMOVED, must probe
348 // compute the double hash
349 final int probe = 1 + (hash % (length - 2));
351 // if the slot we landed on is FULL (but not removed), probe
352 // until we find an empty slot, a REMOVED slot, or an element
353 // equal to the one we are trying to insert.
354 // finding an empty slot means that the value is not present
355 // and that we should use that slot as the insertion point;
356 // finding a REMOVED slot means that we need to keep searching,
357 // however we want to remember the offset of that REMOVED slot
358 // so we can reuse it in case a "new" insertion (i.e. not an update)
360 // finding a matching value means that we've found that our desired
361 // key is already in the table
362 if (cur != REMOVED) {
363 // starting at the natural offset, probe until we find an
364 // offset that isn't full.
376 // if the index we found was removed: continue probing until we
377 // locate a free location or an element which equal()s the
379 if (cur == REMOVED) {
380 int firstRemoved = index;
382 && (cur == REMOVED || (cur != obj))) {
389 // NOTE: cur cannot == REMOVED in this block
390 return (cur != null) ? -index -1 : firstRemoved;
392 // if it's full, the key is already stored
393 // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
394 return (cur != null) ? -index -1 : index;