KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > dods > cache > TransactionCacheImpl


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  */

20 package org.enhydra.dods.cache;
21
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Vector JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Collection JavaDoc;
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 /**
46  * This class contains data and mechanisms needed for caching data objects (or
47  * DataStruct objects) and queries and provides cache configuration and
48  * administration.
49  *
50  * @author Tanja Jovanovic
51  * @author Sinisa Milosevic
52  * @version 1.0 05.08.2003.
53  */

54 public class TransactionCacheImpl extends TransactionQueryCache {
55
56     /**
57      * HashMap for storing data objects. Cache keys are cache handles (String
58      * "<database_name>.<String_presentation_of_oid>"), and cache values are
59      * data objects.
60      */

61     protected DODSHashMap cache = null;
62
63     /**
64      * LRU cache for storing simple queries. LRU cache keys are Strings in form
65      * of "<query_database_name>.<String_presentation_of_query>", and LRU cache
66      * values are objects of org.enhydra.dods.cache.QueryCacheItem class.
67      * QueryCacheItem class stores one query and its necessary data.
68      */

69     protected DODSCache simpleQCache = null;
70
71     /**
72      * LRU cache for storing complex queries. LRU cache keys are Strings in form
73      * of "<query_database_name>.<String_presentation_of_query>", and LRU cache
74      * values are objects of org.enhydra.dods.cache.QueryCacheItem class.
75      * QueryCacheItem class stores one query and its necessary data.
76      */

77     protected DODSCache complexQCache = null;
78
79     /**
80      * Attribute cacheAdministration is array of three objects of the class
81      * CacheAdministration.
82      * CacheAdministration[0] handles configuration settings about data (or
83      * DataStruct object) cache.
84      * CacheAdministration[1] handles configuration settings about simple query
85      * cache.
86      * CacheAdministration[2] handles configuration settings about complex query
87      * cache.
88      */

89     protected CacheAdministration[] cacheAdministration = new CacheAdministration[3];
90
91     /**
92      * TableConfiguration attribute handles configuration settings about table
93      * of this cache.
94      */

95     protected TableConfiguration tableConf = new TableConfiguration();
96
97     /**
98      * Initial query statement. This attribute contains "where" clause which is
99      * used during data (or DataStruct) object cache initialization.
100      * If this attribute is set to "*", all rows from the table (in database)
101      * will be put in the cache.
102      */

103     protected String JavaDoc initialQueryCache = null;
104
105     /**
106      * This attribute is true if the cache is full, otherwise false.
107      */

108     protected boolean fullCachingOn = false;
109     
110     /**
111      * This attribute contains information if multi databases are used.
112      */

113     protected boolean multi = false;
114
115     /**
116      * Table and cache statictics.
117      */

118     protected Statistics statistics = null;
119
120     /**
121      * Old max simple cache size. When data (or DataStruct) cache's maxCacheSize
122      * is set to zero, so is simple query's. This attribute is needed for
123      * retrieving max simple cache size after data (or DataStruct) cache's
124      * maxCacheSize has been set again to any value different from zero.
125      */

126     protected int oldSimpleMaxCacheSize;
127
128     /**
129      * Old max complex cache size. When data (or DataStruct) cache's maxCacheSize
130      * is set to zero, so is complex query's. This attribute is needed for
131      * retrieving max complex cache size after data (or DataStruct) cache's
132      * maxCacheSize has been set again to any value different from zero.
133      */

134     protected int oldComplexMaxCacheSize;
135
136     /**
137      * This attribute is used in query caching. It is percent of how many more
138      * object are taken for evaluation. If <code>num</code> is number of needed
139      * results, then it is used
140      * <code>num</code> + DEFAULT_RESERVE_FACTOR * <code>num</code> of
141      * objects for estimating what is quicker: go to database for all object that
142      * are not in the cache, or run again query on database.
143      * This value is given in percents, as number between 0 and 1
144      * (0.25 means 25%).
145      * For example, if DEFAULT_RESERVE_FACTOR is 0.5, and wanted number of
146      * results is 50, the estimation will be done on 75 (50 + 0.5 * 50) objects.
147      * The value of CacheConstants.DEFAULT_RESERVE_FACTOR is 0.5.
148      */

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     /**
160      * Indicator for disabled data (or DataStruct) cache cache.
161      */

162     private boolean isDisabled = false;
163
164     /**
165      * Indicator for disabled simple query cache.
166      */

167     private boolean isDisabledSimple = false;
168
169     /**
170      * Indicator for disabled complex query cache.
171      */

172     private boolean isDisabledComplex = false;
173
174     /**
175      * Maximal cache size of data (or DataStruct) cache cache before it had been
176      * disabled.
177      */

178     private int disabledMaxCacheSize = 0;
179
180     /**
181      * Maximal cache size of simple query cache before it had been disabled.
182      */

183     private int disabledMaxSimpleQueryCacheSize = 0;
184
185     /**
186      * Maximal cache size of complex query cache before it had been disabled.
187      */

188     private int disabledMaxComplexQueryCacheSize = 0;
189
190     /**
191      * Constructor(int, int).
192      * Creates unlimited data object cache.
193      * and simple and complex query caches with their default maximal sizes.
194      * The value of CacheConstants.DEFAULT_MAX_SIMPLE_QUERY_CACHE_SIZE is 0.
195      * The value of CacheConstants.DEFAULT_MAX_COMPLEX_QUERY_CACHE_SIZE is 0.
196      *
197      * @param maxSQSize maximal data (or DataStruct) object cache size.
198      * @param maxCQSize maximal data (or DataStruct) object cache size.
199      */

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     /**
212      * Constructor().
213      * Creates unlimited data object cache.
214      * and simple and complex query caches with their default maximal sizes.
215      * The value of CacheConstants.DEFAULT_MAX_SIMPLE_QUERY_CACHE_SIZE is 0.
216      * The value of CacheConstants.DEFAULT_MAX_COMPLEX_QUERY_CACHE_SIZE is 0.
217      *
218      */

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     /**
231      * Returns CacheAdministration for data object (or DataStruct object) cache,
232      * simple, or complex query cache. Object CacheAdministration handles
233      * configuration settings about these caches.
234      * Parameter cacheType can have one of these values:
235      * 0 - for CacheAdministration of data object (or DataStruct object) cache
236      * 1 - for CacheAdministration of simple query cache
237      * 2 - for CacheAdministration of complex query cache
238      *
239      * @param cacheType 0 - for data object (or DataStruct object),
240      * 1 for simple query and 2 for complex query cache.
241      */

242     public CacheAdministration getCacheAdministration(int cacheType) {
243         if (cacheType < 0 || cacheType > 2) {
244             return null;
245         }
246         return cacheAdministration[cacheType];
247     }
248
249     /**
250      * Returns initialQueryCache. This attribute contains "where" clause
251      * which is used during data object (or DataStruct object) cache
252      * initialization.
253      *
254      * @return initialQueryCache.
255      */

256     public String JavaDoc getInitialQueryCache() {
257         return initialQueryCache;
258     }
259
260     /**
261      * Sets initialQueryCache attribute. This attribute contains "where" clause
262      * which is used during data object (or DataStruct object) cache
263      * initialization.
264      *
265      * @param initQ New value of initialQueryCache attribute.
266      */

267     protected void setInitialQueryCache(String JavaDoc initQ) {
268         initialQueryCache = initQ;
269     }
270
271     /**
272      * Returns statistics of used table and caches.
273      *
274      * @return statistics of used table and caches.
275      */

276     public Statistics getStatistics() {
277         statistics.stopTime();
278         return statistics;
279     }
280
281     /**
282      * Refreshes statistics.
283      */

284     public void refreshStatistics() {
285         statistics.clear();
286     }
287
288     /**
289      * Returns information if data object (or DataStruct object) cache if "full".
290      * "Full" cache contains all data objects (or DataStruct objects) from
291      * the table. The cache is "full", if data (or DataStruct) object cache is
292      * unbounded, if the cached table is read-only, and if initialQueryCache
293      * attribute is set to "*".
294      *
295      * @return true if data object (or DataStruct object) cache if "full",
296      * otherwise false.
297      */

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     /**
309      * Returns information if data object (or DataStruct object) cache if "full".
310      * "Full" cache contains all data objects (or DataStruct objects) from
311      * the table. The cache is "full", if data (or DataStruct) object cache is
312      * unbounded, and if initialQueryCache attribute is set to "*".
313      *
314      * @return true if data object (or DataStruct object) cache if "full",
315      * otherwise false.
316      */

317     public boolean isFull() {
318         if (fullCachingOn) {
319             checkFull();
320         }
321         return fullCachingOn;
322     }
323
324     /**
325      * Returns data object (or DataStruct object) cache type.
326      * Possible values are:
327      * "none" - no caching available
328      * "lru" - cache with LRU (least-recently used) algorithm
329      * "full" - special case of LRU cache - the whole table is cached
330      *
331      * @return data object (or DataStruct object) cache type.
332      */

333     public String JavaDoc 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     /**
347      * Returns caching level.
348      * Possible values:
349      * org.enhydra.dods.cache.CacheConstants.DATA_CACHING (value 1) for
350      * data object (or DataStruct object) caching without query caching, and
351      * org.enhydra.dods.cache.CacheConstants.QUERY_CACHING (value 2) for
352      * data object (or DataStruct object) caching with query caching.
353      *
354      * @return Value 2 (org.enhydra.dods.cache.CacheConstants.QUERY_CACHING).
355      */

356     public int getLevelOfCaching() {
357         return CacheConstants.QUERY_CACHING;
358     }
359
360     /**
361      * Returns object TableConfiguration.
362      * TableConfiguration contains parameters (and their set and get methods)
363      * for table configuration.
364      *
365      * @return TableConfiguration.
366      */

367     public TableConfiguration getTableConfiguration() {
368         return tableConf;
369     }
370
371     /**
372      * Returns reserveFactor.
373      * This attribute is used in query caching. It defines how many more (exept
374      * needed) objects are taken for evaluation. If <code>num</code> is number
375      * of needed results, then it is used
376      * <code>num</code> + DEFAULT_RESERVE_FACTOR * <code>num</code>
377      * of objects for estimating what is quicker: go to database for all object
378      * that are not in the cache, or run again query on database.
379      * This value is given in percents, as number between 0 and 1
380      * (0.25 means 25%).
381      * For example, if DEFAULT_RESERVE_FACTOR is 0.5, and wanted number of
382      * results is 50, the estimation will be done on 75 (50 + 0.5 * 50) objects.
383      *
384      * @return reserveFactor.
385      */

386     public double getReserveFactor() {
387         return reserveFactor;
388     }
389
390     /**
391      * Sets reserveFactor.
392      * This attribute is used in query caching. It defines how many more (exept
393      * needed) objects are taken for evaluation. If <code>num</code> is number
394      * of needed results, then it is used
395      * <code>num</code> + DEFAULT_RESERVE_FACTOR * <code>num</code>
396      * of objects for estimating what is quicker: go to database for all object
397      * that are not in the cache, or run again query on database.
398      * This value is given in percents, as number between 0 and 1
399      * (0.25 means 25%).
400      * For example, if DEFAULT_RESERVE_FACTOR is 0.5, and wanted number of
401      * results is 50, the estimation will be done on 75 (50 + 0.5 * 50) objects.
402      *
403      * @param res New reserveFactor.
404      */

405     protected void setReserveFactor(double res) {
406         reserveFactor = res;
407     }
408
409     /**
410      *
411      */

412     protected void setCachePercentage(double percent) {
413         cachePercentage = percent;
414     }
415
416     /**
417      *
418      */

419     public double getCachePercentage() {
420         return cachePercentage;
421     }
422
423     /**
424      * Returns information whether caching is disabled.
425      *
426      * @return true is caching is disabled, otherwise false.
427      */

428     public boolean isDisabled() {
429         return isDisabled;
430     }
431
432     /**
433      * Reads table and cache configuration from application's configuration file.
434      *
435      * @param tableConfig configuration for table of this cache.
436      * @param cacheConfig configuration for this cache.
437      */

438     public void readConfiguration(Config tableConfig, Config cacheConfig, String JavaDoc 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     /**
452      * Creates TransactionCacheImpl instance.
453      *
454      * @return created QueryCacheImpl instance as DataCache.
455      */

456     public DOCache newInstance() throws CacheObjectException {
457         if (isDisabled) {
458             throw new CacheObjectException("Caching is disabled");
459         }
460         return new TransactionCacheImpl();
461     }
462
463     /**
464      * Creates array of three CacheAdministration objects.
465      * CacheAdministration[0] handles configuration settings about data (or
466      * DataStruct) object cache.
467      * CacheAdministration[1] handles configuration settings about simple query
468      * cache.
469      * CacheAdministration[2] handles configuration settings about complex query
470      * cache.
471      */

472     protected void init() {
473         cacheAdministration[CacheConstants.SIMPLE_QUERY_CACHE] = new CacheAdministration() {
474
475             /**
476              * Returns maximum size of simple query cache.
477              *
478              * @return Maximum size of simple query cache.
479              */

480             public int getMaxCacheSize() {
481                 if (simpleQCache != null) {
482                     return simpleQCache.getMaxEntries();
483                 }
484                 return 0;
485             }
486
487             /**
488              * Returns maximum simple query cache size. If the cache is unbounded
489              * (the maximum size is negative), the current cache size is returned.
490              *
491              * @param real If this parameter is true, method returns real
492              * maximum cache size, otherwise returns appropriate value for the
493              * statistic.
494              * @return Maximum simple query cache size.
495              */

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             /**
510              * Sets maximum size of simple query cache.
511              *
512              * @param maxSize Maximum size of simple query cache.
513              */

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 JavaDoc ex) {
534                     System.out.println("Error in setMaxCacheSize - simple cache");
535                 }
536             }
537
538             /**
539              * Returns size of currently used simple query cache.
540              *
541              * @return Size of currently used simple query cache.
542              */

543             public int getCacheSize() {
544                 if (simpleQCache != null) {
545                     return simpleQCache.size();
546                 }
547                 return 0;
548             }
549
550             /**
551              * Refreshes simple query cache.
552              */

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             /**
563              * Disables simple query cache.
564              */

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             /**
578              * Enables simple query cache.
579              */

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 JavaDoc ex) {
596                     System.out.println("Error in enable simple");
597                 }
598             }
599         };
600         cacheAdministration[CacheConstants.COMPLEX_QUERY_CACHE] = new CacheAdministration() {
601
602             /**
603              * Returns maximum size of complex query cache.
604              *
605              * @return Maximum size of complex query cache.
606              */

607             public int getMaxCacheSize() {
608                 if (complexQCache != null) {
609                     return complexQCache.getMaxEntries();
610                 }
611                 return 0;
612             }
613
614             /**
615              * Returns maximum complex query cache size. If the cache is unbounded
616              * (the maximum size is negative), the current cache size is returned.
617              *
618              * @param real If this parameter is true, method returns real
619              * maximum cache size, otherwise returns appropriate value for the
620              * statistic.
621              *
622              * @return Maximum complex query cache size.
623              */

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             /**
638              * Sets maximum size of complex query cache.
639              *
640              * @param maxSize Maximum size of complex query cache.
641              */

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 JavaDoc ex) {
662                     System.out.println("Error in setMaxCacheSize - complex cache");
663                 }
664             }
665
666             /**
667              * Returns size of currently used complex query cache.
668              *
669              * @return Size of currently used complex query cache.
670              */

