From: <mrp...@us...> - 2010-09-15 21:43:36
|
Revision: 3561 http://bigdata.svn.sourceforge.net/bigdata/?rev=3561&view=rev Author: mrpersonick Date: 2010-09-15 21:43:28 +0000 (Wed, 15 Sep 2010) Log Message: ----------- adding Sesame to BOp conversion Modified Paths: -------------- branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/Var.java branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/ap/Predicate.java branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/engine/Rule2BOpUtility.java branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/relation/rule/eval/DefaultEvaluationPlan2.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/IVUtility.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/rules/RDFJoinNexus.java branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataEvaluationStrategyImpl.java branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataSail.java branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataTripleSource.java Added Paths: ----------- branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/striterator/ChunkedArraysIterator.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/store/BigdataBindingSetResolverator.java branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestBOps.java Modified: branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/Var.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/Var.java 2010-09-15 20:45:14 UTC (rev 3560) +++ branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/Var.java 2010-09-15 21:43:28 UTC (rev 3561) @@ -2,7 +2,6 @@ import java.io.ObjectStreamException; import java.util.UUID; - import com.bigdata.cache.ConcurrentWeakValueCache; import com.bigdata.relation.rule.Rule; Modified: branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/ap/Predicate.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/ap/Predicate.java 2010-09-15 20:45:14 UTC (rev 3560) +++ branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/ap/Predicate.java 2010-09-15 21:43:28 UTC (rev 3561) @@ -365,6 +365,16 @@ } + public Predicate<E> setBOpId(final int bopId) { + + final Predicate<E> tmp = this.clone(); + + tmp.annotations.put(Annotations.BOP_ID, bopId); + + return tmp; + + } + public String toString() { return toString(null/* bindingSet */); @@ -377,6 +387,8 @@ final StringBuilder sb = new StringBuilder(); + sb.append(getClass().getName()); + sb.append("("); for (int i = 0; i < arity; i++) { Modified: branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/engine/Rule2BOpUtility.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/engine/Rule2BOpUtility.java 2010-09-15 20:45:14 UTC (rev 3560) +++ branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/engine/Rule2BOpUtility.java 2010-09-15 21:43:28 UTC (rev 3561) @@ -27,7 +27,19 @@ package com.bigdata.bop.engine; +import java.util.Iterator; +import java.util.List; import com.bigdata.bop.BOp; +import com.bigdata.bop.BindingSetPipelineOp; +import com.bigdata.bop.IPredicate; +import com.bigdata.bop.IVariableOrConstant; +import com.bigdata.bop.NV; +import com.bigdata.bop.Var; +import com.bigdata.bop.ap.E; +import com.bigdata.bop.ap.Predicate; +import com.bigdata.bop.bset.CopyBindingSetOp; +import com.bigdata.bop.join.PipelineJoin; +import com.bigdata.journal.ITx; import com.bigdata.rdf.sail.BigdataSail; import com.bigdata.relation.rule.IProgram; import com.bigdata.relation.rule.IRule; @@ -60,10 +72,15 @@ * * @return */ - public static BOp convert(final IStep step) { + public static BindingSetPipelineOp convert(final IStep step) { + if (step instanceof Rule) + return convert((Rule) step); + else if (step instanceof Program) + return convert((Program) step); + throw new UnsupportedOperationException(); - + } /** @@ -73,12 +90,71 @@ * * @return */ - public static BOp convert(final Rule rule) { + public static BindingSetPipelineOp convert(final Rule rule) { - throw new UnsupportedOperationException(); + int bopId = 1; + + BindingSetPipelineOp left = new CopyBindingSetOp(new BOp[] {}, + NV.asMap(new NV[] {// + new NV(Predicate.Annotations.BOP_ID, bopId++),// + })); + + Iterator<Predicate> tails = rule.getTail(); + + while (tails.hasNext()) { + + final int joinId = bopId++; + + final Predicate<?> pred = tails.next().setBOpId(bopId++); + + System.err.println(pred); + + final BindingSetPipelineOp joinOp = new PipelineJoin<E>(// + left, pred,// + NV.asMap(new NV[] {// + new NV(Predicate.Annotations.BOP_ID, joinId),// + })); + + left = joinOp; + + } + + System.err.println(toString(left)); + + return left; + + } + + private static String toString(BOp bop) { + + StringBuilder sb = new StringBuilder(); + + toString(bop, sb, 0); + + // chop off the last \n + sb.setLength(sb.length()-1); + + return sb.toString(); + + } + + private static void toString(final BOp bop, final StringBuilder sb, + final int indent) { + + for (int i = 0; i < indent; i++) { + sb.append(' '); + } + sb.append(bop).append('\n'); + if (bop != null) { + List<BOp> args = bop.args(); + for (BOp arg : args) { + toString(arg, sb, indent+4); + } + } + } - + /** * Convert a program into an operator tree. * @@ -86,7 +162,7 @@ * * @return */ - public static BOp convert(final Program program) { + public static BindingSetPipelineOp convert(final Program program) { throw new UnsupportedOperationException(); Modified: branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/relation/rule/eval/DefaultEvaluationPlan2.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/relation/rule/eval/DefaultEvaluationPlan2.java 2010-09-15 20:45:14 UTC (rev 3560) +++ branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/relation/rule/eval/DefaultEvaluationPlan2.java 2010-09-15 21:43:28 UTC (rev 3561) @@ -31,9 +31,7 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; - import org.apache.log4j.Logger; - import com.bigdata.bop.IPredicate; import com.bigdata.bop.IVariableOrConstant; import com.bigdata.journal.ITx; @@ -64,7 +62,7 @@ * @todo not serializable but used by {@link #rangeCount(int)}, which is a * problem. */ - private final IJoinNexus joinNexus; + private final IRangeCountFactory rangeCountFactory; private final IRule rule; @@ -145,15 +143,31 @@ * @param rule * The rule. */ - public DefaultEvaluationPlan2(IJoinNexus joinNexus, IRule rule) { + public DefaultEvaluationPlan2(final IJoinNexus joinNexus, + final IRule rule) { - if (joinNexus == null) + this(joinNexus.getRangeCountFactory(), rule); + + } + + /** + * Computes an evaluation plan for the rule. + * + * @param rangeCountFactory + * The range count factory. + * @param rule + * The rule. + */ + public DefaultEvaluationPlan2(final IRangeCountFactory rangeCountFactory, + final IRule rule) { + + if (rangeCountFactory == null) throw new IllegalArgumentException(); if (rule == null) throw new IllegalArgumentException(); - this.joinNexus = joinNexus; + this.rangeCountFactory = rangeCountFactory; this.rule = rule; @@ -439,7 +453,7 @@ } - final long rangeCount = joinNexus.getRangeCountFactory() + final long rangeCount = rangeCountFactory .rangeCount(rule.getTail(tailIndex)); this.rangeCount[tailIndex] = rangeCount; Added: branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/striterator/ChunkedArraysIterator.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/striterator/ChunkedArraysIterator.java (rev 0) +++ branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/striterator/ChunkedArraysIterator.java 2010-09-15 21:43:28 UTC (rev 3561) @@ -0,0 +1,309 @@ +/** + +Copyright (C) SYSTAP, LLC 2006-2007. All rights reserved. + +Contact: + SYSTAP, LLC + 4501 Tower Road + Greensboro, NC 27410 + lic...@bi... + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +/* + * Created on Oct 24, 2007 + */ + +package com.bigdata.striterator; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Fully buffered iterator. + * + * @author <a href="mailto:tho...@us...">Bryan Thompson</a> + * @version $Id: ChunkedArrayIterator.java 2265 2009-10-26 12:51:06Z thompsonbry $ + */ +public class ChunkedArraysIterator<E> implements IChunkedOrderedIterator<E> { + + private boolean open = true; + + /** buffer iterator. */ + private ICloseableIterator<E[]> bufferIt; + + /** current buffer. */ + private E[] buffer; + + /** The order of the elements in the buffer or <code>null</code> iff not known. */ + private final IKeyOrder<E> keyOrder; + + /** + * The index of the next entry in {@link #buffer} that will be returned by + * {@link #next()}. + */ + private int i = 0; + +// /** +// * The element most recently returned by {@link #next()}. +// */ +// private E current = null; + +// /** +// * The #of elements that this iterator buffered. +// */ +// public int getBufferCount() { +// +// return bufferCount; +// +// } + + /** + * An iterator that visits the elements in the given iterator of arrays. + * + * @param a + * The iterator of arrays of elements. + */ + public ChunkedArraysIterator(final ICloseableIterator<E[]> a) { + + this(a, null); + + } + + /** + * An iterator that visits the elements in the given iterator of arrays. + * + * @param a + * The iterator of arrays of elements. + * @param keyOrder + * The order of the elements in the buffer or <code>null</code> + * iff not known. + */ + public ChunkedArraysIterator(final ICloseableIterator<E[]> a, + final IKeyOrder<E> keyOrder) { + + if (a == null) + throw new IllegalArgumentException(); + + this.bufferIt = a; + + this.keyOrder = keyOrder; + + } + + public boolean hasNext() { + + if(!open) return false; + + if (buffer == null) { + + return bufferIt.hasNext(); + + } +// else { +// +// assert i <= buffer.length; +// +// if (i == buffer.length) { +// +// return false; +// +// } +// +// } + + return true; + + } + + public E next() { + + if (!hasNext()) { + + throw new NoSuchElementException(); + + } + + if (buffer == null) { + + buffer = bufferIt.next(); + + } + + E e = buffer[i++]; + + if (i == buffer.length) { + + buffer = null; + + i = 0; + + } + + return e; + +// current = buffer[i++]; +// +// return current; + + } + + /** + * @throws UnsupportedOperationException + */ + public void remove() { + + throw new UnsupportedOperationException(); + + } + +// /** +// * Return the backing array. +// * +// * @see #getBufferCount() +// */ +// public E[] array() { +// +// assertOpen(); +// +// return buffer; +// +// } + + /** + * Returns the remaining statements. + * + * @throws NoSuchElementException + * if {@link #hasNext()} returns false. + */ + @SuppressWarnings("unchecked") + public E[] nextChunk() { + + if (!hasNext()) { + + throw new NoSuchElementException(); + + } + + final E[] ret; + + if (buffer == null) { + + /* + * We need to fetch the next buffer from the source iterator, and + * then we can just return it directly. + */ + buffer = bufferIt.next(); + + ret = buffer; + + } else if (i == 0) { + + /* + * Nothing has been returned to the caller by next() so we can just + * return the current buffer in this case. + */ + ret = buffer; + + } else { + + /* + * We have a buffer but we've already started return elements from + * it via next(), so we need to create a new buffer to return. + */ + final int remaining = buffer.length - i; + + /* + * Dynamically instantiation an array of the same component type + * as the objects that we are visiting. + */ + + ret = (E[]) java.lang.reflect.Array.newInstance(buffer.getClass() + .getComponentType(), remaining); + + + System.arraycopy(buffer, i, ret, 0, remaining); + + } + + // reset the current buffer + + buffer = null; + + i = 0; + + return ret; + + } + + public IKeyOrder<E> getKeyOrder() { + + return keyOrder; + + } + + public E[] nextChunk(IKeyOrder<E> keyOrder) { + + if (keyOrder == null) + throw new IllegalArgumentException(); + + final E[] chunk = nextChunk(); + + if (!keyOrder.equals(getKeyOrder())) { + + // sort into the required order. + + Arrays.sort(chunk, 0, chunk.length, keyOrder.getComparator()); + + } + + return chunk; + + } + + /* + * Note: Do NOT eagerly close the iterator since the makes it impossible to + * implement {@link #remove()}. + */ + public void close() { + + if (!open) { + + // already closed. + + return; + + } + + bufferIt.close(); + + open = false; + + buffer = null; + + i = 0; + + } + +// private final void assertOpen() { +// +// if (!open) { +// +// throw new IllegalStateException(); +// +// } +// +// } + +} Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/IVUtility.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/IVUtility.java 2010-09-15 20:45:14 UTC (rev 3560) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/IVUtility.java 2010-09-15 21:43:28 UTC (rev 3561) @@ -136,10 +136,8 @@ return iv1.compareTo(iv2); // otherwise we need to try to convert them into comparable numbers - final AbstractLiteralIV num1 = - (AbstractLiteralIV) iv1; - final AbstractLiteralIV num2 = - (AbstractLiteralIV) iv2; + final AbstractLiteralIV num1 = (AbstractLiteralIV) iv1; + final AbstractLiteralIV num2 = (AbstractLiteralIV) iv2; // if one's a BigDecimal we should use the BigDecimal comparator for both if (dte1 == DTE.XSDDecimal || dte2 == DTE.XSDDecimal) { Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/rules/RDFJoinNexus.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/rules/RDFJoinNexus.java 2010-09-15 20:45:14 UTC (rev 3560) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/rules/RDFJoinNexus.java 2010-09-15 21:43:28 UTC (rev 3561) @@ -605,7 +605,7 @@ } - final SPORelation r = (SPORelation) (IMutableRelation<?>) getRelation(); + final SPORelation r = (SPORelation) (IMutableRelation) getRelation(); /* * Use a thread pool to write out the statement and the Added: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/store/BigdataBindingSetResolverator.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/store/BigdataBindingSetResolverator.java (rev 0) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/store/BigdataBindingSetResolverator.java 2010-09-15 21:43:28 UTC (rev 3561) @@ -0,0 +1,222 @@ +package com.bigdata.rdf.store; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; + +import org.openrdf.model.Value; + +import com.bigdata.bop.Constant; +import com.bigdata.bop.IBindingSet; +import com.bigdata.bop.IConstant; +import com.bigdata.bop.IVariable; +import com.bigdata.rdf.internal.IV; +import com.bigdata.rdf.model.BigdataValue; +import com.bigdata.relation.accesspath.BlockingBuffer; +import com.bigdata.relation.rule.eval.ISolution; +import com.bigdata.striterator.AbstractChunkedResolverator; +import com.bigdata.striterator.IChunkedOrderedIterator; + +/** + * Efficiently resolve term identifiers in Bigdata {@link ISolution}s to RDF + * {@link BigdataValue}s. + * + * @author <a href="mailto:tho...@us...">Bryan Thompson</a> + * @version $Id: BigdataSolutionResolverator.java 3448 2010-08-18 20:55:58Z thompsonbry $ + */ +public class BigdataBindingSetResolverator + extends + AbstractChunkedResolverator<IBindingSet, IBindingSet, AbstractTripleStore> { + + /** + * + * @param db + * Used to resolve term identifiers to {@link Value} objects. + * @param src + * The source iterator (will be closed when this iterator is + * closed). + * + * FIXME must accept reverse bnodes map (from term identifier to + * blank nodes) for resolution of blank nodes within a Sesame + * connection context. + */ + public BigdataBindingSetResolverator(final AbstractTripleStore db, + final IChunkedOrderedIterator<IBindingSet> src) { + + super(db, src, new BlockingBuffer<IBindingSet[]>( + db.getChunkOfChunksCapacity(), + db.getChunkCapacity(), + db.getChunkTimeout(), + TimeUnit.MILLISECONDS)); + + } + + /** + * Strengthens the return type. + */ + public BigdataBindingSetResolverator start(ExecutorService service) { + + return (BigdataBindingSetResolverator) super.start(service); + + } + + /** + * Resolve a chunk of {@link ISolution}s into a chunk of + * {@link IBindingSet}s in which term identifiers have been resolved to + * {@link BigdataValue}s. + */ + protected IBindingSet[] resolveChunk(final IBindingSet[] chunk) { + + if (log.isInfoEnabled()) + log.info("Fetched chunk: size=" + chunk.length); + + /* + * Create a collection of the distinct term identifiers used in this + * chunk. + */ + + final Collection<IV> ids = new HashSet<IV>(chunk.length + * state.getSPOKeyArity()); + + for (IBindingSet solution : chunk) { + + final IBindingSet bindingSet = solution; + + assert bindingSet != null; + + final Iterator<Map.Entry<IVariable, IConstant>> itr = bindingSet + .iterator(); + + while (itr.hasNext()) { + + final Map.Entry<IVariable, IConstant> entry = itr.next(); + + final IV iv = (IV) entry.getValue().get(); + + if (iv == null) { + + throw new RuntimeException("NULL? : var=" + entry.getKey() + + ", " + bindingSet); + + } + + ids.add(iv); + + } + + } + + if (log.isInfoEnabled()) + log.info("Resolving " + ids.size() + " term identifiers"); + + // batch resolve term identifiers to terms. + final Map<IV, BigdataValue> terms = state.getLexiconRelation() + .getTerms(ids); + + /* + * Assemble a chunk of resolved elements. + */ + { + + final IBindingSet[] chunk2 = new IBindingSet[chunk.length]; + int i = 0; + for (IBindingSet e : chunk) { + + final IBindingSet f = getBindingSet(e, terms); + + chunk2[i++] = f; + + } + + // return the chunk of resolved elements. + return chunk2; + + } + + } + + /** + * Resolve the term identifiers in the {@link ISolution} using the map + * populated when we fetched the current chunk and return the + * {@link IBindingSet} for that solution in which term identifiers have been + * resolved to their corresponding {@link BigdataValue}s. + * + * @param solution + * A solution whose {@link Long}s will be interpreted as term + * identifiers and resolved to the corresponding + * {@link BigdataValue}s. + * + * @return The corresponding {@link IBindingSet} in which the term + * identifiers have been resolved to {@link BigdataValue}s. + * + * @throws IllegalStateException + * if the {@link IBindingSet} was not materialized with the + * {@link ISolution}. + * + * @todo this points out a problem where we would be better off strongly + * typing the term identifiers with their own class rather than using + * {@link Long} since we can not distinguish a {@link Long} + * materialized by a join against some non-RDF relation from a + * {@link Long} that is a term identifier. + */ + private IBindingSet getBindingSet(final IBindingSet solution, + final Map<IV, BigdataValue> terms) { + + if (solution == null) + throw new IllegalArgumentException(); + + if (terms == null) + throw new IllegalArgumentException(); + + final IBindingSet bindingSet = solution; + + if(bindingSet == null) { + + throw new IllegalStateException("BindingSet was not materialized"); + + } + + final Iterator<Map.Entry<IVariable, IConstant>> itr = bindingSet + .iterator(); + + while (itr.hasNext()) { + + final Map.Entry<IVariable, IConstant> entry = itr.next(); + + final Object boundValue = entry.getValue().get(); + + if (!(boundValue instanceof IV)) { + + continue; + + } + + final IV iv = (IV) boundValue; + + final BigdataValue value = terms.get(iv); + + if (value == null) { + + throw new RuntimeException("Could not resolve termId=" + + iv); + } + + /* + * Replace the binding. + * + * FIXME This probably needs to strip out the BigdataSail#NULL_GRAPH + * since that should not become bound. + */ + bindingSet.set(entry.getKey(), new Constant<BigdataValue>( + value)); + + } + + return bindingSet; + + } + +} Modified: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataEvaluationStrategyImpl.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataEvaluationStrategyImpl.java 2010-09-15 20:45:14 UTC (rev 3560) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataEvaluationStrategyImpl.java 2010-09-15 21:43:28 UTC (rev 3561) @@ -13,6 +13,7 @@ import java.util.Map; import java.util.Properties; import java.util.Set; +import java.util.UUID; import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger; import org.openrdf.model.Literal; @@ -48,7 +49,9 @@ import org.openrdf.query.algebra.evaluation.iterator.FilterIterator; import org.openrdf.query.algebra.helpers.QueryModelVisitorBase; import com.bigdata.BigdataStatics; +import com.bigdata.bop.BindingSetPipelineOp; import com.bigdata.bop.Constant; +import com.bigdata.bop.HashBindingSet; import com.bigdata.bop.IBindingSet; import com.bigdata.bop.IConstraint; import com.bigdata.bop.IPredicate; @@ -61,6 +64,10 @@ import com.bigdata.bop.constraint.NE; import com.bigdata.bop.constraint.NEConstant; import com.bigdata.bop.constraint.OR; +import com.bigdata.bop.engine.LocalChunkMessage; +import com.bigdata.bop.engine.QueryEngine; +import com.bigdata.bop.engine.Rule2BOpUtility; +import com.bigdata.bop.engine.RunningQuery; import com.bigdata.bop.solutions.ISortOrder; import com.bigdata.btree.keys.IKeyBuilderFactory; import com.bigdata.rdf.internal.DummyIV; @@ -74,7 +81,6 @@ import com.bigdata.rdf.internal.constraints.InlineNE; import com.bigdata.rdf.lexicon.LexiconRelation; import com.bigdata.rdf.model.BigdataValue; -import com.bigdata.rdf.rules.RuleContextEnum; import com.bigdata.rdf.sail.BigdataSail.Options; import com.bigdata.rdf.spo.DefaultGraphSolutionExpander; import com.bigdata.rdf.spo.ExplicitSPOFilter; @@ -84,11 +90,13 @@ import com.bigdata.rdf.spo.SPOStarJoin; import com.bigdata.rdf.store.AbstractTripleStore; import com.bigdata.rdf.store.BD; -import com.bigdata.rdf.store.BigdataSolutionResolverator; +import com.bigdata.rdf.store.BigdataBindingSetResolverator; import com.bigdata.rdf.store.IRawTripleStore; import com.bigdata.relation.accesspath.IAccessPath; +import com.bigdata.relation.accesspath.IAsynchronousIterator; import com.bigdata.relation.accesspath.IBuffer; import com.bigdata.relation.accesspath.IElementFilter; +import com.bigdata.relation.accesspath.ThickAsynchronousIterator; import com.bigdata.relation.rule.IProgram; import com.bigdata.relation.rule.IQueryOptions; import com.bigdata.relation.rule.IRule; @@ -97,17 +105,13 @@ import com.bigdata.relation.rule.Program; import com.bigdata.relation.rule.QueryOptions; import com.bigdata.relation.rule.Rule; -import com.bigdata.relation.rule.eval.ActionEnum; -import com.bigdata.relation.rule.eval.DefaultEvaluationPlanFactory2; -import com.bigdata.relation.rule.eval.IEvaluationPlanFactory; -import com.bigdata.relation.rule.eval.IJoinNexus; -import com.bigdata.relation.rule.eval.IJoinNexusFactory; import com.bigdata.relation.rule.eval.IRuleTaskFactory; import com.bigdata.relation.rule.eval.ISolution; import com.bigdata.relation.rule.eval.NestedSubqueryWithJoinThreadsTask; import com.bigdata.relation.rule.eval.RuleStats; import com.bigdata.search.FullTextIndex; import com.bigdata.search.IHit; +import com.bigdata.striterator.ChunkedArraysIterator; import com.bigdata.striterator.DistinctFilter; import com.bigdata.striterator.IChunkedOrderedIterator; @@ -473,6 +477,18 @@ return super.evaluate(union, bindings); + } catch (Exception ex) { + + // Use Sesame 2 evaluation + + ex.printStackTrace(); + + if (log.isInfoEnabled()) { + log.info("could not evaluate natively, punting to Sesame"); + } + + return super.evaluate(union, bindings); + } } @@ -590,6 +606,18 @@ return super.evaluate(join, bindings); + } catch (Exception ex) { + + // Use Sesame 2 evaluation + + ex.printStackTrace(); + + if (log.isInfoEnabled()) { + log.info("could not evaluate natively, punting to Sesame"); + } + + return super.evaluate(join, bindings); + } } @@ -682,6 +710,18 @@ return super.evaluate(join, bindings); + } catch (Exception ex) { + + // Use Sesame 2 evaluation + + ex.printStackTrace(); + + if (log.isInfoEnabled()) { + log.info("could not evaluate natively, punting to Sesame"); + } + + return super.evaluate(join, bindings); + } } @@ -1598,64 +1638,91 @@ */ protected CloseableIteration<BindingSet, QueryEvaluationException> execute( final IStep step) - throws QueryEvaluationException { + throws Exception { - final boolean backchain = // - tripleSource.getDatabase().getAxioms().isRdfSchema() - && tripleSource.includeInferred - && tripleSource.conn.isQueryTimeExpander(); + final BindingSetPipelineOp query = Rule2BOpUtility.convert(step); - if (log.isDebugEnabled()) { - log.debug("Running tupleExpr as native rule:\n" + step); - log.debug("backchain: " + backchain); + if (log.isInfoEnabled()) { + log.info(query); } - // run the query as a native rule. - final IChunkedOrderedIterator<ISolution> itr1; - try { - final IEvaluationPlanFactory planFactory = - DefaultEvaluationPlanFactory2.INSTANCE; - - /* - * alternative evaluation orders for LUBM Q9 (default is 1 4, 2, 3, - * 0, 5). All three evaluation orders are roughly as good as one - * another. Note that tail[2] (z rdf:type ...) is entailed by the - * ontology and could be dropped from evaluation. - */ - // final IEvaluationPlanFactory planFactory = new - // FixedEvaluationPlanFactory( - // // new int[] { 1, 4, 3, 0, 5, 2 } good - // // new int[] { 1, 3, 0, 4, 5, 2 } good - // ); - - final IJoinNexusFactory joinNexusFactory = database - .newJoinNexusFactory(RuleContextEnum.HighLevelQuery, - ActionEnum.Query, IJoinNexus.BINDINGS, - null, // filter - false, // justify - backchain, // - planFactory, // - queryHints - ); - - final IJoinNexus joinNexus = joinNexusFactory.newInstance(database - .getIndexManager()); - itr1 = joinNexus.runQuery(step); - - } catch (Exception ex) { - throw new QueryEvaluationException(ex); - } + final int startId = query.getProperty(Predicate.Annotations.BOP_ID); - /* - * Efficiently resolve term identifiers in Bigdata ISolutions to RDF - * Values in Sesame 2 BindingSets and align the resulting iterator with - * the Sesame 2 API. - */ + final QueryEngine queryEngine = tripleSource.getSail().getQueryEngine(); + + final UUID queryId = UUID.randomUUID(); + final RunningQuery runningQuery = queryEngine.eval(queryId, query, + new LocalChunkMessage<IBindingSet>(queryEngine, queryId, + startId, -1/* partitionId */, + newBindingSetIterator(new HashBindingSet()))); + + final IAsynchronousIterator<IBindingSet[]> it1 = + runningQuery.iterator(); + + final IChunkedOrderedIterator<IBindingSet> it2 = + new ChunkedArraysIterator<IBindingSet>(it1); + CloseableIteration<BindingSet, QueryEvaluationException> result = new Bigdata2Sesame2BindingSetIterator<QueryEvaluationException>( - new BigdataSolutionResolverator(database, itr1).start(database + new BigdataBindingSetResolverator(database, it2).start(database .getExecutorService())); +// final boolean backchain = // +// tripleSource.getDatabase().getAxioms().isRdfSchema() +// && tripleSource.includeInferred +// && tripleSource.conn.isQueryTimeExpander(); +// +// if (log.isDebugEnabled()) { +// log.debug("Running tupleExpr as native rule:\n" + step); +// log.debug("backchain: " + backchain); +// } +// +// // run the query as a native rule. +// final IChunkedOrderedIterator<ISolution> itr1; +// try { +// final IEvaluationPlanFactory planFactory = +// DefaultEvaluationPlanFactory2.INSTANCE; +// +// /* +// * alternative evaluation orders for LUBM Q9 (default is 1 4, 2, 3, +// * 0, 5). All three evaluation orders are roughly as good as one +// * another. Note that tail[2] (z rdf:type ...) is entailed by the +// * ontology and could be dropped from evaluation. +// */ +// // final IEvaluationPlanFactory planFactory = new +// // FixedEvaluationPlanFactory( +// // // new int[] { 1, 4, 3, 0, 5, 2 } good +// // // new int[] { 1, 3, 0, 4, 5, 2 } good +// // ); +// +// final IJoinNexusFactory joinNexusFactory = database +// .newJoinNexusFactory(RuleContextEnum.HighLevelQuery, +// ActionEnum.Query, IJoinNexus.BINDINGS, +// null, // filter +// false, // justify +// backchain, // +// planFactory, // +// queryHints +// ); +// +// final IJoinNexus joinNexus = joinNexusFactory.newInstance(database +// .getIndexManager()); +// itr1 = joinNexus.runQuery(step); +// +// } catch (Exception ex) { +// throw new QueryEvaluationException(ex); +// } +// +// /* +// * Efficiently resolve term identifiers in Bigdata ISolutions to RDF +// * Values in Sesame 2 BindingSets and align the resulting iterator with +// * the Sesame 2 API. +// */ +// CloseableIteration<BindingSet, QueryEvaluationException> result = +// new Bigdata2Sesame2BindingSetIterator<QueryEvaluationException>( +// new BigdataSolutionResolverator(database, itr1).start(database +// .getExecutorService())); + // use the basic filter iterator for remaining filters if (step instanceof ProxyRuleWithSesameFilters) { Collection<Filter> filters = @@ -1675,6 +1742,21 @@ } + /** + * Return an {@link IAsynchronousIterator} that will read a single, + * empty {@link IBindingSet}. + * + * @param bindingSet + * the binding set. + */ + protected ThickAsynchronousIterator<IBindingSet[]> newBindingSetIterator( + final IBindingSet bindingSet) { + + return new ThickAsynchronousIterator<IBindingSet[]>( + new IBindingSet[][] { new IBindingSet[] { bindingSet } }); + + } + @SuppressWarnings("serial") private class UnknownOperatorException extends RuntimeException { private TupleExpr operator; Modified: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataSail.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataSail.java 2010-09-15 20:45:14 UTC (rev 3560) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataSail.java 2010-09-15 21:43:28 UTC (rev 3561) @@ -112,6 +112,7 @@ import org.openrdf.sail.SailConnectionListener; import org.openrdf.sail.SailException; +import com.bigdata.bop.engine.QueryEngine; import com.bigdata.journal.IIndexManager; import com.bigdata.journal.ITransactionService; import com.bigdata.journal.ITx; @@ -513,6 +514,11 @@ * {@link BigdataSailConnection} instances and across all transactions. */ private Map<String, String> namespaces; + + /** + * The query engine. + */ + final private QueryEngine queryEngine; /** * When true, the RDFS closure will be maintained by the <em>SAIL</em> @@ -915,6 +921,10 @@ namespaces = Collections.synchronizedMap(new LinkedHashMap<String, String>()); + queryEngine = new QueryEngine(database.getIndexManager()); + + queryEngine.init(); + } /** @@ -1332,7 +1342,13 @@ } + public QueryEngine getQueryEngine() { + + return queryEngine; + + } + /** * Inner class implements the {@link SailConnection}. Some additional * functionality is available on this class, including @@ -1406,6 +1422,13 @@ */ private Lock lock; + + public BigdataSail getBigdataSail() { + + return BigdataSail.this; + + } + /** * Return the assertion buffer. * <p> Modified: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataTripleSource.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataTripleSource.java 2010-09-15 20:45:14 UTC (rev 3560) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataTripleSource.java 2010-09-15 21:43:28 UTC (rev 3561) @@ -42,6 +42,12 @@ } + public BigdataSail getSail() { + + return conn.getBigdataSail(); + + } + /** * This wraps * {@link BigdataSailConnection#getStatements(Resource, URI, Value, boolean, Resource[])}. Added: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestBOps.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestBOps.java (rev 0) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestBOps.java 2010-09-15 21:43:28 UTC (rev 3561) @@ -0,0 +1,170 @@ +/** +Copyright (C) SYSTAP, LLC 2006-2007. All rights reserved. + +Contact: + SYSTAP, LLC + 4501 Tower Road + Greensboro, NC 27410 + lic...@bi... + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +/* + * Created on Sep 16, 2009 + */ + +package com.bigdata.rdf.sail; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.Properties; +import org.apache.log4j.Logger; +import org.openrdf.model.Literal; +import org.openrdf.model.URI; +import org.openrdf.model.ValueFactory; +import org.openrdf.model.impl.LiteralImpl; +import org.openrdf.model.impl.URIImpl; +import org.openrdf.model.vocabulary.RDF; +import org.openrdf.model.vocabulary.RDFS; +import org.openrdf.query.Binding; +import org.openrdf.query.BindingSet; +import org.openrdf.query.QueryLanguage; +import org.openrdf.query.TupleQuery; +import org.openrdf.query.TupleQueryResult; +import org.openrdf.query.impl.BindingImpl; +import com.bigdata.rdf.axioms.NoAxioms; +import com.bigdata.rdf.store.BD; +import com.bigdata.rdf.vocab.NoVocabulary; + +/** + * @author <a href="mailto:mrp...@us...">Mike Personick</a> + * @version $Id$ + */ +public class TestBOps extends ProxyBigdataSailTestCase { + + protected static final Logger log = Logger.getLogger(TestBOps.class); + + @Override + public Properties getProperties() { + + Properties props = super.getProperties(); + + props.setProperty(BigdataSail.Options.TRUTH_MAINTENANCE, "false"); + props.setProperty(BigdataSail.Options.AXIOMS_CLASS, NoAxioms.class.getName()); + props.setProperty(BigdataSail.Options.VOCABULARY_CLASS, NoVocabulary.class.getName()); + props.setProperty(BigdataSail.Options.JUSTIFY, "false"); + props.setProperty(BigdataSail.Options.TEXT_INDEX, "false"); + + return props; + + } + + /** + * + */ + public TestBOps() { + } + + /** + * @param arg0 + */ + public TestBOps(String arg0) { + super(arg0); + } + + public void testSimpleJoin() throws Exception { + + final BigdataSail sail = getSail(); + sail.initialize(); + final BigdataSailRepository repo = new BigdataSailRepository(sail); + final BigdataSailRepositoryConnection cxn = + (BigdataSailRepositoryConnection) repo.getConnection(); + cxn.setAutoCommit(false); + + try { + + final ValueFactory vf = sail.getValueFactory(); + + final String ns = BD.NAMESPACE; + + URI mike = new URIImpl(ns+"Mike"); + URI bryan = new URIImpl(ns+"Bryan"); + URI person = new URIImpl(ns+"Person"); + URI likes = new URIImpl(ns+"likes"); + URI rdf = new URIImpl(ns+"RDF"); + Literal l1 = new LiteralImpl("Mike"); + Literal l2 = new LiteralImpl("Bryan"); +/**/ + cxn.setNamespace("ns", ns); + + cxn.add(mike, RDF.TYPE, person); + cxn.add(mike, likes, rdf); + cxn.add(mike, RDFS.LABEL, l1); + cxn.add(bryan, RDF.TYPE, person); + cxn.add(bryan, likes, rdf); + cxn.add(bryan, RDFS.LABEL, l2); + + /* + * Note: The either flush() or commit() is required to flush the + * statement buffers to the database before executing any operations + * that go around the sail. + */ + cxn.flush();//commit(); + + if (log.isInfoEnabled()) { + log.info("\n" + sail.getDatabase().dumpStore()); + } + + { + + String query = + "PREFIX rdf: <"+RDF.NAMESPACE+"> " + + "PREFIX rdfs: <"+RDFS.NAMESPACE+"> " + + "PREFIX ns: <"+ns+"> " + + + "select * " + + "WHERE { " + + " ?s rdf:type ns:Person . " + + " ?s ns:likes ?likes . " + + " ?s rdfs:label ?label . " + + "}"; + + final TupleQuery tupleQuery = + cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); + TupleQueryResult result = tupleQuery.evaluate(); + + Collection<BindingSet> solution = new LinkedList<BindingSet>(); + solution.add(createBindingSet(new Binding[] { + new BindingImpl("s", mike), + new BindingImpl("likes", rdf), + new BindingImpl("label", l1) + })); + solution.add(createBindingSet(new Binding[] { + new BindingImpl("s", bryan), + new BindingImpl("likes", rdf), + new BindingImpl("label", l2) + })); + + compare(result, solution); + + } + + } finally { + cxn.close(); + sail.__tearDownUnitTest(); + } + + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |