Lines 51-58
Link Here
|
51 |
private HashMap hashOnId; |
51 |
private HashMap hashOnId; |
52 |
|
52 |
|
53 |
/* Hard references */ |
53 |
/* Hard references */ |
54 |
private Object hardRef[]; |
54 |
//private Object hardRef[]; |
55 |
|
55 |
private CacheReference hardRef[]; |
|
|
56 |
|
56 |
/* current slot in above index */ |
57 |
/* current slot in above index */ |
57 |
private int hardIndex; |
58 |
private int hardIndex; |
58 |
|
59 |
|
Lines 97-103
Link Here
|
97 |
public MDRCache(int size) { |
98 |
public MDRCache(int size) { |
98 |
hashOnId = new HashMap(); |
99 |
hashOnId = new HashMap(); |
99 |
deleted = new HashMap(); |
100 |
deleted = new HashMap(); |
100 |
hardRef = new Object[size]; |
101 |
//hardRef = new Object[size]; |
|
|
102 |
hardRef = new CacheReference[size]; |
101 |
hardIndex = 0; |
103 |
hardIndex = 0; |
102 |
queue = new ReferenceQueue(); |
104 |
queue = new ReferenceQueue(); |
103 |
dirty = new HashMap(); |
105 |
dirty = new HashMap(); |
Lines 119-134
Link Here
|
119 |
public synchronized void put(Object m, Object o) throws StorageException { |
121 |
public synchronized void put(Object m, Object o) throws StorageException { |
120 |
CacheReference cr = new CacheReference(m, o, queue); |
122 |
CacheReference cr = new CacheReference(m, o, queue); |
121 |
CacheReference oldCr = (CacheReference)hashOnId.put(m, cr); |
123 |
CacheReference oldCr = (CacheReference)hashOnId.put(m, cr); |
122 |
cr.inCache = true; |
124 |
//cr.inCache = true; |
123 |
if (oldCr != null) { |
125 |
if (oldCr != null) { |
124 |
oldCr.inCache = false; |
126 |
//oldCr.inCache = false; |
125 |
if (oldCr.get() != null && oldCr.get() != o) { |
127 |
if (oldCr.get() != null && oldCr.get() != o) { |
126 |
throw new StorageBadRequestException( |
128 |
throw new StorageBadRequestException( |
127 |
MessageFormat.format("Duplicate MOF ID: {0}", |
129 |
MessageFormat.format("Duplicate MOF ID: {0}", |
128 |
new Object[] {m} )); |
130 |
new Object[] {m} )); |
129 |
} |
131 |
} |
|
|
132 |
oldCr.clear(); |
130 |
} |
133 |
} |
131 |
makeHardRef(o); |
134 |
//makeHardRef(o); |
|
|
135 |
makeHardRef(cr); |
132 |
checkQueue(); |
136 |
checkQueue(); |
133 |
int curSize = hashOnId.size(); |
137 |
int curSize = hashOnId.size(); |
134 |
if (curSize > maxSize) |
138 |
if (curSize > maxSize) |
Lines 144-150
Link Here
|
144 |
if (cr != null) |
148 |
if (cr != null) |
145 |
o = cr.get(); |
149 |
o = cr.get(); |
146 |
if (o != null) { |
150 |
if (o != null) { |
147 |
makeHardRef(o); |
151 |
makeHardRef(cr); |
148 |
hits++; |
152 |
hits++; |
149 |
} |
153 |
} |
150 |
else { |
154 |
else { |
Lines 185-191
Link Here
|
185 |
|
189 |
|
186 |
if (cr != null) { |
190 |
if (cr != null) { |
187 |
cr.clear(); |
191 |
cr.clear(); |
188 |
cr.inCache = false; |
192 |
//cr.inCache = false; |
189 |
} |
193 |
} |
190 |
boolean wasNew = (newOnes.remove(m) != null); |
194 |
boolean wasNew = (newOnes.remove(m) != null); |
191 |
dirty.remove(m); |
195 |
dirty.remove(m); |
Lines 203-230
Link Here
|
203 |
} |
207 |
} |
204 |
|
208 |
|
205 |
/* create a hard reference to the supplied object */ |
209 |
/* create a hard reference to the supplied object */ |
206 |
private void makeHardRef(Object o) { |
210 |
//private void makeHardRef(Object o) { |
|
|
211 |
private void makeHardRef(CacheReference cr) { |
212 |
/** |
207 |
hardRef[hardIndex++] = o; |
213 |
hardRef[hardIndex++] = o; |
208 |
if (hardIndex >= hardRef.length) { |
214 |
if (hardIndex >= hardRef.length) { |
209 |
hardIndex = 0; |
215 |
hardIndex = 0; |
210 |
} |
216 |
} |
|
|
217 |
|
218 |
BHM: This was not very nice after 20 gets We'd have 20 |
219 |
identical objects in the cache which definitely isn't |
220 |
what we want. |
221 |
*/ |
222 |
|
223 |
if ( cr.isHard() ) { |
224 |
// The reference is already in cache we don't need to do |
225 |
// anything |
226 |
return; |
227 |
} |
228 |
else { |
229 |
if (hardRef[hardIndex] != null) { |
230 |
// Old Reference may be GCed |
231 |
hardRef[hardIndex].weaken(); |
232 |
} |
233 |
cr.harden(); |
234 |
hardRef[hardIndex] = cr; |
235 |
|
236 |
hardIndex++; |
237 |
if ( hardIndex >= hardRef.length ) { |
238 |
hardIndex = 0; |
239 |
} |
240 |
} |
211 |
} |
241 |
} |
212 |
|
242 |
|
213 |
/* check for queued references, and remove them from the hash table */ |
243 |
/* check for queued references, and remove them from the hash table */ |
214 |
private void checkQueue() { |
244 |
private void checkQueue() { |
215 |
// int i = 0; |
245 |
int i = 0; |
216 |
CacheReference cr; |
246 |
CacheReference cr; |
217 |
while ((cr = (CacheReference)queue.poll()) != null) { |
247 |
while ((cr = (CacheReference)queue.poll()) != null) { |
218 |
if (cr.inCache) { |
248 |
//if (cr.inCache) { |
219 |
hashOnId.remove(cr); |
249 |
i += hashOnId.remove(cr.getKey()) == null ? 0 : 1; |
220 |
cr.inCache = false; |
250 |
// cr.inCache = false; |
221 |
// i++; |
251 |
//} |
222 |
} |
252 |
} |
223 |
} |
253 |
/* |
224 |
// if (i > 0) { |
254 |
if (i > 0) { |
225 |
// System.out.println("Removed " + i + |
255 |
System.out.println("Removed " + i + |
226 |
// " references; hash size now was " + (hashOnId.size() + i)); |
256 |
" references; hash size now " + hashOnId.size() + " was " + (hashOnId.size() + i)); |
227 |
// } |
257 |
System.out.println( " HardRefs " + hardIndex ); |
|
|
258 |
} |
259 |
*/ |
228 |
} |
260 |
} |
229 |
|
261 |
|
230 |
/* Check to see if we have exceeded our threshhold for dirty objects |
262 |
/* Check to see if we have exceeded our threshhold for dirty objects |