]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DoubleKeyQueryHashMap.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / DoubleKeyQueryHashMap.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 java.lang.reflect.Array;
15 import java.util.ArrayList;
16
17
18
19 /**
20  * An implementation of the Map interface which uses an open addressed
21  * hash table to store its contents.
22  *
23  * Created: Sun Nov  4 08:52:45 2001
24  *
25  * @author Eric D. Friedman
26  * @version $Id: BinaryQueryHashMap.java,v 1.2 2008/03/14 11:38:53 tuoksk Exp $
27  */
28 public class DoubleKeyQueryHashMap<Procedure> extends DoubleKeyQueryHash<Procedure> {
29
30     /**
31      * Creates a new <code>THashMap</code> instance with the default
32      * capacity and load factor.
33      */
34     public DoubleKeyQueryHashMap() {
35         super();
36     }
37
38     /**
39      * initialize the value array of the map.
40      *
41      * @param initialCapacity an <code>int</code> value
42      * @return an <code>int</code> value
43      */
44     protected int setUp(int initialCapacity) {
45         int capacity;
46         
47         capacity = super.setUp(initialCapacity);
48 //        _values = (Object[]) new Object[capacity];
49         return capacity;
50     }
51     
52     private int sizeInternal = 0;
53     
54     /**
55      * Inserts a key/value pair into the map.
56      *
57      * @param key an <code>Object</code> value
58      * @param value an <code>Object</code> value
59      * @return the previous value associated with <tt>key</tt>,
60      * or null if none was found.
61      */
62     @SuppressWarnings("unchecked")
63         public DoubleKeyValueMap put(final int id, final DoubleKeyValueMap value) {
64         DoubleKeyValueMap previous = null;
65         Object oldKey;
66         int index = insertionIndex(id);
67         boolean isNewMapping = true;
68         if (index < 0) {
69             index = -index -1;
70             previous = _set[index];
71             isNewMapping = false;
72         }
73         oldKey = _set[index];
74         _set[index] = value;
75         if (isNewMapping) {
76             postInsertHook(oldKey == null);
77         }
78
79         return previous;
80     }
81     
82     public CacheEntry put(final long id, BinaryQuery value) {
83         int r1 = r1(id);
84         DoubleKeyValueMap map = get(r1);
85         if(map == null) {
86                 map = new DoubleKeyValueMap(r1);
87                 put(r1, map);
88         }
89         CacheEntry old = map.put(id, value);
90         if(old == null) sizeInternal++;
91         return old;
92     }
93
94     /**
95      * rehashes the map to the new capacity.
96      *
97      * @param newCapacity an <code>int</code> value
98      */
99         protected void rehash(int newCapacity) {
100         
101         int oldCapacity = _set.length;
102         DoubleKeyValueMap<Procedure> oldKeys[] = _set;
103
104         DoubleKeyValueMap<Procedure> newKeys[]  = (DoubleKeyValueMap<Procedure>[])Array.newInstance(DoubleKeyValueMap.class, newCapacity);
105
106         for (int i = oldCapacity; i-- > 0;) {
107             if(oldKeys[i] != null && oldKeys[i] != REMOVED) {
108                 DoubleKeyValueMap<Procedure> o = oldKeys[i];
109                 int index = insertionIndex2(o.id, newKeys);
110                 if (index < 0) {
111                     throwObjectContractViolation(newKeys[(-index -1)], o);
112                 }
113                 newKeys[index] = o;
114             }
115         }
116         
117         _set = newKeys;
118         
119     }
120
121     /**
122      * retrieves the value for <tt>key</tt>
123      *
124      * @param key an <code>Object</code> value
125      * @return the value of <tt>key</tt> or null if no such mapping exists.
126      */
127     final public DoubleKeyValueMap<Procedure> get(final int id) {
128 //        int index = index(id);
129 //        return index < 0 ? null : _set[index];
130         return index2(id);
131     }
132     
133     final protected static long id(long r1, long r2) {
134         long result = (r1<<32) | (r2 & 0xffffffffL); 
135         return result;
136     }
137
138     final public int r1(final long id) {
139         return (int)(id>>>32);
140     }
141     
142     final public int r2(final long id) {
143         return (int)id;
144     }
145     
146     final public BinaryQuery get(final int r1, final int r2) {
147         DoubleKeyValueMap<Procedure> map = get(r1);
148         if(map == null) return null;
149         return map.get(id(r1,r2));
150     }
151
152     final public BinaryQuery get(final long id) {
153         DoubleKeyValueMap<Procedure> map = get(r1(id));
154         if(map == null) return null;
155         return map.get(id);
156     }
157
158     /**
159      * Deletes a key/value pair from the map.
160      *
161      * @param key an <code>Object</code> value
162      * @return an <code>Object</code> value
163      */
164     public Object remove(final int id) {
165         DoubleKeyValueMap<Procedure> prev = null;
166         int index = index(id);
167         if (index >= 0) {
168             prev = _set[index];
169             removeAt(index);    // clear key,state; adjust size
170             sizeInternal-=prev.size();
171         }
172         return prev;
173     }
174
175     public Object remove(final long id) {
176         int r1 = r1(id);
177         DoubleKeyValueMap<Procedure> map = get(r1);
178         if(map == null) return null;
179         Object removed = map.remove(id);
180         if(removed != null) sizeInternal--;
181         if(map.isEmpty()) remove(r1);
182         return removed;
183     }
184
185     final public void values(int level, CacheCollectionResult result) {
186         
187         for (int i = _set.length; i-- > 0;) {
188             if(_set[i] != null && _set[i] != REMOVED) {
189                 DoubleKeyValueMap map = _set[i];
190                 map.values(level, result);
191             }
192         }
193         
194     }
195
196     final public <T extends BinaryQuery> ArrayList<T> values() {
197
198         ArrayList<T> result = new ArrayList<T>();
199         
200         for (int i = _set.length; i-- > 0;) {
201             if(_set[i] != null && _set[i] != REMOVED) {
202                 DoubleKeyValueMap map = _set[i];
203                 result.addAll(map.values());
204             }
205         }
206     
207         return result;
208         
209     }
210     
211     @Override
212     public int size() {
213         return sizeInternal;
214     }
215     
216     final public static ArrayList<BinaryQuery> NO_VALUES = new ArrayList<BinaryQuery>();
217
218     final public <T extends BinaryQuery> ArrayList<T> values(int r1) {
219
220         DoubleKeyValueMap map = get(r1);
221         if(map == null) return (ArrayList<T>)NO_VALUES;
222         
223         return map.values();
224         
225     }
226     
227 } // THashMap