1 20 package org.enhydra.dods.cache; 21 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Vector ; 26 import java.util.Date ; 27 import java.util.Enumeration ; 28 import java.util.Collection ; 29 import org.enhydra.dods.statistics.CacheStatistics; 30 import org.enhydra.dods.statistics.Statistics; 31 import org.enhydra.dods.statistics.TableStatistics; 32 import org.enhydra.dods.cache.CacheAdministration; 33 import org.enhydra.dods.cache.CacheConstants; 34 import org.enhydra.dods.cache.DODSHashMap; 35 import org.enhydra.dods.cache.TableConfiguration; 36 import org.enhydra.dods.cache.TransactionQueryCache; 37 import org.enhydra.dods.cache.base.BaseCacheManager; 38 import org.enhydra.dods.cache.base.DODSCache; 39 import org.enhydra.dods.cache.lru.DODSLRUCache; 40 import org.enhydra.dods.exceptions.CacheObjectException; 41 import com.lutris.dods.builder.generator.dataobject.GenericDO; 42 import com.lutris.util.Config; 43 import com.lutris.appserver.server.sql.DatabaseManagerException; 44 45 54 public class TransactionCacheImpl extends TransactionQueryCache { 55 56 61 protected DODSHashMap cache = null; 62 63 69 protected DODSCache simpleQCache = null; 70 71 77 protected DODSCache complexQCache = null; 78 79 89 protected CacheAdministration[] cacheAdministration = new CacheAdministration[3]; 90 91 95 protected TableConfiguration tableConf = new TableConfiguration(); 96 97 103 protected String initialQueryCache = null; 104 105 108 protected boolean fullCachingOn = false; 109 110 113 protected boolean multi = false; 114 115 118 protected Statistics statistics = null; 119 120 126 protected int oldSimpleMaxCacheSize; 127 128 134 protected int oldComplexMaxCacheSize; 135 136 149 protected double reserveFactor = CacheConstants.DEFAULT_RESERVE_FACTOR; 150 protected double cachePercentage = CacheConstants.DEFAULT_CACHE_PERCENTAGE; 151 152 153 private int initialCacheFetchSize = CacheConstants.DEFAULT_INITIAL_CACHE_FETCH_SIZE; 154 155 private int initialDSCacheSize = CacheConstants.DEFAULT_INITIAL_DS_CACHE_SIZE; 156 157 158 159 162 private boolean isDisabled = false; 163 164 167 private boolean isDisabledSimple = false; 168 169 172 private boolean isDisabledComplex = false; 173 174 178 private int disabledMaxCacheSize = 0; 179 180 183 private int disabledMaxSimpleQueryCacheSize = 0; 184 185 188 private int disabledMaxComplexQueryCacheSize = 0; 189 190 200 public TransactionCacheImpl(int maxSQSize, int maxCQSize) throws CacheObjectException { 201 if (isDisabled) { 202 throw new CacheObjectException("Caching is disabled"); 203 } 204 cache = new DODSHashMap(); 205 simpleQCache = BaseCacheManager.getDODSCache(maxSQSize); 206 complexQCache = BaseCacheManager.getDODSCache(maxCQSize); 207 statistics = new QueryCacheImplStatistics(); 208 init(); 209 } 210 211 219 public TransactionCacheImpl() throws CacheObjectException { 220 if (isDisabled) { 221 throw new CacheObjectException("Caching is disabled"); 222 } 223 cache = new DODSHashMap(); 224 simpleQCache = BaseCacheManager.getDODSCache(0); 225 complexQCache = BaseCacheManager.getDODSCache(0); 226 statistics = new QueryCacheImplStatistics(); 227 init(); 228 } 229 230 242 public CacheAdministration getCacheAdministration(int cacheType) { 243 if (cacheType < 0 || cacheType > 2) { 244 return null; 245 } 246 return cacheAdministration[cacheType]; 247 } 248 249 256 public String getInitialQueryCache() { 257 return initialQueryCache; 258 } 259 260 267 protected void setInitialQueryCache(String initQ) { 268 initialQueryCache = initQ; 269 } 270 271 276 public Statistics getStatistics() { 277 statistics.stopTime(); 278 return statistics; 279 } 280 281 284 public void refreshStatistics() { 285 statistics.clear(); 286 } 287 288 298 public void checkFull() { 299 if ((cacheAdministration[0].getMaxCacheSize() < 0) 300 && (getInitialQueryCache() != null) 301 && (getInitialQueryCache().equals("*"))) { 302 fullCachingOn = true; 303 } else { 304 fullCachingOn = false; 305 } 306 } 307 308 317 public boolean isFull() { 318 if (fullCachingOn) { 319 checkFull(); 320 } 321 return fullCachingOn; 322 } 323 324 333 public String getCacheType() { 334 if (cacheAdministration[CacheConstants.DATA_CACHE].getMaxCacheSize() 335 == 0) { 336 return "none"; 337 } else { 338 if (isFull()) { 339 return "full"; 340 } else { 341 return "lru"; 342 } 343 } 344 } 345 346 356 public int getLevelOfCaching() { 357 return CacheConstants.QUERY_CACHING; 358 } 359 360 367 public TableConfiguration getTableConfiguration() { 368 return tableConf; 369 } 370 371 386 public double getReserveFactor() { 387 return reserveFactor; 388 } 389 390 405 protected void setReserveFactor(double res) { 406 reserveFactor = res; 407 } 408 409 412 protected void setCachePercentage(double percent) { 413 cachePercentage = percent; 414 } 415 416 419 public double getCachePercentage() { 420 return cachePercentage; 421 } 422 423 428 public boolean isDisabled() { 429 return isDisabled; 430 } 431 432 438 public void readConfiguration(Config tableConfig, Config cacheConfig, String dbName) throws CacheObjectException { 439 if (isDisabled) { 440 throw new CacheObjectException("Caching is disabled"); 441 } 442 int maxSize = -1; 443 int maxSimple = 0; 444 int maxComplex = 0; 445 446 cacheAdministration[CacheConstants.DATA_CACHE].setMaxCacheSize(maxSize); 447 cacheAdministration[CacheConstants.SIMPLE_QUERY_CACHE].setMaxCacheSize(maxSimple); 448 cacheAdministration[CacheConstants.COMPLEX_QUERY_CACHE].setMaxCacheSize(maxComplex); 449 } 450 451 456 public DOCache newInstance() throws CacheObjectException { 457 if (isDisabled) { 458 throw new CacheObjectException("Caching is disabled"); 459 } 460 return new TransactionCacheImpl(); 461 } 462 463 472 protected void init() { 473 cacheAdministration[CacheConstants.SIMPLE_QUERY_CACHE] = new CacheAdministration() { 474 475 480 public int getMaxCacheSize() { 481 if (simpleQCache != null) { 482 return simpleQCache.getMaxEntries(); 483 } 484 return 0; 485 } 486 487 496 public int getMaxCacheSize(boolean real) { 497 int size = getMaxCacheSize(); 498 499 if (size < 0) { 500 if (real) { 501 return -1; 502 } else { 503 return getCacheSize(); 504 } 505 } 506 return size; 507 } 508 509 514 protected void setMaxCacheSize(int maxSize) throws CacheObjectException { 515 try { 516 if (isDisabled) { 517 throw new CacheObjectException("Caching is disabled"); 518 } 519 if (maxSize == 0) { 520 if (simpleQCache != null) { 521 statistics.getCacheStatistics(CacheConstants.SIMPLE_QUERY_CACHE).clearStatistics(); 522 simpleQCache = null; 523 } 524 return; 525 } 526 if (simpleQCache == null) { 527 if (cache != null) { 528 simpleQCache = BaseCacheManager.getDODSCache(maxSize); 529 } 530 } else { 531 simpleQCache.setMaxEntries(maxSize); 532 } 533 } catch (Exception ex) { 534 System.out.println("Error in setMaxCacheSize - simple cache"); 535 } 536 } 537 538 543 public int getCacheSize() { 544 if (simpleQCache != null) { 545 return simpleQCache.size(); 546 } 547 return 0; 548 } 549 550 553 public void refresh() { 554 if (cache != null) { 555 if (simpleQCache != null) { 556 simpleQCache = BaseCacheManager.getDODSCache(simpleQCache.getMaxEntries()); 557 statistics.getCacheStatistics(CacheConstants.SIMPLE_QUERY_CACHE).setCacheHitsNum(0); 558 } 559 } 560 } 561 562 565 public void disable() { 566 if (!isDisabledSimple) { 567 isDisabledSimple = true; 568 if (simpleQCache != null) { 569 disabledMaxSimpleQueryCacheSize = simpleQCache.getMaxEntries(); 570 simpleQCache = null; 571 } else { 572 disabledMaxSimpleQueryCacheSize = 0; 573 } 574 } 575 } 576 577 580 public void enable() { 581 try { 582 if (isDisabledSimple) { 583 if (disabledMaxSimpleQueryCacheSize != 0) { 584 if (disabledMaxCacheSize != 0) { 585 simpleQCache = BaseCacheManager.getDODSCache(disabledMaxSimpleQueryCacheSize); 586 } 587 } 588 isDisabledSimple = false; 589 } 590 DODSLRUCache obj = (DODSLRUCache) statistics.getCacheStatistics(CacheConstants.SIMPLE_QUERY_CACHE); 591 592 if (obj != null) { 593 obj.clearStatistics(); 594 } 595 } catch (Exception ex) { 596 System.out.println("Error in enable simple"); 597 } 598 } 599 }; 600 cacheAdministration[CacheConstants.COMPLEX_QUERY_CACHE] = new CacheAdministration() { 601 602 607 public int getMaxCacheSize() { 608 if (complexQCache != null) { 609 return complexQCache.getMaxEntries(); 610 } 611 return 0; 612 } 613 614 624 public int getMaxCacheSize(boolean real) { 625 int size = getMaxCacheSize(); 626 627 if (size < 0) { 628 if (real) { 629 return -1; 630 } else { 631 return getCacheSize(); 632 } 633 } 634 return size; 635 } 636 637 642 protected void setMaxCacheSize(int maxSize) throws CacheObjectException { 643 try { 644 if (isDisabled) { 645 throw new CacheObjectException("Caching is disabled"); 646 } 647 if (maxSize == 0) { 648 if (complexQCache != null) { 649 statistics.getCacheStatistics(CacheConstants.COMPLEX_QUERY_CACHE).clearStatistics(); 650 complexQCache = null; 651 } 652 return; 653 } 654 if (complexQCache == null) { 655 if (cache != null) { 656 complexQCache = BaseCacheManager.getDODSCache(maxSize); 657 } 658 } else { 659 complexQCache.setMaxEntries(maxSize); 660 } 661 } catch (Exception ex) { 662 System.out.println("Error in setMaxCacheSize - complex cache"); 663 } 664 } 665 666 671 public int getCacheSize() { 672 if (complexQCache != null) { 673 return complexQCache.size(); 674 } 675 return 0; 676 } 677 678 681 public void refresh() { 682 if (cache != null) { 683 if (complexQCache != null) { 684 complexQCache = BaseCacheManager.getDODSCache(complexQCache.getMaxEntries()); 685 statistics.getCacheStatistics(CacheConstants.COMPLEX_QUERY_CACHE).setCacheHitsNum(0); 686 } 687 } 688 } 689 690 693 public void disable() { 694 if (!isDisabledComplex) { 695 isDisabledComplex = true; 696 if (complexQCache != null) { 697 disabledMaxComplexQueryCacheSize = complexQCache.getMaxEntries(); 698 complexQCache = null; 699 } else { 700 disabledMaxComplexQueryCacheSize = 0; 701 } 702 } 703 } 704 705 708 public void enable() { 709 try { 710 if (isDisabledComplex) { 711 if (disabledMaxComplexQueryCacheSize != 0) { 712 if (disabledMaxCacheSize != 0) { 713 complexQCache = BaseCacheManager.getDODSCache(disabledMaxComplexQueryCacheSize); 714 } 715 } 716 isDisabledComplex = false; 717 } 718 DODSLRUCache obj = (DODSLRUCache) statistics.getCacheStatistics(CacheConstants.COMPLEX_QUERY_CACHE); 719 720 if (obj != null) { 721 obj.clearStatistics(); 722 } 723 } catch (Exception ex) { 724 System.out.println("Error in enable complex"); 725 } 726 } 727 }; 728 cacheAdministration[CacheConstants.DATA_CACHE] = new CacheAdministration() { 729 730 735 public int getMaxCacheSize() { 736 return -1; 737 } 738 739 750 public int getMaxCacheSize(boolean real) { 751 if (real) { 752 return getMaxCacheSize(); 753 } else { 754 return getCacheSize(); 755 } 756 } 757 758 763 public int getCacheSize() { 764 if (cache != null) { 765 return cache.size(); 766 } 767 return 0; 768 } 769 770 775 protected void setMaxCacheSize(int maxSize) throws CacheObjectException {} 811 812 816 public void refresh() { 817 if (cache != null) { 818 cache = new DODSHashMap(); 819 } 820 if (simpleQCache != null) { 821 simpleQCache = BaseCacheManager.getDODSCache(simpleQCache.getMaxEntries()); 822 } 823 if (complexQCache != null) { 824 complexQCache = BaseCacheManager.getDODSCache(complexQCache.getMaxEntries()); 825 } 826 statistics.clear(); 827 } 828 829 833 public void disable() { 834 if (!isDisabled) { 835 isDisabled = true; 836 if (cache != null) { 837 cache = null; 838 if (simpleQCache != null) { 839 getCacheAdministration(CacheConstants.SIMPLE_QUERY_CACHE).disable(); 840 } 841 if (complexQCache != null) { 842 cacheAdministration[CacheConstants.COMPLEX_QUERY_CACHE].disable(); 843 } 844 statistics.clear(); 845 } 846 } 847 } 848 849 854 public void enable() { 855 try { 856 if (isDisabled) { 857 cache = new DODSHashMap(); 858 if (isDisabledSimple) { 859 getCacheAdministration(CacheConstants.SIMPLE_QUERY_CACHE).enable(); 860 } 861 if (isDisabledComplex) { 862 getCacheAdministration(CacheConstants.COMPLEX_QUERY_CACHE).enable(); 863 } 864 statistics.clear(); 865 isDisabled = false; 866 } 867 } catch (Exception ex) { 868 System.out.println("Error in enable DO"); 869 } 870 } 871 }; 872 } 873 874 879 public Map getCacheContent() { 880 return cache; 881 } 882 883 888 public boolean isMulti() { 889 return multi; 890 } 891 892 897 public boolean toReconfigure() { 898 return false; 899 } 900 901 908 public synchronized GenericDO addDO(GenericDO newDO) { 909 if (cache == null) { 910 return newDO; 911 } 912 try { 913 String handle = newDO.get_CacheHandle(); 914 GenericDO ret = (GenericDO) cache.put(handle, newDO); 915 916 return newDO; 917 } catch (Exception e) { 918 e.printStackTrace(); 919 } 920 return null; 921 } 922 923 931 public synchronized GenericDO removeDO(GenericDO DO) { 932 if (cache != null) { 933 try { 934 String handle = DO.get_CacheHandle(); 935 936 return (GenericDO) cache.remove(handle); 937 } catch (Exception e) {} 938 } 939 return null; 940 } 941 942 952 public synchronized GenericDO removeDO(String handle) { 953 if (cache != null) { 954 try { 955 return (GenericDO) cache.remove(handle); 956 } catch (Exception e) {} 957 } 958 return null; 959 } 960 961 970 public GenericDO updateDO(GenericDO DO) { 971 DO = addDO(DO); 972 QueryCacheItem queryItem = null; 973 HashSet del; 974 Iterator iter = null; 975 976 if (complexQCache != null) { 977 del = new HashSet (); 978 for (iter = complexQCache.values().iterator(); iter.hasNext();) { 979 queryItem = (QueryCacheItem) iter.next(); 980 if (DO.get_OriginDatabase().equals(queryItem.get_OriginDatabase())) { 981 del.add(queryItem); 982 } 983 } 984 iter = null; 985 for (iter = del.iterator(); iter.hasNext();) { 986 removeComplexQuery((QueryCacheItem) iter.next()); 987 } 988 } 989 if (simpleQCache != null) { 990 del = new HashSet (); 991 iter = null; 992 for (iter = simpleQCache.values().iterator(); iter.hasNext();) { 993 queryItem = (QueryCacheItem) iter.next(); 994 if (queryItem.isCompleteResult()) { 995 queryItem.update(DO); 996 } else { 997 del.add(queryItem); 998 } 999 } 1000 iter = null; 1001 for (iter = del.iterator(); iter.hasNext();) { 1002 removeSimpleQuery((QueryCacheItem) iter.next()); 1003 } 1004 } 1005 return DO; 1006 } 1007 1008 1016 public GenericDO deleteDO(GenericDO DO) { 1017 GenericDO oldDO = removeDO(DO); 1018 Iterator iter = null; 1019 QueryCacheItem queryItem = null; 1020 1021 if (simpleQCache != null) { 1022 for (iter = simpleQCache.values().iterator(); iter.hasNext();) { 1023 queryItem = (QueryCacheItem) iter.next(); 1024 queryItem.delete(DO); 1025 } 1026 } 1027 iter = null; 1028 if (complexQCache != null) { 1029 for (iter = complexQCache.values().iterator(); iter.hasNext();) { 1030 queryItem = (QueryCacheItem) iter.next(); 1031 queryItem.delete(DO); 1032 } 1033 } 1034 return oldDO; 1035 } 1036 1037 1045 public synchronized GenericDO getDOByHandle(String handle) { 1046 if (cache == null) { 1047 return null; 1048 } 1049 return (GenericDO) cache.get(handle); 1050 } 1051 1052 1058 public QueryCacheItem newQueryCacheItemInstance(String dbName) { 1059 return new QueryCacheItemImpl(dbName); 1060 } 1061 1062 1070 public QueryCacheItem getSimpleQueryCacheItem(String dbName, String query) { 1071 if (simpleQCache != null) { 1072 return (QueryCacheItem) simpleQCache.get(dbName + "." + query); 1073 } 1074 return null; 1075 } 1076 1077 1085 public QueryCacheItem getComplexQueryCacheItem(String dbName, String query) { 1086 if (complexQCache != null) { 1087 return (QueryCacheItem) complexQCache.get(dbName + "." + query); 1088 } 1089 return null; 1090 } 1091 1092 1098 public synchronized QueryCacheItem addSimpleQuery(QueryCacheItem queryItem) { 1099 if (simpleQCache != null) { 1100 return (QueryCacheItem) simpleQCache.add(queryItem.get_OriginDatabase() 1101 + "." + queryItem.getQueryId(), 1102 queryItem); 1103 } 1104 return null; 1105 } 1106 1107 1113 public synchronized QueryCacheItem removeSimpleQuery(QueryCacheItem queryItem) { 1114 if (simpleQCache != null) { 1115 return (QueryCacheItem) simpleQCache.remove(queryItem.get_OriginDatabase() 1116 + "." + queryItem.getQueryId()); 1117 } 1118 return null; 1119 } 1120 1121 1127 public synchronized QueryCacheItem addComplexQuery(QueryCacheItem queryItem) { 1128 if (complexQCache != null) { 1129 return (QueryCacheItem) complexQCache.add(queryItem.get_OriginDatabase() 1130 + "." + queryItem.getQueryId(), 1131 queryItem); 1132 } 1133 return null; 1134 } 1135 1136 1142 public synchronized QueryCacheItem removeComplexQuery(QueryCacheItem queryItem) { 1143 if (complexQCache != null) { 1144 return (QueryCacheItem) complexQCache.remove(queryItem.get_OriginDatabase() 1145 + "." + queryItem.getQueryId()); 1146 } 1147 return null; 1148 } 1149 1150 1158 public QueryResult getSimpleQueryResults(String dbName, String query) { 1159 return getSimpleQueryResults(dbName, query, 0, 0, false); 1160 } 1161 1162 1172 public QueryResult getSimpleQueryResults(String dbName, String query, int limit, int maxdb) { 1173 return getSimpleQueryResults(dbName, query, limit, maxdb, false); 1174 } 1175 1176 1187 public QueryResult getSimpleQueryResults(String dbName, String query, int limit, int maxdb, boolean unique) { 1188 if (simpleQCache == null) { 1189 return null; 1190 } 1191 QueryResult result = null; 1192 String queryHandle = dbName + "." + query; 1193 QueryCacheItem queryItem = (QueryCacheItem) simpleQCache.get(queryHandle); 1194 int i = 0; 1195 1196 if (queryItem != null) { 1197 result = new QueryResult(); 1198 result.database = queryItem.get_OriginDatabase(); 1199 DOShell shell = null; 1200 String handle = null; 1201 String cachePrefix = queryItem.get_OriginDatabase() + "."; 1202 GenericDO cacheDO = null; 1203 1204 synchronized(queryItem.getOIds()) { 1205 Iterator iter = queryItem.getOIds().iterator(); 1206 1207 if (unique) { 1208 HashSet allResultOids = new HashSet (); 1209 int skippedNum = 0; 1210 1211 while ((maxdb == 0 || i < maxdb) && (iter.hasNext()) 1212 && ((limit == 0 || result.DOs.size() < limit))) { 1213 handle = (String ) iter.next(); 1214 if (allResultOids.contains(handle)) { 1215 skippedNum++; 1216 } else { 1217 allResultOids.add(handle); 1218 try { 1219 cacheDO = (GenericDO) cache.get(cachePrefix + handle); 1220 shell = new DOShell(handle); 1221 if (cacheDO != null) { 1222 shell.dataObject = cacheDO; 1223 } else { 1224 result.lazy.add(shell); 1225 } 1226 result.DOs.add(shell); 1227 } catch (Exception e) {} 1228 } 1229 i++; 1230 } result.skippedUnique = skippedNum; 1232 } else { 1234 while ((maxdb == 0 || i < maxdb) && (iter.hasNext()) 1235 && ((limit == 0 || result.DOs.size() < limit))) { 1236 handle = (String ) iter.next(); 1237 try { 1238 cacheDO = (GenericDO) cache.get(cachePrefix + handle); 1239 shell = new DOShell(handle); 1240 if (cacheDO != null) { 1241 shell.dataObject = cacheDO; 1242 } else { 1243 result.lazy.add(shell); 1244 } 1245 result.DOs.add(shell); 1246 } catch (Exception e) {} 1247 i++; 1248 } } } } 1252 if (result.DOs.size() < limit) { 1253 if ((maxdb == 0) || (i < maxdb)) { 1254 result = null; 1255 } 1256 } 1257 return result; 1258 } 1259 1260 1268 public QueryResult getComplexQueryResults(String dbName, String query) { 1269 return getComplexQueryResults(dbName, query, 0, 0, false); 1270 } 1271 1272 1282 public QueryResult getComplexQueryResults(String dbName, String query, int limit, int maxdb) { 1283 return getComplexQueryResults(dbName, query, limit, maxdb, false); 1284 } 1285 1286 1297 public QueryResult getComplexQueryResults(String dbName, String query, int limit, int maxdb, boolean unique) { 1298 if (complexQCache == null) { 1299 return null; 1300 } 1301 QueryResult result = null; 1302 String queryHandle = dbName + "." + query; 1303 QueryCacheItem queryItem = (QueryCacheItem) complexQCache.get(queryHandle); 1304 int i = 0; 1305 1306 if (queryItem != null) { 1307 result = new QueryResult(); 1308 result.database = queryItem.get_OriginDatabase(); 1309 DOShell shell = null; 1310 String handle = null; 1311 String cachePrefix = queryItem.get_OriginDatabase() + "."; 1312 GenericDO cacheDO = null; 1313 1314 synchronized(queryItem.getOIds()) { 1315 Iterator iter = queryItem.getOIds().iterator(); 1316 1317 if (unique) { 1318 HashSet allResultOids = new HashSet (); 1319 int skippedNum = 0; 1320 1321 while ((maxdb == 0 || i < maxdb) && (iter.hasNext()) 1322 && ((limit == 0 || result.DOs.size() < limit))) { 1323 handle = (String ) iter.next(); 1324 if (allResultOids.contains(handle)) { 1325 skippedNum++; 1326 } else { 1327 allResultOids.add(handle); 1328 try { 1329 cacheDO = (GenericDO) cache.get(cachePrefix + handle); 1330 shell = new DOShell(handle); 1331 if (cacheDO != null) { 1332 shell.dataObject = cacheDO; 1333 } else { 1334 result.lazy.add(shell); 1335 } 1336 result.DOs.add(shell); 1337 } catch (Exception e) {} 1338 } 1339 i++; 1340 } result.skippedUnique = skippedNum; 1342 } else { 1344 while ((maxdb == 0 || i < maxdb) && (iter.hasNext()) 1345 && ((limit == 0 || result.DOs.size() < limit))) { 1346 handle = (String ) iter.next(); 1347 try { 1348 cacheDO = (GenericDO) cache.get(cachePrefix + handle); 1349 shell = new DOShell(handle); 1350 if (cacheDO != null) { 1351 shell.dataObject = cacheDO; 1352 } else { 1353 result.lazy.add(shell); 1354 } 1355 result.DOs.add(shell); 1356 } catch (Exception e) {} 1357 i++; 1358 } } } } 1362 if (result.DOs.size() < limit) { 1363 if ((maxdb == 0) || (i < maxdb)) { 1364 result = null; 1365 } 1366 } 1367 return result; 1368 } 1369 1370 1379 public QueryResult getQueryResults(String dbName, String query) { 1380 QueryResult result = getSimpleQueryResults(dbName, query); 1381 1382 if (result == null) { 1383 result = getComplexQueryResults(dbName, query); 1384 } 1385 return result; 1386 } 1387 1388 1392 private class QueryCacheImplStatistics extends TableStatistics { 1393 1394 1397 public QueryCacheImplStatistics() { 1398 try { 1399 this.reset(); 1400 if (cache != null) { 1401 getCacheStatistics(CacheConstants.DATA_CACHE).clearStatistics(); 1402 } 1403 if (simpleQCache != null) { 1404 getCacheStatistics(CacheConstants.SIMPLE_QUERY_CACHE).clearStatistics(); 1405 } 1406 if (complexQCache != null) { 1407 getCacheStatistics(CacheConstants.COMPLEX_QUERY_CACHE).clearStatistics(); 1408 } 1409 } catch (Exception ex) {} 1410 } 1411 1412 1415 public void reset() { 1416 insertNum = 0; 1417 updateNum = 0; 1418 deleteNum = 0; 1419 lazyLoadingNum = 0; 1420 startTime = new Date (); 1421 stopTime = new Date (); 1422 queryNum = 0; 1423 queryByOIdNum = 0; 1424 averageQueryTime = 0; 1425 averageQueryByOIdTime = 0; 1426 } 1427 1428 1434 public int getStatisticsType() { 1435 return QUERY_CACHE_STATISTICS; 1436 } 1437 1438 1441 public void clear() { 1442 this.reset(); 1443 if (cache != null) { 1444 getCacheStatistics(CacheConstants.DATA_CACHE).clearStatistics(); 1445 } 1446 if (simpleQCache != null) { 1447 getCacheStatistics(CacheConstants.SIMPLE_QUERY_CACHE).clearStatistics(); 1448 } 1449 if (complexQCache != null) { 1450 getCacheStatistics(CacheConstants.COMPLEX_QUERY_CACHE).clearStatistics(); 1451 } 1452 } 1453 1454 1469 public CacheStatistics getCacheStatistics(int type) { 1470 switch (type) { 1471 case CacheConstants.DATA_CACHE: 1472 return cache; 1473 1474 case CacheConstants.SIMPLE_QUERY_CACHE: 1475 return simpleQCache; 1476 1477 case CacheConstants.COMPLEX_QUERY_CACHE: 1478 return complexQCache; 1479 1480 default: 1481 return null; 1482 } 1483 } 1484 } 1485 1486 1490 public String toString() { 1491 StringBuffer ret = new StringBuffer (); 1492 1493 ret.append("\n TransactionCacheImpl: "); 1494 ret.append("\n cache: " + cache); 1495 ret.append("\n simpleQCache : " + simpleQCache); 1496 ret.append("\n complexQCache : " + complexQCache); 1497 ret.append("\n cacheReadOnly : " + tableConf.isReadOnly()); 1498 ret.append("\n initialQueryCache : " + initialQueryCache); 1499 ret.append("\n fullCaching : " + isFull()); 1500 return ret.toString(); 1501 } 1502 1505 public void removeEntries(Vector vec) { 1506 if (cache.size() > vec.size()) { 1508 for (Enumeration e = vec.elements(); e.hasMoreElements();) { 1509 String cacheHandle = (String ) e.nextElement(); 1510 removeDO(cacheHandle); 1511 } 1512 } else { 1513 Iterator it=cache.values().iterator(); 1514 Vector vecToRemove = new Vector (); 1515 while (it.hasNext()) { 1516 GenericDO obj = (GenericDO)it.next(); 1517 try { 1518 if (vec.contains((obj.get_CacheHandle()))) 1519 vecToRemove.add(obj); 1520 } catch (DatabaseManagerException e) {} 1521 } 1522 for (Enumeration e = vecToRemove.elements(); e.hasMoreElements();) { 1523 removeDO((GenericDO)e.nextElement()); 1524 } 1525 } 1526 } 1527 1528 1532 public void removeEntries(Class tableClass) { 1533 Iterator it=cache.values().iterator(); 1535 Vector vecToRemove = new Vector (); 1536 while (it.hasNext()) { 1537 GenericDO obj = (GenericDO)it.next(); 1538 if (tableClass.equals(obj.getClass())) 1539 vecToRemove.add(obj); 1540 } for (Enumeration e = vecToRemove.elements(); e.hasMoreElements();) { 1542 removeDO((GenericDO)e.nextElement()); 1543 } 1544 } 1545 1546 1549 public void emptyEntries(Vector vec, boolean incrementVersion) { 1550 for (Enumeration e = vec.elements(); e.hasMoreElements();) { 1552 String cacheHandle = (String ) e.nextElement(); 1553 GenericDO obj = (GenericDO) cache.get(cacheHandle); 1554 if (obj != null){ 1555 obj.dumpData(incrementVersion); 1556 } 1557 } } 1559 1560 1566 public void emptyEntries(Class tableClass) { 1567 Collection c = cache.values(); 1569 Iterator it=c.iterator(); 1570 while (it.hasNext()) { 1571 GenericDO obj = (GenericDO)it.next(); 1572 if (tableClass.equals(obj.getClass())) { 1573 obj.dumpData(false); 1575 } 1576 } } 1578 1579 1580 1583 public int getInitialCacheFetchSize() { 1584 return initialCacheFetchSize; 1585 } 1586 1587 1590 public int getInitialDSCacheSize() { 1591 return initialDSCacheSize; 1592 } 1593 1594 1597 public void setInitialCacheFetchSize(int i) { 1598 initialCacheFetchSize = i; 1599 } 1600 1601 1604 public void setInitialDSCacheSize(int i) { 1605 initialDSCacheSize = i; 1606 } 1607 1608 1609 1610} 1611 | Popular Tags |