[Mulgara-svn] r902 - trunk/src/jar/tuples/java/org/mulgara/store/tuples

pag at mulgara.org pag at mulgara.org
Wed May 7 14:19:47 UTC 2008


Author: pag
Date: 2008-05-07 07:19:46 -0700 (Wed, 07 May 2008)
New Revision: 902

Modified:
   trunk/src/jar/tuples/java/org/mulgara/store/tuples/LeftJoin.java
Log:
updated to handle inner filters

Modified: trunk/src/jar/tuples/java/org/mulgara/store/tuples/LeftJoin.java
===================================================================
--- trunk/src/jar/tuples/java/org/mulgara/store/tuples/LeftJoin.java	2008-05-06 15:30:31 UTC (rev 901)
+++ trunk/src/jar/tuples/java/org/mulgara/store/tuples/LeftJoin.java	2008-05-07 14:19:46 UTC (rev 902)
@@ -21,8 +21,15 @@
 
 // Locally written packages
 import org.mulgara.query.Constraint;
+import org.mulgara.query.QueryException;
 import org.mulgara.query.TuplesException;
 import org.mulgara.query.Variable;
+import org.mulgara.query.filter.Context;
+import org.mulgara.query.filter.ContextOwner;
+import org.mulgara.query.filter.Filter;
+import org.mulgara.query.filter.value.Bool;
+import org.mulgara.resolver.spi.QueryEvaluationContext;
+import org.mulgara.resolver.spi.TuplesContext;
 import org.mulgara.store.tuples.AbstractTuples;
 
 /**
@@ -42,7 +49,7 @@
  * @copyright &copy; 2005 <A href="mailto:pgearon at users.sourceforge.net">Paul Gearon</A>
  * @licence <a href="{@docRoot}/../../LICENCE">Open Software License v3.0</a>
  */
-public class LeftJoin extends AbstractTuples {
+public class LeftJoin extends AbstractTuples implements ContextOwner {
 
   @SuppressWarnings("unused")
   private static Logger logger = Logger.getLogger(LeftJoin.class.getName());
@@ -53,6 +60,12 @@
   /** The set of tuples to add to the lhs. */
   protected Tuples rhs;
 
+  /** The filter to apply. */
+  private Filter filter;
+
+  /** The tuples context */
+  protected TuplesContext context = null;
+
   /** The set of variables common to both the lhs and the rhs. */
   protected Set<Variable> commonVars;
 
@@ -83,10 +96,17 @@
    *         contain no variables in common.
    */
   @SuppressWarnings("unchecked")
-  LeftJoin(Tuples lhs, Tuples rhs) throws TuplesException, IllegalArgumentException {
+  LeftJoin(Tuples lhs, Tuples rhs, Filter filter, QueryEvaluationContext queryContext) throws TuplesException, IllegalArgumentException {
     // store the operands
     this.lhs = (Tuples)lhs.clone();
     this.rhs = (Tuples)rhs.clone();
+    this.filter = filter;
+    if (this.filter == null) this.filter = Bool.TRUE;
+    if (this.filter != Bool.TRUE) {
+      this.context = new TuplesContext(this, queryContext.getResolverSession());
+      this.filter.setContextOwner(this);
+    }
+    if (this.filter == null) throw new IllegalStateException("Null Filter");
 
     // get the variables to merge on
     commonVars = Collections.unmodifiableSet((Set<Variable>)TuplesOperations.getMatchingVars(lhs, rhs));
@@ -138,6 +158,15 @@
     int nrLeftVars = lhs.getNumberOfVariables();
     if (column < nrLeftVars) return lhs.getColumnValue(column);
     // return the column minus the LHS columns, and then skip over the matching vars
+    return rightMatches && testFilter() ? rhs.getColumnValue(column - rhsOffset) : UNBOUND;
+  }
+
+  
+  /** {@inheritDoc} */
+  public long getRawColumnValue(int column) throws TuplesException {
+    int nrLeftVars = lhs.getNumberOfVariables();
+    if (column < nrLeftVars) return lhs.getColumnValue(column);
+    // return the column minus the LHS columns, and then skip over the matching vars
     return rightMatches ? rhs.getColumnValue(column - rhsOffset) : UNBOUND;
   }
 
@@ -179,9 +208,13 @@
   }
 
 
-  /** {@inheritDoc} */
+  /**
+   * {@inheritDoc}
+   * If filtering, then this is necessarily false since there could be multiple matches
+   * on the right that all filter out.
+   */
   public boolean hasNoDuplicates() throws TuplesException {
-    return lhs.hasNoDuplicates();
+    return lhs.hasNoDuplicates() && filter == Bool.TRUE;
   }
 
 
@@ -309,7 +342,24 @@
     return prefix;
   }
 
+
   /**
+   * Tests a filter using the current context.
+   * @return The test result.
+   * @throws QueryException If there was an error accessing data needed for the test.
+   */
+  private boolean testFilter() {
+    // re-root the filter expression to this Tuples
+    filter.setContextOwner(this);
+    try {
+      return filter.test(context);
+    } catch (QueryException qe) {
+      return false;
+    }
+  }
+
+
+  /**
    * Closes all the operands.
    * @throws TuplesException If either the lhs or the rhs can't be closed.
    */
@@ -328,11 +378,31 @@
     // Copy mutable fields by value
     cloned.lhs = (Tuples)lhs.clone();
     cloned.rhs = (Tuples)rhs.clone();
+    cloned.context = (context == null) ? null : new TuplesContext(cloned, context);
+    if (cloned.filter == null) throw new IllegalStateException("Unexpectedly lost a filter: " + filter);
 
     return cloned;
   }
 
+  /**
+   * Tells a filter what the current context is.
+   * @see org.mulgara.query.filter.ContextOwner#getCurrentContext()
+   */
+  public Context getCurrentContext() {
+    return context;
+  }
 
+
+  /**
+   * Allows the context to be set manually. This is not expected.
+   * @see org.mulgara.query.filter.ContextOwner#setCurrentContext(org.mulgara.query.filter.Context)
+   */
+  public void setCurrentContext(Context context) {
+    if (!(context instanceof TuplesContext)) throw new IllegalArgumentException("LeftJoin can only accept a TuplesContext.");
+    this.context = (TuplesContext)context;
+  }
+
+
   //
   // Internal methods and classes
   //
@@ -523,6 +593,11 @@
       return wrapped.getColumnValue(column + offset);
     }
 
+    /** @see org.mulgara.store.tuples.Tuples#getRawColumnValue(int) */
+    public long getRawColumnValue(int column) throws TuplesException {
+      return getColumnValue(column);
+    }
+
     /** @see org.mulgara.store.tuples.Tuples#getComparator() */
     public RowComparator getComparator() {
       return wrapped.getComparator();




More information about the Mulgara-svn mailing list