KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 package org.enhydra.dods.cache;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import org.enhydra.dods.DODS;
31 import org.enhydra.dods.cache.base.BaseCacheManager;
32 import org.enhydra.dods.cache.base.DODSCache;
33 import org.enhydra.dods.exceptions.CacheObjectException;
34 import org.enhydra.dods.statistics.CacheStatistics;
35 import org.enhydra.dods.statistics.Statistics;
36 import org.enhydra.dods.statistics.TableStatistics;
37
38 import com.lutris.appserver.server.sql.CoreDataStruct;
39 import com.lutris.appserver.server.sql.DatabaseManagerException;
40 import com.lutris.appserver.server.sql.standard.DatabaseConfiguration;
41 import com.lutris.appserver.server.sql.standard.StandardLogicalDatabase;
42 import com.lutris.util.Config;
43
44 /**
45  * This class contains data and mechanisms needed for caching data objects (or
46  * DataStruct objects) and queries and provides cache configuration and
47  * administration.
48  *
49  * @author Tanja Jovanovic
50  * @author Nenad Vico
51  * @author Zorica Suvajdzin
52  * @version 2.0 15.06.2003.
53  */

54 public class QueryCacheImpl extends QueryCache {
55
56     /**
57      * LRU cache for storing data (or DataStruct) objects. LRU cache keys are
58      * cache handles (String " <database_name>. <String_presentation_of_oid>"),
59      * and LRU cache values are data (or DataStruct) objects.
60      */

61     protected DODSCache 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
66      * cache 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
74      * cache 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      * LRU cache for storing multi join queries. LRU cache keys are Strings in
81      * form of " <query_database_name>. <String_presentation_of_query>", and LRU
82      * cache values are objects of org.enhydra.dods.cache.QueryCacheItem class.
83      * QueryCacheItem class stores one query and its necessary data.
84      */

85     protected DODSCache multiJoinQCache = null;
86
87     /**
88      * Attribute cacheAdministration is array of three objects of the class
89      * CacheAdministration. CacheAdministration[0] handles configuration
90      * settings about data (or DataStruct object) cache. CacheAdministration[1]
91      * handles configuration settings about simple query cache.
92      * CacheAdministration[2] handles configuration settings about complex query
93      * cache. CacheAdministration[3] handles configuration settings about multi
94      * join query cache.
95      */

96     protected CacheAdministration[] cacheAdministration = new CacheAdministration[4];
97
98     /**
99      * TableConfiguration attribute handles configuration settings about table
100      * of this cache.
101      */

102     protected TableConfiguration tableConf = new TableConfiguration();
103
104     /**
105      * Initial query statement. This attribute contains "where" clause which is
106      * used during data (or DataStruct) object cache initialization. If this
107      * attribute is set to "*", all rows from the table (in database) will be
108      * put in the cache.
109      */

110     protected String JavaDoc initialQueryCache = null;
111
112     /**
113      * This attribute contains information if multi databases are used.
114      */

115     protected boolean multi = false;
116
117     /**
118      * This attribute is true if the cache is full, otherwise false.
119      */

120     protected boolean fullCachingOn = false;
121
122     /**
123      * Table and cache statictics.
124      */

125     protected Statistics statistics = null;
126
127     /**
128      * List of objects that are unvisible in the cache at the moment. Keys of
129      * this map sre cache handles (String " <database_name>.
130      * <String_presentation_of_oid>"), and values are counters of how many
131      * transactions have made object with that cache handle invisible.
132      */

133     protected HashMap JavaDoc nonVisibleList = null;
134
135     /**
136      * Old max simple cache size. When data (or DataStruct) cache's maxCacheSize
137      * is set to zero, so is simple query's. This attribute is needed for
138      * retrieving max simple cache size after data (or DataStruct) cache's
139      * maxCacheSize has been set again to any value different from zero.
140      */

141     protected int oldSimpleMaxCacheSize;
142
143     /**
144      * Old max complex cache size. When data (or DataStruct) cache's
145      * maxCacheSize is set to zero, so is complex query's. This attribute is
146      * needed for retrieving max complex cache size after data (or DataStruct)
147      * cache's maxCacheSize has been set again to any value different from zero.
148      */

149     protected int oldComplexMaxCacheSize;
150
151     /**
152      * Old max multi join cache size. When data (or DataStruct) cache's
153      * maxCacheSize is set to zero, so is multi join query's. This attribute is
154      * needed for retrieving max multi join cache size after data (or
155      * DataStruct) cache's maxCacheSize has been set again to any value
156      * different from zero.
157      */

158     protected int oldMultiJoinMaxCacheSize;
159
160     /**
161      * This attribute is used in query caching. It is percent of how many more
162      * object are taken for evaluation. If <code>num</code> is number of
163      * needed results, then it is used <code>num</code>+
164      * DEFAULT_RESERVE_FACTOR *<code>num</code> of objects for estimating
165      * what is quicker: go to database for all object that are not in the cache,
166      * or run again query on database. This value is given in percents, as
167      * number between 0 and 1 (0.25 means 25%). For example, if
168      * DEFAULT_RESERVE_FACTOR is 0.5, and wanted number of results is 50, the
169      * estimation will be done on 75 (50 + 0.5 * 50) objects. The value of
170      * CacheConstants.DEFAULT_RESERVE_FACTOR is 0.5.
171      */

172     protected double reserveFactor = CacheConstants.DEFAULT_RESERVE_FACTOR;
173
174     /**
175      * Indicator for disabled data (or DataStruct) cache cache.
176      */

177     private boolean isDisabled = false;
178
179     /**
180      * Indicator for disabled simple query cache.
181      */

182     private boolean isDisabledSimple = false;
183
184     /**
185      * Indicator for disabled complex query cache.
186      */

187     private boolean isDisabledComplex = false;
188
189     /**
190      * Indicator for disabled multi join query cache.
191      */

192     private boolean isDisabledMultiJoin = false;
193
194     private double cachePercentage = CacheConstants.DEFAULT_CACHE_PERCENTAGE;
195
196     private int initialCacheFetchSize = CacheConstants.DEFAULT_INITIAL_CACHE_FETCH_SIZE;
197
198     private int initialDSCacheSize = CacheConstants.DEFAULT_INITIAL_DS_CACHE_SIZE;
199
200     /**
201      * Maximal cache size of data (or DataStruct) cache cache before it had been
202      * disabled.
203      */

204     private int disabledMaxCacheSize = 0;
205
206     /**
207      * Maximal cache size of simple query cache before it had been disabled.
208      */

209     private int disabledMaxSimpleQueryCacheSize = 0;
210
211     /**
212      * Maximal cache size of complex query cache before it had been disabled.
213      */

214     private int disabledMaxComplexQueryCacheSize = 0;
215
216     /**
217      * Maximal cache size of multi join query cache before it had been disabled.
218      */

219     private int disabledMaxMultiJoinQueryCacheSize = 0;
220
221     /**
222      * Constructor(int). Creates data (or DataStruct) object cache with maximal
223      * size <code>maxCSize</code> and simple and complex query caches with
224      * their default maximal sizes. The value of
225      * CacheConstants.DEFAULT_MAX_SIMPLE_QUERY_CACHE_SIZE is 0. The value of
226      * CacheConstants.DEFAULT_MAX_COMPLEX_QUERY_CACHE_SIZE is 0.
227      *
228      * @param maxCSize
229      * maximal data (or DataStruct) object cache size.
230      */

231     public QueryCacheImpl(int maxCSize) throws CacheObjectException {
232         if (isDisabled) {
233             throw new CacheObjectException("Caching is disabled");
234         }
235         cache = BaseCacheManager.getDODSCache(maxCSize);
236         if (cache != null) {
237             simpleQCache = BaseCacheManager.getDODSCache(CacheConstants.DEFAULT_MAX_SIMPLE_QUERY_CACHE_SIZE);
238             complexQCache = BaseCacheManager.getDODSCache(CacheConstants.DEFAULT_MAX_COMPLEX_QUERY_CACHE_SIZE);
239             multiJoinQCache = BaseCacheManager.getDODSCache(CacheConstants.DEFAULT_MAX_MULTI_JOIN_QUERY_CACHE_SIZE);
240         }
241         statistics = new QueryCacheImplStatistics();
242         nonVisibleList = new HashMap JavaDoc();
243         init();
244     }
245
246     /**
247      * Constructor(). Creates data (or DataStruct) object cache with its default
248      * maximal size and simple and complex query caches with their default
249      * maximal sizes. The value of CacheConstants.DEFAULT_MAX_CACHE_SIZE is 0.
250      * The value of CacheConstants.DEFAULT_MAX_SIMPLE_QUERY_CACHE_SIZE is 0. The
251      * value of CacheConstants.DEFAULT_MAX_COMPLEX_QUERY_CACHE_SIZE is 0.
252      */

253     public QueryCacheImpl() throws CacheObjectException {
254         this(CacheConstants.DEFAULT_MAX_CACHE_SIZE);
255     }
256
257     /**
258      * Returns CacheAdministration for data object (or DataStruct object) cache,
259      * simple, or complex query cache. Object CacheAdministration handles
260      * configuration settings about these caches. Parameter cacheType can have
261      * one of these values: 0 - for CacheAdministration of data object (or
262      * DataStruct object) cache 1 - for CacheAdministration of simple query
263      * cache 2 - for CacheAdministration of complex query cache 3 - for
264      * CacheAdministration of multi join query cache
265      *
266      * @param cacheType
267      * 0 - for data object (or DataStruct object), 1 for simple
268      * query, 2 for complex query cache and 3 for complex query
269      * cache.
270      */

271     public CacheAdministration getCacheAdministration(int cacheType) {
272         if (cacheType < 0 || cacheType > 3) {
273             return null;
274         }
275         return cacheAdministration[cacheType];
276     }
277
278     /**
279      * Returns initialQueryCache. This attribute contains "where" clause which
280      * is used during data object (or DataStruct object) cache initialization.
281      *
282      * @return initialQueryCache.
283      */

284     public String JavaDoc getInitialQueryCache() {
285         return initialQueryCache;
286     }
287
288     /**
289      * Sets initialQueryCache attribute. This attribute contains "where" clause
290      * which is used during data object (or DataStruct object) cache
291      * initialization.
292      *
293      * @param initQ
294      * New value of initialQueryCache attribute.
295      */

296     protected void setInitialQueryCache(String JavaDoc initQ) {
297         initialQueryCache = initQ;
298     }
299
300     public void makeInvisible(String JavaDoc cacheHandle) {
301         if (getCacheAdministration(CacheConstants.DATA_CACHE).getMaxCacheSize() != 0) {
302             Integer JavaDoc intObj = (Integer JavaDoc) nonVisibleList.get(cacheHandle);
303             int num;
304
305             if (intObj != null) {
306                 num = intObj.intValue();
307                 num++;
308                 nonVisibleList.put(cacheHandle, new Integer JavaDoc(num));
309             } else {
310                 nonVisibleList.put(cacheHandle, new Integer JavaDoc(1));
311             }
312         }
313     }
314
315     public void makeVisible(String JavaDoc cacheHandle) {
316         if (getCacheAdministration(CacheConstants.DATA_CACHE).getMaxCacheSize() != 0) {
317             Integer JavaDoc intObj = (Integer JavaDoc) nonVisibleList.get(cacheHandle);
318             int num;
319
320             if (intObj != null) {
321                 num = intObj.intValue();
322                 num--;
323                 if (num == 0) {
324                     nonVisibleList.remove(cacheHandle);
325                 } else {
326                     nonVisibleList.put(cacheHandle, new Integer JavaDoc(num));
327                 }
328             }
329         }
330     }
331
332     /**
333      * Returns statistics of used table and caches.
334      *
335      * @return statistics of used table and caches.
336      */

337     public Statistics getStatistics() {
338         statistics.stopTime();
339         return statistics;
340     }
341
342     /**
343      * Refreshes statistics.
344      */

345     public void refreshStatistics() {
346         statistics.clear();
347     }
348
349     /**
350      * Returns information if data object (or DataStruct object) cache if
351      * "full". "Full" cache contains all data objects (or DataStruct objects)
352      * from the table. The cache is "full", if data (or DataStruct) object cache
353      * is unbounded, and if initialQueryCache attribute is set to "*".
354      *
355      * @return true if data object (or DataStruct object) cache if "full",
356      * otherwise false.
357      */

358     public void checkFull() {
359         if ((cacheAdministration[0].getMaxCacheSize() < 0) && (getInitialQueryCache() != null)
360                         && (getInitialQueryCache().equalsIgnoreCase("*"))) {
361             fullCachingOn = true;
362         } else {
363             fullCachingOn = false;
364         }
365     }
366
367     /**
368      * Returns information if data object (or DataStruct object) cache if
369      * "full". "Full" cache contains all data objects (or DataStruct objects)
370      * from the table. The cache is "full", if data (or DataStruct) object cache
371      * is unbounded, and if initialQueryCache attribute is set to "*".
372      *
373      * @return true if data object (or DataStruct object) cache if "full",
374      * otherwise false.
375      */

376     public boolean isFull() {
377         if (fullCachingOn) {
378             checkFull();
379         }
380         return fullCachingOn;
381     }
382
383     /**
384      * Returns data object (or DataStruct object) cache type. Possible values
385      * are: "none" - no caching available "lru" - cache with LRU (least-recently
386      * used) algorithm "full" - special case of LRU cache - the whole table is
387      * cached
388      *
389      * @return data object (or DataStruct object) cache type.
390      */

391     public String JavaDoc getCacheType() {
392         if (cacheAdministration[CacheConstants.DATA_CACHE].getMaxCacheSize() == 0) {
393             return "none";
394         } else {
395             if (isFull()) {
396                 return "full";
397             } else {
398                 return "lru";
399             }
400         }
401     }
402
403     /**
404      * Returns caching level. Possible values:
405      * org.enhydra.dods.cache.CacheConstants.DATA_CACHING (value 1) for data
406      * object (or DataStruct object) caching without query caching, and
407      * org.enhydra.dods.cache.CacheConstants.QUERY_CACHING (value 2) for data
408      * object (or DataStruct object) caching with query caching.
409      *
410      * @return Value 2 (org.enhydra.dods.cache.CacheConstants.QUERY_CACHING).
411      */

412     public int getLevelOfCaching() {
413         return CacheConstants.QUERY_CACHING;
414     }
415
416     /**
417      * Returns object TableConfiguration. TableConfiguration contains parameters
418      * (and their set and get methods) for table configuration.
419      *
420      * @return TableConfiguration.
421      */

422     public TableConfiguration getTableConfiguration() {
423         return tableConf;
424     }
425
426     /**
427      * Returns reserveFactor. This attribute is used in query caching. It
428      * defines how many more (exept needed) objects are taken for evaluation. If
429      * <code>num</code> is number of needed results, then it is used
430      * <code>num</code>+ DEFAULT_RESERVE_FACTOR *<code>num</code> of
431      * objects for estimating what is quicker: go to database for all object
432      * that are not in the cache, or run again query on database. This value is
433      * given in percents, as number between 0 and 1 (0.25 means 25%). For
434      * example, if DEFAULT_RESERVE_FACTOR is 0.5, and wanted number of results
435      * is 50, the estimation will be done on 75 (50 + 0.5 * 50) objects.
436      *
437      * @return reserveFactor.
438      */

439     public double getReserveFactor() {
440         return reserveFactor;
441     }
442
443     /**
444      * Sets reserveFactor. This attribute is used in query caching. It defines
445      * how many more (exept needed) objects are taken for evaluation. If
446      * <code>num</code> is number of needed results, then it is used
447      * <code>num</code>+ DEFAULT_RESERVE_FACTOR *<code>num</code> of
448      * objects for estimating what is quicker: go to database for all object
449      * that are not in the cache, or run again query on database. This value is
450      * given in percents, as number between 0 and 1 (0.25 means 25%). For
451      * example, if DEFAULT_RESERVE_FACTOR is 0.5, and wanted number of results
452      * is 50, the estimation will be done on 75 (50 + 0.5 * 50) objects.
453      *
454      * @param res
455      * New reserveFactor.
456      */

457     protected void setReserveFactor(double res) {
458         reserveFactor = res;
459     }
460
461     /**
462      *
463      */

464     protected void setCachePercentage(double percent) {
465         cachePercentage = percent;
466     }
467
468     /**
469      *
470      */

471     public double getCachePercentage() {
472         return cachePercentage;
473     }
474
475     /**
476      * Returns information whether caching is disabled.
477      *
478      * @return true is caching is disabled, otherwise false.
479      */

480     public boolean isDisabled() {
481         return isDisabled;
482     }
483
484     /**
485      * Reads table and cache configuration from application's configuration
486      * file.
487      *
488      * @param tableConfig
489      * configuration for table of this cache.
490      * @param cacheConfig
491      * configuration for this cache.
492      */

493     public void readConfiguration(Config tableConfig, Config cacheConfig, String JavaDoc dbName) throws CacheObjectException {
494         if (isDisabled) {
495             throw new CacheObjectException("Caching is disabled");
496         }
497         boolean initialAllCaches = CacheConstants.DEFAULT_INITIAL_ALL_CACHES;
498         int maxSize = CacheConstants.DEFAULT_MAX_CACHE_SIZE;
499         int maxSimple = CacheConstants.DEFAULT_MAX_SIMPLE_QUERY_CACHE_SIZE;
500         int maxComplex = CacheConstants.DEFAULT_MAX_COMPLEX_QUERY_CACHE_SIZE;
501         int maxMultiJoin = CacheConstants.DEFAULT_MAX_MULTI_JOIN_QUERY_CACHE_SIZE;
502         Config defaultCacheConfig = null;
503
504         this.tableConf.readTableConfiguration(tableConfig, dbName);
505
506         DatabaseConfiguration dbConf;
507
508         try {
509             dbConf = ((StandardLogicalDatabase) (DODS.getDatabaseManager().findLogicalDatabase(dbName)))
510                             .getDatabaseConfiguration();
511         } catch (Exception JavaDoc ex) {
512             throw new CacheObjectException("Error reading database configuration");
513         }
514         if (dbConf != null) {
515             try {
516                 maxSize = dbConf.getMaxCacheSize();
517             } catch (Exception JavaDoc e) {
518             }
519             try {
520                 maxSimple = dbConf.getMaxSimpleCacheSize();
521             } catch (Exception JavaDoc e) {
522             }
523             try {
524                 maxComplex = dbConf.getMaxComplexCacheSize();
525             } catch (Exception JavaDoc e) {
526             }
527             try {
528                 maxMultiJoin = dbConf.getMaxMultiJoinCacheSize();
529             } catch (Exception JavaDoc e) {
530             }
531             try {
532                 reserveFactor = dbConf.getReserveFactor();
533             } catch (Exception JavaDoc e) {
534             }
535             try {
536                 cachePercentage = dbConf.getCachePercentage();
537             } catch (Exception JavaDoc e) {
538             }
539             try {
540                 initialAllCaches = dbConf.getInitAllCaches();
541             } catch (Exception JavaDoc e) {
542             }
543             try {
544                 initialCacheFetchSize = dbConf.getInitialCacheFetchSize();
545             } catch (Exception JavaDoc e) {
546             }
547             try {
548                 initialDSCacheSize = dbConf.getInitialDSCacheSize();
549             } catch (Exception JavaDoc e) {
550             }
551
552         }
553         if (cacheConfig != null) {
554             try {
555                 maxSize = cacheConfig.getInt(CacheConstants.PARAMNAME_MAX_CACHE_SIZE);
556             } catch (Exception JavaDoc e) {
557             }
558             try {
559                 maxSimple = cacheConfig.getInt(CacheConstants.PARAMNAME_MAX_SIMPLE_CACHE_SIZE);
560             } catch (Exception JavaDoc e) {
561             }
562             try {
563                 maxComplex = cacheConfig.getInt(CacheConstants.PARAMNAME_MAX_COMPLEX_CACHE_SIZE);
564             } catch (Exception JavaDoc e) {
565             }
566             try {
567                 maxMultiJoin = cacheConfig.getInt(CacheConstants.PARAMNAME_MAX_MULTI_JOIN_CACHE_SIZE);
568             } catch (Exception JavaDoc e) {
569             }
570             try {
571                 initialQueryCache = cacheConfig.getString(CacheConstants.PARAMNAME_INITIAL_CONDITION);
572             } catch (Exception JavaDoc e) {
573                 if (initialAllCaches) {
574                     initialQueryCache = "*";
575                 }
576             }
577             try {
578                 reserveFactor = cacheConfig.getDouble(CacheConstants.PARAMNAME_RESERVE_FACTOR);
579             } catch (Exception JavaDoc e) {
580             }
581             try {
582                 cachePercentage = cacheConfig.getDouble(CacheConstants.PARAMNAME_CACHE_PERCENTAGE);
583             } catch (Exception JavaDoc e) {
584             }
585             try {
586                 initialCacheFetchSize = cacheConfig.getInt(CacheConstants.PARAMNAME_INITIAL_CACHE_FETCH_SIZE);
587             } catch (Exception JavaDoc e) {
588             }
589             try {
590                 initialDSCacheSize = cacheConfig.getInt(CacheConstants.PARAMNAME_INITIAL_DS_CACHE_SIZE);
591             } catch (Exception JavaDoc e) {
592             }
593         } else {
594             if (initialAllCaches) {
595                 initialQueryCache = "*";
596             }
597         }
598         cacheAdministration[CacheConstants.DATA_CACHE].setMaxCacheSize(maxSize);
599         cacheAdministration[CacheConstants.SIMPLE_QUERY_CACHE].setMaxCacheSize(maxSimple);
600         cacheAdministration[CacheConstants.COMPLEX_QUERY_CACHE].setMaxCacheSize(maxComplex);
601         cacheAdministration[CacheConstants.MULTI_JOIN_QUERY_CACHE].setMaxCacheSize(maxMultiJoin);
602     }
603
604     /**
605      * Creates QueryCacheImpl instance.
606      *
607      * @return created QueryCacheImpl instance as DataStructCache.
608      */

609     public DataStructCache newInstance() throws CacheObjectException {
610         if (isDisabled) {
611             throw new CacheObjectException("Caching is disabled");
612         }
613         return new QueryCacheImpl();
614     }
615
616     /**
617      * Creates array of three CacheAdministration objects.
618      * CacheAdministration[0] handles configuration settings about data (or
619      * DataStruct) object cache. CacheAdministration[1] handles configuration
620      * settings about simple query cache. CacheAdministration[2] handles
621      * configuration settings about complex query cache.
622      */

623     protected void init() {
624         cacheAdministration[CacheConstants.SIMPLE_QUERY_CACHE] = new CacheAdministration() {
625
626             /**
627              * Returns maximum size of simple query cache.
628              *
629              * @return Maximum size of simple query cache.
630              */

631             public int getMaxCacheSize() {
632                 if (simpleQCache != null) {
633                     return simpleQCache.getMaxEntries();
634                 }
635                 return 0;
636             }
637
638             /**
639              * Returns maximum simple query cache size. If the cache is
640              * unbounded (the maximum size is negative), the current cache size
641              * is returned.
642              *
643              * @param real
644              * If this parameter is true, method returns real maximum
645              * cache size, otherwise returns appropriate value for
646              * the statistic.
647              * @return Maximum simple query cache size.
648              */

649             public int getMaxCacheSize(boolean real) {
650                 int size = getMaxCacheSize();
651
652                 if (size < 0) {
653                     if (real) {
654                         return -1;
655                     } else {
656                         return getCacheSize();
657                     }
658                 }
659                 return size;
660             }
661
662             /**
663              * Sets maximum size of simple query cache.
664              *
665              * @param maxSize
666              * Maximum size of simple query cache.
667              */

668             protected void setMaxCacheSize(int maxSize) throws CacheObjectException {
669                 try {
670                     if (isDisabled) {
671                         throw new CacheObjectException("Caching is disabled");
672                     }
673                     if (maxSize == 0) {
674                         if (simpleQCache != null) {
675                             statistics.getCacheStatistics(CacheConstants.SIMPLE_QUERY_CACHE).clearStatistics();
676                             simpleQCache = null;
677                         }
678                         return;
679                     }
680                     if (simpleQCache == null) {
681                         if (cache != null) {
682                             simpleQCache = BaseCacheManager.getDODSCache(maxSize);
683                         }
684                     } else {
685                         simpleQCache.setMaxEntries(maxSize);
686                     }
687                 } catch (Exception JavaDoc ex) {
688                     System.out.println("Error in setMaxCacheSize - simple cache");
689                 }
690             }
691
692             /**
693              * Returns size of currently used simple query cache.
694              *
695              * @return Size of currently used simple query cache.
696              */

697             public int getCacheSize() {
698                 if (simpleQCache != null) {
699                     return simpleQCache.size();
700                 }
701                 return 0;
702             }
703
704             /**
705              * Refreshes simple query cache.
706              */

707             public void refresh() {
708                 if (cache != null) {
709                     if (simpleQCache != null) {
710                         simpleQCache = BaseCacheManager.getDODSCache(simpleQCache.getMaxEntries());
711                         statistics.getCacheStatistics(CacheConstants.SIMPLE_QUERY_CACHE).setCacheHitsNum(0);
712                     }
713                 }
714             }
715
716             /**
717              * Disables simple query cache.
718              */

719             public void disable() {
720                 if (!isDisabledSimple) {
721                     isDisabledSimple = true;
722                     if (simpleQCache != null) {
723                         disabledMaxSimpleQueryCacheSize = simpleQCache.getMaxEntries();
724                         simpleQCache = null;
725                     } else {
726                         disabledMaxSimpleQueryCacheSize = 0;
727                     }
728                 }
729             }
730
731             /**
732              * Enables simple query cache.
733              */

734             public void enable() {
735                 try {
736                     if (isDisabledSimple) {
737                         if (disabledMaxSimpleQueryCacheSize != 0) {
738                             if (disabledMaxCacheSize != 0) {
739                                 simpleQCache = BaseCacheManager.getDODSCache(disabledMaxSimpleQueryCacheSize);
740                             }
741                         }
742                         isDisabledSimple = false;
743                     }
744                     DODSCache obj = (DODSCache) statistics.getCacheStatistics(CacheConstants.SIMPLE_QUERY_CACHE);
745
746                     if (obj != null) {
747                         obj.clearStatistics();
748                     }
749                 } catch (Exception JavaDoc ex) {
750                     System.out.println("Error in enable simple");
751                 }
752             }
753         };
754         cacheAdministration[CacheConstants.COMPLEX_QUERY_CACHE] = new CacheAdministration() {
755
756             /**
757              * Returns maximum size of complex query cache.
758              *
759              * @return Maximum size of complex query cache.
760              */

761             public int getMaxCacheSize() {
762                 if (complexQCache != null) {
763                     return complexQCache.getMaxEntries();
764                 }
765                 return 0;
766             }
767
768             /**
769              * Returns maximum complex query cache size. If the cache is
770              * unbounded (the maximum size is negative), the current cache size
771              * is returned.
772              *
773              * @param real
774              * If this parameter is true, method returns real maximum
775              * cache size, otherwise returns appropriate value for
776              * the statistic.
777              *
778              * @return Maximum complex query cache size.
779              */

780             public int getMaxCacheSize(boolean real) {
781                 int size = getMaxCacheSize();
782
783                 if (size < 0) {
784                     if (real) {
785                         return -1;
786                     } else {
787                         return getCacheSize();
788                     }
789                 }
790                 return size;
791             }
792
793             /**
794              * Sets maximum size of complex query cache.
795              *
796              * @param maxSize
797              * Maximum size of complex query cache.
798              */

799             protected void setMaxCacheSize(int maxSize) throws CacheObjectException {
800                 try {
801                     if (isDisabled) {
802                         throw new CacheObjectException("Caching is disabled");
803                     }
804                     if (maxSize == 0) {
805                         if (complexQCache != null) {
806                             statistics.getCacheStatistics(CacheConstants.COMPLEX_QUERY_CACHE).clearStatistics();
807                             complexQCache = null;
808                         }
809                         return;
810                     }
811                     if (complexQCache == null) {
812                         if (cache != null) {
813                             complexQCache = BaseCacheManager.getDODSCache(maxSize);
814                         }
815                     } else {
816                         complexQCache.setMaxEntries(maxSize);
817                     }
818                 } catch (Exception JavaDoc ex) {
819                     System.out.println("Error in setMaxCacheSize - complex cache");
820                 }
821             }
822
823             /**
824              * Returns size of currently used complex query cache.
825              *
826              * @return Size of currently used complex query cache.
827              */

828             public int getCacheSize() {
829                 if (complexQCache != null) {
830                     return complexQCache.size();
831                 }
832                 return 0;
833             }
834
835             /**
836              * Refreshes complex query cache.
837              */

838             public void refresh() {
839                 if (cache != null) {
840                     if (complexQCache != null) {
841                         complexQCache = BaseCacheManager.getDODSCache(complexQCache.getMaxEntries());
842                         statistics.getCacheStatistics(CacheConstants.COMPLEX_QUERY_CACHE).setCacheHitsNum(0);
843                     }
844                 }
845             }
846
847             /**
848              * Disables complex query cache.
849              */

850             public void disable() {
851                 if (!isDisabledComplex) {
852                     isDisabledComplex = true;
853                     if (complexQCache != null) {
854                         disabledMaxComplexQueryCacheSize = complexQCache.getMaxEntries();
855                         complexQCache = null;
856                     } else {
857                         disabledMaxComplexQueryCacheSize = 0;
858                     }
859                 }
860             }
861
862             /**
863              * Enables complex query cache.
864              */

865             public void enable() {
866                 try {
867                     if (isDisabledComplex) {
868                         if (disabledMaxComplexQueryCacheSize != 0) {
869                             if (disabledMaxCacheSize != 0) {
870                                 complexQCache = BaseCacheManager.getDODSCache(disabledMaxComplexQueryCacheSize);
871                             }
872                         }
873                         isDisabledComplex = false;
874                     }
875                     DODSCache obj = (DODSCache) statistics.getCacheStatistics(CacheConstants.COMPLEX_QUERY_CACHE);
876
877                     if (obj != null) {
878                         obj.clearStatistics();
879                     }
880                 } catch (Exception JavaDoc ex) {
881                     System.out.println("Error in enable complex");
882                 }
883             }
884         };
885
886         cacheAdministration[CacheConstants.MULTI_JOIN_QUERY_CACHE] = new CacheAdministration() {
887
888             /**
889              * Returns maximum size of multi join query cache.
890              *
891              * @return Maximum size of multi join query cache.
892              */

893             public int getMaxCacheSize() {
894                 if (multiJoinQCache != null) {
895                     return multiJoinQCache.getMaxEntries();
896                 }
897                 return 0;
898             }
899
900             /**
901              * Returns maximum multi join query cache size. If the cache is
902              * unbounded (the maximum size is negative), the current cache size
903              * is returned.
904              *
905              * @param real
906              * If this parameter is true, method returns real maximum
907              * cache size, otherwise returns appropriate value for
908              * the statistic.
909              *
910              * @return Maximum multi join query cache size.
911              */

912             public int getMaxCacheSize(boolean real) {
913                 int size = getMaxCacheSize();
914
915                 if (size < 0) {
916                     if (real) {
917                         return -1;
918                     } else {
919                         return getCacheSize();
920                     }
921                 }
922                 return size;
923             }
924
925             /**
926              * Sets maximum size of multi join query cache.
927              *
928              * @param maxSize
929              * Maximum size of multi join query cache.
930              */

931             protected void setMaxCacheSize(int maxSize) throws CacheObjectException {
932                 try {
933                     if (isDisabled) {
934                         throw new CacheObjectException("Caching is disabled");
935                     }
936                     if (maxSize == 0) {
937                         if (multiJoinQCache != null) {
938                             statistics.getCacheStatistics(CacheConstants.MULTI_JOIN_QUERY_CACHE).clearStatistics();
939                             multiJoinQCache = null;
940                         }
941                         return;
942                     }
943                     if (multiJoinQCache == null) {
944                         if (cache != null) {
945                             multiJoinQCache = BaseCacheManager.getDODSCache(maxSize);
946                         }
947                     } else {
948                         multiJoinQCache.setMaxEntries(maxSize);
949                     }
950                 } catch (Exception JavaDoc ex) {
951                     System.out.println("Error in setMaxCacheSize - complex cache");
952                 }
953             }
954
955             /**
956              * Returns size of currently used multi join query cache.
957              *
958              * @return Size of currently used multi join query cache.
959              */

960             public int getCacheSize() {
961                 if (multiJoinQCache != null) {
962                     return multiJoinQCache.size();
963                 }
964                 return 0;
965             }
966
967             /**
968              * Refreshes multi join query cache.
969              */

970             public void refresh() {
971                 if (cache != null) {
972                     if (multiJoinQCache != null) {
973                         multiJoinQCache = BaseCacheManager.getDODSCache(multiJoinQCache.getMaxEntries());
974                         statistics.getCacheStatistics(CacheConstants.MULTI_JOIN_QUERY_CACHE).setCacheHitsNum(0);
975                     }
976                 }
977             }
978
979             /**
980              * Disables multi join query cache.
981              */

982             public void disable() {
983                 if (!isDisabledMultiJoin) {
984                     isDisabledMultiJoin = true;
985                     if (multiJoinQCache != null) {
986                         disabledMaxMultiJoinQueryCacheSize = multiJoinQCache.getMaxEntries();
987                         multiJoinQCache = null;
988                     } else {
989                         disabledMaxMultiJoinQueryCacheSize = 0;
990                     }
991                 }
992             }
993
994             /**
995              * Enables multi join query cache.
996              */

997             public void enable() {
998                 try {
999                     if (isDisabledMultiJoin) {
1000                        if (disabledMaxMultiJoinQueryCacheSize != 0) {
1001                            if (disabledMaxCacheSize != 0) {
1002                                multiJoinQCache = BaseCacheManager.getDODSCache(disabledMaxMultiJoinQueryCacheSize);
1003                            }
1004                        }
1005                        isDisabledMultiJoin = false;
1006                    }
1007                    DODSCache obj = (DODSCache) statistics.getCacheStatistics(CacheConstants.MULTI_JOIN_QUERY_CACHE);
1008
1009                    if (obj != null) {
1010                        obj.clearStatistics();
1011                    }
1012                } catch (Exception JavaDoc ex) {
1013                    System.out.println("Error in enable complex");
1014                }
1015            }
1016        };
1017
1018        cacheAdministration[CacheConstants.DATA_CACHE] = new CacheAdministration() {
1019
1020            /**
1021             * Returns maximum size of data (or DataStruct) object cache.
1022             *
1023             * @return Maximum data (or DataStruct) object cache size.
1024             */

1025            public int getMaxCacheSize() {
1026                if (cache != null) {
1027                    return cache.getMaxEntries();
1028                }
1029                return 0;
1030            }
1031
1032            /**
1033             * Returns maximum size of data (or DataStruct) object cache. If the
1034             * cache is unbounded (the maximum size is negative), the current
1035             * cache size is returned.
1036             *
1037             * @param real
1038             * If this parameter is true, method returns real maximum
1039             * cache size, otherwise returns appropriate value for
1040             * the statistic.
1041             *
1042             * @return Maximum data (or DataStruct) object cache size.
1043             */

1044            public int getMaxCacheSize(boolean real) {
1045                int size = getMaxCacheSize();
1046
1047                if (size < 0) {
1048                    if (real) {
1049                        return -1;
1050                    } else {
1051                        return getCacheSize();
1052                    }
1053                }
1054                return size;
1055            }
1056
1057            /**
1058             * Returns size of currently used data (or DataStruct) object cache.
1059             *
1060             * @return Size of currently used data (or DataStruct) object cache.
1061             */

1062            public int getCacheSize() {
1063                if (cache != null) {
1064                    return cache.size();
1065                }
1066                return 0;
1067            }
1068
1069            /**
1070             * Sets maximum size of data (or DataStruct) object cache.
1071             *
1072             * @param maxSize
1073             * Maximum DO cache size.
1074             */

1075            protected void setMaxCacheSize(int maxSize) throws CacheObjectException {
1076                try {
1077                    if (isDisabled) {
1078                        throw new CacheObjectException("Caching is disabled");
1079                    }
1080                    if (maxSize == 0) {
1081                        cache = null;
1082                        if (simpleQCache != null) {
1083                            oldSimpleMaxCacheSize = getCacheAdministration(CacheConstants.SIMPLE_QUERY_CACHE)
1084                                            .getMaxCacheSize();
1085                            simpleQCache = null;
1086                        } else {
1087                            oldSimpleMaxCacheSize = 0;
1088                        }
1089                        if (complexQCache != null) {
1090                            oldComplexMaxCacheSize = getCacheAdministration(CacheConstants.COMPLEX_QUERY_CACHE)
1091                                            .getMaxCacheSize();
1092                            complexQCache = null;
1093                        } else {
1094                            oldComplexMaxCacheSize = 0;
1095                        }
1096                        if (multiJoinQCache != null) {
1097                            oldMultiJoinMaxCacheSize = getCacheAdministration(CacheConstants.MULTI_JOIN_QUERY_CACHE)
1098                                            .getMaxCacheSize();
1099                            multiJoinQCache = null;
1100                        } else {
1101                            oldMultiJoinMaxCacheSize = 0;
1102                        }
1103                        statistics.clear();
1104                        return;
1105                    }
1106                    if (cache == null) {
1107                        cache = BaseCacheManager.getDODSCache(maxSize);
1108                        if (oldSimpleMaxCacheSize != 0) {
1109                            simpleQCache = BaseCacheManager.getDODSCache(oldSimpleMaxCacheSize);
1110                        }
1111                        if (oldComplexMaxCacheSize != 0) {
1112                            complexQCache = BaseCacheManager.getDODSCache(oldComplexMaxCacheSize);
1113                        }
1114                        if (oldMultiJoinMaxCacheSize != 0) {
1115                            multiJoinQCache = BaseCacheManager.getDODSCache(oldMultiJoinMaxCacheSize);
1116                        }
1117                    } else {
1118                        cache.setMaxEntries(maxSize);
1119                    }
1120                } catch (Exception JavaDoc ex) {
1121                    System.out.println("Error in setMaxCacheSize - DO cache");
1122                }
1123            }
1124
1125            /**
1126             * Refreshes caches (data (or DataStruct) object, simple query and
1127             * complex query).
1128             */

1129            public void refresh() {
1130                if (cache != null) {
1131                    cache = BaseCacheManager.getDODSCache(cache.getMaxEntries());
1132                }
1133                if (simpleQCache != null) {
1134                    simpleQCache = BaseCacheManager.getDODSCache(simpleQCache.getMaxEntries());
1135                }
1136                if (complexQCache != null) {
1137                    complexQCache = BaseCacheManager.getDODSCache(complexQCache.getMaxEntries());
1138                }
1139                if (multiJoinQCache != null) {
1140                    multiJoinQCache = BaseCacheManager.getDODSCache(multiJoinQCache.getMaxEntries());
1141                }
1142                statistics.clear();
1143            }
1144
1145            /**
1146             * Disables data (or DataStruct) object cache. When this cache is
1147             * disabled, so are and simple and complex query caches.
1148             */

1149            public void disable() {
1150                if (!isDisabled) {
1151                    isDisabled = true;
1152                    if (cache != null) {
1153                        disabledMaxCacheSize = cache.getMaxEntries();
1154                        cache = null;
1155                        if (simpleQCache != null) {
1156                            getCacheAdministration(CacheConstants.SIMPLE_QUERY_CACHE).disable();
1157                        }
1158                        if (complexQCache != null) {
1159                            cacheAdministration[CacheConstants.COMPLEX_QUERY_CACHE].disable();
1160                        }
1161                        if (multiJoinQCache != null) {
1162                            cacheAdministration[CacheConstants.MULTI_JOIN_QUERY_CACHE].disable();
1163                        }
1164                        statistics.clear();
1165                    }
1166                }
1167            }
1168
1169            /**
1170             * Enables data (or DataStruct) object cache. When this cache is
1171             * enabled, so are and simple and complex query caches (if they
1172             * existed before DO cache had been disabled).
1173             */

1174            public void enable() {
1175                try {
1176                    if (isDisabled) {
1177                        if (disabledMaxCacheSize != 0) {
1178                            cache = BaseCacheManager.getDODSCache(disabledMaxCacheSize);
1179                        }
1180                        if (isDisabledSimple) {
1181                            getCacheAdministration(CacheConstants.SIMPLE_QUERY_CACHE).enable();
1182                        }
1183                        if (isDisabledComplex) {
1184                            getCacheAdministration(CacheConstants.COMPLEX_QUERY_CACHE).enable();
1185                        }
1186                        if (isDisabledMultiJoin) {
1187                            getCacheAdministration(CacheConstants.MULTI_JOIN_QUERY_CACHE).enable();
1188                        }
1189                        statistics.clear();
1190                        isDisabled = false;
1191                    }
1192                } catch (Exception JavaDoc ex) {
1193                    System.out.println("Error in enable DO");
1194                }
1195            }
1196        };
1197    }
1198
1199    /**
1200     * Returns cache (data or DataStruct) content.
1201     *
1202     * @return Cache content as <code>Map</code> of data (or DataStruct)
1203     * objects.
1204     */

1205    public Map JavaDoc getCacheContent() {
1206        return cache;
1207    }
1208
1209    /**
1210     * Returns information if multi databases are supported.
1211     *
1212     * @return true if multi databases are used, otherwise false.
1213     */

1214    public boolean isMulti() {
1215        return multi;
1216    }
1217
1218    /**
1219     * Checks wheather cache reconfiguration needs to be done.
1220     *
1221     * @return true if cache reconfiguration needs to be done, otherwise false.
1222     */

1223    public boolean toReconfigure() {
1224        return false;
1225    }
1226
1227    /**
1228     * Adds DataStruct object to the cache.
1229     *
1230     * @param newDS
1231     * DataStruct object that will be added to the cache.
1232     *
1233     * @return Added DataStruct object.
1234     */

1235    public CoreDataStruct addDataStruct(CoreDataStruct newDS) {
1236        String JavaDoc handle;
1237        try {
1238            handle = newDS.get_CacheHandle();
1239        } catch (Exception JavaDoc e) {
1240            handle = null;
1241        }
1242        if (cache == null) {
1243            return newDS;
1244        }
1245        try {
1246            if (null != handle) {
1247                synchronized (cache) {
1248                    cache.add(handle, newDS);
1249                }
1250                return newDS;
1251            }
1252        } catch (Exception JavaDoc e) {
1253            e.printStackTrace();
1254        }
1255        return null;
1256    }
1257
1258    /**
1259     * Removes DataStruct object from the cache.
1260     *
1261     * @param data
1262     * DataStruct object that will be removed from the cache.
1263     *
1264     * @return Removed DataStruct object, or <tt>null</tt> if there was no
1265     * object removed from the cache.
1266     */

1267    public CoreDataStruct removeDataStruct(CoreDataStruct data) {
1268        try {
1269            String JavaDoc handle = data.get_CacheHandle();
1270            if (cache != null) {
1271                synchronized (cache) {
1272                    return (CoreDataStruct) cache.remove(handle);
1273                }
1274            }
1275        } catch (Exception JavaDoc e) {
1276        }
1277        return null;
1278    }
1279
1280    /**
1281     * Removes DataStruct object from the cache.
1282     *
1283     * @param handle
1284     * Cache handle of DataStruct object that will be removed from
1285     * the cache. The form of cache handle is: " <database_name>.
1286     * <String_presentation_of_oid>".
1287     *
1288     * @return Removed DataStruct object, or <tt>null</tt> if there was no
1289     * object removed from the cache.
1290     */

1291    public CoreDataStruct removeDataStruct(String JavaDoc handle) {
1292        if (cache != null) {
1293            synchronized (cache) {
1294                try {
1295                    return (CoreDataStruct) cache.remove(handle);
1296                } catch (Exception JavaDoc e) {
1297                }
1298            }
1299        }
1300        return null;
1301    }
1302
1303    /**
1304     * Updates cached DataStruct object, or inserts it in the cache if it didn't
1305     * exist in the cache.
1306     *
1307     * @param data
1308     * DataStruct object that will be updated (or inserted if didn't
1309     * exist in the cache).
1310     *
1311     * @return Updated or inserted DataStruct object.
1312     */

1313    public CoreDataStruct updateDataStruct(CoreDataStruct data) {
1314        try {
1315            data = addDataStruct(data);
1316            QueryCacheItem queryItem = null;
1317            HashSet JavaDoc del;
1318            Iterator JavaDoc iter = null;
1319
1320            if (complexQCache != null) {
1321                del = new HashSet JavaDoc();
1322                synchronized (complexQCache) {
1323                    for (iter = complexQCache.values().iterator(); iter.hasNext();) {
1324                        queryItem = (QueryCacheItem) iter.next();
1325                        if (data.get_Database().equals(queryItem.get_OriginDatabase())) {
1326                            del.add(queryItem);
1327                        }
1328                    }
1329                } // end synchronized
1330
iter = null;
1331                for (iter = del.iterator(); iter.hasNext();) {
1332                    removeComplexQuery((QueryCacheItem) iter.next());
1333                }
1334            }
1335            if (multiJoinQCache != null) {
1336                del = new HashSet JavaDoc();
1337                synchronized (multiJoinQCache) {
1338                    for (iter = multiJoinQCache.values().iterator(); iter.hasNext();) {
1339                        queryItem = (QueryCacheItem) iter.next();
1340                        if (data.get_Database().equals(queryItem.get_OriginDatabase())) {
1341                            del.add(queryItem);
1342                        }
1343                    }
1344                } // end synchronized
1345
iter = null;
1346                for (iter = del.iterator(); iter.hasNext();) {
1347                    removeMultiJoinQuery((QueryCacheItem) iter.next());
1348                }
1349            }
1350            if (simpleQCache != null) {
1351                iter = null;
1352                synchronized (simpleQCache) {
1353                    for (iter = simpleQCache.values().iterator(); iter.hasNext();) {
1354                        queryItem = (QueryCacheItem) iter.next();
1355                        String JavaDoc db = data.get_Database();
1356
1357                        if (db.equals(queryItem.get_OriginDatabase())) {
1358                            if (queryItem.checkConditions(data)) {
1359                                if (queryItem.getOIds().contains(data.get_Handle())) {
1360                                    queryItem.setModifiedQuery(true);
1361                                } else {
1362                                    if (queryItem.isCompleteResult()) {
1363                                        queryItem.add(data);
1364                                    }
1365                                    queryItem.setModifiedQuery(true);
1366
1367                                }
1368                            } // queryItem.checkConditions(data)
1369
else {
1370                                if (queryItem.getOIds().contains(data.get_Handle())) {
1371                                    queryItem.delete(data);
1372                                    queryItem.setModifiedQuery(true);
1373                                }
1374                            } // else of queryItem.checkConditions(data)
1375
} // db.equals(queryItem.get_OriginDatabase())
1376
} // for
1377
} // end synchronized
1378
} // simpleQCache != null
1379
} catch (DatabaseManagerException ex) {
1380        }
1381        return data;
1382    }
1383
1384    /**
1385     * Deletes DataStruct object from the cache.
1386     *
1387     * @param data
1388     * DataStruct object that will be deleted from the cache.
1389     *
1390     * @return Deleted DataStruct object, or <tt>null</tt> if there was no
1391     * object deleted from the cache.
1392     */

1393    public CoreDataStruct deleteDataStruct(CoreDataStruct data) {
1394        if (data != null) {
1395            CoreDataStruct oldDS = removeDataStruct(data);
1396            Iterator JavaDoc iter = null;
1397            QueryCacheItem queryItem = null;
1398
1399            if (simpleQCache != null) {
1400                synchronized (simpleQCache) {
1401                    for (iter = simpleQCache.values().iterator(); iter.hasNext();) {
1402                        queryItem = (QueryCacheItem) iter.next();
1403                        try {
1404                            String JavaDoc db = data.get_Database();
1405
1406                            if (db.equals(queryItem.get_OriginDatabase())) {
1407                                if (queryItem.getOIds().contains(data.get_Handle())) {
1408                                    queryItem.delete(data);
1409                                    queryItem.setModifiedQuery(true);
1410                                }
1411                            }
1412                        } catch (Exception JavaDoc e) {
1413                            System.out.println("Error in deleteDataStruct of QueryCacheImpl");
1414                        }
1415                    }
1416                } // end synchronized
1417
}
1418            iter = null;
1419            if (complexQCache != null) {
1420                synchronized (complexQCache) {
1421                    for (iter = complexQCache.values().iterator(); iter.hasNext();) {
1422                        queryItem = (QueryCacheItem) iter.next();
1423                        try {
1424                            String JavaDoc db = data.get_Database();
1425
1426                            if (db.equals(queryItem.get_OriginDatabase())) {
1427                                if (queryItem.getOIds().contains(data.get_Handle())) {
1428                                    queryItem.delete(data);
1429                                    queryItem.setModifiedQuery(true);
1430                                }
1431                            }
1432                        } catch (Exception JavaDoc e) {
1433                            System.out.println("Error in deleteDataStruct of QueryCacheImpl");
1434                        }
1435                    }
1436                } // end synchronized
1437
}
1438            if (multiJoinQCache != null) {
1439                synchronized (multiJoinQCache) {
1440                    for (iter = multiJoinQCache.values().iterator(); iter.hasNext();) {
1441                        queryItem = (QueryCacheItem) iter.next();
1442                        try {
1443                            String JavaDoc db = data.get_Database();
1444
1445                            if (db.equals(queryItem.get_OriginDatabase())) {
1446                                if (queryItem.getOIds().contains(data.get_Handle())) {
1447                                    queryItem.delete(data);
1448                                    queryItem.setModifiedQuery(true);
1449                                }
1450                            }
1451                        } catch (Exception JavaDoc e) {
1452                            System.out.println("Error in deleteDataStruct of QueryCacheImpl");
1453                        }
1454                    }
1455                } // end synchronized
1456
}
1457            return oldDS;
1458        }
1459        return null;
1460    }
1461
1462    /**
1463     * Returns DataStruct object whose String representation of OID is parameter
1464     * handle.
1465     *
1466     * @param cacheHandle
1467     * String representation of OID of DataStruct object that is
1468     * being searched in the cache.
1469     *
1470     * @return DataStruct object whose String representation of OID is handle.
1471     */

1472    public CoreDataStruct getDataStructByHandle(String JavaDoc cacheHandle) {
1473        if (cache == null) {
1474            return null;
1475        }
1476        if (isLocked()) {
1477            return null;
1478        }
1479        CoreDataStruct tmpDO = null;
1480        if(cache.isNeedToSynchronize()) {
1481            synchronized (cache) {
1482                tmpDO = getCacheItem(cacheHandle, tmpDO);
1483            }
1484        }else {
1485            tmpDO = getCacheItem(cacheHandle, tmpDO);
1486        }
1487        return tmpDO;
1488    }
1489
1490    /**
1491     * @param cacheHandle
1492     * @param tmpDO
1493     * @return
1494     */

1495    private CoreDataStruct getCacheItem(String JavaDoc cacheHandle, CoreDataStruct tmpDO) {
1496        cache.incrementCacheAccessNum(1);
1497        if (!nonVisibleList.containsKey(cacheHandle)) {
1498            tmpDO = (CoreDataStruct) cache.get(cacheHandle);
1499            if (tmpDO != null) {
1500                cache.incrementCacheHitsNum(1);
1501            }
1502        }
1503        return tmpDO;
1504    }
1505
1506    /**
1507     * Creates new QueryCacheItem instance.
1508     *
1509     * @param dbName
1510     * Database name.
1511     * @return Created QueryCacheItem instance.
1512     */

1513    public QueryCacheItem newQueryCacheItemInstance(String JavaDoc dbName) {
1514        return new QueryCacheItemImpl(dbName);
1515    }
1516
1517    /**
1518     * Returns QueryCacheItem object for specified database and simple query, if
1519     * exists, otherwise null.
1520     *
1521     * @param dbName
1522     * Database name.
1523     * @param query
1524     * Query in form of String.
1525     * @return QueryCacheItem object.
1526     */

1527    public QueryCacheItem getSimpleQueryCacheItem(String JavaDoc dbName, String JavaDoc query) {
1528        if (simpleQCache != null && !isLockedSimpleComplexQCache()) {
1529            return _getItem(simpleQCache, dbName + "." + query);
1530        }
1531        return null;
1532    }
1533
1534    private QueryCacheItem _getItem(DODSCache whereFrom, String JavaDoc whichOne) {
1535       if(whereFrom.isNeedToSynchronize()) {
1536           synchronized (whereFrom) {
1537               return (QueryCacheItem) whereFrom.get(whichOne);
1538           }
1539       } else {
1540           return (QueryCacheItem) whereFrom.get(whichOne);
1541       }
1542    }
1543    /**
1544     * Returns QueryCacheItem object for specified database and complex query,
1545     * if exists, otherwise null.
1546     *
1547     * @param dbName
1548     * Database name.
1549     * @param query
1550     * Query in form of String.
1551     * @return QueryCacheItem object.
1552     */

1553    public QueryCacheItem getComplexQueryCacheItem(String JavaDoc dbName, String JavaDoc query) {
1554        if (complexQCache != null && !isLockedSimpleComplexQCache()) {
1555           return _getItem(complexQCache, dbName + "." + query);
1556        }
1557        return null;
1558    }
1559
1560    /**
1561     * Returns QueryCacheItem object for specified database and multi join
1562     * query, if exists, otherwise null.
1563     *
1564     * @param dbName
1565     * Database name.
1566     * @param query
1567     * Query in form of String.
1568     * @return QueryCacheItem object.
1569     */

1570    public QueryCacheItem getMultiJoinQueryCacheItem(String JavaDoc dbName, String JavaDoc query) {
1571        if (multiJoinQCache != null && !isLockedMultiJoinQCache()) {
1572           return _getItem(multiJoinQCache, dbName + "." + query);
1573        }
1574        return null;
1575    }
1576
1577    /**
1578     * Adds simple query to simple query cache.
1579     *
1580     * @param queryItem
1581     * Query that will be added to simple query cache.
1582     * @return Query added to simple query cache.
1583     */

1584    public QueryCacheItem addSimpleQuery(QueryCacheItem queryItem) {
1585        if (simpleQCache != null) {
1586            synchronized (simpleQCache) {
1587                return (QueryCacheItem) simpleQCache.add(queryItem.get_OriginDatabase() + "." + queryItem.getQueryId(),
1588                                queryItem);
1589            }
1590        }
1591        return null;
1592    }
1593
1594    /**
1595     * Removes simple query from simple query cache.
1596     *
1597     * @param queryItem
1598     * Query that will be removed from simple query cache.
1599     * @return Query removed from simple query cache.
1600     */

1601    public QueryCacheItem removeSimpleQuery(QueryCacheItem queryItem) {
1602        if (simpleQCache != null) {
1603            synchronized (simpleQCache) {
1604                return (QueryCacheItem) simpleQCache.remove(queryItem.get_OriginDatabase() + "."
1605                                + queryItem.getQueryId());
1606            }
1607        }
1608        return null;
1609    }
1610
1611    /**
1612     * Adds complex query to complex query cache.
1613     *
1614     * @param queryItem
1615     * Query that will be added to complex query cache.
1616     * @return Query added to complex query cache.
1617     */

1618    public QueryCacheItem addComplexQuery(QueryCacheItem queryItem) {
1619        if (complexQCache != null) {
1620            synchronized (complexQCache) {
1621                return (QueryCacheItem) complexQCache.add(
1622                                queryItem.get_OriginDatabase() + "." + queryItem.getQueryId(), queryItem);
1623            }
1624        }
1625        return null;
1626    }
1627
1628    /**
1629     * Removes complex query from complex query cache.
1630     *
1631     * @param queryItem
1632     * Query that will be removed from complex query cache.
1633     * @return Query removed from complex query cache.
1634     */

1635    public QueryCacheItem removeComplexQuery(QueryCacheItem queryItem) {
1636        if (complexQCache != null) {
1637            synchronized (complexQCache) {
1638                return (QueryCacheItem) complexQCache.remove(queryItem.get_OriginDatabase() + "."
1639                                + queryItem.getQueryId());
1640            }
1641        }
1642        return null;
1643    }
1644
1645    /**
1646     * Adds multi join query to multi join query cache.
1647     *
1648     * @param queryItem
1649     * Query that will be added to multi join query cache.
1650     * @return Query added to multi join query cache.
1651     */

1652    public QueryCacheItem addMultiJoinQuery(QueryCacheItem queryItem) {
1653        if (multiJoinQCache != null) {
1654            synchronized (multiJoinQCache) {
1655                return (QueryCacheItem) multiJoinQCache.add(queryItem.get_OriginDatabase() + "."
1656                                + queryItem.getQueryId(), queryItem);
1657            }
1658        }
1659        return null;
1660    }
1661
1662    /**
1663     * Removes multi join query from multi join query cache.
1664     *
1665     * @param queryItem
1666     * Query that will be removed from multi join query cache.
1667     * @return Query removed from multi join query cache.
1668     */

1669    public QueryCacheItem removeMultiJoinQuery(QueryCacheItem queryItem) {
1670        if (multiJoinQCache != null) {
1671            synchronized (multiJoinQCache) {
1672                return (QueryCacheItem) multiJoinQCache.remove(queryItem.get_OriginDatabase() + "."
1673                                + queryItem.getQueryId());
1674            }
1675        }
1676        return null;
1677    }
1678
1679    /**
1680     * Returns query results from simple query cache.
1681     *
1682     * @param dbName
1683     * Database name.
1684     * @param query
1685     * Query for which are results searched in simple query cache.
1686     * @return Query results retrieved from simple cache, or null, if there are
1687     * no results retrieved from simple query cache.
1688     */

1689    public QueryResult getSimpleQueryResults(String JavaDoc dbName, String JavaDoc query) {
1690        return getSimpleQueryResults(dbName, query, 0, 0, false);
1691    }
1692
1693    /**
1694     * Returns query results from simple query cache.
1695     *
1696     * @param dbName
1697     * Database name.
1698     * @param query
1699     * Query for which are results searched in simple query cache.
1700     * @param limit
1701     * Specified number of results (database limit and read skip).
1702     * @param maxdb
1703     * Number of rows retrieved from database (or cache).
1704     * @return Query results retrieved from simple cache, or null, if there are
1705     * no results retrieved from simple query cache.
1706     */

1707    public QueryResult getSimpleQueryResults(String JavaDoc dbName, String JavaDoc query, int limit, int maxdb) {
1708        return getSimpleQueryResults(dbName, query, limit, maxdb, false);
1709    }
1710
1711    /**
1712     * Returns query results from simple query cache.
1713     *
1714     * @param dbName
1715     * Database name.
1716     * @param query
1717     * Query for which are results searched in simple query cache.
1718     * @param limit
1719     * Specified number of results (database limit and read skip).
1720     * @param maxdb
1721     * Number of rows retrieved from database (or cache).
1722     * @param unique
1723     * If true, only unique results are returned.
1724     * @return Query results retrieved from simple cache, or null, if there are
1725     * no results retrieved from simple query cache.
1726     */

1727    public QueryResult getSimpleQueryResults(String JavaDoc dbName, String JavaDoc query, int limit, int maxdb, boolean unique) {
1728        if (simpleQCache == null || isLockedSimpleComplexQCache()) {
1729            return null;
1730        }
1731        QueryResult result = null;
1732        String JavaDoc queryHandle = dbName + "." + query;
1733        QueryCacheItem queryItem = _getItem(simpleQCache, queryHandle);
1734        int i = 0;
1735
1736        if (queryItem != null) {
1737            synchronized (queryItem) {
1738                result = new QueryResult();
1739                result.database = queryItem.get_OriginDatabase();
1740                String JavaDoc handle = null;
1741                String JavaDoc cachePrefix = queryItem.get_OriginDatabase() + ".";
1742                Iterator JavaDoc iter = queryItem.getOIds().iterator();
1743
1744                if (unique) {
1745                    HashSet JavaDoc allResultOids = new HashSet JavaDoc();
1746                    int skippedNum = 0;
1747
1748                    while ((maxdb == 0 || i < maxdb) && (iter.hasNext()) && ((limit == 0 || result.DOs.size() < limit))) {
1749                        handle = (String JavaDoc) iter.next();
1750                        if (allResultOids.contains(handle)) {
1751                            skippedNum++;
1752                        } else {
1753                            allResultOids.add(handle);
1754                            result.DOs.add(handle);
1755
1756                        }
1757                        i++;
1758                    } // while
1759
result.skippedUnique = skippedNum;
1760                } // (unique)
1761
else {
1762                    while ((maxdb == 0 || i < maxdb) && (iter.hasNext()) && ((limit == 0 || result.DOs.size() < limit))) {
1763                        handle = (String JavaDoc) iter.next();
1764                        result.DOs.add(handle);
1765                        i++;
1766                    } // while
1767
} // (unique)
1768
} // (queryItem != null)
1769
}
1770        if (result.DOs.size() < limit) {
1771            if ((maxdb == 0) || (i < maxdb)) {
1772                result = null;
1773            }
1774        }
1775        return result;
1776    }
1777
1778    /**
1779     * Returns query results from complex query cache.
1780     *
1781     * @param dbName
1782     * Database name.
1783     * @param query
1784     * Query for which are results searched in complex query cache.
1785     * @return Query results retrieved from complex cache, or null, if there are
1786     * no results retrieved from complex query cache.
1787     */

1788    public QueryResult getComplexQueryResults(String JavaDoc dbName, String JavaDoc query) {
1789        return getComplexQueryResults(dbName, query, 0, 0, false);
1790    }
1791
1792    /**
1793     * Returns query results from complex query cache.
1794     *
1795     * @param dbName
1796     * Database name.
1797     * @param query
1798     * Query for which are results searched in complex query cache.
1799     * @param limit
1800     * Specified number of results (database limit and read skip).
1801     * @param maxdb
1802     * Number of rows retrieved from database (or cache).
1803     * @return Query results retrieved from complex cache, or null, if there are
1804     * no results retrieved from complex query cache.
1805     */

1806    public QueryResult getComplexQueryResults(String JavaDoc dbName, String JavaDoc query, int limit, int maxdb) {
1807        return getComplexQueryResults(dbName, query, limit, maxdb, false);
1808    }
1809
1810    /**
1811     * Returns query results from complex query cache.
1812     *
1813     * @param dbName
1814     * Database name.
1815     * @param query
1816     * Query for which are results searched in complex query cache.
1817     * @param limit
1818     * Specified number of results (database limit and read skip).
1819     * @param maxdb
1820     * Number of rows retrieved from database (or cache).
1821     * @param unique
1822     * If true, only unique results are returned.
1823     * @return Query results retrieved from complex cache, or null, if there are
1824     * no results retrieved from complex query cache.
1825     */

1826    public QueryResult getComplexQueryResults(String JavaDoc dbName, String JavaDoc query, int limit, int maxdb, boolean unique) {
1827        if (complexQCache == null || isLockedSimpleComplexQCache()) {
1828            return null;
1829        }
1830        QueryResult result = null;
1831        String JavaDoc queryHandle = dbName + "." + query;
1832        QueryCacheItem queryItem = _getItem(complexQCache, queryHandle);
1833        int i = 0;
1834
1835        if (queryItem != null) {
1836            synchronized (queryItem) {
1837                result = new QueryResult();
1838                result.database = queryItem.get_OriginDatabase();
1839                String JavaDoc handle = null;
1840                String JavaDoc cachePrefix = queryItem.get_OriginDatabase() + ".";
1841                Iterator JavaDoc iter = queryItem.getOIds().iterator();
1842
1843                if (unique) {
1844                    HashSet JavaDoc allResultOids = new HashSet JavaDoc();
1845                    int skippedNum = 0;
1846
1847                    while ((maxdb == 0 || i < maxdb) && (iter.hasNext()) && ((limit == 0 || result.DOs.size() < limit))) {
1848                        handle = (String JavaDoc) iter.next();
1849                        if (allResultOids.contains(handle)) {
1850                            skippedNum++;
1851                        } else {
1852                            allResultOids.add(handle);
1853                            result.DOs.add(handle);
1854                        }
1855                        i++;
1856                    } // while
1857
result.skippedUnique = skippedNum;
1858                } // (unique)
1859
else {
1860                    while ((maxdb == 0 || i < maxdb) && (iter.hasNext()) && ((limit == 0 || result.DOs.size() < limit))) {
1861                        handle = (String JavaDoc) iter.next();
1862                        result.DOs.add(handle);
1863                        i++;
1864                    } // while
1865
} // (unique)
1866
}
1867        } // (queryItem != null)
1868
if (result.DOs.size() < limit) {
1869            if ((maxdb == 0) || (i < maxdb)) {
1870                result = null;
1871            }
1872        }
1873        return result;
1874    }
1875
1876    /**
1877     * Returns query results from multi join query cache.
1878     *
1879     * @param dbName
1880     * Database name.
1881     * @param query
1882     * Query for which are results searched in multi join query
1883     * cache.
1884     * @return Query results retrieved from multi join cache, or null, if there
1885     * are no results retrieved from multi join query cache.
1886     */

1887    public QueryResult getMultiJoinQueryResults(String JavaDoc dbName, String JavaDoc query) {
1888        return getMultiJoinQueryResults(dbName, query, 0, 0, false);
1889    }
1890
1891    /**
1892     * Returns query results from multi join query cache.
1893     *
1894     * @param dbName
1895     * Database name.
1896     * @param query
1897     * Query for which are results searched in multi join query
1898     * cache.
1899     * @param limit
1900     * Specified number of results (database limit and read skip).
1901     * @param maxdb
1902     * Number of rows retrieved from database (or cache).
1903     * @return Query results retrieved from multi join cache, or null, if there
1904     * are no results retrieved from multi join query cache.
1905     */

1906    public QueryResult getMultiJoinQueryResults(String JavaDoc dbName, String JavaDoc query, int limit, int maxdb) {
1907        return getMultiJoinQueryResults(dbName, query, limit, maxdb, false);
1908    }
1909
1910    /**
1911     * Returns query results from multi join query cache.
1912     *
1913     * @param dbName
1914     * Database name.
1915     * @param query
1916     * Query for which are results searched in multi join query
1917     * cache.
1918     * @param limit
1919     * Specified number of results (database limit and read skip).
1920     * @param maxdb
1921     * Number of rows retrieved from database (or cache).
1922     * @param unique
1923     * If true, only unique results are returned.
1924     * @return Query results retrieved from multi join cache, or null, if there
1925     * are no results retrieved from multi join query cache.
1926     */

1927    public QueryResult getMultiJoinQueryResults(String JavaDoc dbName, String JavaDoc query, int limit, int maxdb, boolean unique) {
1928        if (multiJoinQCache == null || isLockedMultiJoinQCache()) {
1929            return null;
1930        }
1931        QueryResult result = null;
1932        String JavaDoc queryHandle = dbName + "." + query;
1933        QueryCacheItem queryItem = _getItem(multiJoinQCache, queryHandle);
1934        int i = 0;
1935
1936        if (queryItem != null) {
1937            synchronized (queryItem) {
1938                result = new QueryResult();
1939                result.database = queryItem.get_OriginDatabase();
1940                String JavaDoc handle = null;
1941                String JavaDoc cachePrefix = queryItem.get_OriginDatabase() + ".";
1942                Iterator JavaDoc iter = queryItem.getOIds().iterator();
1943
1944                if (unique) {
1945                    HashSet JavaDoc allResultOids = new HashSet JavaDoc();
1946                    int skippedNum = 0;
1947
1948                    while ((maxdb == 0 || i < maxdb) && (iter.hasNext()) && ((limit == 0 || result.DOs.size() < limit))) {
1949                        handle = (String JavaDoc) iter.next();
1950                        if (allResultOids.contains(handle)) {
1951                            skippedNum++;
1952                        } else {
1953                            allResultOids.add(handle);
1954                            result.DOs.add(handle);
1955                        }
1956                        i++;
1957                    } // while
1958
result.skippedUnique = skippedNum;
1959                } // (unique)
1960
else {
1961                    while ((maxdb == 0 || i < maxdb) && (iter.hasNext()) && ((limit == 0 || result.DOs.size() < limit))) {
1962                        handle = (String JavaDoc) iter.next();
1963                        result.DOs.add(handle);
1964                        i++;
1965                    } // while
1966
} // (unique)
1967
}
1968        } // (queryItem != null)
1969
if (result.DOs.size() < limit) {
1970            if ((maxdb == 0) || (i < maxdb)) {
1971                result = null;
1972            }
1973        }
1974        return result;
1975    }
1976
1977    /**
1978     * Returns query results from simple, complex or multi join query cache.
1979     *
1980     * @param dbName
1981     * Database name.
1982     * @param query
1983     * Query for which are results searched in simple, complex or
1984     * multi join query cache.
1985     * @return Query results retrieved from simple, complex or multi join cache,
1986     * or null, if there are no results retrieved from simple, complex
1987     * or multi join query cache.
1988     */

1989    public QueryResult getQueryResults(String JavaDoc dbName, String JavaDoc query) {
1990        QueryResult result = getSimpleQueryResults(dbName, query);
1991
1992        if (result == null) {
1993            result = getComplexQueryResults(dbName, query);
1994        }
1995        if (result == null) {
1996            result = getMultiJoinQueryResults(dbName, query);
1997        }
1998        return result;
1999    }
2000
2001    /**
2002     * Inner class that implements TableStatistics. This is class for
2003     * administrating table and cache.
2004     */

2005    private class QueryCacheImplStatistics extends TableStatistics {
2006
2007        /**
2008         * Constructor().
2009         */

2010        public QueryCacheImplStatistics() {
2011            try {
2012                this.reset();
2013                if (cache != null) {
2014                    getCacheStatistics(CacheConstants.DATA_CACHE).clearStatistics();
2015                }
2016                if (simpleQCache != null) {
2017                    getCacheStatistics(CacheConstants.SIMPLE_QUERY_CACHE).clearStatistics();
2018                }
2019                if (complexQCache != null) {
2020                    getCacheStatistics(CacheConstants.COMPLEX_QUERY_CACHE).clearStatistics();
2021                }
2022                if (multiJoinQCache != null) {
2023                    getCacheStatistics(CacheConstants.MULTI_JOIN_QUERY_CACHE).clearStatistics();
2024                }
2025            } catch (Exception JavaDoc ex) {
2026            }
2027        }
2028
2029        /**
2030         * Resets parameters.
2031         */

2032        public void reset() {
2033            insertNum = 0;
2034            updateNum = 0;
2035            deleteNum = 0;
2036            lazyLoadingNum = 0;
2037            startTime = new Date JavaDoc();
2038            stopTime = new Date JavaDoc();
2039            queryNum = 0;
2040            queryByOIdNum = 0;
2041            averageQueryTime = 0;
2042            averageQueryByOIdTime = 0;
2043        }
2044
2045        /**
2046         * Returns type of the statistics. In this case, this is
2047         * QUERY_CACHE_STATISTICS.
2048         *
2049         * @return Type of the statistics: QUERY_CACHE_STATISTICS.
2050         */

2051        public int getStatisticsType() {
2052            return QUERY_CACHE_STATISTICS;
2053        }
2054
2055        /**
2056         * Clears statistics.
2057         */

2058        public void clear() {
2059            this.reset();
2060            if (cache != null) {
2061                getCacheStatistics(CacheConstants.DATA_CACHE).clearStatistics();
2062            }
2063            if (simpleQCache != null) {
2064                getCacheStatistics(CacheConstants.SIMPLE_QUERY_CACHE).clearStatistics();
2065            }
2066            if (complexQCache != null) {
2067                getCacheStatistics(CacheConstants.COMPLEX_QUERY_CACHE).clearStatistics();
2068            }
2069            if (multiJoinQCache != null) {
2070                getCacheStatistics(CacheConstants.MULTI_JOIN_QUERY_CACHE).clearStatistics();
2071            }
2072        }
2073
2074        /**
2075         * Returns query statistics for DO cache, simple query or complex query
2076         * cache.
2077         *
2078         * @param type
2079         * Indicator of cache for which query statistics is returned
2080         * for. Possible values:
2081         * org.enhydra.dods.cache.CacheConstants.DATA_CACHE (value 0)
2082         * for query statistics of DO cache
2083         * org.enhydra.dods.cache.CacheConstants.SIMPLE_QUERY_CACHE
2084         * (value 1) for query statistics of simple query cache
2085         * org.enhydra.dods.cache.CacheConstants.COMPLEX_QUERY_CACHE
2086         * (value 2) for query statistics of complex query cache
2087         * org.enhydra.dods.cache.CacheConstants.MULTI_JOIN_QUERY_CACHE
2088         * (value 3) for query statistics of multi join query cache
2089         *
2090         * @return Query statistics for specified cache.
2091         */

2092        public CacheStatistics getCacheStatistics(int type) {
2093            switch (type) {
2094            case CacheConstants.DATA_CACHE:
2095                return cache;
2096
2097            case CacheConstants.SIMPLE_QUERY_CACHE:
2098                return simpleQCache;
2099
2100            case CacheConstants.COMPLEX_QUERY_CACHE:
2101                return complexQCache;
2102
2103            case CacheConstants.MULTI_JOIN_QUERY_CACHE:
2104                return multiJoinQCache;
2105
2106            default:
2107                return null;
2108            }
2109        }
2110    }
2111
2112    /**
2113     * Shows content of this class. Can be used for debugging.
2114     */

2115    public String JavaDoc toString() {
2116        StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
2117
2118        ret.append("\n QueryCacheImpl: ");
2119        ret.append("\n cache: " + cache);
2120        ret.append("\n simpleQCache : " + simpleQCache);
2121        ret.append("\n complexQCache : " + complexQCache);
2122        ret.append("\n multiJoinQCache : " + multiJoinQCache);
2123        ret.append("\n cacheReadOnly : " + tableConf.isReadOnly());
2124        ret.append("\n initialQueryCache : " + initialQueryCache);
2125        ret.append("\n fullCaching : " + isFull());
2126        return ret.toString();
2127    }
2128
2129    public void removeEntries(Vector JavaDoc vec) {
2130        //System.err.println(getClass()+".removeEntries(Vector)");
2131
_refreshSimpleQuery();
2132        _refreshComplexQuery();
2133        _refreshMultiJoinQuery();
2134
2135        if (cache.size() > vec.size()) {
2136            for (Enumeration JavaDoc e = vec.elements(); e.hasMoreElements();) {
2137                removeDataStruct((String JavaDoc) e.nextElement());
2138            }
2139        } else {
2140            Iterator JavaDoc it = cache.values().iterator();
2141            Vector JavaDoc vecToRemove = new Vector JavaDoc();
2142            while (it.hasNext()) {
2143                CoreDataStruct obj = (CoreDataStruct) it.next();
2144                try {
2145                    if (vec.contains((obj.get_CacheHandle())))
2146                        vecToRemove.add(obj);
2147                } catch (DatabaseManagerException e) {
2148                }
2149            }
2150            for (Enumeration JavaDoc e = vecToRemove.elements(); e.hasMoreElements();) {
2151                removeDataStruct((String JavaDoc) e.nextElement());
2152            }
2153        }
2154    }
2155
2156    public void removeEntries() {
2157        //System.err.println(getClass()+".removeEntries()");
2158
_refreshSimpleQuery();
2159        _refreshComplexQuery();
2160        _refreshMultiJoinQuery();
2161
2162        if (cache != null)
2163            synchronized (cache) {
2164                cache.clear();
2165                cache.clearStatistics();
2166                // -dp cache = new DODSLinkedHashCache(cache.getMaxEntries());
2167
}
2168    }
2169
2170    public void emptyEntries(Vector JavaDoc vec, boolean incrementVersion) {
2171        //System.err.println(getClass()+".emptyEntries(Vector)");
2172
_refreshSimpleQuery();
2173        _refreshComplexQuery();
2174        _refreshMultiJoinQuery();
2175
2176        for (Enumeration JavaDoc e = vec.elements(); e.hasMoreElements();) {
2177            String JavaDoc cacheHandle = (String JavaDoc) e.nextElement();
2178            CoreDataStruct ds = (CoreDataStruct) cache.get(cacheHandle);
2179            if (ds != null) {
2180                updateDataStruct(ds.dumpData(incrementVersion));
2181            }
2182        }
2183    }
2184
2185    public void emptyEntries() {
2186        //System.err.println(getClass()+".emptyEntries()");
2187
_refreshSimpleQuery();
2188        _refreshComplexQuery();
2189        _refreshMultiJoinQuery();
2190
2191        Collection JavaDoc c = cache.values();
2192        Iterator JavaDoc it = c.iterator();
2193        while (it.hasNext()) {
2194            CoreDataStruct ds = (CoreDataStruct) it.next();
2195            updateDataStruct(ds.dumpData(false));
2196        }
2197    }
2198
2199    private void _refreshSimpleQuery() {
2200        if (simpleQCache != null)
2201            synchronized (simpleQCache) {
2202                simpleQCache.clear();
2203                simpleQCache.clearStatistics();
2204                // -dp simpleQCache = new
2205
// DODSLinkedHashCache(simpleQCache.getMaxEntries());
2206
}
2207    }
2208
2209    private void _refreshComplexQuery() {
2210        if (complexQCache != null)
2211            synchronized (complexQCache) {
2212                complexQCache.clear();
2213                complexQCache.clearStatistics();
2214                // -dp complexQCache = new
2215
// DODSLinkedHashCache(complexQCache.getMaxEntries());
2216
}
2217    }
2218
2219    private void _refreshMultiJoinQuery() {
2220        if (multiJoinQCache != null)
2221            synchronized (multiJoinQCache) {
2222                multiJoinQCache.clear();
2223                multiJoinQCache.clearStatistics();
2224                // multiJoinQCache =
2225
// new DODSLinkedHashCache(multiJoinQCache.getMaxEntries());
2226
}
2227    }
2228
2229    /**
2230     * @return
2231     */

2232    public int getInitialCacheFetchSize() {
2233        return initialCacheFetchSize;
2234    }
2235
2236    /**
2237     * @return
2238     */

2239    public int getInitialDSCacheSize() {
2240        return initialDSCacheSize;
2241    }
2242
2243    /**
2244     * @param i
2245     */

2246    public void setInitialCacheFetchSize(int i) {
2247        initialCacheFetchSize = i;
2248    }
2249
2250    /**
2251     * @param i
2252     */

2253    public void setInitialDSCacheSize(int i) {
2254        initialDSCacheSize = i;
2255    }
2256
2257}
Popular Tags