KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > query > NativeQueryHandler


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.inside.query;
22
23 import com.db4o.*;
24 import com.db4o.foundation.*;
25 import com.db4o.inside.diagnostic.*;
26 import com.db4o.query.*;
27
28 /**
29  * @sharpen.ignore
30  */

31 public class NativeQueryHandler {
32     
33     private static final String JavaDoc OPTIMIZER_IMPL_NAME = "com.db4o.nativequery.optimization.Db4oOnTheFlyEnhancer";
34
35     public final static String JavaDoc UNOPTIMIZED = "UNOPTIMIZED";
36     public final static String JavaDoc PREOPTIMIZED = "PREOPTIMIZED";
37     public final static String JavaDoc DYNOPTIMIZED = "DYNOPTIMIZED";
38     
39     private ObjectContainer _container;
40     private Db4oNQOptimizer _enhancer;
41     private List4 _listeners;
42     
43     public NativeQueryHandler(ObjectContainer container) {
44         _container = container;
45         loadQueryOptimizer();
46     }
47
48     public void addListener(Db4oQueryExecutionListener listener) {
49         _listeners=new List4(_listeners,listener);
50     }
51
52     public void clearListeners() {
53         _listeners=null;
54     }
55     
56     public ObjectSet execute(Predicate predicate,QueryComparator comparator) {
57         return configureQuery(predicate,comparator).execute();
58     }
59     
60     private Query configureQuery(Predicate predicate,QueryComparator comparator) {
61         Query q=_container.query();
62         if(comparator!=null) {
63             q.sortBy(comparator);
64         }
65         q.constrain(predicate.extentType());
66         if(predicate instanceof Db4oEnhancedFilter) {
67             ((Db4oEnhancedFilter)predicate).optimizeQuery(q);
68             notifyListeners(predicate,NativeQueryHandler.PREOPTIMIZED,null);
69             return q;
70         }
71         try {
72             if (shouldOptimize()) {
73                 Object JavaDoc optimized=_enhancer.optimize(q,predicate);
74                 notifyListeners(predicate,NativeQueryHandler.DYNOPTIMIZED,optimized);
75                 return q;
76             }
77         } catch (Exception JavaDoc exc) {
78             //exc.printStackTrace();
79
}
80         q.constrain(new PredicateEvaluation(predicate));
81         notifyListeners(predicate,NativeQueryHandler.UNOPTIMIZED,null);
82         if(shouldOptimize()){
83             DiagnosticProcessor dp = ((YapStream)_container).i_handlers._diagnosticProcessor;
84             if(dp.enabled()){
85                 dp.nativeQueryUnoptimized(predicate);
86             }
87         }
88         return q;
89     }
90
91     private boolean shouldOptimize() {
92         return _container.ext().configure().optimizeNativeQueries() && _enhancer!=null;
93     }
94
95     private void notifyListeners(Predicate predicate, String JavaDoc msg,Object JavaDoc optimized) {
96         NQOptimizationInfo info=new NQOptimizationInfo(predicate,msg,optimized);
97         for(Iterator4 iter=new Iterator4Impl(_listeners);iter.moveNext();/**/) {
98             ((Db4oQueryExecutionListener)iter.current()).notifyQueryExecuted(info);
99         }
100     }
101     
102     private void loadQueryOptimizer() {
103         try {
104             Class JavaDoc enhancerClass = Class.forName(NativeQueryHandler.OPTIMIZER_IMPL_NAME);
105             _enhancer=(Db4oNQOptimizer)enhancerClass.newInstance();
106         } catch (Throwable JavaDoc ignored) {
107             _enhancer=null;
108         }
109     }
110 }
111
Free Books   Free Magazines  
Popular Tags