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 java.util.ArrayList;
24:
25: import EDU.purdue.cs.bloat.context.CachingBloatContext;
26: import EDU.purdue.cs.bloat.editor.ClassEditor;
27: import EDU.purdue.cs.bloat.editor.EditorContext;
28: import EDU.purdue.cs.bloat.file.ClassFileLoader;
29:
30: import com.db4o.instrumentation.core.*;
31: import com.db4o.internal.query.Db4oNQOptimizer;
32: import com.db4o.nativequery.expr.Expression;
33: import com.db4o.query.Predicate;
34: import com.db4o.query.Query;
35: import com.db4o.reflect.Reflector;
36:
37: // only introduced to keep Db4oListFacade clean of Bloat references
38: public class Db4oOnTheFlyEnhancer implements Db4oNQOptimizer {
39: private transient ClassFileLoader loader;
40: private transient BloatLoaderContext bloatUtil;
41: private transient EditorContext context;
42: private Reflector reflector;
43:
44: public Db4oOnTheFlyEnhancer() {
45: this (new ClassFileLoader());
46: }
47:
48: public Db4oOnTheFlyEnhancer(Reflector reflector) {
49: this (new ClassFileLoader(new Db4oClassSource(
50: new JdkReverseLookupClassFactory(reflector))));
51: this .reflector = reflector;
52: }
53:
54: private Db4oOnTheFlyEnhancer(ClassFileLoader loader) {
55: this .loader = loader;
56: this .bloatUtil = new BloatLoaderContext(loader);
57: this .context = new CachingBloatContext(loader, new ArrayList(),
58: false);
59: }
60:
61: public Object optimize(Query query, Predicate filter) {
62: try {
63: //long start=System.currentTimeMillis();
64: Expression expr = analyzeInternal(filter);
65: //System.err.println((System.currentTimeMillis()-start)+" ms");
66: //System.err.println(expr);
67: if (expr == null) {
68: throw new RuntimeException("Could not analyze "
69: + filter);
70: }
71: //start=System.currentTimeMillis();
72: new SODAQueryBuilder().optimizeQuery(expr, query, filter,
73: new JdkReverseLookupClassFactory(reflector));
74: //System.err.println((System.currentTimeMillis()-start)+" ms");
75: return expr;
76: } catch (ClassNotFoundException exc) {
77: throw new RuntimeException(exc.getMessage());
78: }
79: }
80:
81: private Expression analyzeInternal(Predicate filter)
82: throws ClassNotFoundException {
83: ClassEditor classEditor = new ClassEditor(context, loader
84: .loadClass(filter.getClass().getName()));
85: Expression expr = new NativeQueryEnhancer().analyze(bloatUtil,
86: classEditor, Predicate.PREDICATEMETHOD_NAME, null);
87: return expr;
88: }
89:
90: public static Expression analyze(Predicate filter)
91: throws ClassNotFoundException {
92: return new Db4oOnTheFlyEnhancer().analyzeInternal(filter);
93: }
94: }
|