1 20 package org.enhydra.dods.cache; 21 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import org.enhydra.dods.DODS; 26 import org.enhydra.dods.cache.base.BaseCacheManager; 27 import org.enhydra.dods.cache.base.DODSCache; 28 import org.enhydra.dods.exceptions.CacheObjectException; 29 import org.enhydra.dods.statistics.CacheStatistics; 30 import org.enhydra.dods.statistics.Statistics; 31 import org.enhydra.dods.statistics.TableStatistics; 32 33 import com.lutris.appserver.server.sql.CoreDataStruct; 34 import com.lutris.appserver.server.sql.standard.DatabaseConfiguration; 35 import com.lutris.appserver.server.sql.standard.StandardLogicalDatabase; 36 import com.lutris.util.Config; 37 38 48 public class DataStructCacheImpl extends DataStructCache { 49 50 55 protected DODSCache cache = null; 56 57 61 protected CacheAdministration cacheAdministration = null; 62 63 67 protected TableConfiguration tableConf = new TableConfiguration(); 68 69 75 protected String initialQueryCache = null; 76 77 80 protected boolean multi = false; 81 82 85 protected boolean fullCachingOn = false; 86 87 90 protected Statistics statistics = null; 91 92 99 protected HashMap nonVisibleList = null; 100 101 106 protected double reserveFactor = 0; 107 protected double cachePercentage = -1; 108 109 112 private boolean isDisabled = false; 113 114 117 private int disabledMaxCacheSize = 0; 118 119 120 121 private int initialCacheFetchSize = CacheConstants.DEFAULT_INITIAL_CACHE_FETCH_SIZE; 122 123 private int initialDSCacheSize = CacheConstants.DEFAULT_INITIAL_DS_CACHE_SIZE; 124 125 126 127 133 public DataStructCacheImpl(int maxCSize) throws CacheObjectException { 134 if (isDisabled) { 135 throw new CacheObjectException("Caching is disabled"); 136 } 137 cache = BaseCacheManager.getDODSCache(maxCSize); 138 statistics = new DataStructCacheImplStatistics(); 139 nonVisibleList = new HashMap (); 140 init(); 141 } 142 143 148 public DataStructCacheImpl() throws CacheObjectException { 149 this(CacheConstants.DEFAULT_MAX_CACHE_SIZE); 150 } 151 152 167 public CacheAdministration getCacheAdministration(int cacheType) { 168 return cacheAdministration; 169 } 170 171 178 public String getInitialQueryCache() { 179 return initialQueryCache; 180 } 181 182 189 protected void setInitialQueryCache(String initQ) { 190 initialQueryCache = initQ; 191 } 192 193 public void makeInvisible(String cacheHandle) { 194 Integer intObj = (Integer ) nonVisibleList.get(cacheHandle); 195 int num; 196 197 if (intObj != null) { 198 num = intObj.intValue(); 199 num++; 200 nonVisibleList.put(cacheHandle, new Integer (num)); 201 } else { 202 nonVisibleList.put(cacheHandle, new Integer (1)); 203 } 204 } 205 206 public void makeVisible(String cacheHandle) { 207 Integer intObj = (Integer ) nonVisibleList.get(cacheHandle); 208 int num; 209 210 if (intObj != null) { 211 num = intObj.intValue(); 212 num--; 213 if (num == 0) { 214 nonVisibleList.remove(cacheHandle); 215 } else { 216 nonVisibleList.put(cacheHandle, new Integer (num)); 217 } 218 } 219 } 220 221 226 public Statistics getStatistics() { 227 statistics.stopTime(); 228 return statistics; 229 } 230 231 234 public void refreshStatistics() { 235 statistics.clear(); 236 } 237 238 248 public void checkFull() { 249 if ((cacheAdministration.getMaxCacheSize() < 0) 250 && (getInitialQueryCache() != null) 251 && (getInitialQueryCache().equals("*"))) { 252 fullCachingOn = true; 253 } else { 254 fullCachingOn = false; 255 } 256 } 257 258 267 public boolean isFull() { 268 if (fullCachingOn) { 269 checkFull(); 270 } 271 return fullCachingOn; 272 } 273 274 283 public String getCacheType() { 284 if (cacheAdministration.getMaxCacheSize() == 0) { 285 return "none"; 286 } else { 287 if (isFull()) { 288 return "full"; 289 } else { 290 return "lru"; 291 } 292 } 293 } 294 295 307 public int getLevelOfCaching() { 308 return CacheConstants.DATA_CACHING; 309 } 310 311 316 public TableConfiguration getTableConfiguration() { 317 return tableConf; 318 } 319 320 327 public double getReserveFactor() { 328 return 0; 329 } 330 331 339 protected void setReserveFactor(double res) {} 340 341 344 protected void setCachePercentage(double percent) { 345 cachePercentage = percent; 346 } 347 348 351 public double getCachePercentage() { 352 return cachePercentage; 353 } 354 355 360 public boolean isDisabled() { 361 return isDisabled; 362 } 363 364 370 public void readConfiguration(Config tableConfig, Config cacheConfig, String dbName) throws CacheObjectException { 371 if (isDisabled) { 372 throw new CacheObjectException("Caching is disabled"); 373 } 374 int maxSize = CacheConstants.DEFAULT_MAX_CACHE_SIZE; 375 boolean initialAllCaches = CacheConstants.DEFAULT_INITIAL_ALL_CACHES; 376 Config defaultCacheConfig = null; 377 378 this.tableConf.readTableConfiguration(tableConfig, dbName); 379 DatabaseConfiguration dbConf; 380 381 try { 382 dbConf = ((StandardLogicalDatabase) (DODS.getDatabaseManager().findLogicalDatabase(dbName))).getDatabaseConfiguration(); 383 } catch (Exception ex) { 384 throw new CacheObjectException("Error reading database configuration"); 385 } 386 if (dbConf != null) { 387 try { 388 maxSize = dbConf.getMaxCacheSize(); 389 } catch (Exception e) {} 390 try { 391 reserveFactor = dbConf.getReserveFactor(); 392 } catch (Exception e) {} 393 try { 394 cachePercentage = dbConf.getCachePercentage(); 395 } catch (Exception e) {} 396 try { 397 initialAllCaches = dbConf.getInitAllCaches(); 398 } catch (Exception e) {} 399 try { 400 initialCacheFetchSize = dbConf.getInitialCacheFetchSize (); 401 } catch (Exception e) {} 402 try { 403 initialDSCacheSize = dbConf.getInitialDSCacheSize(); 404 } catch (Exception e) {} 405 } 406 if (cacheConfig != null) { 407 try { 408 maxSize = cacheConfig.getInt(CacheConstants.PARAMNAME_MAX_CACHE_SIZE); 409 } catch (Exception e) {} 410 try { 411 initialQueryCache = cacheConfig.getString(CacheConstants.PARAMNAME_INITIAL_CONDITION); 412 } catch (Exception e) { 413 if (initialAllCaches) { 414 initialQueryCache = "*"; 415 } 416 } 417 try { 418 reserveFactor = cacheConfig.getDouble(CacheConstants.PARAMNAME_RESERVE_FACTOR); 419 } catch (Exception e) {} 420 try { 421 cachePercentage = cacheConfig.getDouble(CacheConstants.PARAMNAME_CACHE_PERCENTAGE); 422 } catch (Exception e) {} 423 try { 424 initialCacheFetchSize = cacheConfig.getInt(CacheConstants.PARAMNAME_INITIAL_CACHE_FETCH_SIZE); 425 } catch (Exception e) {} 426 try { 427 initialDSCacheSize = cacheConfig.getInt(CacheConstants.PARAMNAME_INITIAL_DS_CACHE_SIZE); 428 } catch (Exception e) {} 429 } else if (initialAllCaches) { 430 initialQueryCache = "*"; 431 } 432 cacheAdministration.setMaxCacheSize(maxSize); 433 } 434 435 440 public DataStructCache newInstance() throws CacheObjectException { 441 if (isDisabled) { 442 throw new CacheObjectException("Caching is disabled"); 443 } 444 return new DataStructCacheImpl(); 445 } 446 447 450 protected void init() { 451 cacheAdministration = new CacheAdministration() { 452 453 458 public int getMaxCacheSize() { 459 if (cache != null) { 460 return cache.getMaxEntries(); 461 } 462 return 0; 463 } 464 465 473 public int getMaxCacheSize(boolean real) { 474 int size = getMaxCacheSize(); 475 476 if (size < 0) { 477 if (real) { 478 return -1; 479 } else { 480 return getCacheSize(); 481 } 482 } 483 return size; 484 } 485 486 492 public int getCacheSize() { 493 if (cache != null) { 494 return cache.size(); 495 } 496 return 0; 497 } 498 499 504 protected void setMaxCacheSize(int maxSize) throws CacheObjectException { 505 if (isDisabled) { 506 throw new CacheObjectException("Caching is disabled"); 507 } 508 if (maxSize == 0) { 509 cache = null; 510 statistics.clear(); 511 return; 512 } 513 if (cache == null) { 514 cache = BaseCacheManager.getDODSCache(maxSize); 515 } else { 516 cache.setMaxEntries(maxSize); 517 } 518 } 519 520 523 public void refresh() { 524 if (cache != null) { 525 cache = BaseCacheManager.getDODSCache(cache.getMaxEntries()); 526 statistics.clear(); 527 } 528 } 529 530 533 public void disable() { 534 if (!isDisabled) { 535 isDisabled = true; 536 if (cache != null) { 537 disabledMaxCacheSize = cache.getMaxEntries(); 538 statistics.clear(); 539 } 540 } 541 } 542 543 546 public void enable() { 547 if (isDisabled) { 548 if (disabledMaxCacheSize != 0) { 549 cache = BaseCacheManager.getDODSCache(disabledMaxCacheSize); 550 } 551 statistics.clear(); 552 isDisabled = false; 553 } 554 } 555 }; 556 } 557 558 563 public Map getCacheContent() { 564 return cache; 565 } 566 567 572 public boolean isMulti() { 573 return multi; 574 } 575 576 581 public boolean toReconfigure() { 582 return false; 583 } 584 585 592 public synchronized CoreDataStruct addDataStruct(CoreDataStruct newDS) { 593 if (cache == null) { 594 return newDS; 595 } 596 try { 597 String handle = newDS.get_CacheHandle(); 598 599 cache.add(handle, newDS); 600 return newDS; 601 } catch (Exception e) { 602 e.printStackTrace(); 603 } 604 return null; 605 } 606 607 615 public synchronized CoreDataStruct removeDataStruct(CoreDataStruct data) { 616 if (cache != null) { 617 try { 618 String handle = data.get_CacheHandle(); 619 620 return (CoreDataStruct) cache.remove(handle); 621 } catch (Exception e) {} 622 } 623 return null; 624 } 625 626 636 public synchronized CoreDataStruct removeDataStruct(String handle) { 637 if (cache != null) { 638 try { 639 return (CoreDataStruct) cache.remove(handle); 640 } catch (Exception e) {} 641 } 642 return null; 643 } 644 645 654 public CoreDataStruct updateDataStruct(CoreDataStruct data) { 655 data = addDataStruct(data); 656 return data; 657 } 658 659 667 public CoreDataStruct deleteDataStruct(CoreDataStruct data) { 668 CoreDataStruct oldData = removeDataStruct(data); 669 670 return oldData; 671 } 672 673 682 public CoreDataStruct getDataStructByHandle(String handle) { 683 if (cache == null) { 684 return null; 685 } 686 CoreDataStruct cdt = null; 687 if(cache.isNeedToSynchronize()) { 688 synchronized (cache) { 689 cdt = getDataCacheItem(handle, cdt); 690 } 691 }else { 692 cdt = getDataCacheItem(handle, cdt); 693 } 694 return cdt; 695 } 696 697 702 private CoreDataStruct getDataCacheItem(String handle, CoreDataStruct cdt) { 703 if (!nonVisibleList.containsKey(handle)) { 704 cdt = (CoreDataStruct) cache.get(handle); 705 } 706 return cdt; 707 } 708 709 713 public void show() { 714 System.out.println("-------------------------------------------------"); 715 System.out.println(" maxCacheSize : " + cache.getMaxEntries()); 716 System.out.println(" cacheReadOnly : " + tableConf.isReadOnly()); 717 System.out.println(" initialQueryCache : " + initialQueryCache); 718 System.out.println(" fullCaching : " + isFull()); 719 } 720 721 725 public String toString() { 726 StringBuffer ret = new StringBuffer (); 727 728 ret.append("\n DataStructCacheImpl: "); 729 ret.append("\n cacheReadOnly : " + tableConf.isReadOnly()); 730 ret.append("\n initialQueryCache : " + initialQueryCache); 731 ret.append("\n fullCaching : " + isFull()); 732 return ret.toString(); 733 } 734 735 736 739 public int getInitialCacheFetchSize() { 740 return initialCacheFetchSize; 741 } 742 743 746 public int getInitialDSCacheSize() { 747 return initialDSCacheSize; 748 } 749 750 753 public void setInitialCacheFetchSize(int i) { 754 initialCacheFetchSize = i; 755 } 756 757 760 public void setInitialDSCacheSize(int i) { 761 initialDSCacheSize = i; 762 } 763 764 765 769 class DataStructCacheImplStatistics extends TableStatistics { 770 771 774 public DataStructCacheImplStatistics() { 775 super(); 776 } 777 778 783 public int getStatisticsType() { 784 return CACHE_STATISTICS; 785 } 786 787 799 public CacheStatistics getCacheStatistics(int type) { 800 switch (type) { 801 case CacheConstants.DATA_CACHE: 802 return cache; 803 804 default: 805 return null; 806 } 807 } 808 } 809 } 810 | Popular Tags |