671             public int getCacheSize() {
672                 if (complexQCache != null) {
673                     return complexQCache.size();
674                 }
675                 return 0;
676             }
677
678             /**
679              * Refreshes complex query cache.
680              */

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             /**
691              * Disables complex query cache.
692              */

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             /**
706              * Enables complex query cache.
707              */

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 JavaDoc ex) {
724                     System.out.println("Error in enable complex");
725                 }
726             }
727         };
728         cacheAdministration[CacheConstants.DATA_CACHE] = new CacheAdministration() {
729
730             /**
731              * Returns maximum size of data (or DataStruct) object cache.
732              *
733              * @return Maximum data (or DataStruct) object cache size.
734              */

735             public int getMaxCacheSize() {
736                 return -1;
737             }
738
739             /**
740              * Returns maximum size of data (or DataStruct) object cache. If the
741              * cache is unbounded (the maximum size is negative), the current cache
742              * size is returned.
743              *
744              * @param real If this parameter is true, method returns real
745              * maximum cache size, otherwise returns appropriate value for the
746              * statistic.
747              *
748              * @return Maximum data (or DataStruct) object cache size.
749              */

750             public int getMaxCacheSize(boolean real) {
751                 if (real) {
752                     return getMaxCacheSize();
753                 } else {
754                     return getCacheSize();
755                 }
756             }
757
758             /**
759              * Returns size of currently used data (or DataStruct) object cache.
760              *
761              * @return Size of currently used data (or DataStruct) object cache.
762              */

763             public int getCacheSize() {
764                 if (cache != null) {
765                     return cache.size();
766                 }
767                 return 0;
768             }
769
770             /**
771              * Sets maximum size of data (or DataStruct) object cache.
772              *
773              * @param maxSize Maximum DO cache size.
774              */

775             protected void setMaxCacheSize(int maxSize) throws CacheObjectException {/* tanja 16.09.2003 kept for future changes
776                  try {
777                  if (isDisabled) {
778                  throw new CacheObjectException("Caching is disabled");
779                  }
780                  if (maxSize == 0) {
781                  cache = null;
782                  if (simpleQCache != null) {
783                  oldSimpleMaxCacheSize = getCacheAdministration(CacheConstants.SIMPLE_QUERY_CACHE).getMaxCacheSize();
784                  simpleQCache = null;
785                  }
786                  else
787                  oldSimpleMaxCacheSize = 0;
788                  if (complexQCache != null) {
789                  oldComplexMaxCacheSize = getCacheAdministration(CacheConstants.COMPLEX_QUERY_CACHE).getMaxCacheSize();
790                  complexQCache = null;
791                  }
792                  else
793                  oldComplexMaxCacheSize = 0;
794                  statistics.clear();
795                  return;
796                  }
797                  if (cache == null){
798                  cache = new DODSHashMap();
799                  if (oldSimpleMaxCacheSize != 0)
800                  simpleQCache = new DODSLRUCache(oldSimpleMaxCacheSize);
801                  if (oldComplexMaxCacheSize != 0)
802                  complexQCache = new DODSLRUCache(oldComplexMaxCacheSize);
803                  }
804                  // else
805                  // cache.setMaxEntries(maxSize);
806                  }
807                  catch (Exception ex){
808                  System.out.println("Error in setMaxCacheSize - DO cache");
809                  }
810                  */
}
811
812             /**
813              * Refreshes caches (data (or DataStruct) object, simple query and
814              * complex query).
815              */

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             /**
830              * Disables data (or DataStruct) object cache. When this cache is
831              * disabled, so are and simple and complex query caches.
832              */

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             /**
850              * Enables data (or DataStruct) object cache. When this cache is
851              * enabled, so are and simple and complex query caches (if they existed
852              * before DO cache had been disabled).
853              */

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 JavaDoc ex) {
868                     System.out.println("Error in enable DO");
869                 }
870             }
871         };
872     }
873
874     /**
875      * Returns cache (data or DataStruct) content.
876      *
877      * @return Cache content as <code>Map</code> of data (or DataStruct) objects.
878      */

