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 java.lang.reflect.Array;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
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: BinaryQueryHash.java,v 1.2 2008/03/14 11:38:53 tuoksk Exp $
30 abstract public class BinaryQueryHash<Procedure> extends THash {
31 static final long serialVersionUID = -3461112548087185871L;
33 /** the set of Objects */
34 protected transient BinaryQuery<Procedure>[] _set;
36 protected final BinaryQuery<Procedure> REMOVED = new BinaryQuery<Procedure>(-1, -1) {
39 public void removeEntry(QueryProcessor provider) {
40 throw new Error("Not possible!");
45 throw new Error("Not possible!");
49 Object performFromCache(ReadGraphImpl graph, Procedure procedure) throws DatabaseException {
50 throw new Error("Not possible!");
54 public void recompute(ReadGraphImpl graph) throws DatabaseException {
55 throw new Error("Not possible!");
61 * Creates a new <code>TObjectHash</code> instance with the
62 * default capacity and load factor.
64 public BinaryQueryHash() {
65 super(DEFAULT_CAPACITY, 0.75f);
68 public int capacity() {
72 protected void removeAt(int index) {
73 _set[index] = REMOVED;
74 super.removeAt(index);
78 * initializes the Object set of this hash table.
80 * @param initialCapacity an <code>int</code> value
81 * @return an <code>int</code> value
83 @SuppressWarnings("unchecked")
84 protected int setUp(int initialCapacity) {
87 capacity = super.setUp(initialCapacity);
88 _set = (BinaryQuery[])Array.newInstance(BinaryQuery.class, capacity);
93 final protected int index(final long id) {
95 final BinaryQuery<Procedure>[] set = _set;
96 final int length = set.length;
97 final int hash = ((31 * ((int)(id>>>32)) + (int)id) )& 0x7fffffff;
98 int index = hash % length;
99 BinaryQuery<Procedure> cur = set[index];
101 if ( cur == null ) return -1;
103 // NOTE: here it has to be REMOVED or FULL (some user-given value)
104 if ( cur == REMOVED || !(id == cur.id)) {
106 final int probe = 1 + (hash % (length - 2));
115 && (cur == REMOVED || !(id == cur.id)));
118 return cur == null ? -1 : index;
124 * Locates the index at which <tt>obj</tt> can be inserted. if
125 * there is already a value equal()ing <tt>obj</tt> in the set,
126 * returns that value's index as <tt>-index - 1</tt>.
128 * @param obj an <code>Object</code> value
129 * @return the index of a FREE slot at which obj can be inserted
130 * or, if obj is already stored in the hash, the negative value of
131 * that index, minus 1: -index -1.
133 final protected int insertionIndex(final long id) {
135 final BinaryQuery<Procedure>[] set = _set;
136 final int length = set.length;
137 final int hash = (31 * ((int)(id>>>32)) + (int)id) & 0x7fffffff;
138 int index = hash % length;
139 BinaryQuery<Procedure> cur = set[index];
142 return index; // empty, all done
143 } else if (cur != REMOVED && (id == cur.id)) {
144 return -index -1; // already stored
145 } else { // already FULL or REMOVED, must probe
146 // compute the double hash
147 final int probe = 1 + (hash % (length - 2));
149 // if the slot we landed on is FULL (but not removed), probe
150 // until we find an empty slot, a REMOVED slot, or an element
151 // equal to the one we are trying to insert.
152 // finding an empty slot means that the value is not present
153 // and that we should use that slot as the insertion point;
154 // finding a REMOVED slot means that we need to keep searching,
155 // however we want to remember the offset of that REMOVED slot
156 // so we can reuse it in case a "new" insertion (i.e. not an update)
158 // finding a matching value means that we've found that our desired
159 // key is already in the table
160 if (cur != REMOVED) {
161 // starting at the natural offset, probe until we find an
162 // offset that isn't full.
171 && ! (id == cur.id));
174 // if the index we found was removed: continue probing until we
175 // locate a free location or an element which equal()s the
177 if (cur == REMOVED) {
178 int firstRemoved = index;
180 && (cur == REMOVED || ! (id == cur.id))) {
187 // NOTE: cur cannot == REMOVED in this block
188 return (cur != null) ? -index -1 : firstRemoved;
190 // if it's full, the key is already stored
191 // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
192 return (cur != null) ? -index -1 : index;
197 * Convenience methods for subclasses to use in throwing exceptions about
198 * badly behaved user objects employed as keys. We have to throw an
199 * IllegalArgumentException with a rather verbose message telling the
200 * user that they need to fix their object implementation to conform
201 * to the general contract for java.lang.Object.
203 * @param o1 the first of the equal elements with unequal hash codes.
204 * @param o2 the second of the equal elements with unequal hash codes.
205 * @exception IllegalArgumentException the whole point of this method.
207 protected final void throwObjectContractViolation(Object o1, Object o2)
208 throws IllegalArgumentException {
209 throw new IllegalArgumentException("Equal objects must have equal hashcodes. "
210 + "During rehashing, Trove discovered that "
211 + "the following two objects claim to be "
212 + "equal (as in java.lang.Object.equals()) "
213 + "but their hashCodes (or those calculated by "
214 + "your TObjectHashingStrategy) are not equal."
215 + "This violates the general contract of "
216 + "java.lang.Object.hashCode(). See bullet point two "
217 + "in that method's documentation. "
218 + "object #1 =" + o1 + " object #1 hash = " + o1.hashCode() + " object #1 id = " + System.identityHashCode(o1)
219 + "; object #2 =" + o2 + " object #2 hash = " + o2.hashCode() + " object #2 id = " + System.identityHashCode(o2));