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.exception.DatabaseException;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
21 * An open addressed hashing implementation for Object types.
23 * Created: Sun Nov 4 08:56:06 2001
25 * @author Eric D. Friedman
26 * @version $Id: QueryIdentityHash.java,v 1.1 2008/03/14 11:32:01 tuoksk Exp $
28 abstract public class QueryIdentityHash extends THash {
30 static final long serialVersionUID = -3461112548087185871L;
32 /** the set of Objects */
33 protected transient CacheEntry[] _set;
35 protected static final CacheEntry REMOVED = new CacheEntry() {
38 public void addParent(CacheEntry entry) {
39 // TODO Auto-generated method stub
44 public void clearResult(QuerySupport support) {
45 // TODO Auto-generated method stub
50 public void discard() {
51 // TODO Auto-generated method stub
56 public void except(Throwable t) {
57 // TODO Auto-generated method stub
62 public Iterable<CacheEntry> getParents(QueryProcessor processor) {
63 // TODO Auto-generated method stub
68 public Query getQuery() {
69 // TODO Auto-generated method stub
74 public boolean hasParents() {
75 // TODO Auto-generated method stub
80 public boolean isDiscarded() {
81 // TODO Auto-generated method stub
86 public boolean isExcepted() {
87 // TODO Auto-generated method stub
92 public boolean isFresh() {
93 // TODO Auto-generated method stub
98 public boolean isPending() {
99 // TODO Auto-generated method stub
104 public boolean isReady() {
105 // TODO Auto-generated method stub
110 public boolean isRefuted() {
111 // TODO Auto-generated method stub
116 public void refute() {
117 // TODO Auto-generated method stub
122 CacheEntry pruneFirstParents() {
123 // TODO Auto-generated method stub
128 public void removeParent(CacheEntry entry) {
129 // TODO Auto-generated method stub
134 public void setPending() {
135 // TODO Auto-generated method stub
140 public void setReady() {
141 // TODO Auto-generated method stub
146 public void setResult(Object result) {
147 // TODO Auto-generated method stub
152 boolean isImmutable(ReadGraphImpl graph) {
157 boolean shouldBeCollected() {
158 // TODO Auto-generated method stub
163 CacheEntry getFirstParent(QueryProcessor processor) {
164 // TODO Auto-generated method stub
169 boolean moreThanOneParent(QueryProcessor processor) {
170 // TODO Auto-generated method stub
175 int parentCount(QueryProcessor processor) {
176 // TODO Auto-generated method stub
182 // TODO Auto-generated method stub
187 short setLevel(short level) {
188 // TODO Auto-generated method stub
193 void prepareRecompute(QuerySupport querySupport) {
194 // TODO Auto-generated method stub
199 // TODO Auto-generated method stub
204 int setGCStatus(int status) {
205 // TODO Auto-generated method stub
210 void setGCStatusFlag(int flag, boolean value) {
211 // TODO Auto-generated method stub
215 public Object getOriginalRequest() {
216 throw new UnsupportedOperationException();
220 Object performFromCache(ReadGraphImpl graph, Object procedure) throws DatabaseException {
221 // TODO Auto-generated method stub
226 public Object getResult() {
227 // TODO Auto-generated method stub
234 * Creates a new <code>TObjectHash</code> instance with the
235 * default capacity and load factor.
237 public QueryIdentityHash() {
242 * Creates a new <code>TObjectHash</code> instance whose capacity
243 * is the next highest prime above <tt>initialCapacity + 1</tt>
244 * unless that value is already prime.
246 * @param initialCapacity an <code>int</code> value
248 public QueryIdentityHash(int initialCapacity) {
249 super(initialCapacity, 0.75f);
252 final public int capacity() {
256 final protected void removeAt(int index) {
257 _set[index] = REMOVED;
258 super.removeAt(index);
262 * initializes the Object set of this hash table.
264 * @param initialCapacity an <code>int</code> value
265 * @return an <code>int</code> value
267 final protected int setUp(int initialCapacity) {
270 capacity = super.setUp(initialCapacity);
271 _set = new CacheEntry[capacity];
277 * Searches the set for <tt>obj</tt>
279 * @param obj an <code>Object</code> value
280 * @return a <code>boolean</code> value
282 final public boolean contains(Object obj) {
283 return index(obj) >= 0;
287 * Locates the index of <tt>obj</tt>.
289 * @param obj an <code>Object</code> value
290 * @return the index of <tt>obj</tt> or -1 if it isn't in the set.
292 final protected int index(Object obj) {
294 final Object[] set = _set;
295 final int length = set.length;
296 //final int hash = System.identityHashCode(obj) & 0x7fffffff;
297 final int hash = obj.hashCode() & 0x7fffffff;
298 int index = hash % length;
299 Object cur = set[index];
301 if ( cur == null ) return -1;
303 // NOTE: here it has to be REMOVED or FULL (some user-given value)
304 if ( cur == REMOVED || (cur != obj)) {
306 final int probe = 1 + (hash % (length - 2));
315 && (cur == REMOVED || (cur != obj)));
318 return cur == null ? -1 : index;
323 * Locates the index at which <tt>obj</tt> can be inserted. if
324 * there is already a value equal()ing <tt>obj</tt> in the set,
325 * returns that value's index as <tt>-index - 1</tt>.
327 * @param obj an <code>Object</code> value
328 * @return the index of a FREE slot at which obj can be inserted
329 * or, if obj is already stored in the hash, the negative value of
330 * that index, minus 1: -index -1.
332 final protected int insertionIndex(Object obj) {
334 final Object[] set = _set;
335 final int length = set.length;
336 //final int hash = System.identityHashCode(obj) & 0x7fffffff;
337 final int hash = obj.hashCode() & 0x7fffffff;
338 int index = hash % length;
339 Object cur = set[index];
342 return index; // empty, all done
343 } else if (cur != REMOVED && (cur == obj)) {
344 return -index -1; // already stored
345 } else { // already FULL or REMOVED, must probe
346 // compute the double hash
347 final int probe = 1 + (hash % (length - 2));
349 // if the slot we landed on is FULL (but not removed), probe
350 // until we find an empty slot, a REMOVED slot, or an element
351 // equal to the one we are trying to insert.
352 // finding an empty slot means that the value is not present
353 // and that we should use that slot as the insertion point;
354 // finding a REMOVED slot means that we need to keep searching,
355 // however we want to remember the offset of that REMOVED slot
356 // so we can reuse it in case a "new" insertion (i.e. not an update)
358 // finding a matching value means that we've found that our desired
359 // key is already in the table
360 if (cur != REMOVED) {
361 // starting at the natural offset, probe until we find an
362 // offset that isn't full.
374 // if the index we found was removed: continue probing until we
375 // locate a free location or an element which equal()s the
377 if (cur == REMOVED) {
378 int firstRemoved = index;
380 && (cur == REMOVED || (cur != obj))) {
387 // NOTE: cur cannot == REMOVED in this block
388 return (cur != null) ? -index -1 : firstRemoved;
390 // if it's full, the key is already stored
391 // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
392 return (cur != null) ? -index -1 : index;