879     public Map JavaDoc getCacheContent() {
880         return cache;
881     }
882
883     /**
884      * Returns information if multi databases are supported.
885      *
886      * @return true if multi databases are used, otherwise false.
887      */

888     public boolean isMulti() {
889         return multi;
890     }
891
892     /**
893      * Checks wheather cache reconfiguration needs to be done.
894      *
895      * @return true if cache reconfiguration needs to be done, otherwise false.
896      */

897     public boolean toReconfigure() {
898         return false;
899     }
900
901     /**
902      * Adds DO (data object) to the cache.
903      *
904      * @param newDO Data object that will be added to the cache.
905      *
906      * @return Added data object.
907      */

908     public synchronized GenericDO addDO(GenericDO newDO) {
909         if (cache == null) {
910             return newDO;
911         }
912         try {
913             String JavaDoc handle = newDO.get_CacheHandle();
914             GenericDO ret = (GenericDO) cache.put(handle, newDO);
915
916             return newDO;
917         } catch (Exception JavaDoc e) {
918             e.printStackTrace();
919         }
920         return null;
921     }
922
923     /**
924      * Removes DO (data object) from the cache.
925      *
926      * @param DO Data object that will be removed from the cache.
927      *
928      * @return Removed data object, or <tt>null</tt> if there was no object
929      * removed from the cache.
930      */

