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

alexhall at mulgara.org alexhall at mulgara.org
Fri Oct 10 21:56:33 UTC 2008


Author: alexhall
Date: 2008-10-10 14:56:32 -0700 (Fri, 10 Oct 2008)
New Revision: 1301

Modified:
   trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java
   trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperationsUnitTest.java
Log:
Fixed a bug in TuplesOperations.optionalJoin.  This method should not be closing the tuples that are passed to it, but if TuplesOperations.reSort() actually sorts the optional tuples then it closes it.  An exception is then thrown when the constraint handler that calls optionalJoin() closes the input tuples.  I modified reSort to never close its input tuples, and always return a new tuples.

Modified: trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java
===================================================================
--- trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java	2008-10-10 21:17:17 UTC (rev 1300)
+++ trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java	2008-10-10 21:56:32 UTC (rev 1301)
@@ -398,7 +398,7 @@
       try {
         return new LeftJoin(standard, sortedOptional, filter, context);
       } finally {
-        if (sortedOptional != optional) sortedOptional.close();
+        sortedOptional.close();
       }
 
     } catch (RuntimeException re) {
@@ -886,9 +886,10 @@
   }
 
   /**
-   * Sort into an order given by the list of variables
+   * Sort into an order given by the list of variables.  The parameter is not closed, and this
+   * method will create and return a new tuples.
    *
-   * @param tuples The parameter to sort. This will be closed if a new tuples is created.
+   * @param tuples The parameter to sort. This will be not be closed.
    * @param variableList the list of {@link Variable}s to sort by
    * @return A {@link Tuples} that meets the sort criteria. This may be the original tuples parameter.
    * @throws TuplesException if the sort operation fails
@@ -923,7 +924,7 @@
 
       if (!sortNeeded) {
         if (logger.isDebugEnabled()) logger.debug("No sort needed on tuples.");
-        return tuples;
+        return (Tuples)tuples.clone();
       }
       
       if (logger.isDebugEnabled()) logger.debug("Sorting on " + variableList);
@@ -936,18 +937,15 @@
       assert fullVarList.containsAll(Arrays.asList(tuples.getVariables()));
 
       // Reorder the columns - the projection here does not remove any columns
-      Tuples oldTuples = tuples;
-      tuples = new UnorderedProjection(tuples, fullVarList);
-      assert tuples != oldTuples;
-      oldTuples.close();
+      Tuples projectedTuples = new UnorderedProjection(tuples, fullVarList);
+      assert projectedTuples != tuples;
 
       // Perform the actual sort
-      oldTuples = tuples;
-      tuples = tuplesFactory.newTuples(tuples);
-      assert tuples != oldTuples;
-      oldTuples.close();
+      Tuples sortedTuples = tuplesFactory.newTuples(projectedTuples);
+      assert sortedTuples != projectedTuples;
+      projectedTuples.close();
 
-      return tuples;
+      return sortedTuples;
     } catch (TuplesException e) {
       throw new TuplesException("Couldn't perform projection", e);
     }

Modified: trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperationsUnitTest.java
===================================================================
--- trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperationsUnitTest.java	2008-10-10 21:17:17 UTC (rev 1300)
+++ trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperationsUnitTest.java	2008-10-10 21:56:32 UTC (rev 1301)
@@ -84,6 +84,7 @@
 
     suite.addTest(new TuplesOperationsUnitTest("testReorderedAppend"));
     suite.addTest(new TuplesOperationsUnitTest("testOptionalJoin"));
+    suite.addTest(new TuplesOperationsUnitTest("testOptionalJoinWithReSort"));
 
     return suite;
   }
@@ -148,8 +149,8 @@
   
   public void testOptionalJoin() throws Exception {
 
-    LiteralTuples standard = new LiteralTuples(new String[] {"x", "y"}, true);
-    LiteralTuples optional = new LiteralTuples(new String[] {"y", "z"}, true);
+    LiteralTuples standard = new LiteralTuples(new String[] {"x", "y"}, true, true);
+    LiteralTuples optional = new LiteralTuples(new String[] {"y", "z"}, true, true);
     
     standard.appendTuple(new long[] { 1, 2 });
     standard.appendTuple(new long[] { 1, 3 });
@@ -174,6 +175,35 @@
 
     assertFalse(optionalJoin.next());
 
-    TuplesTestingUtil.closeTuples(new Tuples[] { optionalJoin });
+    // TuplesOperations.optionalJoin doesn't close its parameters, so close them here.
+    TuplesTestingUtil.closeTuples(new Tuples[] { optionalJoin, standard, optional });
   }
+  
+  public void testOptionalJoinWithReSort() throws Exception {
+    LiteralTuples standard = new LiteralTuples(new String[] {"x"}, true, true);
+    LiteralTuples optional = new LiteralTuples(new String[] {"y", "x"}, true, true);
+    
+    standard.appendTuple(new long[] { 1 });
+    standard.appendTuple(new long[] { 2 });
+    standard.appendTuple(new long[] { 3 });
+    
+    optional.appendTuple(new long[] { 1, 2 });
+    optional.appendTuple(new long[] { 1, 4 });
+    optional.appendTuple(new long[] { 3, 2 });
+    optional.appendTuple(new long[] { 4, 3 });
+    
+    Tuples optionalJoin = TuplesOperations.optionalJoin(standard, optional, TRUE, null);
+    
+    optionalJoin.beforeFirst();
+    
+    TuplesTestingUtil.testTuplesRow(optionalJoin, new long[] { 1, UNBOUND });
+    TuplesTestingUtil.testTuplesRow(optionalJoin, new long[] { 2, 1 });
+    TuplesTestingUtil.testTuplesRow(optionalJoin, new long[] { 2, 3 });
+    TuplesTestingUtil.testTuplesRow(optionalJoin, new long[] { 3, 4 });
+
+    assertFalse(optionalJoin.next());
+
+    // TuplesOperations.optionalJoin doesn't close its parameters, so close them here.
+    TuplesTestingUtil.closeTuples(new Tuples[] { optionalJoin, standard, optional });
+  }
 }




More information about the Mulgara-svn mailing list