001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms.optimizers;
007:
008: import org.openrdf.query.BindingSet;
009: import org.openrdf.query.Dataset;
010: import org.openrdf.query.algebra.QueryRoot;
011: import org.openrdf.query.algebra.TupleExpr;
012: import org.openrdf.query.algebra.evaluation.EvaluationStrategy;
013: import org.openrdf.query.algebra.evaluation.impl.BindingAssigner;
014: import org.openrdf.query.algebra.evaluation.impl.CompareOptimizer;
015: import org.openrdf.query.algebra.evaluation.impl.ConjunctiveConstraintSplitter;
016: import org.openrdf.query.algebra.evaluation.impl.ConstantOptimizer;
017: import org.openrdf.query.algebra.evaluation.impl.QueryJoinOptimizer;
018: import org.openrdf.query.algebra.evaluation.impl.SameTermFilterOptimizer;
019: import org.openrdf.query.algebra.evaluation.util.QueryOptimizerList;
020: import org.openrdf.sail.rdbms.RdbmsValueFactory;
021: import org.openrdf.sail.rdbms.schema.BNodeTable;
022: import org.openrdf.sail.rdbms.schema.HashTable;
023: import org.openrdf.sail.rdbms.schema.LiteralTable;
024: import org.openrdf.sail.rdbms.schema.URITable;
025:
026: /**
027: * Facade to the underlying RDBMS optimizations.
028: *
029: * @author James Leigh
030: *
031: */
032: public class RdbmsQueryOptimizer {
033: private RdbmsValueFactory vf;
034: private URITable uris;
035: private BNodeTable bnodes;
036: private LiteralTable literals;
037: private SelectQueryOptimizerFactory factory;
038: private HashTable hashTable;
039:
040: public void setSelectQueryOptimizerFactory(
041: SelectQueryOptimizerFactory factory) {
042: this .factory = factory;
043: }
044:
045: public void setValueFactory(RdbmsValueFactory vf) {
046: this .vf = vf;
047: }
048:
049: public void setUriTable(URITable uris) {
050: this .uris = uris;
051: }
052:
053: public void setBnodeTable(BNodeTable bnodes) {
054: this .bnodes = bnodes;
055: }
056:
057: public void setLiteralTable(LiteralTable literals) {
058: this .literals = literals;
059: }
060:
061: public void setHashTable(HashTable hashTable) {
062: this .hashTable = hashTable;
063: }
064:
065: public TupleExpr optimize(TupleExpr expr, Dataset dataset,
066: BindingSet bindings, EvaluationStrategy strategy) {
067: // Clone the tuple expression to allow for more aggressive optimisations
068: TupleExpr tupleExpr = expr.clone();
069:
070: if (!(tupleExpr instanceof QueryRoot)) {
071: // Add a dummy root node to the tuple expressions to allow the
072: // optimisers to modify the actual root node
073: tupleExpr = new QueryRoot(tupleExpr);
074: }
075:
076: QueryOptimizerList optimizerList = new QueryOptimizerList();
077: addCoreOptimizations(strategy, optimizerList);
078: addRdbmsOptimizations(optimizerList);
079: optimizerList.add(new SqlConstantOptimizer());
080:
081: optimizerList.optimize(tupleExpr, dataset, bindings);
082: return tupleExpr;
083: }
084:
085: protected void addCoreOptimizations(EvaluationStrategy strategy,
086: QueryOptimizerList optimizerList) {
087: optimizerList.add(new BindingAssigner());
088: optimizerList.add(new ConstantOptimizer(strategy));
089: optimizerList.add(new CompareOptimizer());
090: optimizerList.add(new ConjunctiveConstraintSplitter());
091: optimizerList.add(new SameTermFilterOptimizer());
092: optimizerList.add(new QueryJoinOptimizer());
093: }
094:
095: protected void addRdbmsOptimizations(
096: QueryOptimizerList optimizerList) {
097: optimizerList.add(new ValueIdLookupOptimizer(vf));
098: optimizerList.add(factory.createRdbmsFilterOptimizer());
099: optimizerList.add(new VarColumnLookupOptimizer());
100: ValueJoinOptimizer valueJoins = new ValueJoinOptimizer();
101: valueJoins.setBnodeTable(bnodes);
102: valueJoins.setUriTable(uris);
103: valueJoins.setLiteralTable(literals);
104: valueJoins.setHashTable(hashTable);
105: optimizerList.add(valueJoins);
106: }
107:
108: }
|