931     public synchronized GenericDO removeDO(GenericDO DO) {
932         if (cache != null) {
933             try {
934                 String JavaDoc handle = DO.get_CacheHandle();
935
936                 return (GenericDO) cache.remove(handle);
937             } catch (Exception JavaDoc e) {}
938         }
939         return null;
940     }
941
942     /**
943      * Removes DO (data object) from the cache.
944      *
945      * @param handle Cache handle of DO (data object) that will be removed from
946      * the cache. The form of cache handle is:
947      * "<database_name>.<String_presentation_of_oid>".
948      *
949      * @return Removed data object, or <tt>null</tt> if there was no object
950      * removed from the cache.
951      */

952     public synchronized GenericDO removeDO(String JavaDoc handle) {
953         if (cache != null) {
954             try {
955                 return (GenericDO) cache.remove(handle);
956             } catch (Exception JavaDoc e) {}
957         }
958         return null;
959     }
960
961     /**
962      * Updates cached DO, or inserts DO in the cache if it didn't exist in the
963      * cache.
964      *
965      * @param DO Data object that will be updated (or inserted if didn't
966      * exist in the cache).
967      *
968      * @return Updated or inserted data object.
969      */

970     public GenericDO updateDO(GenericDO DO) {
971         DO = addDO(DO);
972         QueryCacheItem queryItem = null;
973         HashSet JavaDoc del;
974         Iterator JavaDoc iter = null;
975
976         if (complexQCache != null) {
977             del = new HashSet JavaDoc();
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 JavaDoc();
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    /**
1009     * Deletes DO from the cache.
1010     *
1011     * @param DO Data object that will be deleted from the cache.
1012     *
1013     * @return Deleted data object, or <tt>null</tt> if there was no object
1014     * deleted from the cache.
1015     */

1016    public GenericDO deleteDO(GenericDO DO) {
1017        GenericDO oldDO = removeDO(DO);
1018        Iterator JavaDoc 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    /**
1038     * Returns data object whose String representation of OID is parameter handle.
1039     *
1040     * @param handle String representation of OID of object that is being
1041     * searched in the cache.
1042     *
1043     * @return Data object whose String representation of OID is handle.
1044     */

1045    public synchronized GenericDO getDOByHandle(String JavaDoc handle) {
1046        if (cache == null) {
1047            return null;
1048        }
1049        return (GenericDO) cache.get(handle);
1050    }
1051
1052    /**
1053     * Creates new QueryCacheItem instance.
1054     *
1055     * @param dbName Database name.
1056     * @return Created QueryCacheItem instance.
1057     */

1058    public QueryCacheItem newQueryCacheItemInstance(String JavaDoc dbName) {
1059        return new QueryCacheItemImpl(dbName);
1060    }
1061
1062    /**
1063     * Returns QueryCacheItem object for specified database and simple query,
1064     * if exists, otherwise null.
1065     *
1066     * @param dbName Database name.
1067     * @param query Query in form of String.
1068     * @return QueryCacheItem object.
1069     */

1070    public QueryCacheItem getSimpleQueryCacheItem(String JavaDoc dbName, String JavaDoc query) {
1071        if (simpleQCache != null) {
1072            return (QueryCacheItem) simpleQCache.get(dbName + "." + query);
1073        }
1074        return null;
1075    }
1076
1077    /**
1078     * Returns QueryCacheItem object for specified database and complex query,
1079     * if exists, otherwise null.
1080     *
1081     * @param dbName Database name.
1082     * @param query Query in form of String.
1083     * @return QueryCacheItem object.
1084     */

1085    public QueryCacheItem getComplexQueryCacheItem(String JavaDoc dbName, String JavaDoc query) {
1086        if (complexQCache != null) {
1087            return (QueryCacheItem) complexQCache.get(dbName + "." + query);
1088        }
1089        return null;
1090    }
1091
1092    /**
1093     * Adds simple query to simple query cache.
1094     *
1095     * @param queryItem Query that will be added to simple query cache.
1096     * @return Query added to simple query cache.
1097     */

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    /**
1108     * Removes simple query from simple query cache.
1109     *
1110     * @param queryItem Query that will be removed from simple query cache.
1111     * @return Query removed from simple query cache.
1112     */

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    /**
1122     * Adds complex query to complex query cache.
1123     *
1124     * @param queryItem Query that will be added to complex query cache.
1125     * @return Query added to complex query cache.
1126     */

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    /**
1137     * Removes complex query from complex query cache.
1138     *
1139     * @param queryItem Query that will be removed from complex query cache.
1140     * @return Query removed from complex query cache.
1141     */

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    /**
1151     * Returns query results from simple query cache.
1152     *
1153     * @param dbName Database name.
1154     * @param query Query for which are results searched in simple query cache.
1155     * @return Query results retrieved from simple cache, or null, if there are
1156     * no results retrieved from simple query cache.
1157     */

1158    public QueryResult getSimpleQueryResults(String JavaDoc dbName, String JavaDoc query) {
1159        return getSimpleQueryResults(dbName, query, 0, 0, false);
1160    }
1161
1162    /**
1163     * Returns query results from simple query cache.
1164     *
1165     * @param dbName Database name.
1166     * @param query Query for which are results searched in simple query cache.
1167     * @param limit Specified number of results (database limit and read skip).
1168     * @param maxdb Number of rows retrieved from database (or cache).
1169     * @return Query results retrieved from simple cache, or null, if there are
1170     * no results retrieved from simple query cache.
1171     */

1172    public QueryResult getSimpleQueryResults(String JavaDoc dbName, String JavaDoc query, int limit, int maxdb) {
1173        return getSimpleQueryResults(dbName, query, limit, maxdb, false);
1174    }
1175
1176    /**
1177     * Returns query results from simple query cache.
1178     *
1179     * @param dbName Database name.
1180     * @param query Query for which are results searched in simple query cache.
1181     * @param limit Specified number of results (database limit and read skip).
1182     * @param maxdb Number of rows retrieved from database (or cache).
1183     * @param unique If true, only unique results are returned.
1184     * @return Query results retrieved from simple cache, or null, if there are
1185     * no results retrieved from simple query cache.
1186     */

1187    public QueryResult getSimpleQueryResults(String JavaDoc dbName, String JavaDoc query, int limit, int maxdb, boolean unique) {
1188        if (simpleQCache == null) {
1189            return null;
1190        }
1191        QueryResult result = null;
1192        String JavaDoc 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 JavaDoc handle = null;
1201            String JavaDoc cachePrefix = queryItem.get_OriginDatabase() + ".";
1202            GenericDO cacheDO = null;
1203          
1204          synchronized(queryItem.getOIds()) {
1205            Iterator JavaDoc iter = queryItem.getOIds().iterator();
1206
1207            if (unique) {
1208                HashSet JavaDoc allResultOids = new HashSet JavaDoc();
1209                int skippedNum = 0;
1210
1211                while ((maxdb == 0 || i < maxdb) && (iter.hasNext())
1212                        && ((limit == 0 || result.DOs.size() < limit))) {
1213                    handle = (String JavaDoc) 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 JavaDoc e) {}
1228                    }
1229                    i++;
1230                } // while
1231
result.skippedUnique = skippedNum;
1232            } // (unique)
1233
else {
1234                while ((maxdb == 0 || i < maxdb) && (iter.hasNext())
1235                        && ((limit == 0 || result.DOs.size() < limit))) {
1236                    handle = (String JavaDoc) 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 JavaDoc e) {}
1247                    i++;
1248                } // while
1249
} // (unique)
1250
} // (queryItem != null)
1251
}
1252        if (result.DOs.size() < limit) {
1253            if ((maxdb == 0) || (i < maxdb)) {
1254                result = null;
1255            }
1256        }
1257        return result;
1258    }
1259
1260    /**
1261     * Returns query results from complex query cache.
1262     *
1263     * @param dbName Database name.
1264     * @param query Query for which are results searched in complex query cache.
1265     * @return Query results retrieved from complex cache, or null, if there are
1266     * no results retrieved from complex query cache.
1267     */

