| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
package org.melati.poem.util; |
| 47 | |
|
| 48 | |
import java.lang.ref.ReferenceQueue; |
| 49 | |
import java.lang.ref.SoftReference; |
| 50 | |
import java.util.Enumeration; |
| 51 | |
import java.util.Hashtable; |
| 52 | |
import java.util.Vector; |
| 53 | |
|
| 54 | |
import org.melati.poem.PoemException; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | 6 | public final class Cache { |
| 85 | |
|
| 86 | |
|
| 87 | |
private interface Node { |
| 88 | |
|
| 89 | |
Object key(); |
| 90 | |
|
| 91 | |
Object value(); |
| 92 | |
} |
| 93 | |
|
| 94 | |
|
| 95 | |
private static class HeldNode implements Node { |
| 96 | |
Object key; |
| 97 | |
Object value; |
| 98 | 6261 | HeldNode nextMRU = null; |
| 99 | 6261 | HeldNode prevMRU = null; |
| 100 | |
|
| 101 | 6261 | HeldNode(Object key, Object value) { |
| 102 | 6261 | this.key = key; |
| 103 | 6261 | this.value = value; |
| 104 | 6261 | } |
| 105 | |
|
| 106 | |
synchronized void putBefore(HeldNode nextMRU_P) { |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | 7507 | if (this.nextMRU != null) |
| 130 | 203 | this.nextMRU.prevMRU = prevMRU; |
| 131 | |
|
| 132 | 7507 | if (prevMRU != null) |
| 133 | 1146 | prevMRU.nextMRU = this.nextMRU; |
| 134 | |
|
| 135 | 7507 | if (nextMRU_P != null) { |
| 136 | 6991 | if (nextMRU_P.prevMRU != null) |
| 137 | |
|
| 138 | |
|
| 139 | 0 | nextMRU_P.prevMRU.nextMRU = this; |
| 140 | 6991 | prevMRU = nextMRU_P.prevMRU; |
| 141 | 6991 | nextMRU_P.prevMRU = this; |
| 142 | |
} |
| 143 | |
else |
| 144 | 516 | prevMRU = null; |
| 145 | 7507 | this.nextMRU = nextMRU_P; |
| 146 | 7507 | } |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
public Object key() { |
| 153 | 146 | return key; |
| 154 | |
} |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
public Object value() { |
| 161 | 14197 | return value; |
| 162 | |
} |
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
public String toString() { |
| 169 | 16 | StringBuffer ret = new StringBuffer(); |
| 170 | 16 | if(prevMRU == null) |
| 171 | 11 | ret.append("null"); |
| 172 | |
else |
| 173 | 5 | ret.append(prevMRU.key()); |
| 174 | 16 | ret.append(">>"+ key + "=" + value + ">>"); |
| 175 | 16 | if(nextMRU == null) |
| 176 | 11 | ret.append("null"); |
| 177 | |
else |
| 178 | 5 | ret.append(nextMRU.key()); |
| 179 | 16 | return ret.toString(); |
| 180 | |
} |
| 181 | |
|
| 182 | |
|
| 183 | |
} |
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
private static class DroppedNode extends SoftReference<Object> implements Node { |
| 189 | |
|
| 190 | |
Object key; |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
DroppedNode(Object key, Object value, ReferenceQueue<Object> queue) { |
| 199 | 59 | super(value, queue); |
| 200 | 59 | this.key = key; |
| 201 | 59 | } |
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
public Object key() { |
| 208 | 0 | return key; |
| 209 | |
} |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
public Object value() { |
| 216 | 171 | return this.get(); |
| 217 | |
} |
| 218 | |
} |
| 219 | |
|
| 220 | 932 | private Hashtable<Object, Object> table = new Hashtable<Object, Object>(); |
| 221 | 932 | private HeldNode theMRU = null, theLRU = null; |
| 222 | 932 | private int heldNodes = 0; |
| 223 | |
private int maxSize; |
| 224 | 932 | private int collectedEver = 0; |
| 225 | |
|
| 226 | 932 | private ReferenceQueue<Object> collectedValuesQueue = new ReferenceQueue<Object>(); |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
public class InconsistencyException extends PoemException { |
| 240 | |
|
| 241 | |
|
| 242 | |
private static final long serialVersionUID = 1832694552964508864L; |
| 243 | |
|
| 244 | |
public Vector<Object> problems; |
| 245 | |
|
| 246 | |
|
| 247 | |
public InconsistencyException(Vector<Object> probs) { |
| 248 | |
this.problems = probs; |
| 249 | |
} |
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
public String getMessage() { |
| 255 | |
return EnumUtils.concatenated("\n", problems.elements()); |
| 256 | |
} |
| 257 | |
} |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
private Vector<Object> invariantBreaches() { |
| 263 | 9 | Vector<Object> probs = new Vector<Object>(); |
| 264 | |
|
| 265 | 9 | if (theMRU != null && theMRU.prevMRU != null) |
| 266 | 0 | probs.addElement("theMRU.prevMRU == " + theMRU.prevMRU); |
| 267 | 9 | if (theLRU != null && theLRU.nextMRU != null) |
| 268 | 0 | probs.addElement("theLRU.nextMRU == " + theLRU.nextMRU); |
| 269 | |
|
| 270 | 9 | Object[] held = new Object[heldNodes]; |
| 271 | 9 | Hashtable<HeldNode, Boolean> heldHash = new Hashtable<HeldNode, Boolean>(); |
| 272 | |
|
| 273 | 9 | int countML = 0; |
| 274 | 43 | for (HeldNode n = theMRU; n != null; n = n.nextMRU, ++countML) { |
| 275 | 34 | if (table.get(n.key()) != n) |
| 276 | 0 | probs.addElement("MRU list check: table.get(" + n + ".key()) == " + table.get(n.key())); |
| 277 | 34 | if (countML < heldNodes) |
| 278 | 34 | held[countML] = n; |
| 279 | 34 | heldHash.put(n, Boolean.TRUE); |
| 280 | |
} |
| 281 | |
|
| 282 | 9 | if (countML != heldNodes) |
| 283 | 0 | probs.addElement(countML + " nodes in MRU->LRU not " + heldNodes); |
| 284 | |
|
| 285 | 9 | Hashtable<Object,HeldNode> keys = new Hashtable<Object,HeldNode>(); |
| 286 | 9 | int countLM = 0; |
| 287 | 43 | for (HeldNode n = theLRU; n != null; n = n.prevMRU, ++countLM) { |
| 288 | 34 | HeldNode oldn = (HeldNode)keys.get(n.key()); |
| 289 | 34 | if (oldn != null) |
| 290 | 0 | probs.addElement("key " + n.key() + " duplicated in " + n + " and " + |
| 291 | |
oldn); |
| 292 | 34 | keys.put(n.key(), n); |
| 293 | |
|
| 294 | 34 | if (table.get(n.key()) != n) |
| 295 | 0 | probs.addElement("LRU list check: table.get(" + n + ".key()) == " + table.get(n.key())); |
| 296 | 34 | if (countLM < heldNodes) { |
| 297 | 34 | int o = heldNodes - (1 + countLM); |
| 298 | 34 | if (n != held[o]) |
| 299 | 0 | probs.addElement("lm[" + countLM + "] == " + n + " != ml[" + |
| 300 | |
o + "] == " + held[o]); |
| 301 | |
} |
| 302 | |
} |
| 303 | |
|
| 304 | 9 | for (Enumeration<Object> nodes = table.elements(); nodes.hasMoreElements();) { |
| 305 | 91 | Node n = (Node)nodes.nextElement(); |
| 306 | 91 | if (n instanceof HeldNode && !heldHash.containsKey(n)) |
| 307 | 0 | probs.addElement(n + " in table but not MRU->LRU"); |
| 308 | 91 | } |
| 309 | |
|
| 310 | 9 | if (countLM != heldNodes) |
| 311 | 0 | probs.addElement(countLM + " nodes in LRU->MRU not " + heldNodes); |
| 312 | |
|
| 313 | 9 | return probs; |
| 314 | |
} |
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
private void assertInvariant() { |
| 320 | 19004 | boolean debug = false; |
| 321 | 19004 | if (debug) { |
| 322 | 0 | Vector<Object> probs = invariantBreaches(); |
| 323 | 0 | if (probs.size() != 0) { |
| 324 | 0 | throw new InconsistencyException(probs); |
| 325 | |
} |
| 326 | |
} |
| 327 | 19004 | } |
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | 932 | public Cache(int maxSize) { |
| 334 | 932 | setSize(maxSize); |
| 335 | 932 | } |
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
public void setSize(int maxSize) { |
| 342 | 1657 | if (maxSize < 0) |
| 343 | 0 | throw new IllegalArgumentException(); |
| 344 | 1657 | this.maxSize = maxSize; |
| 345 | 1657 | } |
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
public int getSize() { |
| 351 | 0 | return maxSize; |
| 352 | |
} |
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
private synchronized void checkForGarbageCollection() { |
| 361 | |
DroppedNode collected; |
| 362 | 23706 | while ((collected = (DroppedNode)collectedValuesQueue.poll()) != null) { |
| 363 | 0 | table.remove(collected.key()); |
| 364 | 0 | ++collectedEver; |
| 365 | |
} |
| 366 | 23706 | } |
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
public synchronized void trim(int maxSize_P) { |
| 379 | 6272 | checkForGarbageCollection(); |
| 380 | |
|
| 381 | 6272 | HeldNode n = theLRU; |
| 382 | 6329 | while (n != null && heldNodes > maxSize_P) { |
| 383 | 57 | HeldNode nn = n.prevMRU; |
| 384 | 57 | n.putBefore(null); |
| 385 | 57 | table.put(n.key, new DroppedNode(n.key, n.value, collectedValuesQueue)); |
| 386 | 57 | --heldNodes; |
| 387 | 57 | n = nn; |
| 388 | 57 | } |
| 389 | |
|
| 390 | 6272 | if (n == null) { |
| 391 | 344 | theLRU = null; |
| 392 | 344 | theMRU = null; |
| 393 | |
} else |
| 394 | 5928 | theLRU = n; |
| 395 | |
|
| 396 | 6272 | assertInvariant(); |
| 397 | 6272 | } |
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
public synchronized void delete(Object key) { |
| 407 | 118 | Node n = (Node)table.get(key); |
| 408 | 118 | if (n == null) |
| 409 | 0 | return; |
| 410 | |
|
| 411 | 118 | if (n instanceof HeldNode) { |
| 412 | 118 | HeldNode h = (HeldNode)n; |
| 413 | |
|
| 414 | 118 | if (theLRU == h) |
| 415 | 29 | theLRU = h.prevMRU; |
| 416 | 118 | if (theMRU == h) |
| 417 | 100 | theMRU = h.nextMRU; |
| 418 | |
|
| 419 | 118 | h.putBefore(null); |
| 420 | |
|
| 421 | 118 | --heldNodes; |
| 422 | |
} |
| 423 | |
|
| 424 | 118 | table.remove(key); |
| 425 | |
|
| 426 | 118 | assertInvariant(); |
| 427 | 118 | } |
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
public synchronized void put(Object key, Object value) { |
| 436 | 6263 | if (key == null || value == null) |
| 437 | 0 | throw new NullPointerException(); |
| 438 | |
|
| 439 | 6263 | trim(maxSize); |
| 440 | |
|
| 441 | 6263 | if (maxSize == 0) { |
| 442 | 2 | table.put(key, new DroppedNode(key, value, collectedValuesQueue)); |
| 443 | |
} else { |
| 444 | 6261 | HeldNode node = new HeldNode(key, value); |
| 445 | |
|
| 446 | 6261 | Object previous = table.put(key, node); |
| 447 | 6261 | if (previous != null) { |
| 448 | |
|
| 449 | 0 | table.put(key, previous); |
| 450 | 0 | throw new CacheDuplicationException("Key already in cache for key=" + key + ", value="+value); |
| 451 | |
} |
| 452 | |
|
| 453 | 6261 | node.putBefore(theMRU); |
| 454 | 6261 | theMRU = node; |
| 455 | 6261 | if (theLRU == null) theLRU = node; |
| 456 | |
|
| 457 | 6261 | ++heldNodes; |
| 458 | |
|
| 459 | 6261 | assertInvariant(); |
| 460 | |
} |
| 461 | 6263 | } |
| 462 | |
|
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
|
| 467 | |
|
| 468 | |
public synchronized Object get(Object key) { |
| 469 | |
|
| 470 | 15761 | checkForGarbageCollection(); |
| 471 | |
|
| 472 | 15761 | Node node = (Node)table.get(key); |
| 473 | 15761 | if (node == null) |
| 474 | 9408 | return null; |
| 475 | |
else { |
| 476 | |
HeldNode held; |
| 477 | 6353 | if (node instanceof HeldNode) { |
| 478 | 6353 | held = (HeldNode)node; |
| 479 | 6353 | if (held != theMRU) { |
| 480 | 1071 | if (held == theLRU) |
| 481 | 957 | theLRU = held.prevMRU; |
| 482 | 1071 | held.putBefore(theMRU); |
| 483 | 1071 | theMRU = held; |
| 484 | |
} |
| 485 | |
} else { |
| 486 | 0 | if (node.value() == null) |
| 487 | |
|
| 488 | |
|
| 489 | |
|
| 490 | 0 | return null; |
| 491 | 0 | held = new HeldNode(key, node.value()); |
| 492 | 0 | table.put(key, held); |
| 493 | 0 | ++heldNodes; |
| 494 | 0 | held.putBefore(theMRU); |
| 495 | 0 | theMRU = held; |
| 496 | 0 | if (theLRU == null) |
| 497 | 0 | theLRU = held; |
| 498 | 0 | trim(maxSize); |
| 499 | |
} |
| 500 | |
|
| 501 | 6353 | assertInvariant(); |
| 502 | 6353 | return held.value; |
| 503 | |
} |
| 504 | |
} |
| 505 | |
|
| 506 | |
|
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
|
| 512 | |
public synchronized void iterate(Procedure f) { |
| 513 | 1670 | checkForGarbageCollection(); |
| 514 | 1670 | for (Enumeration<Object> n = table.elements(); n.hasMoreElements();) { |
| 515 | 14356 | Object value = ((Node)n.nextElement()).value(); |
| 516 | 14356 | if (value != null) |
| 517 | |
|
| 518 | 14356 | f.apply(value); |
| 519 | 14356 | } |
| 520 | 1670 | } |
| 521 | |
|
| 522 | |
|
| 523 | |
|
| 524 | |
|
| 525 | |
|
| 526 | |
public Enumeration<Object> getReport() { |
| 527 | 9 | return new ConsEnumeration<Object>("" + |
| 528 | |
maxSize + " maxSize, " + |
| 529 | |
theMRU + " theMRU, " + |
| 530 | |
theLRU + " theLRU, " + |
| 531 | |
collectedEver + " collectedEver", |
| 532 | |
new ConsEnumeration<Object>( |
| 533 | 9 | heldNodes + " held, " + table.size() + " total ", |
| 534 | 9 | invariantBreaches().elements())); |
| 535 | |
} |
| 536 | |
|
| 537 | |
|
| 538 | |
|
| 539 | |
|
| 540 | 3 | public final class Info { |
| 541 | |
|
| 542 | 3 | private Info() {} |
| 543 | |
|
| 544 | |
|
| 545 | |
|
| 546 | |
|
| 547 | |
public Enumeration<Object> getHeldElements() { |
| 548 | 3 | checkForGarbageCollection(); |
| 549 | 3 | return new MappedEnumeration<Object, Object>( |
| 550 | 3 | new FilteredEnumeration<Object>(table.elements()) { |
| 551 | |
public boolean isIncluded(Object o) { |
| 552 | 12 | return o instanceof HeldNode; |
| 553 | |
} |
| 554 | 3 | }) { |
| 555 | |
public Object mapped(Object o) { |
| 556 | 12 | return ((Node)o).value(); |
| 557 | |
} |
| 558 | |
}; |
| 559 | |
} |
| 560 | |
|
| 561 | |
|
| 562 | |
|
| 563 | |
|
| 564 | |
public Enumeration<Object> getDroppedElements() { |
| 565 | 0 | checkForGarbageCollection(); |
| 566 | 0 | return new MappedEnumeration<Object, Object>( |
| 567 | 0 | new FilteredEnumeration<Object>(table.elements()) { |
| 568 | |
public boolean isIncluded(Object o) { |
| 569 | 0 | return o instanceof DroppedNode; |
| 570 | |
} |
| 571 | 0 | }) { |
| 572 | |
public Object mapped(Object o) { |
| 573 | 0 | return ((Node)o).value(); |
| 574 | |
} |
| 575 | |
}; |
| 576 | |
} |
| 577 | |
|
| 578 | |
|
| 579 | |
|
| 580 | |
|
| 581 | |
public Enumeration<Object> getReport() { |
| 582 | 0 | return Cache.this.getReport(); |
| 583 | |
} |
| 584 | |
|
| 585 | |
|
| 586 | |
} |
| 587 | |
|
| 588 | |
|
| 589 | |
|
| 590 | |
|
| 591 | |
public Info getInfo() { |
| 592 | 3 | return new Info(); |
| 593 | |
} |
| 594 | |
|
| 595 | |
|
| 596 | |
|
| 597 | |
|
| 598 | |
public void dumpAnalysis() { |
| 599 | 9 | for (Enumeration<Object> l = getReport(); l.hasMoreElements();) |
| 600 | 18 | System.err.println(l.nextElement()); |
| 601 | 9 | } |
| 602 | |
|
| 603 | |
|
| 604 | |
|
| 605 | |
|
| 606 | |
public void dump() { |
| 607 | 0 | System.err.println("Keys: " + table.size()); |
| 608 | 0 | System.err.println("maxSize: " + maxSize); |
| 609 | 0 | System.err.println("theMRU: " + theMRU); |
| 610 | 0 | System.err.println("theLRU: " + theLRU); |
| 611 | 0 | System.err.println("heldNodes: " + heldNodes); |
| 612 | 0 | System.err.println("collectedEver: " + collectedEver); |
| 613 | 0 | Enumeration<Object> e = table.keys(); |
| 614 | 0 | while(e.hasMoreElements()) { |
| 615 | 0 | Object k = e.nextElement(); |
| 616 | 0 | System.err.print(k ); |
| 617 | 0 | System.err.print(" : "); |
| 618 | 0 | System.err.println(table.get(k) ); |
| 619 | 0 | } |
| 620 | 0 | } |
| 621 | |
} |
| 622 | |
|
| 623 | |
|
| 624 | |
|
| 625 | |
|