KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > nativequery > optimization > Db4oOnTheFlyEnhancer


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.nativequery.optimization;
22
23 import java.util.*;
24
25 import EDU.purdue.cs.bloat.context.*;
26 import EDU.purdue.cs.bloat.editor.*;
27 import EDU.purdue.cs.bloat.file.*;
28
29 import com.db4o.inside.query.*;
30 import com.db4o.nativequery.bloat.*;
31 import com.db4o.nativequery.expr.*;
32 import com.db4o.query.*;
33
34 // only introduced to keep Db4oListFacade clean of Bloat references
35
public class Db4oOnTheFlyEnhancer implements Db4oNQOptimizer {
36     private transient ClassFileLoader loader=new ClassFileLoader();
37     private transient BloatUtil bloatUtil=new BloatUtil(loader);
38     private transient EditorContext context=new CachingBloatContext(loader,new ArrayList(),false);
39     
40     public Object JavaDoc optimize(Query query,Predicate filter) {
41         try {
42             //long start=System.currentTimeMillis();
43
Expression expr = analyzeInternal(filter);
44             //System.err.println((System.currentTimeMillis()-start)+" ms");
45
//System.err.println(expr);
46
if(expr==null) {
47                 throw new RuntimeException JavaDoc("Could not analyze "+filter);
48             }
49             //start=System.currentTimeMillis();
50
new SODAQueryBuilder().optimizeQuery(expr,query,filter);
51             //System.err.println((System.currentTimeMillis()-start)+" ms");
52
return expr;
53         } catch (ClassNotFoundException JavaDoc exc) {
54             throw new RuntimeException JavaDoc(exc.getMessage());
55         }
56     }
57
58     private Expression analyzeInternal(Predicate filter) throws ClassNotFoundException JavaDoc {
59         ClassEditor classEditor=new ClassEditor(context,loader.loadClass(filter.getClass().getName()));
60         Expression expr=new NativeQueryEnhancer().analyze(bloatUtil,classEditor,Predicate.PREDICATEMETHOD_NAME);
61         return expr;
62     }
63     
64     public static Expression analyze(Predicate filter) throws ClassNotFoundException JavaDoc {
65         return new Db4oOnTheFlyEnhancer().analyzeInternal(filter);
66     }
67 }
68
Popular Tags