1268    public QueryResult getComplexQueryResults(String JavaDoc dbName, String JavaDoc query) {
1269        return getComplexQueryResults(dbName, query, 0, 0, false);
1270    }
1271
1272    /**
1273     * Returns query results from complex query cache.
1274     *
1275     * @param dbName Database name.
1276     * @param query Query for which are results searched in complex query cache.
1277     * @param limit Specified number of results (database limit and read skip).
1278     * @param maxdb Number of rows retrieved from database (or cache).
1279     * @return Query results retrieved from complex cache, or null, if there are
1280     * no results retrieved from complex query cache.
1281     */

1282    public QueryResult getComplexQueryResults(String JavaDoc dbName, String JavaDoc query, int limit, int maxdb) {
1283        return getComplexQueryResults(dbName, query, limit, maxdb, false);
1284    }
1285
1286    /**
1287     * Returns query results from complex query cache.
1288     *
1289     * @param dbName Database name.
1290     * @param query Query for which are results searched in complex query cache.
1291     * @param limit Specified number of results (database limit and read skip).
1292     * @param maxdb Number of rows retrieved from database (or cache).
1293     * @param unique If true, only unique results are returned.
1294     * @return Query results retrieved from complex cache, or null, if there are
1295     * no results retrieved from complex query cache.
1296     */

