]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHash.java
First step to enable reading of cache when not writing
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryIdentityHash.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import gnu.trove.impl.hash.THash;
15
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
18
19
20 /**
21  * An open addressed hashing implementation for Object types.
22  *
23  * Created: Sun Nov  4 08:56:06 2001
24  *
25  * @author Eric D. Friedman
26  * @version $Id: QueryIdentityHash.java,v 1.1 2008/03/14 11:32:01 tuoksk Exp $
27  */
28 abstract public class QueryIdentityHash extends THash {
29
30         static final long serialVersionUID = -3461112548087185871L;
31
32     /** the set of Objects */
33     protected transient CacheEntry[] _set;
34
35     protected static final CacheEntry REMOVED = new CacheEntry() {
36
37         @Override
38         public void addParent(CacheEntry entry) {
39             // TODO Auto-generated method stub
40             
41         }
42
43         @Override
44         public void clearResult(QuerySupport support) {
45             // TODO Auto-generated method stub
46             
47         }
48
49         @Override
50         public void discard() {
51             // TODO Auto-generated method stub
52             
53         }
54
55         @Override
56         public void except(Throwable t) {
57             // TODO Auto-generated method stub
58             
59         }
60
61         @Override
62         public Iterable<CacheEntry> getParents(QueryProcessor processor) {
63             // TODO Auto-generated method stub
64             return null;
65         }
66
67         @Override
68         public Query getQuery() {
69             // TODO Auto-generated method stub
70             return null;
71         }
72
73         @Override
74         public boolean hasParents() {
75             // TODO Auto-generated method stub
76             return false;
77         }
78
79         @Override
80         public boolean isDiscarded() {
81             // TODO Auto-generated method stub
82             return false;
83         }
84
85         @Override
86         public boolean isExcepted() {
87             // TODO Auto-generated method stub
88             return false;
89         }
90
91         @Override
92         public boolean isFresh() {
93             // TODO Auto-generated method stub
94             return false;
95         }
96
97         @Override
98         public boolean isPending() {
99             // TODO Auto-generated method stub
100             return false;
101         }
102
103         @Override
104         public boolean isReady() {
105             // TODO Auto-generated method stub
106             return false;
107         }
108
109         @Override
110         public boolean isRefuted() {
111             // TODO Auto-generated method stub
112             return false;
113         }
114
115         @Override
116         public void refute() {
117             // TODO Auto-generated method stub
118             
119         }
120
121         @Override
122         CacheEntry pruneFirstParents() {
123             // TODO Auto-generated method stub
124                 return null;
125         }
126         
127         @Override
128         public void removeParent(CacheEntry entry) {
129             // TODO Auto-generated method stub
130             
131         }
132
133         @Override
134         public void setPending(QuerySupport querySupport) {
135             // TODO Auto-generated method stub
136             
137         }
138
139         @Override
140         public void setReady() {
141             // TODO Auto-generated method stub
142             
143         }
144
145         @Override
146         public void setResult(Object result) {
147             // TODO Auto-generated method stub
148             
149         }
150         
151         @Override
152         boolean isImmutable(ReadGraphImpl graph) {
153                 return true;
154         }
155
156                 @Override
157                 boolean shouldBeCollected() {
158                         // TODO Auto-generated method stub
159                         return false;
160                 }
161
162                 @Override
163                 CacheEntry getFirstParent(QueryProcessor processor) {
164                         // TODO Auto-generated method stub
165                         return null;
166                 }
167                 
168                 @Override
169                 boolean moreThanOneParent(QueryProcessor processor) {
170                         // TODO Auto-generated method stub
171                         return false;
172                 }
173
174                 @Override
175                 int parentCount(QueryProcessor processor) {
176                         // TODO Auto-generated method stub
177                         return 0;
178                 }
179                 
180                 @Override
181                 short getLevel() {
182                         // TODO Auto-generated method stub
183                         return 0;
184                 }
185
186                 @Override
187                 short setLevel(short level) {
188                         // TODO Auto-generated method stub
189                         return level;
190                 }
191                 
192                 @Override
193                 void prepareRecompute(QuerySupport querySupport) {
194                         // TODO Auto-generated method stub
195                 }
196
197                 @Override
198                 int getGCStatus() {
199                         // TODO Auto-generated method stub
200                         return 0;
201                 }
202
203                 @Override
204                 int setGCStatus(int status) {
205                         // TODO Auto-generated method stub
206                         return 0;
207                 }
208
209                 @Override
210                 void setGCStatusFlag(int flag, boolean value) {
211                         // TODO Auto-generated method stub
212                         
213                 }
214                 
215                 public Object getOriginalRequest() {
216                         throw new UnsupportedOperationException();
217                 }
218
219                 @Override
220                 Object performFromCache(ReadGraphImpl graph, Object procedure) throws DatabaseException {
221                         // TODO Auto-generated method stub
222                         return null;
223                 }
224
225                 @Override
226                 public Object getResult() {
227                         // TODO Auto-generated method stub
228                         return null;
229                 }
230         
231     };
232
233     /**
234      * Creates a new <code>TObjectHash</code> instance with the
235      * default capacity and load factor.
236      */
237     public QueryIdentityHash() {
238         super();
239     }
240
241     /**
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.
245      *
246      * @param initialCapacity an <code>int</code> value
247      */
248     public QueryIdentityHash(int initialCapacity) {
249         super(initialCapacity, 0.75f);
250     }
251
252     final public int capacity() {
253         return _set.length;
254     }
255
256     final protected void removeAt(int index) {
257         _set[index] = REMOVED;
258         super.removeAt(index);
259     }
260
261     /**
262      * initializes the Object set of this hash table.
263      *
264      * @param initialCapacity an <code>int</code> value
265      * @return an <code>int</code> value
266      */
267     final protected int setUp(int initialCapacity) {
268         int capacity;
269
270         capacity = super.setUp(initialCapacity);
271         _set = new CacheEntry[capacity];
272         return capacity;
273         
274     }
275
276     /**
277      * Searches the set for <tt>obj</tt>
278      *
279      * @param obj an <code>Object</code> value
280      * @return a <code>boolean</code> value
281      */
282     final public boolean contains(Object obj) {
283         return index(obj) >= 0;
284     }
285
286     /**
287      * Locates the index of <tt>obj</tt>.
288      *
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.
291      */
292     final protected int index(Object obj) {
293
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];
300
301         if ( cur == null ) return -1;
302
303         // NOTE: here it has to be REMOVED or FULL (some user-given value)
304         if ( cur == REMOVED || (cur != obj)) {
305             // see Knuth, p. 529
306             final int probe = 1 + (hash % (length - 2));
307
308             do {
309                 index -= probe;
310                 if (index < 0) {
311                     index += length;
312                 }
313                 cur = set[index];
314             } while (cur != null
315                  && (cur == REMOVED || (cur != obj)));
316         }
317
318         return cur == null ? -1 : index;
319     }
320     
321
322     /**
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>.
326      *
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.
331      */
332     final protected int insertionIndex(Object obj) {
333
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];
340
341         if (cur == null) {
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));
348
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)
357             // is possible.
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.
363                 do {
364                     index -= probe;
365                     if (index < 0) {
366                         index += length;
367                     }
368                     cur = set[index];
369                 } while (cur != null
370                          && cur != REMOVED
371                          && (cur != obj));
372             }
373
374             // if the index we found was removed: continue probing until we
375             // locate a free location or an element which equal()s the
376             // one we have.
377             if (cur == REMOVED) {
378                 int firstRemoved = index;
379                 while (cur != null
380                        && (cur == REMOVED || (cur != obj))) {
381                     index -= probe;
382                     if (index < 0) {
383                         index += length;
384                     }
385                     cur = set[index];
386                 }
387                 // NOTE: cur cannot == REMOVED in this block
388                 return (cur != null) ? -index -1 : firstRemoved;
389             }
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;
393         }
394     }
395
396 } // TObjectHash