01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: 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 EDU.purdue.cs.bloat.cfg.FlowGraph;
24: import EDU.purdue.cs.bloat.editor.ClassEditor;
25: import EDU.purdue.cs.bloat.editor.MethodEditor;
26: import EDU.purdue.cs.bloat.editor.Type;
27: import EDU.purdue.cs.bloat.file.ClassSource;
28: import EDU.purdue.cs.bloat.tree.PrintVisitor;
29:
30: import com.db4o.instrumentation.core.*;
31: import com.db4o.nativequery.NQDebug;
32: import com.db4o.nativequery.analysis.BloatExprBuilderVisitor;
33: import com.db4o.nativequery.expr.Expression;
34:
35: public class NativeQueryEnhancer {
36:
37: public static final String OPTIMIZE_QUERY_METHOD_NAME = "optimizeQuery";
38:
39: private static SODABloatMethodBuilder BLOAT_BUILDER = new SODABloatMethodBuilder();
40:
41: public boolean enhance(BloatLoaderContext bloatUtil,
42: ClassEditor classEditor, String methodName,
43: Type[] argTypes, ClassLoader classLoader,
44: ClassSource classSource) throws Exception {
45: if (NQDebug.LOG) {
46: System.err.println("Enhancing " + classEditor.name());
47: }
48: Expression expr = analyze(bloatUtil, classEditor, methodName,
49: argTypes);
50: if (expr == null) {
51: return false;
52: }
53: MethodEditor methodEditor = BLOAT_BUILDER.injectOptimization(
54: expr, classEditor, classLoader, classSource);
55: if (NQDebug.LOG) {
56: System.out.println("SODA BYTE CODE:");
57: methodEditor.print(System.out);
58: }
59: methodEditor.commit();
60: classEditor.commit();
61: return true;
62: }
63:
64: public Expression analyze(BloatLoaderContext bloatUtil,
65: ClassEditor classEditor, String methodName) {
66: return analyze(bloatUtil, classEditor, methodName, null);
67: }
68:
69: public Expression analyze(BloatLoaderContext bloatUtil,
70: ClassEditor classEditor, String methodName, Type[] argTypes) {
71: FlowGraph flowGraph = null;
72: try {
73: flowGraph = bloatUtil.flowGraph(classEditor, methodName,
74: argTypes);
75: } catch (ClassNotFoundException e) {
76: return null;
77: }
78: if (flowGraph != null) {
79: BloatExprBuilderVisitor builder = new BloatExprBuilderVisitor(
80: bloatUtil);
81: if (NQDebug.LOG) {
82: System.out.println("FLOW GRAPH:");
83: flowGraph.visit(new PrintVisitor());
84: }
85: flowGraph.visit(builder);
86: Expression expr = builder.expression();
87: if (NQDebug.LOG) {
88: System.out.println("EXPRESSION TREE:");
89: System.out.println(expr);
90: }
91: return expr;
92: }
93: return null;
94: }
95: }
|