1297    public QueryResult getComplexQueryResults(String JavaDoc dbName, String JavaDoc query, int limit, int maxdb, boolean unique) {
1298        if (complexQCache == null) {
1299            return null;
1300        }
1301        QueryResult result = null;
1302        String JavaDoc 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 JavaDoc handle = null;
1311            String JavaDoc cachePrefix = queryItem.get_OriginDatabase() + ".";
1312            GenericDO cacheDO = null;
1313          
1314          synchronized(queryItem.getOIds()) {
1315            Iterator JavaDoc iter = queryItem.getOIds().iterator();
1316
1317            if (unique) {
1318                HashSet JavaDoc allResultOids = new HashSet JavaDoc();
1319                int skippedNum = 0;
1320
1321                while ((maxdb == 0 || i < maxdb) && (iter.hasNext())
1322                        && ((limit == 0 || result.DOs.size() < limit))) {
1323                    handle = (String JavaDoc) 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 JavaDoc e) {}
1338                    }
1339                    i++;
1340                } // while
1341
result.skippedUnique = skippedNum;
1342            } // (unique)
1343
else {
1344                while ((maxdb == 0 || i < maxdb) && (iter.hasNext())
1345                        && ((limit == 0 || result.DOs.size() < limit))) {
1346                    handle = (String JavaDoc) 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 JavaDoc e) {}
1357                    i++;
1358                } // while
1359
} // (unique)
1360
} // (queryItem != null)
1361
}
1362        if (result.DOs.size() < limit) {
1363            if ((maxdb == 0) || (i < maxdb)) {
1364                result = null;
1365            }
1366        }
1367        return result;
1368    }
1369
1370    /**
1371     * Returns query results from simple or complex query cache.
1372     *
1373     * @param dbName Database name.
1374     * @param query Query for which are results searched in simple and complex
1375     * query cache.
1376     * @return Query results retrieved from simple or complex cache, or null,
1377     * if there are no results retrieved from simple and complex query cache.
1378     */

