Produced by Araxis Merge on 12/5/2017 12:06:35 PM Central Standard Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | IV-eHMP_CIF.zip\IMAG_Source\VISA\Java\CacheImpl\main\src\java\gov\va\med\imaging\storage\cache\impl | PersistentSet.java | Mon Dec 4 21:35:18 2017 UTC |
| 2 | IV-eHMP_CIF.zip\IMAG_Source\VISA\Java\CacheImpl\main\src\java\gov\va\med\imaging\storage\cache\impl | PersistentSet.java | Mon Dec 4 21:57:36 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 2 | 1616 |
| Changed | 1 | 2 |
| Inserted | 0 | 0 |
| Removed | 0 | 0 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | /** | |
| 2 | * | |
| 3 | */ | |
| 4 | package go v.va.med.i maging.sto rage.cache .impl; | |
| 5 | ||
| 6 | import gov .va.med.im aging.stor age.cache. EvictionJu dge; | |
| 7 | import gov .va.med.im aging.stor age.cache. InstanceBy teChannelF actory; | |
| 8 | import gov .va.med.im aging.stor age.cache. MutableNam edObject; | |
| 9 | import gov .va.med.im aging.stor age.cache. exceptions .CacheExce ption; | |
| 10 | ||
| 11 | import jav a.lang.ref .Reference ; | |
| 12 | import jav a.lang.ref .Reference Queue; | |
| 13 | import jav a.lang.ref .SoftRefer ence; | |
| 14 | import jav a.util.Arr ayList; | |
| 15 | import jav a.util.Col lection; | |
| 16 | import jav a.util.Con currentMod ificationE xception; | |
| 17 | import jav a.util.Has hSet; | |
| 18 | import jav a.util.Ite rator; | |
| 19 | import jav a.util.Lis t; | |
| 20 | import jav a.util.Set ; | |
| 21 | import jav a.util.con current.Ti meUnit; | |
| 22 | import jav a.util.con current.lo cks.Reentr antReadWri teLock; | |
| 23 | import jav a.util.con current.lo cks.Reentr antReadWri teLock.Rea dLock; | |
| 24 | import jav a.util.con current.lo cks.Reentr antReadWri teLock.Wri teLock; | |
| 25 | ||
| 26 | import org .apache.lo gging.log4 j.LogManag er; | |
| 27 | import org .apache.lo gging.log4 j.Logger; | |
| 28 | ||
| 29 | /** | |
| 30 | * @author
|
|
| 31 | * | |
| 32 | * An abst ract class that repr esents a s et of pers istent ins tances in a cache im plementati on. | |
| 33 | * This cl ass is res ponsible f or synchro nizing the persisten t and the internal ( transient) | |
| 34 | * set ref erences. | |
| 35 | * In prac tical term s, this cl ass and it s subclass es, keep t he file in stances sy nchronized | |
| 36 | * with th e internal reference s. | |
| 37 | * | |
| 38 | * NOTE: m ethods tha t may make changes t o the Set are declar ed as fina l so that the lockin g | |
| 39 | * can be relied on. | |
| 40 | * | |
| 41 | * Known D erivations : | |
| 42 | * @see go v.va.med.i maging.sto rage.cache .impl.Pers istentGrou pSet | |
| 43 | * @see go v.va.med.i maging.sto rage.cache .impl.Pers istentInst anceSet | |
| 44 | * @see Nu llMutableN amedObject Set | |
| 45 | */ | |
| 46 | public abs tract clas s Persiste ntSet<T ex tends Muta bleNamedOb ject> | |
| 47 | extends Ha shSet<Soft Reference< ? extends T>> | |
| 48 | { | |
| 49 | pr ivate stat ic final l ong serial VersionUID = 1L; | |
| 50 | pr ivate Logg er log = L ogManager. getLogger( this.getCl ass()); | |
| 51 | // protected Set<SoftRe ference<? extends T> > childGro ups = new HashSet<So ftReferenc e<? extend s T>>(); / / the tran sient copy of the se t members | |
| 52 | pr ivate Reen trantReadW riteLock a ccessLock = new Reen trantReadW riteLock() ; // a rea d write lo ck proteci ng against simultane ous access where cha nges may o ccur | |
| 53 | ||
| 54 | pr ivate Inst anceByteCh annelFacto ry byteCha nnelFactor y = null; // passed to the Gro up instanc es created within he re so they can creat e Instance s, which n eed the fa ctory | |
| 55 | pr ivate int secondsRea dWaitsForW riteComple tion; | |
| 56 | pr ivate bool ean setMod ificationT imeOnRead; | |
| 57 | ||
| 58 | // set to tr ue to queu e soft ref erences wh en they ar e GC'd and print a l og message | |
| 59 | pr ivate stat ic boolean monitorSo ftReferenc es = false ; | |
| 60 | pr ivate stat ic Referen ceQueue de referenced Members; | |
| 61 | pr ivate stat ic Referen ceQueueNot ification queueNotif ication; | |
| 62 | ||
| 63 | st atic | |
| 64 | { | |
| 65 | if(m onitorSoft References ) | |
| 66 | { | |
| 67 | derefe rencedMemb ers = new ReferenceQ ueue(); | |
| 68 | queueN otificatio n = new Re ferenceQue ueNotifica tion(deref erencedMem bers); | |
| 69 | ||
| 70 | (new T hread(queu eNotificat ion)).star t(); | |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== = | |
| 75 | // Construct ors | |
| 76 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== = | |
| 77 | pr otected Pe rsistentSe t( | |
| 78 | Inst anceByteCh annelFacto ry byteCha nnelFactor y, | |
| 79 | int secondsRea dWaitsForW riteComple tion, | |
| 80 | bool ean setMod ificationT imeOnRead) | |
| 81 | { | |
| 82 | this .byteChann elFactory = byteChan nelFactory ; | |
| 83 | this .secondsRe adWaitsFor WriteCompl etion = se condsReadW aitsForWri teCompleti on; | |
| 84 | this .setModifi cationTime OnRead = s etModifica tionTimeOn Read; | |
| 85 | } | |
| 86 | ||
| 87 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== = | |
| 88 | // Property Accessors | |
| 89 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== = | |
| 90 | pu blic Insta nceByteCha nnelFactor y getByteC hannelFact ory() | |
| 91 | { | |
| 92 | retu rn this.by teChannelF actory; | |
| 93 | } | |
| 94 | ||
| 95 | pu blic int g etSecondsR eadWaitsFo rWriteComp letion() | |
| 96 | { | |
| 97 | retu rn this.se condsReadW aitsForWri teCompleti on; | |
| 98 | } | |
| 99 | ||
| 100 | pu blic boole an isSetMo dification TimeOnRead () | |
| 101 | { | |
| 102 | retu rn this.se tModificat ionTimeOnR ead; | |
| 103 | } | |
| 104 | ||
| 105 | pr ivate Reen trantReadW riteLock g etAccessLo ck() | |
| 106 | { | |
| 107 | retu rn this.ac cessLock; | |
| 108 | } | |
| 109 | ||
| 110 | pr ivate Read Lock getRe adLock() | |
| 111 | { | |
| 112 | retu rn getAcce ssLock().r eadLock(); | |
| 113 | } | |
| 114 | ||
| 115 | pr ivate Writ eLock getW riteLock() | |
| 116 | { | |
| 117 | retu rn getAcce ssLock().w riteLock() ; | |
| 118 | } | |
| 119 | ||
| 120 | pu blic boole an isWrite Locked() | |
| 121 | th rows Inter ruptedExce ption | |
| 122 | { | |
| 123 | bool ean lockAc quired = g etWriteLoc k().tryLoc k(0L, Time Unit.SECON DS); | |
| 124 | if(l ockAcquire d) | |
| 125 | getWri teLock().u nlock(); | |
| 126 | ||
| 127 | retu rn !lockAc quired; | |
| 128 | } | |
| 129 | ||
| 130 | /* * | |
| 131 | * This will step thro ugh the co llection a nd remove any SoftRe ferences t o null. | |
| 132 | * Note that this does not guara ntee that there will be no mor e SoftRefe rences to null, | |
| 133 | * because s ome may fa ll out of reference as this me thod runs. | |
| 134 | * / | |
| 135 | pu blic void pruneNullS oftReferen ces() | |
| 136 | { | |
| 137 | getW riteLock() .lock(); | |
| 138 | try | |
| 139 | { | |
| 140 | List<S oftReferen ce<? exten ds T>> dea dMen = new ArrayList <SoftRefer ence<? ext ends T>>() ; | |
| 141 | ||
| 142 | // pru ne unused references in two st eps to avo id Concurr entModicic ationExcep tion | |
| 143 | for( I terator<So ftReferenc e<? extend s T>> iter =iterator( ); iter.ha sNext(); ) | |
| 144 | { | |
| 145 | SoftRefe rence<? ex tends T> r ef = iter. next(); | |
| 146 | if( ref. get() == n ull ) | |
| 147 | deadMen.ad d(ref); | |
| 148 | } | |
| 149 | for(So ftReferenc e<? extend s T> deadR ef : deadM en) | |
| 150 | remove(d eadRef); | |
| 151 | ||
| 152 | } | |
| 153 | fina lly | |
| 154 | { | |
| 155 | getWri teLock().u nlock(); | |
| 156 | } | |
| 157 | } | |
| 158 | ||
| 159 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== = | |
| 160 | // Abstract (type spec ific) | |
| 161 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== = | |
| 162 | /* * | |
| 163 | * May creat e an imple mentation specific g roup or in stance, so an abstra ct method | |
| 164 | * | |
| 165 | * @param na me | |
| 166 | * @param cr eate | |
| 167 | * @return | |
| 168 | * @throws C acheExcept ion | |
| 169 | * / | |
| 170 | pr otected ab stract T g etOrCreate (String na me, boolea n create) | |
| 171 | th rows Cache Exception; | |
| 172 | ||
| 173 | /* * | |
| 174 | * Method in the deriv ed class t hat actual ly does th e persiste nt to | |
| 175 | * transient synchroni zation. | |
| 176 | * | |
| 177 | * @throws C acheExcept ion | |
| 178 | * / | |
| 179 | pr otected ab stract voi d internal Synchroniz eChildren( ) | |
| 180 | th rows Cache Exception; | |
| 181 | ||
| 182 | /* * | |
| 183 | * Assure th at the int ernal repr esentation of child instances matches | |
| 184 | * what is i n the pers istent sto rage (file system). | |
| 185 | * | |
| 186 | * @throws C acheExcept ion | |
| 187 | * / | |
| 188 | pr ivate fina l void syn chronizeCh ildren() | |
| 189 | th rows Cache Exception | |
| 190 | { | |
| 191 | getW riteLock() .lock(); | |
| 192 | try | |
| 193 | { | |
| 194 | intern alSynchron izeChildre n(); | |
| 195 | } | |
| 196 | fina lly | |
| 197 | { | |
| 198 | getWri teLock().u nlock(); | |
| 199 | } | |
| 200 | ||
| 201 | } | |
| 202 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== = | |
| 203 | // Overrides to provid e locking as require d | |
| 204 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== = | |
| 205 | /* * | |
| 206 | * @se e java.uti l.HashSet# add(java.l ang.Object ) | |
| 207 | */ | |
| 208 | @Overr ide | |
| 209 | public boolean a dd(SoftRef erence<? e xtends T> e) | |
| 210 | { | |
| 211 | ge tWriteLock ().lock(); | |
| 212 | tr y | |
| 213 | { | |
| 214 | retu rn super.a dd(e); | |
| 215 | } | |
| 216 | fi nally | |
| 217 | { | |
| 218 | getW riteLock() .unlock(); | |
| 219 | } | |
| 220 | } | |
| 221 | ||
| 222 | /* * | |
| 223 | * @se e java.uti l.HashSet# clear() | |
| 224 | */ | |
| 225 | @Overr ide | |
| 226 | public void clea r() | |
| 227 | { | |
| 228 | ge tWriteLock ().lock(); | |
| 229 | tr y | |
| 230 | { | |
| 231 | supe r.clear(); | |
| 232 | } | |
| 233 | fi nally | |
| 234 | { | |
| 235 | getW riteLock() .unlock(); | |
| 236 | } | |
| 237 | } | |
| 238 | ||
| 239 | /* * | |
| 240 | * @se e java.uti l.HashSet# remove(jav a.lang.Obj ect) | |
| 241 | */ | |
| 242 | @Overr ide | |
| 243 | public boolean r emove(Obje ct o) | |
| 244 | { | |
| 245 | ge tWriteLock ().lock(); | |
| 246 | tr y | |
| 247 | { | |
| 248 | return s uper.remov e(o); | |
| 249 | } | |
| 250 | fi nally | |
| 251 | { | |
| 252 | getW riteLock() .unlock(); | |
| 253 | } | |
| 254 | } | |
| 255 | ||
| 256 | /** | |
| 257 | * Rem ove the ob ject that has the gi ven that h as the nam e given fr om the Set . | |
| 258 | * | |
| 259 | * @pa ram name | |
| 260 | * @re turn | |
| 261 | */ | |
| 262 | public boolean r emoveByNam e(String n ame) | |
| 263 | { | |
| 264 | if (name == n ull) | |
| 265 | retu rn false; | |
| 266 | ||
| 267 | ge tWriteLock ().lock(); | |
| 268 | tr y | |
| 269 | { | |
| 270 | for( SoftRefere nce<? exte nds T> mem ber : this ) | |
| 271 | { | |
| 272 | T refe rent = mem ber.get(); | |
| 273 | if( re ferent != null && na me.equals( referent.g etName()) ) | |
| 274 | { | |
| 275 | this.rem ove(member ); | |
| 276 | return t rue; | |
| 277 | } | |
| 278 | } | |
| 279 | retu rn false; | |
| 280 | } | |
| 281 | fi nally | |
| 282 | { | |
| 283 | getW riteLock() .unlock(); | |
| 284 | } | |
| 285 | } | |
| 286 | ||
| 287 | /* * | |
| 288 | * @se e java.uti l.Abstract Set#remove All(java.u til.Collec tion) | |
| 289 | */ | |
| 290 | @Overr ide | |
| 291 | public boolean r emoveAll(C ollection< ?> c) | |
| 292 | { | |
| 293 | ge tWriteLock ().lock(); | |
| 294 | tr y | |
| 295 | { | |
| 296 | return s uper.remov eAll(c); | |
| 297 | } | |
| 298 | fi nally | |
| 299 | { | |
| 300 | getW riteLock() .unlock(); | |
| 301 | } | |
| 302 | } | |
| 303 | ||
| 304 | /* * | |
| 305 | * @se e java.uti l.Abstract Collection #addAll(ja va.util.Co llection) | |
| 306 | */ | |
| 307 | @Overr ide | |
| 308 | public boolean a ddAll(Coll ection<? e xtends Sof tReference <? extends T>> c) | |
| 309 | { | |
| 310 | ge tWriteLock ().lock(); | |
| 311 | tr y | |
| 312 | { | |
| 313 | return s uper.addAl l(c); | |
| 314 | } | |
| 315 | fi nally | |
| 316 | { | |
| 317 | getW riteLock() .unlock(); | |
| 318 | } | |
| 319 | } | |
| 320 | ||
| 321 | /* * | |
| 322 | * @se e java.uti l.Abstract Collection #containsA ll(java.ut il.Collect ion) | |
| 323 | */ | |
| 324 | @Overr ide | |
| 325 | public boolean c ontainsAll (Collectio n<?> c) | |
| 326 | { | |
| 327 | ge tReadLock( ).lock(); | |
| 328 | tr y | |
| 329 | { | |
| 330 | return s uper.conta insAll(c); | |
| 331 | } | |
| 332 | fi nally | |
| 333 | { | |
| 334 | getR eadLock(). unlock(); | |
| 335 | } | |
| 336 | } | |
| 337 | ||
| 338 | /* * | |
| 339 | * @se e java.uti l.Abstract Collection #retainAll (java.util .Collectio n) | |
| 340 | */ | |
| 341 | @Overr ide | |
| 342 | public boolean r etainAll(C ollection< ?> c) | |
| 343 | { | |
| 344 | ge tWriteLock ().lock(); | |
| 345 | tr y | |
| 346 | { | |
| 347 | return s uper.retai nAll(c); | |
| 348 | } | |
| 349 | fi nally | |
| 350 | { | |
| 351 | getW riteLock() .unlock(); | |
| 352 | } | |
| 353 | } | |
| 354 | ||
| 355 | /* * | |
| 356 | * @se e java.uti l.HashSet# clone() | |
| 357 | */ | |
| 358 | @Overr ide | |
| 359 | public Object cl one() | |
| 360 | { | |
| 361 | ge tReadLock( ).lock(); | |
| 362 | tr y | |
| 363 | { | |
| 364 | retu rn super.c lone(); | |
| 365 | } | |
| 366 | fi nally | |
| 367 | { | |
| 368 | getR eadLock(). unlock(); | |
| 369 | } | |
| 370 | } | |
| 371 | ||
| 372 | /* * | |
| 373 | * @se e java.uti l.HashSet# contains(j ava.lang.O bject) | |
| 374 | */ | |
| 375 | @Overr ide | |
| 376 | public boolean c ontains(Ob ject o) | |
| 377 | { | |
| 378 | ge tReadLock( ).lock(); | |
| 379 | tr y | |
| 380 | { | |
| 381 | retu rn super.c ontains(o) ; | |
| 382 | } | |
| 383 | fi nally | |
| 384 | { | |
| 385 | getR eadLock(). unlock(); | |
| 386 | } | |
| 387 | } | |
| 388 | ||
| 389 | /* * | |
| 390 | * @se e java.uti l.HashSet# isEmpty() | |
| 391 | */ | |
| 392 | @Overr ide | |
| 393 | public boolean i sEmpty() | |
| 394 | { | |
| 395 | ge tReadLock( ).lock(); | |
| 396 | tr y | |
| 397 | { | |
| 398 | retu rn super.i sEmpty(); | |
| 399 | } | |
| 400 | fi nally | |
| 401 | { | |
| 402 | getR eadLock(). unlock(); | |
| 403 | } | |
| 404 | } | |
| 405 | ||
| 406 | /* * | |
| 407 | * @se e java.uti l.HashSet# size() | |
| 408 | */ | |
| 409 | @Overr ide | |
| 410 | public int size( ) | |
| 411 | { | |
| 412 | ge tReadLock( ).lock(); | |
| 413 | tr y | |
| 414 | { | |
| 415 | retu rn super.s ize(); | |
| 416 | } | |
| 417 | fi nally | |
| 418 | { | |
| 419 | getR eadLock(). unlock(); | |
| 420 | } | |
| 421 | } | |
| 422 | ||
| 423 | /* * | |
| 424 | * @se e java.uti l.Abstract Collection #toArray() | |
| 425 | */ | |
| 426 | @Overr ide | |
| 427 | public Object[] toArray() | |
| 428 | { | |
| 429 | ge tReadLock( ).lock(); | |
| 430 | tr y | |
| 431 | { | |
| 432 | retu rn super.t oArray(); | |
| 433 | } | |
| 434 | fi nally | |
| 435 | { | |
| 436 | getR eadLock(). unlock(); | |
| 437 | } | |
| 438 | } | |
| 439 | ||
| 440 | /* * | |
| 441 | * @se e java.uti l.Abstract Collection #toArray(T []) | |
| 442 | */ | |
| 443 | @Overr ide | |
| 444 | public <S> S[] t oArray(S[] a) | |
| 445 | { | |
| 446 | ge tReadLock( ).lock(); | |
| 447 | tr y | |
| 448 | { | |
| 449 | retu rn super.t oArray(a); | |
| 450 | } | |
| 451 | fi nally | |
| 452 | { | |
| 453 | getR eadLock(). unlock(); | |
| 454 | } | |
| 455 | } | |
| 456 | ||
| 457 | /** | |
| 458 | * Ove rride the iterator a nd throw U nsuppporte dOperation Exception when | |
| 459 | * cal ling remov e() so tha t Concurre ntModifica tionExcept ion cannot occur, | |
| 460 | * at least when single th readed. | |
| 461 | */ | |
| 462 | @O verride | |
| 463 | pu blic Itera tor<SoftRe ference<? extends T> > iterator () | |
| 464 | { | |
| 465 | retu rn new Ite rator<Soft Reference< ? extends T>>() | |
| 466 | { | |
| 467 | privat e Iterator <SoftRefer ence<? ext ends T>> w rappedIter = Persist entSet.sup er.iterato r(); | |
| 468 | privat e SoftRefe rence<? ex tends T> c urrent = n ull; | |
| 469 | privat e SoftRefe rence<? ex tends T> p revious = null; | |
| 470 | ||
| 471 | @Overr ide | |
| 472 | public boolean h asNext() | |
| 473 | { | |
| 474 | return w rappedIter .hasNext() ; | |
| 475 | } | |
| 476 | ||
| 477 | @Overr ide | |
| 478 | public SoftRefer ence<? ext ends T> ne xt() | |
| 479 | { | |
| 480 | previous = current ; | |
| 481 | current = wrappedI ter.next() ; | |
| 482 | return c urrent; | |
| 483 | } | |
| 484 | ||
| 485 | @Overr ide | |
| 486 | public void remo ve() | |
| 487 | { | |
| 488 | if(curre nt != null ) | |
| 489 | { | |
| 490 | Persistent Set.this.g etWriteLoc k().lock() ; | |
| 491 | try | |
| 492 | { | |
| 493 | Pe rsistentSe t.this.rem ove(curren t); | |
| 494 | re setToPrevi ous(); | |
| 495 | } | |
| 496 | finally | |
| 497 | { | |
| 498 | Pe rsistentSe t.this.get WriteLock( ).unlock() ; | |
| 499 | } | |
| 500 | } | |
| 501 | //throw new Unsupp ortedOpera tionExcept ion("Persi stentSet.I terator do es not sup port remov e() operat ion."); | |
| 502 | } | |
| 503 | ||
| 504 | // Cre ate a new wrapped it erator and iterate o n it until we get ba ck to | |
| 505 | // the previous | |
| 506 | privat e void res etToPrevio us() | |
| 507 | { | |
| 508 | wrappedI ter = Pers istentSet. super.iter ator(); | |
| 509 | if(previ ous != nul l) | |
| 510 | while(wrap pedIter.ha sNext() && wrappedIt er.next() != previou s ); | |
| 511 | current = null; | |
| 512 | } | |
| 513 | }; | |
| 514 | } | |
| 515 | ||
| 516 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== = | |
| 517 | // Business Methods (c hild group s instance managemen t) | |
| 518 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== = | |
| 519 | /* * | |
| 520 | * Get, and possibly c reate, a c hild Insta nce. Sync hronize th e transien t and pers istent | |
| 521 | * Instances . | |
| 522 | * | |
| 523 | * @param in stanceName | |
| 524 | * @param cr eate | |
| 525 | * @return | |
| 526 | * @throws C acheExcept ion | |
| 527 | * / | |
| 528 | pu blic final T getChil d(String n ame, boole an create) | |
| 529 | th rows Cache Exception | |
| 530 | { | |
| 531 | if(n ame == nul l) | |
| 532 | return null; | |
| 533 | ||
| 534 | // l ock for wr ite always , we canno t upgrade a read-loc k and we m ust | |
| 535 | // h ave a writ e lock to add to the set | |
| 536 | getW riteLock() .lock(); | |
| 537 | ||
| 538 | try | |
| 539 | { | |
| 540 | T chil d = getTra nsient(nam e); | |
| 541 | if(chi ld != null ) | |
| 542 | return c hild; | |
| 543 | ||
| 544 | child = getOrCre ate(name, create); | |
| 545 | if(chi ld != null ) | |
| 546 | add( | |
| 547 | monitorSof tReference s ? | |
| 548 | new SoftRe ference<T> (child, de referenced Members) : | |
| 549 | new SoftRe ference<T> (child) | |
| 550 | ); | |
| 551 | return child; | |
| 552 | } | |
| 553 | fina lly | |
| 554 | { | |
| 555 | getWri teLock().u nlock(); | |
| 556 | } | |
| 557 | } | |
| 558 | ||
| 559 | /* * | |
| 560 | * Get the c hild insta nce from t ransient ( memory), i f it is lo aded. | |
| 561 | * DO NOT LO AD FROM PE RSISTENT S TORAGE. | |
| 562 | * | |
| 563 | * @param in stanceName | |
| 564 | * @return | |
| 565 | * / | |
| 566 | pr otected T getTransie nt(String name) | |
| 567 | { | |
| 568 | Soft Reference< ? extends T> childRe f = getTra nsientRefe rence(name ); | |
| 569 | retu rn childRe f == null ? null : c hildRef.ge t(); | |
| 570 | } | |
| 571 | ||
| 572 | /* * | |
| 573 | * Get a ref erence to a transien t instance if it is loaded. | |
| 574 | * Not that this just gets the S oftReferen ce, the ex istence of the refer ent Instan ce is | |
| 575 | * not guara nteed, tho ugh it is very likel y to be th ere. | |
| 576 | * | |
| 577 | * @param in stanceName | |
| 578 | * @return | |
| 579 | * / | |
| 580 | pr otected fi nal SoftRe ference<? extends T> getTransi entReferen ce(String name) | |
| 581 | { | |
| 582 | // S ynchronize access so that othe r threads cannot mod ify it whi le we are searching. | |
| 583 | getR eadLock(). lock(); | |
| 584 | try | |
| 585 | { | |
| 586 | for(So ftReferenc e<? extend s T> softR ef : this ) | |
| 587 | { | |
| 588 | T child = softRef. get(); | |
| 589 | if( chil d != null && child.g etName() ! = null && child.getN ame().equa ls(name) ) | |
| 590 | return sof tRef; | |
| 591 | } | |
| 592 | } | |
| 593 | fina lly | |
| 594 | { | |
| 595 | getRea dLock().un lock(); | |
| 596 | } | |
| 597 | retu rn null; | |
| 598 | } | |
| 599 | ||
| 600 | /* * | |
| 601 | * @throws C acheExcept ion | |
| 602 | * | |
| 603 | * / | |
| 604 | pu blic final void dele teChild(T child, boo lean force Delete) | |
| 605 | th rows Cache Exception | |
| 606 | { | |
| 607 | if(c hild==null ) | |
| 608 | return ; | |
| 609 | ||
| 610 | getW riteLock() .lock(); | |
| 611 | try | |
| 612 | { | |
| 613 | log.in fo("Removi ng SoftRef erence to '" + child .getName() + "' from Persisten tSet."); | |
| 614 | SoftRe ference<? extends T> ref = get TransientR eference(c hild.getNa me()); | |
| 615 | if( ! remove(ref ) ) | |
| 616 | log.info ("Failed t o remove S oftReferen ce to '" | |
| 617 | + child.ge tName() | |
| 618 | + "' from Persistent Set. " | |
| 619 | + "This ma y be simpl y because it is no l onger in t he memory view of th e cache bu t that is unusual.") ; | |
| 620 | ref.cl ear(); // c lear the r eference s o there ar e no more hard refer ences to t he child, except the parameter to this m ethod | |
| 621 | child. delete(for ceDelete); // remov e the corr esponding file | |
| 622 | child = null; // w holly unne cessary, j ust here t o reinforc e the idea that no r eferences to child s hould exis t. | |
| 623 | } | |
| 624 | fina lly | |
| 625 | { | |
| 626 | getWri teLock().u nlock(); | |
| 627 | } | |
| 628 | } | |
| 629 | ||
| 630 | pu blic final void dele teAll(bool ean forceD elete) | |
| 631 | th rows Cache Exception | |
| 632 | { | |
| 633 | getW riteLock() .lock(); | |
| 634 | sync hronizeChi ldren(); | |
| 635 | try | |
| 636 | { | |
| 637 | for(So ftReferenc e<? extend s T> child : this) | |
| 638 | { | |
| 639 | T childR ef = child .get(); | |
| 640 | // if th e referenc e still ex ists, remo ve it | |
| 641 | if(child Ref != nul l) | |
| 642 | childRef.d elete(forc eDelete); | |
| 643 | } | |
| 644 | clear( ); | |
| 645 | } | |
| 646 | fina lly | |
| 647 | { | |
| 648 | getWri teLock().u nlock(); | |
| 649 | } | |
| 650 | } | |
| 651 | ||
| 652 | pu blic final long getS ize() | |
| 653 | th rows Cache Exception | |
| 654 | { | |
| 655 | sync hronizeChi ldren(); | |
| 656 | retu rn size(); | |
| 657 | } | |
| 658 | ||
| 659 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ====== | |
| 660 | // The evict ion group as determi ned by the given evi ction judg e | |
| 661 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ====== | |
| 662 | /* * | |
| 663 | * Return th e Set of G roup insta nces that are evicta ble accord ing to the | |
| 664 | * given Evi ctionJudge . | |
| 665 | * NOTE: thi s is not a recursive method. The descen dant group s, that ma y be | |
| 666 | * evictable , are not included i n this lis t. | |
| 667 | * | |
| 668 | * @param ju dge | |
| 669 | * @return | |
| 670 | * / | |
| 671 | pu blic final Set<T> ev ictableChi ldren(Evic tionJudge< T> judge) | |
| 672 | { | |
| 673 | Set< T> evictab leChildren = null; | |
| 674 | ||
| 675 | // w ait a very short tim e for a re ad lock | |
| 676 | // i f we can't get it re turn an em pty set of evictable | |
| 677 | // g roups | |
| 678 | try | |
| 679 | { | |
| 680 | if( ge tReadLock( ).tryLock( 1, TimeUni t.SECONDS) ) | |
| 681 | { | |
| 682 | try | |
| 683 | { | |
| 684 | evictableC hildren = internalEv ictableGro ups(judge) ; | |
| 685 | } | |
| 686 | catch(Co ncurrentMo dification Exception cmX) | |
| 687 | { | |
| 688 | log.warn(" Concurrent modificat ion of chi ld groups while runn ing an evi ction swee p. Region not compl etely clea ned but su bsequent s weeps will do so. ") ; | |
| 689 | } | |
| 690 | finally | |
| 691 | { | |
| 692 | getReadLoc k().unlock (); | |
| 693 | } | |
| 694 | } | |
| 695 | } | |
| 696 | catc h (Interru ptedExcept ion e) | |
| 697 | { | |
| 698 | log.wa rn(e); | |
| 699 | } | |
| 700 | ||
| 701 | retu rn evictab leChildren ; | |
| 702 | } | |
| 703 | ||
| 704 | /* * | |
| 705 | * @param ju dge | |
| 706 | * @param ev ictableChi ldren | |
| 707 | * / | |
| 708 | pr ivate Set< T> interna lEvictable Groups(Evi ctionJudge <T> judge) | |
| 709 | { | |
| 710 | Set< T> evictab leChildren = new Has hSet<T>(); | |
| 711 | ||
| 712 | for( SoftRefere nce<? exte nds T> chi ldRef : th is) | |
| 713 | { | |
| 714 | T chil d = childR ef.get(); | |
| 715 | ||
| 716 | // the child gro up may no longer be referenced | |
| 717 | if(chi ld != null ) | |
| 718 | { | |
| 719 | try | |
| 720 | { | |
| 721 | if( judge. isEvictabl e(child) ) | |
| 722 | { | |
| 723 | lo g.info("Qu eueing gro up '" + ch ild.getNam e() + " fo r eviction ."); | |
| 724 | ev ictableChi ldren.add( child); | |
| 725 | } | |
| 726 | } | |
| 727 | catch (C acheExcept ion e) | |
| 728 | { | |
| 729 | log.error( "CacheExce ption eval uating evi ction crit eria for g roup '" + child.toSt ring() + " ', which m ay have to be manual ly deleted .", e); | |
| 730 | } | |
| 731 | } | |
| 732 | } | |
| 733 | ||
| 734 | retu rn evictab leChildren ; | |
| 735 | } | |
| 736 | ||
| 737 | ||
| 738 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ====== | |
| 739 | // The group iterator and the it erator cla ss | |
| 740 | // ========= ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ========== ====== | |
| 741 | ||
| 742 | pu blic Itera tor<T> har dReference Iterator() | |
| 743 | { | |
| 744 | try | |
| 745 | { | |
| 746 | return new Persi stentSetIt erator<T>( this); | |
| 747 | } | |
| 748 | catc h (CacheEx ception e) | |
| 749 | { | |
| 750 | log.er ror(e); | |
| 751 | return null; | |
| 752 | } | |
| 753 | } | |
| 754 | ||
| 755 | cl ass Persis tentSetIte rator<S ex tends Muta bleNamedOb ject> | |
| 756 | im plements I terator<S> | |
| 757 | { | |
| 758 | priv ate Iterat or<SoftRef erence<? e xtends S>> wrappedIt erator = n ull; | |
| 759 | ||
| 760 | Pers istentSetI terator(Pe rsistentSe t<S> paren t) | |
| 761 | thro ws CacheEx ception | |
| 762 | { | |
| 763 | intern alSynchron izeChildre n(); | |
| 764 | wrappe dIterator = parent.i terator(); | |
| 765 | } | |
| 766 | ||
| 767 | publ ic boolean hasNext() | |
| 768 | { | |
| 769 | return wrappedIt erator.has Next(); | |
| 770 | } | |
| 771 | ||
| 772 | publ ic S next( ) | |
| 773 | { | |
| 774 | SoftRe ference<? extends S> ref = wra ppedIterat or.next(); | |
| 775 | return ref.get() ; | |
| 776 | } | |
| 777 | ||
| 778 | publ ic void re move() | |
| 779 | { | |
| 780 | throw new Unsupp ortedOpera tionExcept ion(); | |
| 781 | } | |
| 782 | }; | |
| 783 | ||
| 784 | st atic class Reference QueueNotif ication | |
| 785 | im plements R unnable | |
| 786 | { | |
| 787 | priv ate static final lon g serialVe rsionUID = 1L; | |
| 788 | priv ate final ReferenceQ ueue queue ; | |
| 789 | ||
| 790 | publ ic Referen ceQueueNot ification( ReferenceQ ueue queue ) | |
| 791 | { | |
| 792 | this.q ueue = que ue; | |
| 793 | } | |
| 794 | ||
| 795 | publ ic void ru n() | |
| 796 | { | |
| 797 | try | |
| 798 | { | |
| 799 | for( Ref erence ref = queue.r emove(); r ef != null ; ref = qu eue.remove () ) | |
| 800 | System.out .println( "Persisten tSet refer ence queue freed " + ref.getCl ass().getN ame() ); | |
| 801 | } | |
| 802 | catch (Exception x) | |
| 803 | { | |
| 804 | // ignor e it | |
| 805 | } | |
| 806 | } | |
| 807 | } | |
| 808 | ||
| 809 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.