1379    public QueryResult getQueryResults(String JavaDoc dbName, String JavaDoc query) {
1380        QueryResult result = getSimpleQueryResults(dbName, query);
1381
1382        if (result == null) {
1383            result = getComplexQueryResults(dbName, query);
1384        }
1385        return result;
1386    }
1387
1388    /**
1389     * Inner class that implements TableStatistics. This is class for
1390     * administrating table and cache.
1391     */

1392    private class QueryCacheImplStatistics extends TableStatistics {
1393
1394        /**
1395         * Constructor().
1396         */

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 JavaDoc ex) {}
1410        }
1411
1412        /**
1413         * Resets parameters.
1414         */

1415        public void reset() {
1416            insertNum = 0;
1417            updateNum = 0;
1418            deleteNum = 0;
1419            lazyLoadingNum = 0;
1420            startTime = new Date JavaDoc();
1421            stopTime = new Date JavaDoc();
1422            queryNum = 0;
1423            queryByOIdNum = 0;
1424            averageQueryTime = 0;
1425            averageQueryByOIdTime = 0;
1426        }
1427
1428        /**
1429         * Returns type of the statistics. In this case, this is
1430         * QUERY_CACHE_STATISTICS.
1431         *
1432         * @return Type of the statistics: QUERY_CACHE_STATISTICS.
1433         */

1434        public int getStatisticsType() {
1435            return QUERY_CACHE_STATISTICS;
1436        }
1437
1438        /**
1439         * Clears statistics.
1440         */

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        /**
1455         * Returns query statistics for DO cache, simple query or complex query
1456         * cache.
1457         *
1458         * @param type Indicator of cache for which query statistics is returned
1459         * for. Possible values:
1460         * org.enhydra.dods.cache.CacheConstants.DATA_CACHE (value 0) for query
1461         * statistics of DO cache
1462         * org.enhydra.dods.cache.CacheConstants.SIMPLE_QUERY_CACHE (value 1)
1463         * for query statistics of simple query cache
1464         * org.enhydra.dods.cache.CacheConstants.COMPLEX_QUERY_CACHE (value 2)
1465         * for query statistics of complex query cache
1466         *
1467         * @return Query statistics for specified cache.
1468         */

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    /**
1487     * Shows content of this class.
1488     * Can be used for debugging.
1489     */

1490    public String JavaDoc toString() {
1491        StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
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    /**
1503     *
1504     */

1505    public void removeEntries(Vector JavaDoc vec) {
1506        //System.err.println(getClass()+".removeEntries(Vector)");
1507
if (cache.size() > vec.size()) {
1508            for (Enumeration JavaDoc e = vec.elements(); e.hasMoreElements();) {
1509                String JavaDoc cacheHandle = (String JavaDoc) e.nextElement();
1510                removeDO(cacheHandle);
1511            }
1512        } else {
1513            Iterator JavaDoc it=cache.values().iterator();
1514            Vector JavaDoc vecToRemove = new Vector JavaDoc();
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 JavaDoc e = vecToRemove.elements(); e.hasMoreElements();) {
1523                removeDO((GenericDO)e.nextElement());
1524            }
1525        }
1526    }
1527
1528    /**
1529     *
1530     * @param tableClass -
1531     */

1532    public void removeEntries(Class JavaDoc tableClass) {
1533        //System.err.println(getClass()+".removeEntries(Class)");
1534
Iterator JavaDoc it=cache.values().iterator();
1535        Vector JavaDoc vecToRemove = new Vector JavaDoc();
1536        while (it.hasNext()) {
1537            GenericDO obj = (GenericDO)it.next();
1538            if (tableClass.equals(obj.getClass()))
1539                vecToRemove.add(obj);
1540        } // while
1541
for (Enumeration JavaDoc e = vecToRemove.elements(); e.hasMoreElements();) {
1542            removeDO((GenericDO)e.nextElement());
1543        }
1544    }
1545
1546    /**
1547     *
1548     */

1549    public void emptyEntries(Vector JavaDoc vec, boolean incrementVersion) {
1550        //System.err.println(getClass()+".emptyEntries(Vector)");
1551
for (Enumeration JavaDoc e = vec.elements(); e.hasMoreElements();) {
1552            String JavaDoc cacheHandle = (String JavaDoc) e.nextElement();
1553            GenericDO obj = (GenericDO) cache.get(cacheHandle);
1554            if (obj != null){
1555                obj.dumpData(incrementVersion);
1556            }
1557        } // for
1558
}
1559    
1560    /**
1561     * Dumps data structs for all instances of tableClass in transaction
1562     * cache.
1563     *
1564     * @param tableClass - Class object whose instances will be emptied
1565     */

1566    public void emptyEntries(Class JavaDoc tableClass) {
1567        //System.err.println(getClass()+".emptyEntries(Class)");
1568
Collection JavaDoc c = cache.values();
1569        Iterator JavaDoc it=c.iterator();
1570        while (it.hasNext()) {
1571            GenericDO obj = (GenericDO)it.next();
1572            if (tableClass.equals(obj.getClass())) {
1573// System.err.println("emptiing: "+obj);
1574
obj.dumpData(false);
1575            }
1576        } // while
1577
}
1578    
1579    
1580    /**
1581     * @return
1582     */

1583    public int getInitialCacheFetchSize() {
1584        return initialCacheFetchSize;
1585    }
1586
1587    /**
1588     * @return
1589     */

1590    public int getInitialDSCacheSize() {
1591        return initialDSCacheSize;
1592    }
1593
1594    /**
1595     * @param i
1596     */

1597    public void setInitialCacheFetchSize(int i) {
1598        initialCacheFetchSize = i;
1599    }
1600
1601    /**
1602     * @param i
1603     */

1604    public void setInitialDSCacheSize(int i) {
1605        initialDSCacheSize = i;
1606    }
1607
1608
1609    
1610}
1611
Popular Tags